-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
340 lines (305 loc) · 8.61 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
338
339
340
package infra
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/outofforest/isolator"
"github.com/outofforest/isolator/client"
"github.com/outofforest/isolator/client/wire"
"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"
)
type cloneFromFn func(srcBuildKey types.BuildKey) (types.BuildInfo, error)
// 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, specFile, name string, tags ...types.Tag) (types.BuildID, error) {
return b.buildFromFile(ctx, map[types.BuildKey]bool{}, specFile, name, tags...)
}
// Build builds images
func (b *Builder) Build(ctx context.Context, img *description.Descriptor) (types.BuildID, error) {
return b.build(ctx, map[types.BuildKey]bool{}, img)
}
func (b *Builder) buildFromFile(ctx context.Context, 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, stack, description.Describe(name, tags, commands...))
}
func (b *Builder) initialize(buildKey types.BuildKey, path string) (retErr error) {
if buildKey.Name == "scratch" {
return nil
}
// permissions on root dir has to be set to 755 to allow read access for everyone so linux boots correctly
root := filepath.Join(path, "root")
if err := os.Mkdir(root, 0o755); err != nil {
return err
}
return b.initializer.Init(root, buildKey)
}
func (b *Builder) build(ctx context.Context, stack map[types.BuildKey]bool, img *description.Descriptor) (retBuildID types.BuildID, retErr error) {
if !types.IsNameValid(img.Name()) {
return "", fmt.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 "", fmt.Errorf("tag %s is invalid", tag)
}
key := types.NewBuildKey(img.Name(), tag)
if stack[key] {
return "", fmt.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
var terminateIsolator func() error
defer func() {
if terminateIsolator != nil {
if err := terminateIsolator(); retErr == nil {
retErr = err
}
}
if path != "" {
if err := os.Remove(filepath.Join(path, "root", ".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 len(img.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(types.NewBuildKey(img.Name(), tags[0]), path); err != nil {
return "", err
}
} else {
var build *imageBuild
build = newImageBuild(func(srcBuildKey types.BuildKey) (types.BuildInfo, error) {
if !types.IsNameValid(srcBuildKey.Name) {
return types.BuildInfo{}, fmt.Errorf("name %s is invalid", srcBuildKey.Name)
}
if !srcBuildKey.Tag.IsValid() {
return types.BuildInfo{}, fmt.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, stack, srcBuildKey.Name, srcBuildKey.Name, description.DefaultTag)
}
default:
return 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, stack, baseImage)
} else {
_, err = b.build(ctx, stack, description.Describe(srcBuildKey.Name, types.Tags{srcBuildKey.Tag}))
}
default:
return types.BuildInfo{}, err
}
if err != nil {
return types.BuildInfo{}, err
}
if !srcBuildID.IsValid() {
srcBuildID, err = b.storage.BuildID(ctx, srcBuildKey)
if err != nil {
return types.BuildInfo{}, err
}
}
if !srcBuildID.Type().Properties().Cloneable {
return types.BuildInfo{}, fmt.Errorf("build %s is not cloneable", srcBuildKey)
}
imgFinalize, path, err = b.storage.Clone(ctx, srcBuildID, img.Name(), buildID)
if err != nil {
return types.BuildInfo{}, err
}
buildInfo, err := b.storage.Info(ctx, srcBuildID)
if err != nil {
return types.BuildInfo{}, err
}
build.isolator, terminateIsolator, err = isolator.Start(isolator.Config{
Dir: path,
Executor: wire.Config{
Mounts: []wire.Mount{
{
Host: ".",
Container: "/.specdir",
Writable: true,
},
},
},
})
if err != nil {
return types.BuildInfo{}, err
}
return buildInfo, nil
})
for _, cmd := range img.Commands() {
select {
case <-ctx.Done():
return "", ctx.Err()
default:
}
if err := cmd.Execute(build); err != nil {
return "", err
}
}
build.manifest.BuildID = buildID
if err := b.storage.StoreManifest(ctx, build.manifest); 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 newImageBuild(cloneFn cloneFromFn) *imageBuild {
return &imageBuild{
cloneFn: cloneFn,
}
}
type imageBuild struct {
cloneFn cloneFromFn
fromDone bool
manifest types.ImageManifest
isolator *client.Client
}
// from is a handler for FROM
func (b *imageBuild) From(cmd *description.FromCommand) error {
if b.fromDone {
return errors.New("directive FROM may be specified only once")
}
buildInfo, err := b.cloneFn(cmd.BuildKey)
if err != nil {
return err
}
b.manifest.BasedOn = buildInfo.BuildID
b.manifest.Params = buildInfo.Params
b.fromDone = true
return nil
}
// params sets kernel params for image
func (b *imageBuild) Params(cmd *description.ParamsCommand) error {
if !b.fromDone {
return errors.New("description has to start with FROM directive")
}
b.manifest.Params = append(b.manifest.Params, cmd.Params...)
return nil
}
// run is a handler for RUN
func (b *imageBuild) Run(cmd *description.RunCommand) (retErr error) {
if !b.fromDone {
return errors.New("description has to start with FROM directive")
}
if err := b.isolator.Send(wire.Execute{Command: cmd.Command}); err != nil {
return err
}
for {
msg, err := b.isolator.Receive()
if err != nil {
return err
}
switch m := msg.(type) {
case wire.Log:
stream, err := toStream(m.Stream)
if err != nil {
return err
}
if _, err := stream.Write([]byte(m.Text)); err != nil {
return err
}
case wire.Result:
if m.Error != "" {
return fmt.Errorf("command failed: %s", m.Error)
}
return nil
default:
return errors.New("unexpected message received")
}
}
}
func toStream(stream wire.Stream) (*os.File, error) {
var f *os.File
switch stream {
case wire.StreamOut:
f = os.Stdout
case wire.StreamErr:
f = os.Stderr
default:
return nil, fmt.Errorf("unknown stream: %d", stream)
}
return f, nil
}