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
/
import.go
337 lines (290 loc) · 8.43 KB
/
import.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 stacker
import (
"io/ioutil"
"os"
"path"
"strings"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/project-stacker/stacker/lib"
"github.com/project-stacker/stacker/log"
"github.com/project-stacker/stacker/types"
"github.com/udhos/equalfile"
"github.com/vbatts/go-mtree"
)
// filesDiffer returns true if the files are different, false if they are the same.
func filesDiffer(p1 string, info1 os.FileInfo, p2 string, info2 os.FileInfo) (bool, error) {
if info1.Name() != info2.Name() {
return false, errors.Errorf("comparing files without the same name?")
}
if info1.Mode()&os.ModeSymlink != 0 {
if info2.Mode()&os.ModeSymlink != 0 {
link1, err := os.Readlink(p1)
if err != nil {
return false, err
}
link2, err := os.Readlink(p2)
if err != nil {
return false, err
}
return link1 != link2, err
}
return false, errors.Errorf("symlink -> not symlink not supported")
}
if info1.Size() != info2.Size() {
return true, nil
}
if info1.Mode() != info2.Mode() {
return true, nil
}
f1, err := os.Open(p1)
if err != nil {
return false, err
}
defer f1.Close()
f2, err := os.Open(p2)
if err != nil {
return false, err
}
defer f2.Close()
eq, err := equalfile.New(nil, equalfile.Options{}).CompareReader(f1, f2)
if err != nil {
return false, err
}
return !eq, nil
}
func verifyImportFileHash(imp string, hash string) error {
if len(hash) == 0 {
return nil
}
actualHash, err := lib.HashFile(imp, false)
if err != nil {
return err
}
actualHash = strings.TrimPrefix(actualHash, "sha256:")
if actualHash != strings.ToLower(hash) {
return errors.Errorf("The requested hash of %s import is different than the actual hash: %s != %s",
imp, hash, actualHash)
}
return nil
}
func importFile(imp string, cacheDir string, hash string) (string, error) {
e1, err := os.Lstat(imp)
if err != nil {
return "", errors.Wrapf(err, "couldn't stat import %s", imp)
}
if !e1.IsDir() {
err := verifyImportFileHash(imp, hash)
if err != nil {
return "", err
}
needsCopy := false
dest := path.Join(cacheDir, path.Base(imp))
e2, err := os.Stat(dest)
if err != nil {
needsCopy = true
} else {
differ, err := filesDiffer(imp, e1, dest, e2)
if err != nil {
return "", err
}
needsCopy = differ
}
if needsCopy {
log.Infof("copying %s", imp)
if err := lib.FileCopy(dest, imp); err != nil {
return "", errors.Wrapf(err, "couldn't copy import %s", imp)
}
} else {
log.Infof("using cached copy of %s", imp)
}
return dest, nil
}
dest := path.Join(cacheDir, path.Base(imp))
if err := os.MkdirAll(dest, 0755); err != nil {
return "", errors.Wrapf(err, "failed making cache dir")
}
existing, err := walkImport(dest)
if err != nil {
return "", errors.Wrapf(err, "failed walking existing import dir")
}
toImport, err := walkImport(imp)
if err != nil {
return "", errors.Wrapf(err, "failed walking dir to import")
}
diff, err := mtree.Compare(existing, toImport, mtreeKeywords)
if err != nil {
return "", errors.Wrapf(err, "failed mtree comparing %s and %s", existing, toImport)
}
for _, d := range diff {
switch d.Type() {
case mtree.Missing:
p := path.Join(cacheDir, path.Base(imp), d.Path())
err := os.RemoveAll(p)
if err != nil {
return "", errors.Wrapf(err, "couldn't remove missing import %s", path.Join(cacheDir, path.Base(imp), d.Path()))
}
case mtree.Modified:
fallthrough
case mtree.Extra:
srcpath := path.Join(imp, d.Path())
destpath := path.Join(cacheDir, path.Base(imp), d.Path())
if d.New().IsDir() {
fi, err := os.Lstat(destpath)
if err != nil {
if !os.IsNotExist(err) {
return "", errors.WithStack(err)
}
} else if !fi.IsDir() {
/*
* if the thing changed from a file to
* a directory, we should delete it.
* Note that we *only* want to do the
* delete in this case, but not if it
* was previously a dir, since we
* iterate over diffs in an arbitrary
* order, so we may have already
* imported stuff below this, resulting
* in an incorrect delete.
*/
err = os.Remove(destpath)
if err != nil {
return "", errors.WithStack(err)
}
}
err = errors.WithStack(os.MkdirAll(destpath, 0755))
if err != nil {
return "", err
}
} else {
err = errors.WithStack(os.MkdirAll(path.Dir(destpath), 0755))
if err != nil {
return "", err
}
err = lib.FileCopy(destpath, srcpath)
}
if err != nil {
return "", err
}
case mtree.ErrorDifference:
return "", errors.Errorf("failed to diff %s", d.Path())
}
}
return dest, nil
}
func validateHash(hash string) error {
if len(hash) > 0 {
log.Debugf("hash: %#v", hash)
// Validate given hash from stackerfile
validator := digest.Algorithm("sha256")
if err := validator.Validate(strings.ToLower(hash)); err != nil {
return errors.Wrapf(err, "Given hash %s is not valid", hash)
}
}
return nil
}
func acquireUrl(c types.StackerConfig, storage types.Storage, i string, cache string, progress bool, expectedHash string) (string, error) {
url, err := types.NewDockerishUrl(i)
if err != nil {
return "", err
}
// validate the given hash
if err = validateHash(expectedHash); err != nil {
return "", err
}
// It's just a path, let's copy it to .stacker.
if url.Scheme == "" {
return importFile(i, cache, expectedHash)
} else if url.Scheme == "http" || url.Scheme == "https" {
// otherwise, we need to download it
// first verify the hashes
remoteHash, remoteSize, err := getHttpFileInfo(i)
if err != nil {
// Needed for "working offline"
// See https://github.com/project-stacker/stacker/issues/44
log.Infof("cannot obtain file info of %s", i)
}
log.Debugf("Remote file: hash: %s length: %s", remoteHash, remoteSize)
// verify if the given hash from stackerfile matches the remote one.
if len(expectedHash) > 0 && len(remoteHash) > 0 && strings.ToLower(expectedHash) != remoteHash {
return "", errors.Errorf("The requested hash of %s import is different than the actual hash: %s != %s",
i, expectedHash, remoteHash)
}
return Download(cache, i, progress, expectedHash, remoteHash, remoteSize)
} else if url.Scheme == "stacker" {
// we always Grab() things from stacker://, because we need to
// mount the container's rootfs to get them and don't
// necessarily have a good way to do that. so this i/o is
// always done.
p := path.Join(cache, path.Base(url.Path))
snap, cleanup, err := storage.TemporaryWritableSnapshot(url.Host)
if err != nil {
return "", err
}
defer cleanup()
err = Grab(c, storage, snap, url.Path, cache)
if err != nil {
return "", err
}
err = verifyImportFileHash(p, expectedHash)
if err != nil {
return "", err
}
return p, nil
}
return "", errors.Errorf("unsupported url scheme %s", i)
}
func CleanImportsDir(c types.StackerConfig, name string, imports types.Imports, cache *BuildCache) error {
dir := path.Join(c.StackerDir, "imports", name)
cacheEntry, cacheHit := cache.Cache[name]
if !cacheHit {
// no previous build means we should delete everything that was
// imported; who knows where it came from.
return os.RemoveAll(dir)
}
// If the base name of two things was the same across builds
// but the URL they were imported from was different, let's
// make sure we invalidate the cached version.
for _, i := range imports {
for cached := range cacheEntry.Imports {
if path.Base(cached) == path.Base(i.Path) && cached != i.Path {
log.Infof("%s url changed to %s, pruning cache", cached, i.Path)
err := os.RemoveAll(path.Join(dir, path.Base(i.Path)))
if err != nil {
return err
}
}
}
}
return nil
}
func Import(c types.StackerConfig, storage types.Storage, name string, imports types.Imports, progress bool) error {
dir := path.Join(c.StackerDir, "imports", name)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
existing, err := ioutil.ReadDir(dir)
if err != nil {
return errors.Wrapf(err, "couldn't read existing directory")
}
for _, i := range imports {
name, err := acquireUrl(c, storage, i.Path, dir, progress, i.Hash)
if err != nil {
return err
}
for i, ext := range existing {
if ext.Name() == path.Base(name) {
existing = append(existing[:i], existing[i+1:]...)
break
}
}
}
// Now, delete all the old imports.
for _, ext := range existing {
err = os.RemoveAll(path.Join(dir, ext.Name()))
if err != nil {
return err
}
}
return nil
}