forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
83 lines (73 loc) · 2 KB
/
extractor.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
package blob
import (
"os"
boshblob "github.com/cloudfoundry/bosh-agent/blobstore"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshcmd "github.com/cloudfoundry/bosh-agent/platform/commands"
boshsys "github.com/cloudfoundry/bosh-agent/system"
)
type Extractor interface {
Extract(blobID string, blobSHA1 string, targetDir string) error
}
type extractor struct {
fs boshsys.FileSystem
compressor boshcmd.Compressor
blobstore boshblob.Blobstore
logger boshlog.Logger
logTag string
}
func NewExtractor(
fs boshsys.FileSystem,
compressor boshcmd.Compressor,
blobstore boshblob.Blobstore,
logger boshlog.Logger,
) Extractor {
return extractor{
fs: fs,
compressor: compressor,
blobstore: blobstore,
logger: logger,
logTag: "blobExtractor",
}
}
func (e extractor) Extract(blobID string, blobSHA1 string, targetDir string) error {
filePath, err := e.blobstore.Get(blobID, blobSHA1)
if err != nil {
return bosherr.WrapErrorf(err, "Getting object from blobstore: %s", blobID)
}
defer e.cleanUpBlob(filePath)
existed := e.fs.FileExists(targetDir)
if !existed {
err = e.fs.MkdirAll(targetDir, os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating target dir: %s", targetDir)
}
}
err = e.compressor.DecompressFileToDir(filePath, targetDir, boshcmd.CompressorOptions{})
if err != nil {
if !existed {
e.cleanUpFile(targetDir)
}
return bosherr.WrapErrorf(err, "Extracting compiled package: BlobID:'%s', BlobSHA1: '%s'", blobID, blobSHA1)
}
return nil
}
func (e extractor) cleanUpBlob(filePath string) {
err := e.blobstore.CleanUp(filePath)
if err != nil {
e.logger.Error(
e.logTag,
bosherr.WrapErrorf(err, "Removing compiled package tarball: %s", filePath).Error(),
)
}
}
func (e extractor) cleanUpFile(filePath string) {
err := e.fs.RemoveAll(filePath)
if err != nil {
e.logger.Error(
e.logTag,
bosherr.WrapErrorf(err, "Removing: %s", filePath).Error(),
)
}
}