forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates_compiler.go
89 lines (77 loc) · 2.35 KB
/
templates_compiler.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
package templatescompiler
import (
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"
bmdepl "github.com/cloudfoundry/bosh-micro-cli/deployment"
bmrel "github.com/cloudfoundry/bosh-micro-cli/release"
)
type TemplatesCompiler interface {
Compile(jobs []bmrel.Job, deployment bmdepl.Deployment) error
}
type templatesCompiler struct {
jobRenderer JobRenderer
compressor boshcmd.Compressor
blobstore boshblob.Blobstore
templatesRepo TemplatesRepo
fs boshsys.FileSystem
logger boshlog.Logger
}
func NewTemplatesCompiler(
jobRenderer JobRenderer,
compressor boshcmd.Compressor,
blobstore boshblob.Blobstore,
templatesRepo TemplatesRepo,
fs boshsys.FileSystem,
logger boshlog.Logger,
) TemplatesCompiler {
return templatesCompiler{
jobRenderer: jobRenderer,
compressor: compressor,
blobstore: blobstore,
templatesRepo: templatesRepo,
fs: fs,
logger: logger,
}
}
func (tc templatesCompiler) Compile(jobs []bmrel.Job, deployment bmdepl.Deployment) error {
for _, job := range jobs {
err := tc.compileJob(job, deployment)
if err != nil {
return err
}
}
return nil
}
func (tc templatesCompiler) compileJob(job bmrel.Job, deployment bmdepl.Deployment) error {
jobSrcDir := job.ExtractedPath
jobCompileDir, err := tc.fs.TempDir("templates-compiler")
if err != nil {
return bosherr.WrapError(err, "Creating compilation directory")
}
defer tc.fs.RemoveAll(jobCompileDir)
err = tc.jobRenderer.Render(jobSrcDir, jobCompileDir, job, deployment.Properties, deployment.Name)
if err != nil {
return bosherr.WrapError(err, "Rendering templates")
}
tarball, err := tc.compressor.CompressFilesInDir(jobCompileDir)
if err != nil {
return bosherr.WrapError(err, "Compressing rendered job templates")
}
defer tc.compressor.CleanUp(tarball)
blobID, blobSHA1, err := tc.blobstore.Create(tarball)
if err != nil {
return bosherr.WrapError(err, "Creating blob")
}
record := TemplateRecord{
BlobID: blobID,
BlobSHA1: blobSHA1,
}
err = tc.templatesRepo.Save(job, record)
if err != nil {
return bosherr.WrapError(err, "Saving job to templates repo")
}
return nil
}