forked from linuxkit/linuxkit
/
pkglib.go
356 lines (307 loc) · 8.8 KB
/
pkglib.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
package pkglib
import (
"crypto/sha1"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
"github.com/linuxkit/linuxkit/src/cmd/linuxkit/moby"
"github.com/linuxkit/linuxkit/src/cmd/linuxkit/util"
)
// Contains fields settable in the build.yml
type pkgInfo struct {
Image string `yaml:"image"`
Org string `yaml:"org"`
Arches []string `yaml:"arches"`
ExtraSources []string `yaml:"extra-sources"`
GitRepo string `yaml:"gitrepo"` // ??
Network bool `yaml:"network"`
DisableCache bool `yaml:"disable-cache"`
Config *moby.ImageConfig `yaml:"config"`
BuildArgs *[]string `yaml:"buildArgs,omitempty"`
Depends struct {
DockerImages struct {
TargetDir string `yaml:"target-dir"`
Target string `yaml:"target"`
FromFile string `yaml:"from-file"`
List []string `yaml:"list"`
} `yaml:"docker-images"`
} `yaml:"depends"`
}
// PkglibConfig contains the configuration for the pkglib package.
// It is used to override the default behaviour of the package.
// Fields that are pointers are so that the caller can leave it as nil
// for "use whatever default pkglib has", while non-nil means "explicitly override".
type PkglibConfig struct {
DisableCache *bool
Network *bool
Org *string
BuildYML string
Hash string
HashCommit string
HashPath string
Dirty bool
Dev bool
}
func NewPkgInfo() pkgInfo {
return pkgInfo{
Org: "linuxkit",
Arches: []string{"amd64", "arm64"},
GitRepo: "https://github.com/linuxkit/linuxkit",
Network: false,
DisableCache: false,
}
}
// Specifies the source directory for a package and their destination in the build context.
type pkgSource struct {
src string
dst string
}
// Pkg encapsulates information about a package's source
type Pkg struct {
// These correspond to pkgInfo fields
image string
org string
arches []string
sources []pkgSource
gitRepo string
network bool
trust bool
cache bool
config *moby.ImageConfig
buildArgs *[]string
dockerDepends dockerDepends
// Internal state
path string
hash string
dirty bool
commitHash string
git *git
}
// NewFromConfig creates a range of Pkg from a PkglibConfig and paths to packages.
func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
// Defaults
piBase := NewPkgInfo()
// TODO(ijc) look for "$(git rev-parse --show-toplevel)/.build-defaults.yml"?
// Ideally want to look at every directory from root to `pkg`
// for this file but might be tricky to arrange ordering-wise.
// These override fields in pi below, bools are in both forms to allow user overrides in either direction.
// These will apply to all packages built.
// Other arguments
var pkgs []Pkg
for _, pkg := range args {
var (
pkgHashPath string
pkgHash = cfg.Hash
)
pkgPath, err := filepath.Abs(pkg)
if err != nil {
return nil, err
}
if cfg.HashPath == "" {
pkgHashPath = pkgPath
} else {
pkgHashPath, err = filepath.Abs(cfg.HashPath)
if err != nil {
return nil, err
}
if !strings.HasPrefix(pkgPath, pkgHashPath) {
return nil, fmt.Errorf("Hash path is not a prefix of the package path")
}
// TODO(ijc) pkgPath and hashPath really ought to be in the same git tree too...
}
// make our own copy of piBase. We could use some deepcopy library, but it is just as easy to marshal/unmarshal
pib, err := yaml.Marshal(&piBase)
if err != nil {
return nil, err
}
var pi pkgInfo
if err := yaml.Unmarshal(pib, &pi); err != nil {
return nil, err
}
b, err := os.ReadFile(filepath.Join(pkgPath, cfg.BuildYML))
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(b, &pi); err != nil {
return nil, err
}
if pi.Image == "" {
return nil, fmt.Errorf("Image field is required")
}
dockerDepends, err := newDockerDepends(pkgPath, &pi)
if err != nil {
return nil, err
}
if cfg.Dev {
// If --org is also used then this will be overwritten
// by argOrg when we iterate over the provided options
// in the fs.Visit block below.
pi.Org = os.Getenv("USER")
if pkgHash == "" {
pkgHash = "dev"
}
}
// Go's flag package provides no way to see if a flag was set
// apart from Visit which iterates over only those which were
// set. This must be run here, rather than earlier, because we need to
// have read it from the build.yml file first, then override based on CLI.
if cfg.DisableCache != nil {
pi.DisableCache = *cfg.DisableCache
}
if cfg.Network != nil {
pi.Network = *cfg.Network
}
if cfg.Org != nil {
pi.Org = *cfg.Org
}
var srcHashes string
sources := []pkgSource{{src: pkgPath, dst: "/"}}
for _, source := range pi.ExtraSources {
tmp := strings.Split(source, ":")
if len(tmp) != 2 {
return nil, fmt.Errorf("Bad source format in %s", source)
}
srcPath := filepath.Clean(tmp[0]) // Should work with windows paths
dstPath := path.Clean(tmp[1]) // 'path' here because this should be a Unix path
if !filepath.IsAbs(srcPath) {
srcPath = filepath.Join(pkgPath, srcPath)
}
g, err := newGit(srcPath)
if err != nil {
return nil, err
}
if g == nil {
return nil, fmt.Errorf("Source %s not in a git repository", srcPath)
}
h, err := g.treeHash(srcPath, cfg.HashCommit)
if err != nil {
return nil, err
}
srcHashes += h
sources = append(sources, pkgSource{src: srcPath, dst: dstPath})
}
git, err := newGit(pkgPath)
if err != nil {
return nil, err
}
var dirty bool
if git != nil {
gitDirty, err := git.isDirty(pkgHashPath, cfg.HashCommit)
if err != nil {
return nil, err
}
dirty = cfg.Dirty || gitDirty
if pkgHash == "" {
if pkgHash, err = git.treeHash(pkgHashPath, cfg.HashCommit); err != nil {
return nil, err
}
if srcHashes != "" {
pkgHash += srcHashes
pkgHash = fmt.Sprintf("%x", sha1.Sum([]byte(pkgHash)))
}
if dirty {
contentHash, err := git.contentHash()
if err != nil {
return nil, err
}
if len(contentHash) < 7 {
return nil, fmt.Errorf("unexpected hash len: %d", len(contentHash))
}
// construct <ls-tree>-dirty-<content hash> tag
pkgHash += fmt.Sprintf("-dirty-%s", contentHash[0:7])
}
}
}
pkgs = append(pkgs, Pkg{
image: pi.Image,
org: pi.Org,
hash: pkgHash,
commitHash: cfg.HashCommit,
arches: pi.Arches,
sources: sources,
gitRepo: pi.GitRepo,
network: pi.Network,
cache: !pi.DisableCache,
config: pi.Config,
buildArgs: pi.BuildArgs,
dockerDepends: dockerDepends,
dirty: dirty,
path: pkgPath,
git: git,
})
}
return pkgs, nil
}
// Hash returns the hash of the package
func (p Pkg) Hash() string {
return p.hash
}
// ReleaseTag returns the tag to use for a particular release of the package
func (p Pkg) ReleaseTag(release string) (string, error) {
if release == "" {
return "", fmt.Errorf("A release tag is required")
}
if p.dirty {
return "", fmt.Errorf("Cannot release a dirty package")
}
tag := p.org + "/" + p.image + ":" + release
return tag, nil
}
// Tag returns the tag to use for the package
func (p Pkg) Tag() string {
t := p.hash
if t == "" {
t = "latest"
}
return p.org + "/" + p.image + ":" + t
}
// FullTag returns a reference expanded tag
func (p Pkg) FullTag() string {
return util.ReferenceExpand(p.Tag())
}
// TrustEnabled returns true if trust is enabled
func (p Pkg) TrustEnabled() bool {
return p.trust
}
// Arches which arches this can be built for
func (p Pkg) Arches() []string {
return p.arches
}
//nolint:unused // will be used when linuxkit cache is eliminated and we return to docker image cache
func (p Pkg) archSupported(want string) bool {
for _, supp := range p.arches {
if supp == want {
return true
}
}
return false
}
func (p Pkg) cleanForBuild() error {
if p.commitHash != "HEAD" {
return fmt.Errorf("Cannot build from commit hash != HEAD")
}
return nil
}
// Expands path from relative to abs against base, ensuring the result is within base, but is not base itself. Field is the fieldname, to be used for constructing the error.
func makeAbsSubpath(field, base, path string) (string, error) {
if path == "" {
return "", nil
}
if filepath.IsAbs(path) {
return "", fmt.Errorf("%s must be relative to package directory", field)
}
p, err := filepath.Abs(filepath.Join(base, path))
if err != nil {
return "", err
}
if p == base {
return "", fmt.Errorf("%s must not be exactly the package directory", field)
}
if !strings.HasPrefix(p, base) {
return "", fmt.Errorf("%s must be within package directory", field)
}
return p, nil
}