-
Notifications
You must be signed in to change notification settings - Fork 200
/
image.go
143 lines (111 loc) · 3.08 KB
/
image.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
package build
import (
"fmt"
"github.com/fatih/color"
"github.com/flant/werf/pkg/build/stage"
"github.com/flant/werf/pkg/image"
"github.com/flant/werf/pkg/logger"
)
type Image struct {
name string
baseImageName string
baseImageImageName string
stages []stage.Interface
baseImage *image.StageImage
isArtifact bool
}
func (i *Image) LogName() string {
return ImageLogName(i.name, i.isArtifact)
}
func (i *Image) LogProcessName() string {
return ImageLogProcessName(i.name, i.isArtifact)
}
func (i *Image) LogProcessColorizeFunc() func(...interface{}) string {
return ImageLogProcessColorizeFunc(i.isArtifact)
}
func (i *Image) LogTagColorizeFunc() func(...interface{}) string {
return ImageLogTagColorizeFunc(i.isArtifact)
}
func ImageLogName(name string, isArtifact bool) string {
if !isArtifact {
if name == "" {
name = "~"
}
}
return name
}
func ImageLogProcessName(name string, isArtifact bool) string {
logName := ImageLogName(name, isArtifact)
if !isArtifact {
return fmt.Sprintf("⛵ image %s", logName)
} else {
return fmt.Sprintf("🛸 artifact %s", logName)
}
}
func ImageLogProcessColorizeFunc(isArtifact bool) func(...interface{}) string {
return imageDefaultColorizeFunc(isArtifact)
}
func ImageLogTagColorizeFunc(isArtifact bool) func(...interface{}) string {
return imageDefaultColorizeFunc(isArtifact)
}
func imageDefaultColorizeFunc(isArtifact bool) func(...interface{}) string {
var colorFormat []color.Attribute
if isArtifact {
colorFormat = []color.Attribute{color.FgCyan, color.Bold}
} else {
colorFormat = []color.Attribute{color.FgYellow, color.Bold}
}
return color.New(colorFormat...).Sprint
}
func (i *Image) SetStages(stages []stage.Interface) {
i.stages = stages
}
func (i *Image) GetStages() []stage.Interface {
return i.stages
}
func (i *Image) GetStage(name stage.StageName) stage.Interface {
for _, s := range i.stages {
if s.Name() == name {
return s
}
}
return nil
}
func (i *Image) LatestStage() stage.Interface {
return i.stages[len(i.stages)-1]
}
func (i *Image) GetName() string {
return i.name
}
func (i *Image) SetupBaseImage(c *Conveyor) {
baseImageName := i.baseImageName
if i.baseImageImageName != "" {
baseImageName = c.GetImage(i.baseImageImageName).LatestStage().GetImage().Name()
}
i.baseImage = c.GetOrCreateImage(nil, baseImageName)
}
func (i *Image) GetBaseImage() *image.StageImage {
return i.baseImage
}
func (i *Image) PrepareBaseImage(c *Conveyor) error {
fromImage := i.stages[0].GetImage()
if fromImage.IsExists() {
return nil
}
if i.baseImageImageName != "" {
return nil
}
return logger.LogProcess("Pulling base image", logger.LogProcessOptions{}, func() error {
if i.baseImage.IsExists() {
if err := i.baseImage.Pull(); err != nil {
logger.LogErrorF("WARNING: cannot pull base image %s: %s\n", i.baseImage.Name(), err)
logger.LogErrorF("WARNING: using existing image %s without pull\n", i.baseImage.Name())
}
return nil
}
if err := i.baseImage.Pull(); err != nil {
return fmt.Errorf("image %s pull failed: %s", i.baseImage.Name(), err)
}
return nil
})
}