/
stage.go
208 lines (172 loc) · 5.34 KB
/
stage.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
package builder
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"github.com/maxlaverse/image-builder/pkg/engine"
"github.com/maxlaverse/image-builder/pkg/fileutils"
"github.com/maxlaverse/image-builder/pkg/template"
log "github.com/sirupsen/logrus"
)
const (
dockerIgnoreName = ".dockerignore"
)
// StageImageStatus represents the status of the image corresponding to a
// stage (e.g. absent, cached, pulled)
type StageImageStatus string
const (
// Initialized is unknown
Initialized StageImageStatus = "initialize"
// ImageAbsent is for images not present locally not remotely
ImageAbsent StageImageStatus = "absent"
// ImageCached is for images that are found in the Application or Builder's cache
ImageCached StageImageStatus = "present-in-cache"
// ImagePulled is for images that were absent but could be pulled
ImagePulled StageImageStatus = "pulled"
// ImageBuilt is for images that have been built
ImageBuilt StageImageStatus = "built"
)
// BuildStage represents a individual stage which can be built
type BuildStage interface {
Build(engineBuild engine.BuildEngine) error
ComputeContentHash() error
ContentHash() string
Dockerfile() string
ContextFiles() ([]string, error)
GetRequiredStages() []string
GetTagAliases() []string
ImageTag() (string, error)
ImageURL() string
Name() string
Render() error
SetImageURL(source string)
SetSourceImageURL(source string)
SetStatus(status StageImageStatus)
SourceImageURL() string
Status() StageImageStatus
}
// buildStage represents a individual stage which can be built
type buildStage struct {
extraIncludePatterns []string
contentHash string
dockerfile template.Dockerfile
imageURL string
name string
sourceImageURL string
status StageImageStatus
}
// NewBuildStage returns a individual stage
func NewBuildStage(name string, dockerfile template.Dockerfile, extraIncludePatterns []string) BuildStage {
return &buildStage{
extraIncludePatterns: extraIncludePatterns,
dockerfile: dockerfile,
name: name,
status: Initialized,
}
}
func (b *buildStage) SetSourceImageURL(source string) {
b.sourceImageURL = source
}
func (b *buildStage) SetImageURL(source string) {
b.imageURL = source
}
func (b *buildStage) SetStatus(status StageImageStatus) {
b.status = status
}
// Build writes a Dockerfile and .dockerignore and calls the engine's build command
func (b *buildStage) Build(engineBuild engine.BuildEngine) error {
log.Infof("Build context for '%s' is '%s'", b.Name(), b.dockerfile.GetBuildContext())
dockerfilePath, err := writeDockerfile(b.dockerfile.GetContent())
if err != nil {
return fmt.Errorf("error writing 'Dockerfile' file: %w", err)
}
defer os.Remove(dockerfilePath)
dockerignorePath := path.Join(b.dockerfile.GetBuildContext(), dockerIgnoreName)
files, err := b.ContextFiles()
if err != nil {
return err
}
err = writeExplicitDockerIgnore(dockerignorePath, files)
if err != nil {
return fmt.Errorf("error writing '.dockerignore' file: %w", err)
}
defer os.Remove(dockerignorePath)
return engineBuild.Build(dockerfilePath, b.imageURL, b.dockerfile.GetBuildContext())
}
func (b *buildStage) ContextFiles() ([]string, error) {
contextFiles, err := fileutils.ListMatchingFiles(b.dockerfile.GetBuildContext(), append(b.extraIncludePatterns, b.dockerfile.GetContextIncludes()...))
if err != nil {
return contextFiles, fmt.Errorf("error listing files in context: %w", err)
}
return contextFiles, nil
}
func (b *buildStage) ComputeContentHash() error {
log.Tracef("Context directory is '%s'", b.dockerfile.GetBuildContext())
files, err := b.ContextFiles()
if err != nil {
return err
}
contentHash, err := fileutils.ContentHashing(b.dockerfile.GetBuildContext(), files, b.dockerfile.GetContentWithoutIgnoredLines())
if err != nil {
return fmt.Errorf("error computing ContentHash: %w", err)
}
b.contentHash = contentHash
return nil
}
func (b *buildStage) ContentHash() string {
if len(b.contentHash) == 0 {
b.ComputeContentHash()
}
return b.contentHash
}
func (b *buildStage) Dockerfile() string {
return b.dockerfile.GetContent()
}
func (b *buildStage) GetRequiredStages() []string {
return b.dockerfile.GetRequiredStages()
}
func (b *buildStage) GetTagAliases() []string {
return b.dockerfile.GetTagAliases()
}
func (b *buildStage) ImageURL() string {
return b.imageURL
}
func (b *buildStage) Render() error {
return b.dockerfile.Render()
}
func (b *buildStage) ImageTag() (string, error) {
if len(b.contentHash) == 0 {
b.ComputeContentHash()
}
if len(b.dockerfile.GetFriendlyTag()) > 0 {
return fmt.Sprintf("%s-%s", b.dockerfile.GetFriendlyTag(), b.contentHash), nil
}
return b.contentHash, nil
}
func (b *buildStage) Name() string {
return b.name
}
func (b *buildStage) Status() StageImageStatus {
return b.status
}
func (b *buildStage) SourceImageURL() string {
return b.sourceImageURL
}
func writeDockerfile(content string) (string, error) {
f, err := ioutil.TempFile("", "Dockerfile")
if err != nil {
return "", err
}
defer f.Close()
_, err = f.WriteString(content)
return f.Name(), err
}
func writeExplicitDockerIgnore(path string, filesToInclude []string) error {
content := "*\n"
if len(filesToInclude) > 0 {
content = content + "!" + strings.Join(filesToInclude, "\n!") + "\n"
}
return ioutil.WriteFile(path, []byte(content), 0644)
}