forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
163 lines (140 loc) · 5.73 KB
/
builder.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package state
import (
boshblob "github.com/cloudfoundry/bosh-agent/blobstore"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshcmd "github.com/cloudfoundry/bosh-agent/platform/commands"
biproperty "github.com/cloudfoundry/bosh-init/common/property"
bideplrel "github.com/cloudfoundry/bosh-init/deployment/release"
biinstalljob "github.com/cloudfoundry/bosh-init/installation/job"
biinstallmanifest "github.com/cloudfoundry/bosh-init/installation/manifest"
biinstallpkg "github.com/cloudfoundry/bosh-init/installation/pkg"
bireljob "github.com/cloudfoundry/bosh-init/release/job"
bistatejob "github.com/cloudfoundry/bosh-init/state/job"
bitemplate "github.com/cloudfoundry/bosh-init/templatescompiler"
biui "github.com/cloudfoundry/bosh-init/ui"
)
type Builder interface {
Build(biinstallmanifest.Manifest, biui.Stage) (State, error)
}
type builder struct {
releaseJobResolver bideplrel.JobResolver
jobDependencyCompiler bistatejob.DependencyCompiler
jobListRenderer bitemplate.JobListRenderer
compressor boshcmd.Compressor
blobstore boshblob.Blobstore
templatesRepo bitemplate.TemplatesRepo
}
func NewBuilder(
releaseJobResolver bideplrel.JobResolver,
jobDependencyCompiler bistatejob.DependencyCompiler,
jobListRenderer bitemplate.JobListRenderer,
compressor boshcmd.Compressor,
blobstore boshblob.Blobstore,
templatesRepo bitemplate.TemplatesRepo,
) Builder {
return &builder{
releaseJobResolver: releaseJobResolver,
jobDependencyCompiler: jobDependencyCompiler,
jobListRenderer: jobListRenderer,
compressor: compressor,
blobstore: blobstore,
templatesRepo: templatesRepo,
}
}
func (b *builder) Build(installationManifest biinstallmanifest.Manifest, stage biui.Stage) (State, error) {
// installation only ever has one job: the cpi job.
releaseJobRefs := []biinstallmanifest.ReleaseJobRef{installationManifest.Template}
// installation jobs do not get rendered with global deployment properties, only the cloud_provider properties
globalProperties := biproperty.Map{}
jobProperties := installationManifest.Properties
releaseJobs, err := b.resolveJobs(releaseJobRefs)
if err != nil {
return nil, bosherr.WrapError(err, "Resolving jobs for installation")
}
compiledPackageRefs, err := b.jobDependencyCompiler.Compile(releaseJobs, stage)
if err != nil {
return nil, bosherr.WrapError(err, "Compiling job package dependencies for installation")
}
renderedJobRefs, err := b.renderJobTemplates(releaseJobs, jobProperties, globalProperties, installationManifest.Name, stage)
if err != nil {
return nil, bosherr.WrapError(err, "Rendering job templates for installation")
}
if len(renderedJobRefs) != 1 {
return nil, bosherr.Error("Too many jobs rendered... oops?")
}
compiledInstallationPackageRefs := make([]biinstallpkg.CompiledPackageRef, len(compiledPackageRefs), len(compiledPackageRefs))
for i, compiledPackageRef := range compiledPackageRefs {
compiledInstallationPackageRefs[i] = biinstallpkg.CompiledPackageRef{
Name: compiledPackageRef.Name,
Version: compiledPackageRef.Version,
BlobstoreID: compiledPackageRef.BlobstoreID,
SHA1: compiledPackageRef.SHA1,
}
}
return NewState(renderedJobRefs[0], compiledInstallationPackageRefs), nil
}
// FIXME: why do i exist here and in deployment/instance/state/builder.go??
func (b *builder) resolveJobs(jobRefs []biinstallmanifest.ReleaseJobRef) ([]bireljob.Job, error) {
releaseJobs := make([]bireljob.Job, len(jobRefs), len(jobRefs))
for i, jobRef := range jobRefs {
release, err := b.releaseJobResolver.Resolve(jobRef.Name, jobRef.Release)
if err != nil {
return releaseJobs, bosherr.WrapErrorf(err, "Resolving job '%s' in release '%s'", jobRef.Name, jobRef.Release)
}
releaseJobs[i] = release
}
return releaseJobs, nil
}
// renderJobTemplates renders all the release job templates for multiple release jobs specified by a deployment job
func (b *builder) renderJobTemplates(
releaseJobs []bireljob.Job,
jobProperties biproperty.Map,
globalProperties biproperty.Map,
deploymentName string,
stage biui.Stage,
) ([]biinstalljob.RenderedJobRef, error) {
renderedJobRefs := make([]biinstalljob.RenderedJobRef, 0, len(releaseJobs))
err := stage.Perform("Rendering job templates", func() error {
renderedJobList, err := b.jobListRenderer.Render(releaseJobs, jobProperties, globalProperties, deploymentName)
if err != nil {
return err
}
defer renderedJobList.DeleteSilently()
for _, renderedJob := range renderedJobList.All() {
renderedJobRecord, err := b.compressAndUpload(renderedJob)
if err != nil {
return err
}
releaseJob := renderedJob.Job()
renderedJobRefs = append(renderedJobRefs, biinstalljob.RenderedJobRef{
Name: releaseJob.Name,
Version: releaseJob.Fingerprint,
BlobstoreID: renderedJobRecord.BlobID,
SHA1: renderedJobRecord.BlobSHA1,
})
}
return nil
})
return renderedJobRefs, err
}
func (b *builder) compressAndUpload(renderedJob bitemplate.RenderedJob) (record bitemplate.TemplateRecord, err error) {
tarballPath, err := b.compressor.CompressFilesInDir(renderedJob.Path())
if err != nil {
return record, bosherr.WrapError(err, "Compressing rendered job templates")
}
defer b.compressor.CleanUp(tarballPath)
blobID, blobSHA1, err := b.blobstore.Create(tarballPath)
if err != nil {
return record, bosherr.WrapError(err, "Creating blob")
}
record = bitemplate.TemplateRecord{
BlobID: blobID,
BlobSHA1: blobSHA1,
}
//TODO: move TemplatesRepo to state/job.TemplatesRepo and reuse in deployment/instance/state.Builder
err = b.templatesRepo.Save(renderedJob.Job(), record)
if err != nil {
return record, bosherr.WrapError(err, "Saving job to templates repo")
}
return record, nil
}