forked from simplesurance/baur
/
app.go
245 lines (194 loc) · 6.15 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package baur
import (
"path"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/rs/xid"
"github.com/simplesurance/baur/cfg"
"github.com/simplesurance/baur/digest"
"github.com/simplesurance/baur/digest/sha384"
)
// App represents an application
type App struct {
RelPath string
Path string
Name string
BuildCmd string
Repository *Repository
Outputs []BuildOutput
BuildInputPaths []BuildInputPathResolver
buildInputs []BuildInput
totalInputDigest *digest.Digest
}
func replaceUUIDvar(in string) string {
return strings.Replace(in, "$UUID", xid.New().String(), -1)
}
func replaceAppNameVar(in, appName string) string {
return strings.Replace(in, "$APPNAME", appName, -1)
}
func replaceGitCommitVar(in string, r *Repository) (string, error) {
commitID, err := r.GitCommitID()
if err != nil {
return "", err
}
return strings.Replace(in, "$GITCOMMIT", commitID, -1), nil
}
func (a *App) setInputsFromCfg(r *Repository, cfg *cfg.App) error {
sliceLen := len(cfg.Build.Input.Files.Paths) + len(cfg.Build.Input.DockerImage)
if len(cfg.Build.Input.GitFiles.Paths) > 0 {
sliceLen++
}
if len(cfg.Build.Input.GolangSources.Paths) > 0 {
sliceLen++
}
a.BuildInputPaths = make([]BuildInputPathResolver, 0, sliceLen)
for _, p := range cfg.Build.Input.Files.Paths {
a.BuildInputPaths = append(a.BuildInputPaths, NewFileGlobPath(r.Path, a.RelPath, p))
}
if len(cfg.Build.Input.GitFiles.Paths) > 0 {
a.BuildInputPaths = append(a.BuildInputPaths,
NewGitPaths(r.Path, a.RelPath, cfg.Build.Input.GitFiles.Paths))
}
for _, d := range cfg.Build.Input.DockerImage {
a.BuildInputPaths = append(a.BuildInputPaths, &DockerImageRef{Repository: d.Repository, Digest: d.Digest})
}
if len(cfg.Build.Input.GolangSources.Paths) > 0 {
var gopath string
if len(cfg.Build.Input.GolangSources.GoPath) > 0 {
gopath = filepath.Join(a.Path, cfg.Build.Input.GolangSources.GoPath)
}
a.BuildInputPaths = append(a.BuildInputPaths,
NewGoSrcDirs(r.Path, a.RelPath, gopath, cfg.Build.Input.GolangSources.Paths))
}
return nil
}
func (a *App) setDockerOutputsFromCfg(cfg *cfg.App) error {
for _, di := range cfg.Build.Output.DockerImage {
tag, err := replaceGitCommitVar(di.RegistryUpload.Tag, a.Repository)
if err != nil {
return errors.Wrap(err, "replacing $GITCOMMIT in tag failed")
}
tag = replaceUUIDvar(tag)
a.Outputs = append(a.Outputs, &DockerArtifact{
ImageIDFile: path.Join(a.Path, di.IDFile),
Tag: tag,
Repository: di.RegistryUpload.Repository,
})
}
return nil
}
func (a *App) setFileOutputsFromCFG(cfg *cfg.App) error {
for _, f := range cfg.Build.Output.File {
destFile, err := replaceGitCommitVar(f.S3Upload.DestFile, a.Repository)
if err != nil {
return errors.Wrap(err, "replacing $GITCOMMIT in dest_file failed")
}
destFile = replaceUUIDvar(replaceAppNameVar(destFile, a.Name))
url := "s3://" + f.S3Upload.Bucket + "/" + destFile
a.Outputs = append(a.Outputs, &FileArtifact{
RelPath: path.Join(a.RelPath, f.Path),
Path: path.Join(a.Path, f.Path),
DestFile: destFile,
UploadURL: url,
})
}
return nil
}
// NewApp reads the configuration file and returns a new App
func NewApp(repository *Repository, cfgPath string) (*App, error) {
cfg, err := cfg.AppFromFile(cfgPath)
if err != nil {
return nil, errors.Wrapf(err,
"reading application config %s failed", cfgPath)
}
err = cfg.Validate()
if err != nil {
return nil, errors.Wrapf(err,
"validating application config %s failed",
cfgPath)
}
appAbsPath := path.Dir(cfgPath)
appRelPath, err := filepath.Rel(repository.Path, appAbsPath)
if err != nil {
return nil, errors.Wrap(err, "resolving repository relative application path failed")
}
app := App{
Repository: repository,
Path: path.Dir(cfgPath),
RelPath: appRelPath,
Name: cfg.Name,
BuildCmd: cfg.Build.Command,
}
if len(app.BuildCmd) == 0 {
app.BuildCmd = repository.DefaultBuildCmd
}
if err := app.setDockerOutputsFromCfg(cfg); err != nil {
return nil, errors.Wrap(err, "processing docker output declarations failed")
}
if err := app.setFileOutputsFromCFG(cfg); err != nil {
return nil, errors.Wrap(err, "processing S3 output declarations failed")
}
if err := app.setInputsFromCfg(repository, cfg); err != nil {
return nil, errors.Wrap(err, "processing input declarations failed")
}
return &app, nil
}
func (a *App) String() string {
return a.Name
}
// BuildInputs returns all BuildInputs.
// If the function is called the first time, the BuildInputPaths are resolved
// and stored. On following calls the stored BuildInputs are returned.
func (a *App) BuildInputs() ([]BuildInput, error) {
var res []BuildInput
if a.buildInputs != nil {
return a.buildInputs, nil
}
if len(a.BuildInputPaths) == 0 {
a.buildInputs = []BuildInput{}
return a.buildInputs, nil
}
for _, inputPath := range a.BuildInputPaths {
buildInputs, err := inputPath.Resolve()
if err != nil {
return nil, errors.Wrapf(err, "resolving %q failed", inputPath)
}
res = append(res, buildInputs...)
}
a.buildInputs = res
return a.buildInputs, nil
}
// TotalInputDigest returns the total input digest that is calculated over all
// input sources. The calculation is only done on the 1. call on following calls
// the stored digest is returned
func (a *App) TotalInputDigest() (digest.Digest, error) {
if a.totalInputDigest != nil {
return *a.totalInputDigest, nil
}
buildInputs, err := a.BuildInputs()
if err != nil {
return digest.Digest{}, err
}
digests := make([]*digest.Digest, 0, len(buildInputs))
for _, bi := range buildInputs {
d, err := bi.Digest()
if err != nil {
return digest.Digest{}, errors.Wrapf(err, "calculating input digest of %q failed", bi)
}
digests = append(digests, &d)
}
totalDigest, err := sha384.Sum(digests)
if err != nil {
return digest.Digest{}, errors.Wrap(err, "calculating total input digest")
}
a.totalInputDigest = totalDigest
return *a.totalInputDigest, nil
}
// SortAppsByName sorts the apps in the slice by Name
func SortAppsByName(apps []*App) {
sort.Slice(apps, func(i int, j int) bool {
return apps[i].Name < apps[j].Name
})
}