This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
forked from project-stacker/stacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
692 lines (575 loc) · 17 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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package stacker
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path"
"runtime"
"strings"
"time"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/opencontainers/umoci"
"github.com/opencontainers/umoci/mutate"
"github.com/opencontainers/umoci/oci/casext"
"github.com/pkg/errors"
"github.com/project-stacker/stacker/container"
"github.com/project-stacker/stacker/log"
"github.com/project-stacker/stacker/types"
)
const DefaultShell = "/usr/bin/sh"
type BuildArgs struct {
Config types.StackerConfig
LeaveUnladen bool
NoCache bool
Substitute []string
OnRunFailure string
LayerTypes []types.LayerType
OrderOnly bool
HashRequired bool
SetupOnly bool
Progress bool
}
// Builder is responsible for building the layers based on stackerfiles
type Builder struct {
builtStackerfiles types.StackerFiles // Keep track of all the Stackerfiles which were built
opts *BuildArgs // Build options
}
// NewBuilder initializes a new Builder struct
func NewBuilder(opts *BuildArgs) *Builder {
return &Builder{
builtStackerfiles: make(map[string]*types.Stackerfile, 1),
opts: opts,
}
}
func (b *Builder) updateOCIConfigForOutput(sf *types.Stackerfile, s types.Storage, oci casext.Engine, layerType types.LayerType, l types.Layer, name string) error {
opts := b.opts
layerName := layerType.LayerName(name)
descPaths, err := oci.ResolveReference(context.Background(), layerName)
if err != nil {
return err
}
mutator, err := mutate.New(oci, descPaths[0])
if err != nil {
return errors.Wrapf(err, "mutator failed")
}
config, err := mutator.Config(context.Background())
if err != nil {
return err
}
imageConfig := config.Config
if imageConfig.Labels == nil {
imageConfig.Labels = map[string]string{}
}
if len(l.GenerateLabels) > 0 {
writable, cleanup, err := s.TemporaryWritableSnapshot(name)
if err != nil {
return err
}
defer cleanup()
dir, err := ioutil.TempDir(opts.Config.StackerDir, fmt.Sprintf("oci-labels-%s-", name))
if err != nil {
return errors.Wrapf(err, "failed to create oci-labels tempdir")
}
defer os.RemoveAll(dir)
c, err := container.New(opts.Config, writable)
if err != nil {
return err
}
defer c.Close()
err = SetupBuildContainerConfig(opts.Config, s, c, writable)
if err != nil {
return err
}
err = c.BindMount(dir, "/oci-labels", "")
if err != nil {
return err
}
rootfs := path.Join(opts.Config.RootFSDir, writable, "rootfs")
runPath := path.Join(dir, ".stacker-run.sh")
err = generateShellForRunning(rootfs, l.GenerateLabels, runPath)
if err != nil {
return err
}
err = c.Execute("/oci-labels/.stacker-run.sh", nil)
if err != nil {
return err
}
ents, err := ioutil.ReadDir(dir)
if err != nil {
return errors.Wrapf(err, "failed to read %s", dir)
}
for _, ent := range ents {
if ent.Name() == ".stacker-run.sh" {
continue
}
content, err := ioutil.ReadFile(path.Join(dir, ent.Name()))
if err != nil {
return errors.Wrapf(err, "couldn't read label %s", ent.Name())
}
imageConfig.Labels[ent.Name()] = string(content)
}
}
pathSet := false
for k, v := range l.Environment {
if k == "PATH" {
pathSet = true
}
imageConfig.Env = append(imageConfig.Env, fmt.Sprintf("%s=%s", k, v))
}
if !pathSet {
for _, s := range imageConfig.Env {
if strings.HasPrefix(s, "PATH=") {
pathSet = true
break
}
}
}
// if the user didn't specify a path, let's set a sane one
if !pathSet {
imageConfig.Env = append(imageConfig.Env, fmt.Sprintf("PATH=%s", container.ReasonableDefaultPath))
}
imageConfig.Cmd = l.Cmd
imageConfig.Entrypoint = l.Entrypoint
if l.FullCommand != nil {
imageConfig.Cmd = nil
imageConfig.Entrypoint = l.FullCommand
}
if imageConfig.Volumes == nil {
imageConfig.Volumes = map[string]struct{}{}
}
for _, v := range l.Volumes {
imageConfig.Volumes[v] = struct{}{}
}
for k, v := range l.Labels {
imageConfig.Labels[k] = v
}
if l.WorkingDir != "" {
imageConfig.WorkingDir = l.WorkingDir
}
if l.RuntimeUser != "" {
imageConfig.User = l.RuntimeUser
}
meta, err := mutator.Meta(context.Background())
if err != nil {
return err
}
username := os.Getenv("SUDO_USER")
if username == "" {
user, err := user.Current()
if err != nil {
return err
}
username = user.Username
}
host, err := os.Hostname()
if err != nil {
return err
}
author := fmt.Sprintf("%s@%s", username, host)
meta.Created = time.Now()
meta.Architecture = runtime.GOARCH
meta.OS = runtime.GOOS
meta.Author = author
annotations, err := mutator.Annotations(context.Background())
if err != nil {
return err
}
// compute the git version for the directory that the stacker file is
// in. we don't care if it's not a git directory, because in that case
// we'll fall back to putting the whole stacker file contents in the
// metadata.
gitVersion, _ := GitVersion(sf.ReferenceDirectory)
if gitVersion != "" {
log.Debugf("setting git version annotation to %s", gitVersion)
annotations[GitVersionAnnotation] = gitVersion
}
annotations[StackerContentsAnnotation] = sf.AfterSubstitutions
history := ispec.History{
EmptyLayer: true, // this is only the history for imageConfig edit
Created: &meta.Created,
CreatedBy: "stacker build",
Author: author,
}
err = mutator.Set(context.Background(), imageConfig, meta, annotations, &history)
if err != nil {
return err
}
newPath, err := mutator.Commit(context.Background())
if err != nil {
return err
}
err = oci.UpdateReference(context.Background(), layerName, newPath.Root())
if err != nil {
return err
}
return nil
}
// Build builds a single stackerfile
func (b *Builder) build(s types.Storage, file string) error {
opts := b.opts
if opts.NoCache {
os.RemoveAll(opts.Config.StackerDir)
}
sf, err := types.NewStackerfile(file, opts.HashRequired, append(opts.Substitute, b.opts.Config.Substitutions()...))
if err != nil {
return err
}
order, err := sf.DependencyOrder(b.builtStackerfiles)
if err != nil {
return err
}
/* check that layers name don't contain ':', it will interfere with overlay mount options
which is using :s as separator */
for _, name := range order {
if strings.Contains(name, ":") {
return errors.Errorf("using ':' in the layer name (%s) is forbidden due to overlay constraints", name)
}
}
log.Debugf("Dependency Order %v", order)
var oci casext.Engine
if _, statErr := os.Stat(opts.Config.OCIDir); statErr != nil {
oci, err = umoci.CreateLayout(opts.Config.OCIDir)
} else {
oci, err = umoci.OpenLayout(opts.Config.OCIDir)
}
if err != nil {
return err
}
defer oci.Close()
// Add this stackerfile to the list of stackerfiles which were built
b.builtStackerfiles[file] = sf
buildCache, err := OpenCache(opts.Config, oci, b.builtStackerfiles)
if err != nil {
return err
}
for _, name := range order {
l, ok := sf.Get(name)
if !ok {
return errors.Errorf("%s not present in stackerfile?", name)
}
// if a container builds on another container in a stacker
// file, we can't correctly render the dependent container's
// filesystem, since we don't know what the output of the
// parent build will be. so let's refuse to run in setup-only
// mode in this case.
if opts.SetupOnly && l.From.Type == types.BuiltLayer {
return errors.Errorf("no built type layers (%s) allowed in setup mode", name)
}
log.Infof("preparing image %s...", name)
// We need to run the imports first since we now compare
// against imports for caching layers. Since we don't do
// network copies if the files are present and we use rsync to
// copy things across, hopefully this isn't too expensive.
err = CleanImportsDir(opts.Config, name, l.Imports, buildCache)
if err != nil {
return err
}
if err := Import(opts.Config, s, name, l.Imports, opts.Progress); err != nil {
return err
}
// Need to check if the image has bind mounts, if the image has bind mounts,
// it needs to be rebuilt regardless of the build cache
// The reason is that tracking build cache for bind mounted folders
// is too expensive, so we don't do it
baseOpts := BaseLayerOpts{
Config: opts.Config,
Name: name,
Layer: l,
Cache: buildCache,
OCI: oci,
LayerTypes: opts.LayerTypes,
Storage: s,
Progress: opts.Progress,
}
if err := GetBase(baseOpts); err != nil {
return err
}
cacheEntry, cacheHit, err := buildCache.Lookup(name)
if err != nil {
return err
}
if cacheHit && (len(l.Binds) == 0) {
if l.BuildOnly {
if cacheEntry.Name != name {
err = s.Snapshot(cacheEntry.Name, name)
if err != nil {
return err
}
}
continue
} else {
foundCount := 0
for _, layerType := range opts.LayerTypes {
blob, ok := cacheEntry.Manifests[layerType]
if ok {
foundCount += 1
layerName := layerType.LayerName(name)
err = oci.UpdateReference(context.Background(), layerName, blob)
if err != nil {
return err
}
log.Infof("found cached layer %s", layerName)
}
}
if foundCount == len(opts.LayerTypes) {
continue
}
log.Infof("missing some cached layer output types, building anyway")
}
} else if cacheHit && (len(l.Binds) > 0) {
log.Infof("rebuilding cached layer due to use of binds in stacker file")
}
err = SetupRootfs(baseOpts)
if err != nil {
return err
}
err = s.SetOverlayDirs(name, l.OverlayDirs, opts.LayerTypes)
if err != nil {
return err
}
c, err := container.New(opts.Config, name)
if err != nil {
return err
}
defer c.Close()
err = SetupBuildContainerConfig(opts.Config, s, c, name)
if err != nil {
return err
}
err = SetupLayerConfig(opts.Config, c, l, name)
if err != nil {
return err
}
if opts.SetupOnly {
err = c.SaveConfigFile(path.Join(opts.Config.RootFSDir, name, "lxc.conf"))
if err != nil {
return errors.Wrapf(err, "error saving config file for %s", name)
}
log.Infof("setup for %s complete", name)
continue
}
if len(l.Run) != 0 {
rootfs := path.Join(opts.Config.RootFSDir, name, "rootfs")
shellScript := path.Join(opts.Config.StackerDir, "imports", name, ".stacker-run.sh")
err = generateShellForRunning(rootfs, l.Run, shellScript)
if err != nil {
return err
}
// These should all be non-interactive; let's ensure that.
err = c.Execute("/stacker/.stacker-run.sh", nil)
if err != nil {
if opts.OnRunFailure != "" {
err2 := c.Execute(opts.OnRunFailure, os.Stdin)
if err2 != nil {
log.Infof("failed executing %s: %s\n", opts.OnRunFailure, err2)
}
}
return errors.Errorf("run commands failed: %s", err)
}
}
// This is a build only layer, meaning we don't need to include
// it in the final image, as outputs from it are going to be
// imported into future images. Let's just snapshot it and add
// a bogus entry to our cache.
if l.BuildOnly {
log.Debugf("build only layer, skipping OCI diff generation")
// A small hack: for build only layers, we keep track
// of the name, so we can make sure it exists when
// there is a cache hit. We should probably make this
// into some sort of proper Either type.
manifests := map[types.LayerType]ispec.Descriptor{opts.LayerTypes[0]: ispec.Descriptor{}}
if err := buildCache.Put(name, manifests); err != nil {
return err
}
continue
}
err = s.Repack(name, opts.LayerTypes, b.builtStackerfiles)
if err != nil {
return err
}
manifests := map[types.LayerType]ispec.Descriptor{}
for _, layerType := range opts.LayerTypes {
err = b.updateOCIConfigForOutput(sf, s, oci, layerType, l, name)
if err != nil {
return err
}
descPaths, err := oci.ResolveReference(context.Background(), layerType.LayerName(name))
if err != nil {
return err
}
manifests[layerType] = descPaths[0].Descriptor()
}
if err := buildCache.Put(name, manifests); err != nil {
return err
}
log.Infof("filesystem %s built successfully", name)
}
return oci.GC(context.Background())
}
// BuildMultiple builds a list of stackerfiles
func (b *Builder) BuildMultiple(paths []string) error {
opts := b.opts
s, locks, err := NewStorage(opts.Config)
if err != nil {
return err
}
defer locks.Unlock()
// Read all the stacker recipes
stackerFiles, err := types.NewStackerFiles(paths, opts.HashRequired, append(opts.Substitute, b.opts.Config.Substitutions()...))
if err != nil {
return err
}
// Initialize the DAG
dag, err := NewStackerFilesDAG(stackerFiles)
if err != nil {
return err
}
sortedPaths := dag.Sort()
// Show the serial build order
log.Debugf("stacker build order:")
for i, p := range sortedPaths {
prerequisites, err := dag.GetStackerFile(p).Prerequisites()
if err != nil {
return err
}
log.Debugf("%d build %s: requires: %v", i, p, prerequisites)
}
if opts.OrderOnly {
// User has requested only to see the build order, so skipping the actual build
return nil
}
// Build all Stackerfiles
for i, p := range sortedPaths {
log.Debugf("building: %d %s", i, p)
err = b.build(s, p)
if err != nil {
return err
}
}
return nil
}
// generateShellForRunning generates a shell script to run inside the
// container, and writes it to the contianer. It checks that the script already
// have a shebang? If so, it leaves it as is, otherwise it prepends a shebang.
func generateShellForRunning(rootfs string, cmd []string, outFile string) error {
shebangLine := fmt.Sprintf("#!%s -xe\n", DefaultShell)
if strings.HasPrefix(cmd[0], "#!") {
shebangLine = ""
}
return ioutil.WriteFile(outFile, []byte(shebangLine+strings.Join(cmd, "\n")+"\n"), 0755)
}
func runInternalGoSubcommand(config types.StackerConfig, args []string) error {
binary, err := os.Readlink("/proc/self/exe")
if err != nil {
return err
}
cmd := []string{
"--oci-dir", config.OCIDir,
"--roots-dir", config.RootFSDir,
"--stacker-dir", config.StackerDir,
"--storage-type", config.StorageType,
"--internal-userns",
}
if config.Debug {
cmd = append(cmd, "--debug")
}
cmd = append(cmd, "internal-go")
cmd = append(cmd, args...)
c := exec.Command(binary, cmd...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return errors.WithStack(c.Run())
}
func SetupBuildContainerConfig(config types.StackerConfig, storage types.Storage, c *container.Container, name string) error {
rootfsPivot := path.Join(config.StackerDir, "rootfsPivot")
if err := os.MkdirAll(rootfsPivot, 0755); err != nil {
return err
}
if err := c.SetConfig("lxc.rootfs.mount", rootfsPivot); err != nil {
return err
}
configs := map[string]string{
"lxc.mount.auto": "proc:mixed",
"lxc.autodev": "1",
"lxc.pty.max": "1024",
"lxc.mount.entry": "none dev/shm tmpfs defaults,create=dir 0 0",
"lxc.uts.name": "stacker",
"lxc.net.0.type": "none",
"lxc.environment": fmt.Sprintf("PATH=%s", container.ReasonableDefaultPath),
"lxc.apparmor.allow_incomplete": "1",
}
if err := c.SetConfigs(configs); err != nil {
return err
}
err := c.BindMount("/sys", "/sys", "")
if err != nil {
return err
}
err = c.BindMount("/etc/resolv.conf", "/etc/resolv.conf", "")
if err != nil {
return err
}
rootfs, err := storage.GetLXCRootfsConfig(name)
if err != nil {
return err
}
err = c.SetConfig("lxc.rootfs.path", rootfs)
if err != nil {
return err
}
// liblxc inserts an apparmor profile if we don't set one by default.
// however, since we may be statically linked with no packaging
// support, the host may not have this default profile. let's check for
// it. of course, we can't check for it by catting the value in
// securityfs, because that's restricted :). so we fork and try to
// change to the profile in question instead.
//
// note that this is not strictly correct: lxc will try to use a
// non-cgns profile if cgns isn't supported by the kernel, but most
// kernels these days support it so we ignore this case.
lxcDefaultProfile := "lxc-container-default-cgns"
err = runInternalGoSubcommand(config, []string{"check-aa-profile", lxcDefaultProfile})
if err != nil {
log.Infof("couldn't find AppArmor profile %s", lxcDefaultProfile)
err = c.SetConfig("lxc.apparmor.profile", "unconfined")
if err != nil {
return err
}
}
return nil
}
func SetupLayerConfig(config types.StackerConfig, c *container.Container, l types.Layer, name string) error {
env, err := l.BuildEnvironment(name)
if err != nil {
return err
}
importsDir := path.Join(config.StackerDir, "imports", name)
if _, err := os.Stat(importsDir); err == nil {
log.Debugf("bind mounting %s into container", importsDir)
err = c.BindMount(importsDir, "/stacker", "ro")
if err != nil {
return err
}
} else {
log.Debugf("not bind mounting %s into container", importsDir)
}
for k, v := range env {
if v != "" {
err = c.SetConfig("lxc.environment", fmt.Sprintf("%s=%s", k, v))
if err != nil {
return err
}
}
}
for _, bind := range l.Binds {
err = c.BindMount(bind.Source, bind.Dest, "")
if err != nil {
return err
}
}
return err
}