forked from project-stacker/stacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
179 lines (156 loc) · 4.52 KB
/
base.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
package stacker
import (
"fmt"
"io"
"os"
"path"
"github.com/klauspost/pgzip"
"github.com/opencontainers/umoci"
"github.com/opencontainers/umoci/oci/casext"
"github.com/opencontainers/umoci/oci/layer"
"github.com/pkg/errors"
"github.com/project-stacker/stacker/lib"
"github.com/project-stacker/stacker/log"
"github.com/project-stacker/stacker/types"
)
type BaseLayerOpts struct {
Config types.StackerConfig
Name string
Layer types.Layer
Cache *BuildCache
OCI casext.Engine
LayerTypes []types.LayerType
Storage types.Storage
Progress bool
}
// GetBase grabs the base layer and puts it in the cache.
func GetBase(o BaseLayerOpts) error {
switch o.Layer.From.Type {
case types.BuiltLayer:
return nil
case types.TarLayer:
cacheDir := path.Join(o.Config.StackerDir, "layer-bases")
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return err
}
_, err := acquireUrl(o.Config, o.Storage, o.Layer.From.Url, cacheDir, o.Progress, "")
return err
/* now we can do all the containers/image types */
case types.OCILayer:
fallthrough
case types.DockerLayer:
return importContainersImage(o.Layer.From, o.Config, o.Progress)
default:
return errors.Errorf("unknown layer type: %v", o.Layer.From.Type)
}
}
// SetupRootfs assumes the base layer is correct in the cache, and sets up the
// base to the output.
//
// If the layer is a build only layer, this code simply initializes the
// filesystem in roots to the built tag's filesystem.
func SetupRootfs(o BaseLayerOpts) error {
err := o.Storage.Delete(o.Name)
if err != nil && !os.IsNotExist(errors.Unwrap(err)) {
return err
}
if o.Layer.From.Type == types.BuiltLayer {
// For built type images, we already have the base fs content
// and umoci metadata. So let's just use that.
return o.Storage.Restore(o.Layer.From.Tag, o.Name)
}
// For everything else, we create a new snapshot and extract whatever
// we can on top of it.
if err := o.Storage.Create(o.Name); err != nil {
return err
}
switch o.Layer.From.Type {
case types.TarLayer:
err := o.Storage.SetupEmptyRootfs(o.Name)
if err != nil {
return err
}
return setupTarRootfs(o)
case types.OCILayer:
fallthrough
case types.DockerLayer:
return setupContainersImageRootfs(o)
default:
return errors.Errorf("unknown layer type: %v", o.Layer.From.Type)
}
}
func importContainersImage(is types.ImageSource, config types.StackerConfig, progress bool) error {
toImport, err := is.ContainersImageURL()
if err != nil {
return err
}
tag, err := is.ParseTag()
if err != nil {
return err
}
// Note that we can do this over the top of the cache every time, since
// skopeo should be smart enough to only copy layers that have changed.
cacheDir := path.Join(config.StackerDir, "layer-bases", "oci")
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return err
}
defer func() {
oci, err := umoci.OpenLayout(cacheDir)
if err != nil {
// Some error might have occurred, in which case we
// don't have a valid OCI layout, which is fine.
return
}
defer oci.Close()
}()
var progressWriter io.Writer
if progress {
progressWriter = os.Stderr
}
log.Infof("loading %s", toImport)
err = lib.ImageCopy(lib.ImageCopyOpts{
Src: toImport,
Dest: fmt.Sprintf("oci:%s:%s", cacheDir, tag),
SrcSkipTLS: is.Insecure,
Progress: progressWriter,
})
if err != nil {
return errors.Wrapf(err, "couldn't import base layer %s", tag)
}
return err
}
func setupContainersImageRootfs(o BaseLayerOpts) error {
target := path.Join(o.Config.RootFSDir, o.Name)
log.Debugf("unpacking to %s", target)
cacheTag, err := o.Layer.From.ParseTag()
if err != nil {
return err
}
return o.Storage.Unpack(cacheTag, o.Name)
}
func setupTarRootfs(o BaseLayerOpts) error {
// initialize an empty image, then extract it
cacheDir := path.Join(o.Config.StackerDir, "layer-bases")
tar := path.Join(cacheDir, path.Base(o.Layer.From.Url))
layerPath := o.Storage.TarExtractLocation(o.Name)
return unpackTar(layerPath, tar)
}
func unpackTar(destDir string, tar string) error {
tarReader, err := os.Open(tar)
if err != nil {
return errors.Wrapf(err, "couldn't open %s", tar)
}
defer tarReader.Close()
var uncompressed io.ReadCloser
uncompressed, err = pgzip.NewReader(tarReader)
if err != nil {
_, err = tarReader.Seek(0, os.SEEK_SET)
if err != nil {
return errors.Wrapf(err, "failed to 0 seek %s", tar)
}
uncompressed = tarReader
} else {
defer uncompressed.Close()
}
return layer.UnpackLayer(destDir, uncompressed, &layer.UnpackOptions{KeepDirlinks: true})
}