This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fs_blobs_dir.go
425 lines (333 loc) · 9.07 KB
/
fs_blobs_dir.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
package releasedir
import (
"io"
"os"
"path/filepath"
"sort"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshfu "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"gopkg.in/yaml.v2"
"fmt"
bicrypto "github.com/cloudfoundry/bosh-cli/crypto"
"github.com/cloudfoundry/bosh-cli/work"
)
type FSBlobsDir struct {
indexPath string
dirPath string
reporter BlobsDirReporter
blobstore boshblob.DigestBlobstore
digestCalculator bicrypto.DigestCalculator
fs boshsys.FileSystem
logTag string
logger boshlog.Logger
}
/*
---
golang/go1.5.1.linux-amd64.tar.gz:
object_id: 36764f38-6274-4a5d-8faa-26c31a745cb2
sha: 46eecd290d8803887dec718c691cc243f2175fe0
size: 77875767
*/
type fsBlobsDirSchema map[string]fsBlobsDirSchema_Blob
type fsBlobsDirSchema_Blob struct {
Size int64 `yaml:"size"`
BlobstoreID string `yaml:"object_id,omitempty"`
SHA1 string `yaml:"sha"`
}
func NewFSBlobsDir(
dirPath string,
reporter BlobsDirReporter,
blobstore boshblob.DigestBlobstore,
digestCalculator bicrypto.DigestCalculator,
fs boshsys.FileSystem,
logger boshlog.Logger,
) FSBlobsDir {
return FSBlobsDir{
indexPath: filepath.Join(dirPath, "config", "blobs.yml"),
dirPath: filepath.Join(dirPath, "blobs"),
reporter: reporter,
blobstore: blobstore,
digestCalculator: digestCalculator,
fs: fs,
logTag: "releasedir.FSBlobsDir",
logger: logger,
}
}
func (d FSBlobsDir) Init() error {
err := d.fs.MkdirAll(filepath.Dir(d.indexPath), os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating blobs/")
}
err = d.fs.WriteFileString(d.indexPath, "--- {}\n")
if err != nil {
return bosherr.WrapErrorf(err, "Initing blobs.yml")
}
return nil
}
func (d FSBlobsDir) Blobs() ([]Blob, error) {
bytes, err := d.fs.ReadFile(d.indexPath)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Reading blobs index")
}
var schema fsBlobsDirSchema
err = yaml.Unmarshal(bytes, &schema)
if err != nil {
return nil, bosherr.WrapError(err, "Unmarshalling blobs index")
}
var blobs []Blob
for recPath, rec := range schema {
blobs = append(blobs, Blob{
Path: recPath,
Size: rec.Size,
BlobstoreID: rec.BlobstoreID,
SHA1: rec.SHA1,
})
}
sort.Sort(BlobSorting(blobs))
return blobs, nil
}
func (d FSBlobsDir) SyncBlobs(parallel int) error {
pool := work.Pool{
Count: parallel,
}
blobs, err := d.Blobs()
if err != nil {
return err
}
symlinksFound, err := d.containsSymlinks()
if err != nil {
return bosherr.WrapErrorf(err, "Syncing blobs")
}
if symlinksFound {
return bosherr.Error("Bailing because symlinks found in blobs directory. If switching from CLI v1, please use the `reset-release` command.")
}
if err := d.removeUnknownBlobs(blobs); err != nil {
return bosherr.WrapErrorf(err, "Syncing blobs")
}
var tasks []func() error
for _, blob := range blobs {
blob := blob
tasks = append(tasks, func() error {
if len(blob.BlobstoreID) > 0 {
return d.downloadBlob(blob)
}
return nil
})
}
err = pool.ParallelDo(tasks...)
if err != nil {
return err
}
return nil
}
func (d FSBlobsDir) removeUnknownBlobs(blobs []Blob) error {
files, err := d.fs.RecursiveGlob(filepath.Join(d.dirPath, "**/*"))
if err != nil {
return bosherr.WrapErrorf(err, "Checking for unknown blobs")
}
for _, file := range files {
fileInfo, err := d.fs.Stat(file)
if err != nil {
return bosherr.WrapErrorf(err, "Determining existing blobs")
}
if fileInfo.IsDir() {
continue
}
found := false
for _, blob := range blobs {
if file == filepath.Join(d.dirPath, blob.Path) {
found = true
break
}
}
if !found {
d.logger.Info(d.logTag, fmt.Sprintf("Deleting blob at '%s' that is not in the blob index.", file))
if err := d.fs.RemoveAll(file); err != nil {
return bosherr.WrapErrorf(err, "Removing unknown blob")
}
}
}
return nil
}
func (d FSBlobsDir) TrackBlob(path string, src io.ReadCloser) (Blob, error) {
tempFile, err := d.fs.TempFile("track-blob")
if err != nil {
return Blob{}, bosherr.WrapErrorf(err, "Creating temp blob")
}
defer tempFile.Close()
_, err = io.Copy(tempFile, src)
if err != nil {
return Blob{}, bosherr.WrapErrorf(err, "Populating temp blob")
}
//generation of digest string
sha1, err := d.digestCalculator.Calculate(tempFile.Name())
if err != nil {
return Blob{}, bosherr.WrapErrorf(err, "Calculating temp blob sha1")
}
fileInfo, err := tempFile.Stat()
if err != nil {
return Blob{}, bosherr.WrapErrorf(err, "Stating temp blob")
}
blobs, err := d.Blobs()
if err != nil {
return Blob{}, err
}
idx := -1
for i, blob := range blobs {
if blob.Path == path {
idx = i
break
}
}
if idx == -1 {
blobs = append(blobs, Blob{})
idx = len(blobs) - 1
}
blobs[idx] = Blob{Path: path, Size: fileInfo.Size(), SHA1: sha1}
tempFile.Close()
err = d.moveBlobLocally(tempFile.Name(), filepath.Join(d.dirPath, path))
if err != nil {
return Blob{}, err
}
return blobs[idx], d.save(blobs)
}
func (d FSBlobsDir) UntrackBlob(path string) error {
blobs, err := d.Blobs()
if err != nil {
return err
}
err = d.fs.RemoveAll(filepath.Join(d.dirPath, path))
if err != nil {
return bosherr.WrapErrorf(err, "Removing blob from blobs/")
}
for i, blob := range blobs {
if blob.Path == path {
return d.save(append(blobs[:i], blobs[i+1:]...))
}
}
return nil
}
func (d FSBlobsDir) UploadBlobs() error {
blobs, err := d.Blobs()
if err != nil {
return err
}
for i, blob := range blobs {
if len(blob.BlobstoreID) == 0 {
blobID, err := d.uploadBlob(blob)
if err != nil {
return err
}
blob.BlobstoreID = blobID
blobs[i] = blob
err = d.save(blobs)
if err != nil {
return bosherr.WrapErrorf(
err, "Saving newly created blob '%s' for path '%s'", blobID, blob.Path)
}
}
}
return nil
}
func (d FSBlobsDir) containsSymlinks() (bool, error) {
files, err := d.fs.RecursiveGlob(filepath.Join(d.dirPath, "**/*"))
if err != nil {
return false, nil
}
for _, file := range files {
fileInfo, err := d.fs.Lstat(file)
if err != nil {
return false, err
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
return true, nil
}
}
return false, nil
}
func (d FSBlobsDir) checkBlobExistence(dstPath string, digest boshcrypto.MultipleDigest) bool {
if d.fs.FileExists(dstPath) {
if err := digest.VerifyFilePath(dstPath, d.fs); err != nil {
d.logger.Error(d.logTag, fmt.Sprintf("Incorrect SHA sum for blob at '%s'. Re-downloading from blobstore.", dstPath))
return false
}
return true
}
return false
}
func (d FSBlobsDir) downloadBlob(blob Blob) error {
dstPath := filepath.Join(d.dirPath, blob.Path)
digest, err := boshcrypto.ParseMultipleDigest(blob.SHA1)
if err != nil {
return bosherr.WrapErrorf(
err, "Generating multi digest for blob '%s' for path '%s' with digest string '%s'", blob.BlobstoreID, blob.Path, blob.SHA1)
}
if d.checkBlobExistence(dstPath, digest) {
return nil
}
d.reporter.BlobDownloadStarted(blob.Path, blob.Size, blob.BlobstoreID, blob.SHA1)
path, err := d.blobstore.Get(blob.BlobstoreID, digest)
if err != nil {
d.reporter.BlobDownloadFinished(blob.Path, blob.BlobstoreID, err)
return bosherr.WrapErrorf(
err, "Getting blob '%s' for path '%s'", blob.BlobstoreID, blob.Path)
}
d.reporter.BlobDownloadFinished(blob.Path, blob.BlobstoreID, nil)
return d.moveBlobLocally(path, dstPath)
}
func (d FSBlobsDir) uploadBlob(blob Blob) (string, error) {
var blobID string
d.reporter.BlobUploadStarted(blob.Path, blob.Size, blob.SHA1)
srcPath := filepath.Join(d.dirPath, blob.Path)
blobID, _, err := d.blobstore.Create(srcPath)
if err != nil {
d.reporter.BlobUploadFinished(blob.Path, "", err)
return "", bosherr.WrapErrorf(err, "Creating blob for path '%s'", blob.Path)
}
d.reporter.BlobUploadFinished(blob.Path, blobID, nil)
return blobID, nil
}
func (d FSBlobsDir) moveBlobLocally(srcPath, dstPath string) error {
err := d.fs.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating subdirs in blobs/")
}
err = boshfu.NewFileMover(d.fs).Move(srcPath, dstPath)
if err != nil {
return bosherr.WrapErrorf(err, "Moving temp blob to blobs/")
}
return nil
}
func (d FSBlobsDir) save(blobs []Blob) error {
schema := fsBlobsDirSchema{}
for _, blob := range blobs {
schema[blob.Path] = fsBlobsDirSchema_Blob{
Size: blob.Size,
BlobstoreID: blob.BlobstoreID,
SHA1: blob.SHA1,
}
}
bytes, err := yaml.Marshal(schema)
if err != nil {
return bosherr.WrapError(err, "Marshalling blobs index")
}
err = d.fs.WriteFile(d.indexPath, bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Writing blobs index")
}
return nil
}
type BlobSorting []Blob
func (s BlobSorting) Len() int {
return len(s)
}
func (s BlobSorting) Less(i, j int) bool {
return s[i].Path < s[j].Path
}
func (s BlobSorting) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}