forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
62 lines (52 loc) · 1.87 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
package release
import (
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(releaseTarballPath string) (Release, error)
}
type extractor struct {
fs boshsys.FileSystem
compressor boshcmd.Compressor
validator Validator
logger boshlog.Logger
logTag string
}
func NewExtractor(
fs boshsys.FileSystem,
compressor boshcmd.Compressor,
validator Validator,
logger boshlog.Logger,
) Extractor {
return &extractor{
fs: fs,
compressor: compressor,
validator: validator,
logger: logger,
logTag: "releaseExtractor",
}
}
// Extract decompresses a release tarball into a temp directory (release.extractedPath),
// parses the release manifest, decompresses the packages and jobs, and validates the release.
// Use release.Delete() to clean up the temp directory.
func (e *extractor) Extract(releaseTarballPath string) (Release, error) {
extractedReleasePath, err := e.fs.TempDir("bosh-init-release")
if err != nil {
return nil, bosherr.WrapErrorf(err, "Creating temp directory to extract release '%s'", releaseTarballPath)
}
e.logger.Info(e.logTag, "Extracting release tarball '%s' to '%s'", releaseTarballPath, extractedReleasePath)
releaseReader := NewReader(releaseTarballPath, extractedReleasePath, e.fs, e.compressor)
release, err := releaseReader.Read()
if err != nil {
return nil, bosherr.WrapErrorf(err, "Reading release from '%s'", releaseTarballPath)
}
err = e.validator.Validate(release)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Validating release '%s-%s'", release.Name(), release.Version())
}
e.logger.Info(e.logTag, "Extracted release %s version %s", release.Name(), release.Version())
return release, nil
}