-
Notifications
You must be signed in to change notification settings - Fork 204
/
werf.go
440 lines (355 loc) · 11.6 KB
/
werf.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package config
import (
"errors"
"fmt"
"strings"
)
type WerfConfig struct {
Meta *Meta
StapelImages []*StapelImage
ImagesFromDockerfile []*ImageFromDockerfile
Artifacts []*StapelImageArtifact
}
func (c *WerfConfig) HasImageOrArtifact(imageName string) bool {
return c.HasImage(imageName) || c.GetArtifact(imageName) != nil
}
func (c *WerfConfig) HasNamelessImage() bool {
return c.HasImage("")
}
func (c *WerfConfig) HasImage(imageName string) bool {
return c.GetImage(imageName) != nil
}
func (c *WerfConfig) GetAllImages() []ImageInterface {
var images []ImageInterface
for _, image := range c.StapelImages {
images = append(images, image)
}
for _, image := range c.ImagesFromDockerfile {
images = append(images, image)
}
return images
}
func (c *WerfConfig) GetImage(imageName string) ImageInterface {
if i := c.GetStapelImage(imageName); i != nil {
return i
}
if i := c.GetDockerfileImage(imageName); i != nil {
return i
}
return nil
}
func (c *WerfConfig) GetStapelImage(imageName string) *StapelImage {
for _, image := range c.StapelImages {
if image.Name == imageName {
return image
}
}
return nil
}
func (c *WerfConfig) GetDockerfileImage(imageName string) *ImageFromDockerfile {
for _, image := range c.ImagesFromDockerfile {
if image.Name == imageName {
return image
}
}
return nil
}
func (c *WerfConfig) GetArtifact(imageName string) *StapelImageArtifact {
for _, artifact := range c.Artifacts {
if artifact.Name == imageName {
return artifact
}
}
return nil
}
func (c *WerfConfig) exportsAutoExcluding() error {
for _, image := range c.StapelImages {
if err := image.exportsAutoExcluding(); err != nil {
return err
}
}
for _, artifact := range c.Artifacts {
if err := artifact.exportsAutoExcluding(); err != nil {
return err
}
}
return nil
}
func (c *WerfConfig) validateImagesNames() error {
imageByName := map[string]ImageInterface{}
for _, image := range c.StapelImages {
name := image.Name
if name == "" && (len(c.StapelImages) > 1 || len(c.ImagesFromDockerfile) > 1) {
return newConfigError(fmt.Sprintf("conflict between images names: a nameless image cannot be specified in the config with multiple images!\n\n%s\n", dumpConfigDoc(image.raw.doc)))
}
if d, ok := imageByName[name]; ok {
return newConfigError(fmt.Sprintf("conflict between images names!\n\n%s%s\n", dumpConfigDoc(d.(*StapelImage).raw.doc), dumpConfigDoc(image.raw.doc)))
} else {
imageByName[name] = image
}
}
for _, image := range c.ImagesFromDockerfile {
name := image.Name
if name == "" && (len(c.StapelImages) > 1 || len(c.ImagesFromDockerfile) > 1) {
return newConfigError(fmt.Sprintf("conflict between images names: a nameless image cannot be specified in the config with multiple images!\n\n%s\n", dumpConfigDoc(image.raw.doc)))
}
if d, ok := imageByName[name]; ok {
var doc string
switch i := d.(type) {
case *StapelImage:
doc = dumpConfigDoc(i.raw.doc)
case *ImageFromDockerfile:
doc = dumpConfigDoc(i.raw.doc)
}
return newConfigError(fmt.Sprintf("conflict between images names!\n\n%s%s\n", doc, dumpConfigDoc(image.raw.doc)))
} else {
imageByName[name] = image
}
}
imageArtifactByName := map[string]*StapelImageArtifact{}
for _, artifact := range c.Artifacts {
name := artifact.Name
if a, ok := imageArtifactByName[name]; ok {
return newConfigError(fmt.Sprintf("conflict between artifacts names!\n\n%s%s\n", dumpConfigDoc(a.raw.doc), dumpConfigDoc(artifact.raw.doc)))
} else {
imageArtifactByName[name] = artifact
}
if iInterface, exist := imageByName[name]; exist {
var doc string
switch i := iInterface.(type) {
case *StapelImage:
doc = dumpConfigDoc(i.raw.doc)
case *ImageFromDockerfile:
doc = dumpConfigDoc(i.raw.doc)
}
return newConfigError(fmt.Sprintf("conflict between image and artifact names!\n\n%s%s\n", doc, dumpConfigDoc(artifact.raw.doc)))
} else {
imageArtifactByName[name] = artifact
}
}
return nil
}
func (c *WerfConfig) associateImportsArtifacts() error {
var relatedImageImages []ImageInterface
var artifactImports []*Import
for _, image := range c.StapelImages {
relatedImageImages = append(relatedImageImages, c.relatedImageImages(image)...)
}
for _, artifact := range c.Artifacts {
relatedImageImages = append(relatedImageImages, c.relatedImageImages(artifact)...)
}
for _, relatedImageInterface := range relatedImageImages {
switch relatedImageInterface.(type) {
case *StapelImage:
artifactImports = append(artifactImports, relatedImageInterface.(*StapelImage).Import...)
case *StapelImageArtifact:
artifactImports = append(artifactImports, relatedImageInterface.(*StapelImageArtifact).Import...)
}
}
for _, artifactImport := range artifactImports {
if err := c.validateImportImage(artifactImport); err != nil {
return err
}
}
return nil
}
func (c *WerfConfig) validateImportImage(i *Import) error {
if i.ImageName != "" {
if interf := c.GetImage(i.ImageName); interf == nil {
var imageName string
switch image := interf.(type) {
case *StapelImage:
imageName = image.Name
case *ImageFromDockerfile:
imageName = image.Name
}
return newDetailedConfigError(fmt.Sprintf("no such image `%s`!", imageName), i.raw, i.raw.rawStapelImage.doc)
}
} else if i.ArtifactName != "" {
if imageArtifact := c.GetArtifact(i.ArtifactName); imageArtifact == nil {
return newDetailedConfigError(fmt.Sprintf("no such artifact `%s`!", i.ArtifactName), i.raw, i.raw.rawStapelImage.doc)
}
}
return nil
}
func (c *WerfConfig) validateImagesFrom() error {
for _, image := range c.StapelImages {
if err := c.validateImageFrom(image.StapelImageBase); err != nil {
return err
}
}
for _, image := range c.Artifacts {
if err := c.validateImageFrom(image.StapelImageBase); err != nil {
return err
}
}
return nil
}
func (c *WerfConfig) validateImageFrom(i *StapelImageBase) error {
if i.raw.FromImage != "" {
fromImageName := i.raw.FromImage
if fromImageName == i.Name {
return newDetailedConfigError(fmt.Sprintf("cannot use own image name as `fromImage` directive value!"), nil, i.raw.doc)
}
if interf := c.GetImage(fromImageName); interf == nil {
return newDetailedConfigError(fmt.Sprintf("no such image `%s`!", fromImageName), i.raw, i.raw.doc)
}
} else if i.raw.FromArtifact != "" {
fromArtifactName := i.raw.FromArtifact
if fromArtifactName == i.Name {
return newDetailedConfigError(fmt.Sprintf("cannot use own image name as `fromArtifact` directive value!"), nil, i.raw.doc)
}
if imageArtifact := c.GetArtifact(fromArtifactName); imageArtifact == nil {
return newDetailedConfigError(fmt.Sprintf("no such image artifact `%s`!", fromArtifactName), i.raw, i.raw.doc)
}
}
return nil
}
func (c *WerfConfig) validateInfiniteLoopBetweenRelatedImages() error {
var imageAndArtifactNames []string
for _, image := range c.StapelImages {
imageAndArtifactNames = append(imageAndArtifactNames, image.Name)
}
for _, artifact := range c.Artifacts {
imageAndArtifactNames = append(imageAndArtifactNames, artifact.Name)
}
for _, imageOrArtifactName := range imageAndArtifactNames {
if err, errImagesStack := c.validateImageInfiniteLoop(imageOrArtifactName, []string{}); err != nil {
return fmt.Errorf("%s: %s", err, strings.Join(errImagesStack, " -> "))
}
}
return nil
}
func (c *WerfConfig) ImagesWithDependenciesBySets(images []ImageInterface) (sets [][]ImageInterface) {
sets = [][]ImageInterface{}
isDepChecked := map[ImageInterface]bool{}
imageDepsToHandle := c.imageDependenciesInOrder(images)
for len(imageDepsToHandle) != 0 {
var currentDeps []ImageInterface
outerLoop:
for image, deps := range imageDepsToHandle {
for _, dep := range deps {
_, ok := isDepChecked[dep]
if !ok {
continue outerLoop
}
}
currentDeps = append(currentDeps, image)
}
for _, dep := range currentDeps {
isDepChecked[dep] = true
delete(imageDepsToHandle, dep)
}
sets = append(sets, currentDeps)
}
return sets
}
func (c *WerfConfig) imageDependenciesInOrder(images []ImageInterface) (imageDeps map[ImageInterface][]ImageInterface) {
imageDeps = map[ImageInterface][]ImageInterface{}
stack := images
for len(stack) != 0 {
current := stack[0]
stack = stack[1:]
imageDeps[current] = c.imageDependencies(current)
outerLoop:
for _, dep := range imageDeps[current] {
for key, _ := range imageDeps {
if key == dep {
continue outerLoop
}
}
stack = append(stack, dep)
}
}
return imageDeps
}
func (c *WerfConfig) imageDependencies(interf ImageInterface) (deps []ImageInterface) {
switch i := interf.(type) {
case StapelImageInterface:
if i.ImageBaseConfig().FromImageName != "" {
deps = append(deps, c.GetImage(i.ImageBaseConfig().FromImageName))
}
if i.ImageBaseConfig().FromArtifactName != "" {
deps = append(deps, c.GetArtifact(i.ImageBaseConfig().FromArtifactName))
}
for _, imp := range i.imports() {
if imp.ImageName != "" {
deps = append(deps, c.GetImage(imp.ImageName))
} else if imp.ArtifactName != "" {
deps = append(deps, c.GetArtifact(imp.ArtifactName))
}
}
case *ImageFromDockerfile:
}
return deps
}
func (c *WerfConfig) relatedImageImages(interf ImageInterface) (images []ImageInterface) {
images = append(images, interf)
switch i := interf.(type) {
case StapelImageInterface:
if i.ImageBaseConfig().FromImageName != "" {
images = append(images, c.relatedImageImages(c.GetImage(i.ImageBaseConfig().FromImageName))...)
}
if i.ImageBaseConfig().FromArtifactName != "" {
images = append(images, c.relatedImageImages(c.GetArtifact(i.ImageBaseConfig().FromArtifactName))...)
}
case *ImageFromDockerfile:
}
return
}
func (c *WerfConfig) validateImageInfiniteLoop(imageOrArtifactName string, imageNameStack []string) (error, []string) {
for _, stackImageName := range imageNameStack {
if stackImageName == imageOrArtifactName {
return errors.New("infinite loop detected"), []string{imageOrArtifactName}
}
}
imageNameStack = append(imageNameStack, imageOrArtifactName)
var image StapelImageInterface
interf := c.GetImage(imageOrArtifactName)
if interf != nil {
switch i := interf.(type) {
case *ImageFromDockerfile:
return nil, imageNameStack
case *StapelImage:
image = i
}
} else {
image = c.GetArtifact(imageOrArtifactName)
if image == nil {
panic("runtime error")
}
}
imageBaseConfig := image.ImageBaseConfig()
if imageBaseConfig == nil {
panic("runtime error")
}
if imageBaseConfig.FromImageName != "" {
if err, errImagesStack := c.validateImageInfiniteLoop(imageBaseConfig.FromImageName, imageNameStack); err != nil {
return err, append([]string{imageOrArtifactName}, errImagesStack...)
}
}
if imageBaseConfig.FromArtifactName != "" {
if err, errImagesStack := c.validateImageInfiniteLoop(imageBaseConfig.FromArtifactName, imageNameStack); err != nil {
return err, append([]string{imageOrArtifactName}, errImagesStack...)
}
}
for _, imp := range image.imports() {
if imp.ImageName != "" {
var importImageName string
switch i := c.GetImage(imp.ImageName).(type) {
case *ImageFromDockerfile:
importImageName = i.Name
case *StapelImage:
importImageName = i.Name
}
if err, errImagesStack := c.validateImageInfiniteLoop(importImageName, imageNameStack); err != nil {
return err, append([]string{imageOrArtifactName}, errImagesStack...)
}
} else if imp.ArtifactName != "" {
if err, errImagesStack := c.validateImageInfiniteLoop(imp.ArtifactName, imageNameStack); err != nil {
return err, append([]string{imageOrArtifactName}, errImagesStack...)
}
}
}
return nil, nil
}