forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendered_job_list_compressor.go
77 lines (63 loc) · 2.2 KB
/
rendered_job_list_compressor.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
package templatescompiler
import (
"path/filepath"
bicrypto "github.com/cloudfoundry/bosh-cli/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type RenderedJobListCompressor interface {
Compress(RenderedJobList) (RenderedJobListArchive, error)
}
type renderedJobListCompressor struct {
fs boshsys.FileSystem
compressor boshcmd.Compressor
digestCalculator bicrypto.DigestCalculator
logTag string
logger boshlog.Logger
}
func NewRenderedJobListCompressor(
fs boshsys.FileSystem,
compressor boshcmd.Compressor,
digestCalculator bicrypto.DigestCalculator,
logger boshlog.Logger,
) RenderedJobListCompressor {
return &renderedJobListCompressor{
fs: fs,
compressor: compressor,
digestCalculator: digestCalculator,
logTag: "renderedJobListCompressor",
logger: logger,
}
}
func (c *renderedJobListCompressor) Compress(list RenderedJobList) (RenderedJobListArchive, error) {
c.logger.Debug(c.logTag, "Compressing rendered job list")
renderedJobListDir, err := c.fs.TempDir("rendered-job-list-archive")
if err != nil {
return nil, bosherr.WrapError(err, "Creating rendered job directory")
}
defer func() {
err := c.fs.RemoveAll(renderedJobListDir)
if err != nil {
c.logger.Error(c.logTag, "Failed to delete rendered job list dir: %s", err.Error())
}
}()
// copy rendered job templates into a sub-dir
for _, renderedJob := range list.All() {
err = c.fs.CopyDir(renderedJob.Path(), filepath.Join(renderedJobListDir, renderedJob.Job().Name()))
if err != nil {
return nil, bosherr.WrapError(err, "Creating rendered job directory")
}
}
archivePath, err := c.compressor.CompressFilesInDir(renderedJobListDir)
if err != nil {
return nil, bosherr.WrapError(err, "Compressing rendered job templates")
}
//generation of digest string
archiveSHA1, err := c.digestCalculator.Calculate(archivePath)
if err != nil {
return nil, bosherr.WrapError(err, "Calculating archived templates SHA1")
}
return NewRenderedJobListArchive(list, archivePath, archiveSHA1, c.fs, c.logger), nil
}