-
Notifications
You must be signed in to change notification settings - Fork 200
/
initialization_phase.go
476 lines (370 loc) · 13 KB
/
initialization_phase.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package build
import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/flant/werf/pkg/build/stage"
"github.com/flant/werf/pkg/config"
"github.com/flant/werf/pkg/git_repo"
"github.com/flant/werf/pkg/logger"
"github.com/flant/werf/pkg/slug"
"github.com/flant/werf/pkg/werf"
)
type InitializationPhase struct{}
func NewInitializationPhase() *InitializationPhase {
return &InitializationPhase{}
}
func (p *InitializationPhase) Run(c *Conveyor) (err error) {
return logger.LogProcess("Determining of stages", logger.LogProcessOptions{}, func() error {
return p.run(c)
})
}
func (p *InitializationPhase) run(c *Conveyor) error {
imagesInOrder, err := generateImagesInOrder(c.werfConfig.Images, c)
if err != nil {
return err
}
c.imagesInOrder = imagesInOrder
return nil
}
func generateImagesInOrder(imageConfigs []*config.Image, c *Conveyor) ([]*Image, error) {
var images []*Image
imagesInterfaceConfigs := getImageConfigsInOrder(imageConfigs, c)
for _, imageInterfaceConfig := range imagesInterfaceConfigs {
imageName := ImageLogProcessName(imageInterfaceConfig.ImageBaseConfig().Name, imageInterfaceConfig.IsArtifact())
err := logger.LogProcess(imageName, logger.LogProcessOptions{ColorizeMsgFunc: ImageLogProcessColorizeFunc(imageInterfaceConfig.IsArtifact())}, func() error {
image, err := generateImage(imageInterfaceConfig, c)
if err != nil {
return err
}
images = append(images, image)
return nil
})
if err != nil {
return nil, err
}
}
return images, nil
}
func generateImage(imageInterfaceConfig config.ImageInterface, c *Conveyor) (*Image, error) {
image := &Image{}
imageBaseConfig := imageInterfaceConfig.ImageBaseConfig()
imageName := imageBaseConfig.Name
imageArtifact := imageInterfaceConfig.IsArtifact()
from, fromImageName := getFromAndFromImageName(imageBaseConfig)
image.name = imageName
image.baseImageName = from
image.baseImageImageName = fromImageName
image.isArtifact = imageArtifact
stages, err := generateStages(imageInterfaceConfig, c)
if err != nil {
return nil, err
}
image.SetStages(stages)
return image, nil
}
func getFromAndFromImageName(imageBaseConfig *config.ImageBase) (string, string) {
var from string
var fromImageName string
if imageBaseConfig.From != "" {
from = imageBaseConfig.From
} else {
fromImage := imageBaseConfig.FromImage
fromImageArtifact := imageBaseConfig.FromImageArtifact
if fromImage != nil {
fromImageName = fromImage.Name
} else {
fromImageName = fromImageArtifact.Name
}
}
return from, fromImageName
}
func getImageConfigsInOrder(imageConfigs []*config.Image, c *Conveyor) []config.ImageInterface {
var images []config.ImageInterface
for _, image := range getImageConfigToProcess(imageConfigs, c) {
imagesInBuildOrder := image.ImageTree()
for i := 0; i < len(imagesInBuildOrder); i++ {
if isNotInArr(images, imagesInBuildOrder[i]) {
images = append(images, imagesInBuildOrder[i])
}
}
}
return images
}
func getImageConfigToProcess(imageConfigs []*config.Image, c *Conveyor) []*config.Image {
var imageConfigsToProcess []*config.Image
if len(c.imageNamesToProcess) == 0 {
imageConfigsToProcess = imageConfigs
} else {
for _, imageName := range c.imageNamesToProcess {
imageToProcess := getImageConfigByName(imageConfigs, imageName)
if imageToProcess == nil {
logger.LogErrorF("WARNING: Specified image '%s' isn't defined in werf.yaml!\n", imageName)
} else {
imageConfigsToProcess = append(imageConfigsToProcess, imageToProcess)
}
}
}
return imageConfigsToProcess
}
func getImageConfigByName(imageConfigs []*config.Image, name string) *config.Image {
for _, image := range imageConfigs {
if image.Name == name {
return image
}
}
return nil
}
func isNotInArr(arr []config.ImageInterface, obj config.ImageInterface) bool {
for _, elm := range arr {
if reflect.DeepEqual(elm, obj) {
return false
}
}
return true
}
func generateStages(imageInterfaceConfig config.ImageInterface, c *Conveyor) ([]stage.Interface, error) {
var stages []stage.Interface
imageBaseConfig := imageInterfaceConfig.ImageBaseConfig()
imageName := imageBaseConfig.Name
imageArtifact := imageInterfaceConfig.IsArtifact()
baseStageOptions := &stage.NewBaseStageOptions{
ImageName: imageName,
ConfigMounts: imageBaseConfig.Mount,
ImageTmpDir: c.GetImageTmpDir(imageBaseConfig.Name),
ContainerWerfDir: c.containerWerfDir,
ProjectName: c.werfConfig.Meta.Project,
}
gitArchiveStageOptions := &stage.NewGitArchiveStageOptions{
ArchivesDir: getImageArchivesDir(imageName, c),
ContainerArchivesDir: getImageArchivesContainerDir(c),
}
gitPatchStageOptions := &stage.NewGitPatchStageOptions{
PatchesDir: getImagePatchesDir(imageName, c),
ContainerPatchesDir: getImagePatchesContainerDir(c),
}
gitPaths, err := generateGitPaths(imageBaseConfig, c)
if err != nil {
return nil, err
}
gitPathsExist := len(gitPaths) != 0
// from
stages = appendIfExist(stages, stage.GenerateFromStage(imageBaseConfig, baseStageOptions))
// before_install
stages = appendIfExist(stages, stage.GenerateBeforeInstallStage(imageBaseConfig, baseStageOptions))
// before_install_artifact
stages = appendIfExist(stages, stage.GenerateArtifactImportBeforeInstallStage(imageBaseConfig, baseStageOptions))
if gitPathsExist {
// git_archive_stage
stages = append(stages, stage.NewGitArchiveStage(gitArchiveStageOptions, baseStageOptions))
}
// install
stages = appendIfExist(stages, stage.GenerateInstallStage(imageBaseConfig, gitPatchStageOptions, baseStageOptions))
// after_install_artifact
stages = appendIfExist(stages, stage.GenerateArtifactImportAfterInstallStage(imageBaseConfig, baseStageOptions))
// before_setup
stages = appendIfExist(stages, stage.GenerateBeforeSetupStage(imageBaseConfig, gitPatchStageOptions, baseStageOptions))
// before_setup_artifact
stages = appendIfExist(stages, stage.GenerateArtifactImportBeforeSetupStage(imageBaseConfig, baseStageOptions))
// setup
stages = appendIfExist(stages, stage.GenerateSetupStage(imageBaseConfig, gitPatchStageOptions, baseStageOptions))
// after_setup_artifact
stages = appendIfExist(stages, stage.GenerateArtifactImportAfterSetupStage(imageBaseConfig, baseStageOptions))
if !imageArtifact {
if gitPathsExist {
// git_cache
stages = append(stages, stage.NewGitCacheStage(gitPatchStageOptions, baseStageOptions))
// git_latest_patch
stages = append(stages, stage.NewGitLatestPatchStage(gitPatchStageOptions, baseStageOptions))
}
// docker_instructions
stages = appendIfExist(stages, stage.GenerateDockerInstructionsStage(imageInterfaceConfig.(*config.Image), baseStageOptions))
}
if len(gitPaths) != 0 {
logger.LogInfoLn("Using git stages")
for _, s := range stages {
s.SetGitPaths(gitPaths)
}
}
return stages, nil
}
func generateGitPaths(imageBaseConfig *config.ImageBase, c *Conveyor) ([]*stage.GitPath, error) {
var gitPaths []*stage.GitPath
var localGitRepo *git_repo.Local
if len(imageBaseConfig.Git.Local) != 0 {
localGitRepo = &git_repo.Local{
Base: git_repo.Base{Name: "own"},
Path: c.projectDir,
GitDir: path.Join(c.projectDir, ".git"),
}
}
for _, localGitPathConfig := range imageBaseConfig.Git.Local {
gitPaths = append(gitPaths, gitLocalPathInit(localGitPathConfig, localGitRepo, imageBaseConfig.Name, c))
}
for _, remoteGitPathConfig := range imageBaseConfig.Git.Remote {
remoteGitRepo, exist := c.remoteGitRepos[remoteGitPathConfig.Name]
if !exist {
clonePath, err := getRemoteGitRepoClonePath(remoteGitPathConfig, c)
if err != nil {
return nil, err
}
if err := os.MkdirAll(filepath.Dir(clonePath), os.ModePerm); err != nil {
return nil, fmt.Errorf("unable to mkdir %s: %s", filepath.Dir(clonePath), err)
}
remoteGitRepo = &git_repo.Remote{
Base: git_repo.Base{Name: remoteGitPathConfig.Name},
Url: remoteGitPathConfig.Url,
ClonePath: clonePath,
}
if err := logger.LogSecondaryProcess(fmt.Sprintf("Refreshing %s git", remoteGitPathConfig.Name), logger.LogProcessOptions{}, func() error {
return remoteGitRepo.CloneAndFetch()
}); err != nil {
return nil, err
}
c.remoteGitRepos[remoteGitPathConfig.Name] = remoteGitRepo
}
gitPaths = append(gitPaths, gitRemoteArtifactInit(remoteGitPathConfig, remoteGitRepo, imageBaseConfig.Name, c))
}
var res []*stage.GitPath
if len(gitPaths) != 0 {
err := logger.LogSecondaryProcess(fmt.Sprintf("Checking git paths"), logger.LogProcessOptions{}, func() error {
nonEmptyGitPaths, err := getNonEmptyGitPaths(gitPaths)
if err != nil {
return err
}
res = nonEmptyGitPaths
return nil
})
if err != nil {
return nil, err
}
}
return res, nil
}
func getNonEmptyGitPaths(gitPaths []*stage.GitPath) ([]*stage.GitPath, error) {
var nonEmptyGitPaths []*stage.GitPath
for _, gitPath := range gitPaths {
commit, err := gitPath.LatestCommit()
if err != nil {
return nil, fmt.Errorf("unable to get commit of repo '%s': %s", gitPath.GitRepo().GetName(), err)
}
cwd := gitPath.Cwd
if cwd == "" {
cwd = "/"
}
if empty, err := gitPath.IsEmpty(); err != nil {
return nil, err
} else if !empty {
logger.LogInfoF("Using commit %s of %s git path %s to %s\n", commit, gitPath.GitRepo().GetName(), cwd, gitPath.To)
nonEmptyGitPaths = append(nonEmptyGitPaths, gitPath)
} else {
logger.LogErrorF("Ignore empty commit %s of %s git path %s to %s\n", commit, gitPath.GitRepo().GetName(), cwd, gitPath.To)
for _, p := range gitPath.IncludePaths {
logger.LogErrorF(" include path: %s\n", p)
}
for _, p := range gitPath.ExcludePaths {
logger.LogErrorF(" exclude path: %s\n", p)
}
}
logger.LogOptionalLn()
}
return nonEmptyGitPaths, nil
}
func getRemoteGitRepoClonePath(remoteGitPathConfig *config.GitRemote, c *Conveyor) (string, error) {
scheme, err := urlScheme(remoteGitPathConfig.Url)
if err != nil {
return "", err
}
clonePath := path.Join(
werf.GetLocalCacheDir(),
"remote_git_repos",
"projects",
c.werfConfig.Meta.Project,
fmt.Sprintf("%v", git_repo.RemoteGitRepoCacheVersion),
slug.Slug(remoteGitPathConfig.Name),
scheme,
)
return clonePath, nil
}
func urlScheme(urlString string) (string, error) {
u, err := url.Parse(urlString)
if err != nil {
if strings.HasSuffix(err.Error(), "first path segment in URL cannot contain colon") {
for _, protocol := range []string{"git", "ssh"} {
if strings.HasPrefix(urlString, fmt.Sprintf("%s@", protocol)) {
return "ssh", nil
}
}
}
return "", err
}
return u.Scheme, nil
}
func gitRemoteArtifactInit(remoteGitPathConfig *config.GitRemote, remoteGitRepo *git_repo.Remote, imageName string, c *Conveyor) *stage.GitPath {
gitPath := baseGitPathInit(remoteGitPathConfig.GitLocalExport, imageName, c)
gitPath.Tag = remoteGitPathConfig.Tag
gitPath.Commit = remoteGitPathConfig.Commit
gitPath.Branch = remoteGitPathConfig.Branch
gitPath.Name = remoteGitPathConfig.Name
gitPath.GitRepoInterface = remoteGitRepo
return gitPath
}
func gitLocalPathInit(localGitPathConfig *config.GitLocal, localGitRepo *git_repo.Local, imageName string, c *Conveyor) *stage.GitPath {
gitPath := baseGitPathInit(localGitPathConfig.GitLocalExport, imageName, c)
gitPath.As = localGitPathConfig.As
gitPath.Name = "own"
gitPath.GitRepoInterface = localGitRepo
return gitPath
}
func baseGitPathInit(local *config.GitLocalExport, imageName string, c *Conveyor) *stage.GitPath {
var stageDependencies map[stage.StageName][]string
if local.StageDependencies != nil {
stageDependencies = stageDependenciesToMap(local.StageDependencies)
}
gitPath := &stage.GitPath{
PatchesDir: getImagePatchesDir(imageName, c),
ContainerPatchesDir: getImagePatchesContainerDir(c),
ArchivesDir: getImageArchivesDir(imageName, c),
ContainerArchivesDir: getImageArchivesContainerDir(c),
RepoPath: path.Join("/", local.Add),
Cwd: local.Add,
To: local.To,
ExcludePaths: local.ExcludePaths,
IncludePaths: local.IncludePaths,
Owner: local.Owner,
Group: local.Group,
StagesDependencies: stageDependencies,
}
return gitPath
}
func getImagePatchesDir(imageName string, c *Conveyor) string {
return path.Join(c.tmpDir, imageName, "patch")
}
func getImagePatchesContainerDir(c *Conveyor) string {
return path.Join(c.containerWerfDir, "patch")
}
func getImageArchivesDir(imageName string, c *Conveyor) string {
return path.Join(c.tmpDir, imageName, "archive")
}
func getImageArchivesContainerDir(c *Conveyor) string {
return path.Join(c.containerWerfDir, "archive")
}
func stageDependenciesToMap(sd *config.StageDependencies) map[stage.StageName][]string {
result := map[stage.StageName][]string{
stage.Install: sd.Install,
stage.BeforeSetup: sd.BeforeSetup,
stage.Setup: sd.Setup,
}
return result
}
func appendIfExist(stages []stage.Interface, stage stage.Interface) []stage.Interface {
if !reflect.ValueOf(stage).IsNil() {
logger.LogInfoF("Using stage %s\n", stage.Name())
return append(stages, stage)
}
return stages
}