forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_renderer.go
87 lines (73 loc) · 2.54 KB
/
job_renderer.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
package templatescompiler
import (
"os"
"path/filepath"
biproperty "github.com/cloudfoundry/bosh-init/common/property"
bireljob "github.com/cloudfoundry/bosh-init/release/job"
bierbrenderer "github.com/cloudfoundry/bosh-init/templatescompiler/erbrenderer"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type JobRenderer interface {
Render(releaseJob bireljob.Job, jobProperties, globalProperties biproperty.Map, deploymentName string) (RenderedJob, error)
}
type jobRenderer struct {
erbRenderer bierbrenderer.ERBRenderer
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
func NewJobRenderer(
erbRenderer bierbrenderer.ERBRenderer,
fs boshsys.FileSystem,
logger boshlog.Logger,
) JobRenderer {
return &jobRenderer{
erbRenderer: erbRenderer,
fs: fs,
logger: logger,
logTag: "jobRenderer",
}
}
func (r *jobRenderer) Render(releaseJob bireljob.Job, jobProperties, globalProperties biproperty.Map, deploymentName string) (RenderedJob, error) {
context := NewJobEvaluationContext(releaseJob, jobProperties, globalProperties, deploymentName, r.logger)
sourcePath := releaseJob.ExtractedPath
destinationPath, err := r.fs.TempDir("rendered-jobs")
if err != nil {
return nil, bosherr.WrapError(err, "Creating rendered job directory")
}
renderedJob := NewRenderedJob(releaseJob, destinationPath, r.fs, r.logger)
for src, dst := range releaseJob.Templates {
err := r.renderFile(
filepath.Join(sourcePath, "templates", src),
filepath.Join(destinationPath, dst),
context,
)
if err != nil {
defer renderedJob.DeleteSilently()
return nil, bosherr.WrapErrorf(err, "Rendering template src: %s, dst: %s", src, dst)
}
}
err = r.renderFile(
filepath.Join(sourcePath, "monit"),
filepath.Join(destinationPath, "monit"),
context,
)
if err != nil {
defer renderedJob.DeleteSilently()
return nil, bosherr.WrapError(err, "Rendering monit file")
}
return renderedJob, nil
}
func (r *jobRenderer) renderFile(sourcePath, destinationPath string, context bierbrenderer.TemplateEvaluationContext) error {
err := r.fs.MkdirAll(filepath.Dir(destinationPath), os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating tempdir '%s'", filepath.Dir(destinationPath))
}
err = r.erbRenderer.Render(sourcePath, destinationPath, context)
if err != nil {
return bosherr.WrapErrorf(err, "Rendering template src: %s, dst: %s", sourcePath, destinationPath)
}
return nil
}