forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendered_job_list_compressor.go
81 lines (66 loc) · 2.31 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
78
79
80
81
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
sha1Calculator bicrypto.SHA1Calculator
logTag string
logger boshlog.Logger
}
func NewRenderedJobListCompressor(
fs boshsys.FileSystem,
compressor boshcmd.Compressor,
sha1Calculator bicrypto.SHA1Calculator,
logger boshlog.Logger,
) RenderedJobListCompressor {
return &renderedJobListCompressor{
fs: fs,
compressor: compressor,
sha1Calculator: sha1Calculator,
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")
}
}
fingerprint, err := c.sha1Calculator.Calculate(renderedJobListDir)
if err != nil {
return nil, bosherr.WrapError(err, "Calculating templates dir SHA1")
}
archivePath, err := c.compressor.CompressFilesInDir(renderedJobListDir)
if err != nil {
return nil, bosherr.WrapError(err, "Compressing rendered job templates")
}
archiveSHA1, err := c.sha1Calculator.Calculate(archivePath)
if err != nil {
return nil, bosherr.WrapError(err, "Calculating archived templates SHA1")
}
return NewRenderedJobListArchive(list, archivePath, fingerprint, archiveSHA1, c.fs, c.logger), nil
}