-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
337 lines (298 loc) · 8.88 KB
/
build.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
package infra
import (
"context"
"os"
"path/filepath"
"github.com/outofforest/isolator"
"github.com/outofforest/isolator/wire"
"github.com/pkg/errors"
"github.com/outofforest/osman/config"
"github.com/outofforest/osman/infra/base"
"github.com/outofforest/osman/infra/description"
"github.com/outofforest/osman/infra/parser"
"github.com/outofforest/osman/infra/storage"
"github.com/outofforest/osman/infra/types"
)
// NewBuilder creates new image builder
func NewBuilder(config config.Build, initializer base.Initializer, repo *Repository, storage storage.Driver, parser parser.Parser) *Builder {
return &Builder{
rebuild: config.Rebuild,
readyBuilds: map[types.BuildKey]bool{},
initializer: initializer,
repo: repo,
storage: storage,
parser: parser,
}
}
// Builder builds images
type Builder struct {
rebuild bool
readyBuilds map[types.BuildKey]bool
initializer base.Initializer
repo *Repository
storage storage.Driver
parser parser.Parser
}
// BuildFromFile builds image from spec file
func (b *Builder) BuildFromFile(ctx context.Context, cacheDir string, specFile, name string, tags ...types.Tag) (types.BuildID, error) {
return b.buildFromFile(ctx, cacheDir, map[types.BuildKey]bool{}, specFile, name, tags...)
}
// Build builds images
func (b *Builder) Build(ctx context.Context, cacheDir string, img *description.Descriptor) (types.BuildID, error) {
return b.build(ctx, cacheDir, map[types.BuildKey]bool{}, img)
}
func (b *Builder) buildFromFile(ctx context.Context, cacheDir string, stack map[types.BuildKey]bool, specFile, name string, tags ...types.Tag) (types.BuildID, error) {
commands, err := b.parser.Parse(specFile)
if err != nil {
return "", err
}
return b.build(ctx, cacheDir, stack, description.Describe(name, tags, commands...))
}
func (b *Builder) initialize(ctx context.Context, cacheDir string, buildKey types.BuildKey, path string) (retErr error) {
if buildKey.Name == "scratch" {
return nil
}
// permissions on path dir has to be set to 755 to allow read access for everyone so linux boots correctly
return b.initializer.Init(ctx, cacheDir, path, buildKey)
}
func (b *Builder) build(ctx context.Context, cacheDir string, stack map[types.BuildKey]bool, img *description.Descriptor) (retBuildID types.BuildID, retErr error) {
if !types.IsNameValid(img.Name()) {
return "", errors.Errorf("name %s is invalid", img.Name())
}
tags := img.Tags()
if len(tags) == 0 {
tags = types.Tags{description.DefaultTag}
}
keys := make([]types.BuildKey, 0, len(tags))
for _, tag := range tags {
if !tag.IsValid() {
return "", errors.Errorf("tag %s is invalid", tag)
}
key := types.NewBuildKey(img.Name(), tag)
if stack[key] {
return "", errors.Errorf("loop in dependencies detected on image %s", key)
}
stack[key] = true
keys = append(keys, key)
}
buildID := types.NewBuildID(types.BuildTypeImage)
var imgFinalize storage.FinalizeFn
var path string
defer func() {
if path != "" {
if err := os.Remove(filepath.Join(path, ".specdir")); err != nil && !os.IsNotExist(err) {
if retErr == nil {
retErr = err
}
return
}
}
if imgFinalize != nil {
if err := imgFinalize(); err != nil {
if retErr == nil {
retErr = err
}
return
}
}
if retErr != nil {
if err := b.storage.Drop(ctx, buildID); err != nil && !errors.Is(err, types.ErrImageDoesNotExist) {
retErr = err
}
return
}
}()
if commands := img.Commands(); len(commands) == 0 {
if len(tags) != 1 {
return "", errors.New("for base image exactly one tag is required")
}
var err error
imgFinalize, path, err = b.storage.CreateEmpty(ctx, img.Name(), buildID)
if err != nil {
return "", err
}
if err := b.initialize(ctx, cacheDir, types.NewBuildKey(img.Name(), tags[0]), path); err != nil {
return "", err
}
} else {
fromCommand, ok := commands[0].(*description.FromCommand)
if !ok {
return "", errors.New("first command must be FROM")
}
var err error
var buildInfo types.BuildInfo
imgFinalize, path, buildInfo, err = b.clone(
ctx,
fromCommand.BuildKey,
cacheDir,
stack,
img,
buildID,
)
if err != nil {
return "", err
}
err = isolator.Run(ctx, isolator.Config{
Dir: path,
Types: []interface{}{
wire.Result{},
wire.Log{},
},
Executor: wire.Config{
ConfigureSystem: true,
UseHostNetwork: true,
Mounts: []wire.Mount{
{
Host: ".",
Namespace: "/.specdir",
Writable: true,
},
},
},
}, func(ctx context.Context, incoming <-chan interface{}, outgoing chan<- interface{}) error {
build := newImageBuild(buildInfo, incoming, outgoing)
for _, cmd := range commands[1:] {
select {
case <-ctx.Done():
return errors.WithStack(ctx.Err())
default:
}
if err := cmd.Execute(ctx, build); err != nil {
return err
}
}
build.manifest.BuildID = buildID
return b.storage.StoreManifest(ctx, build.manifest)
})
if err != nil {
return "", err
}
}
for _, key := range keys {
if err := b.storage.Tag(ctx, buildID, key.Tag); err != nil {
return "", err
}
}
for _, key := range keys {
b.readyBuilds[key] = true
}
return buildID, nil
}
func (b *Builder) clone(
ctx context.Context,
srcBuildKey types.BuildKey,
cacheDir string,
stack map[types.BuildKey]bool,
img *description.Descriptor,
buildID types.BuildID,
) (storage.FinalizeFn, string, types.BuildInfo, error) {
if !types.IsNameValid(srcBuildKey.Name) {
return nil, "", types.BuildInfo{}, errors.Errorf("name %s is invalid", srcBuildKey.Name)
}
if !srcBuildKey.Tag.IsValid() {
return nil, "", types.BuildInfo{}, errors.Errorf("tag %s is invalid", srcBuildKey.Tag)
}
// Try to clone existing image
err := types.ErrImageDoesNotExist
var srcBuildID types.BuildID
if !b.rebuild || b.readyBuilds[srcBuildKey] {
srcBuildID, err = b.storage.BuildID(ctx, srcBuildKey)
}
switch {
case err == nil:
case errors.Is(err, types.ErrImageDoesNotExist):
// If image does not exist try to build it from file in the current directory but only if tag is a default one
if srcBuildKey.Tag == description.DefaultTag {
_, err = b.buildFromFile(ctx, cacheDir, stack, srcBuildKey.Name, srcBuildKey.Name, description.DefaultTag)
}
default:
return nil, "", types.BuildInfo{}, err
}
switch {
case err == nil:
case errors.Is(err, types.ErrImageDoesNotExist):
if baseImage := b.repo.Retrieve(srcBuildKey); baseImage != nil {
// If spec file does not exist, try building from repository
_, err = b.build(ctx, cacheDir, stack, baseImage)
} else {
_, err = b.build(ctx, cacheDir, stack, description.Describe(srcBuildKey.Name, types.Tags{srcBuildKey.Tag}))
}
default:
return nil, "", types.BuildInfo{}, err
}
if err != nil {
return nil, "", types.BuildInfo{}, err
}
if !srcBuildID.IsValid() {
srcBuildID, err = b.storage.BuildID(ctx, srcBuildKey)
if err != nil {
return nil, "", types.BuildInfo{}, err
}
}
if !srcBuildID.Type().Properties().Cloneable {
return nil, "", types.BuildInfo{}, errors.Errorf("build %s is not cloneable", srcBuildKey)
}
imgFinalize, path, err := b.storage.Clone(ctx, srcBuildID, img.Name(), buildID)
if err != nil {
return nil, "", types.BuildInfo{}, err
}
buildInfo, err := b.storage.Info(ctx, srcBuildID)
if err != nil {
return imgFinalize, "", types.BuildInfo{}, err
}
if err != nil {
return imgFinalize, "", types.BuildInfo{}, err
}
return imgFinalize, path, buildInfo, nil
}
var _ description.ImageBuild = &imageBuild{}
func newImageBuild(buildInfo types.BuildInfo, incoming <-chan interface{}, outgoing chan<- interface{}) *imageBuild {
return &imageBuild{
incoming: incoming,
outgoing: outgoing,
manifest: types.ImageManifest{
BasedOn: buildInfo.BuildID,
Params: buildInfo.Params,
},
}
}
type imageBuild struct {
incoming <-chan interface{}
outgoing chan<- interface{}
manifest types.ImageManifest
}
// Params sets kernel params for image
func (b *imageBuild) Params(cmd *description.ParamsCommand) {
b.manifest.Params = append(b.manifest.Params, cmd.Params...)
}
// Run is a handler for RUN
func (b *imageBuild) Run(ctx context.Context, cmd *description.RunCommand) error {
select {
case <-ctx.Done():
return errors.WithStack(ctx.Err())
case b.outgoing <- wire.Execute{Command: cmd.Command}:
}
for content := range b.incoming {
switch m := content.(type) {
case wire.Log:
if _, err := os.Stderr.Write(m.Content); err != nil {
return err
}
if _, err := os.Stderr.Write([]byte{'\n'}); err != nil {
return err
}
case wire.Result:
if m.Error != "" {
return errors.Errorf("command failed: %s", m.Error)
}
return nil
default:
return errors.New("unexpected message received")
}
}
return errors.WithStack(ctx.Err())
}
// Boot sets boot option for an image
func (b *imageBuild) Boot(cmd *description.BootCommand) {
b.manifest.Boots = append(b.manifest.Boots, types.Boot{Title: cmd.Title, Params: cmd.Params})
}