forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
258 lines (217 loc) · 8.46 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package state
import (
"errors"
agentclient "github.com/cloudfoundry/bosh-agent/agentclient"
biblobstore "github.com/cloudfoundry/bosh-cli/blobstore"
bideplmanifest "github.com/cloudfoundry/bosh-cli/deployment/manifest"
bideplrel "github.com/cloudfoundry/bosh-cli/deployment/release"
bireljob "github.com/cloudfoundry/bosh-cli/release/job"
bistatejob "github.com/cloudfoundry/bosh-cli/state/job"
bitemplate "github.com/cloudfoundry/bosh-cli/templatescompiler"
biui "github.com/cloudfoundry/bosh-cli/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
biproperty "github.com/cloudfoundry/bosh-utils/property"
)
type Builder interface {
Build(jobName string, instanceID int, deploymentManifest bideplmanifest.Manifest, stage biui.Stage, agentState agentclient.AgentState) (State, error)
BuildInitialState(jobName string, instanceID int, deploymentManifest bideplmanifest.Manifest) (State, error)
}
type builder struct {
releaseJobResolver bideplrel.JobResolver
jobDependencyCompiler bistatejob.DependencyCompiler
jobListRenderer bitemplate.JobListRenderer
renderedJobListCompressor bitemplate.RenderedJobListCompressor
blobstore biblobstore.Blobstore
logger boshlog.Logger
logTag string
}
func NewBuilder(
releaseJobResolver bideplrel.JobResolver,
jobDependencyCompiler bistatejob.DependencyCompiler,
jobListRenderer bitemplate.JobListRenderer,
renderedJobListCompressor bitemplate.RenderedJobListCompressor,
blobstore biblobstore.Blobstore,
logger boshlog.Logger,
) Builder {
return &builder{
releaseJobResolver: releaseJobResolver,
jobDependencyCompiler: jobDependencyCompiler,
jobListRenderer: jobListRenderer,
renderedJobListCompressor: renderedJobListCompressor,
blobstore: blobstore,
logger: logger,
logTag: "instanceStateBuilder",
}
}
type renderedJobs struct {
BlobstoreID string
Archive bitemplate.RenderedJobListArchive
}
func (b *builder) Build(jobName string, instanceID int, deploymentManifest bideplmanifest.Manifest, stage biui.Stage, agentState agentclient.AgentState) (State, error) {
initialState, err := b.BuildInitialState(jobName, instanceID, deploymentManifest)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Building initial state '%s", jobName)
}
deploymentJob, found := deploymentManifest.FindJobByName(jobName)
if !found {
return nil, bosherr.Errorf("Job '%s' not found in deployment manifest", jobName)
}
releaseJobs, err := b.resolveJobs(deploymentJob.Templates)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Resolving jobs for instance '%s/%d'", jobName, instanceID)
}
releaseJobProperties := make(map[string]*biproperty.Map)
for _, releaseJob := range deploymentJob.Templates {
releaseJobProperties[releaseJob.Name] = releaseJob.Properties
}
defaultAddress, err := b.defaultAddress(initialState.NetworkInterfaces(), agentState)
if err != nil {
return nil, err
}
renderedJobTemplates, err := b.renderJobTemplates(releaseJobs, releaseJobProperties, deploymentJob.Properties, deploymentManifest.Properties, deploymentManifest.Name, defaultAddress, stage)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Rendering job templates for instance '%s/%d'", jobName, instanceID)
}
compiledPackageRefs, err := b.jobDependencyCompiler.Compile(releaseJobs, stage)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Compiling job package dependencies for instance '%s/%d'", jobName, instanceID)
}
compiledDeploymentPackageRefs := make([]PackageRef, len(compiledPackageRefs), len(compiledPackageRefs))
for i, compiledPackageRef := range compiledPackageRefs {
compiledDeploymentPackageRefs[i] = PackageRef{
Name: compiledPackageRef.Name,
Version: compiledPackageRef.Version,
Archive: BlobRef{
BlobstoreID: compiledPackageRef.BlobstoreID,
SHA1: compiledPackageRef.SHA1,
},
}
}
// convert array to array
renderedJobRefs := make([]JobRef, len(releaseJobs), len(releaseJobs))
for i, releaseJob := range releaseJobs {
renderedJobRefs[i] = JobRef{
Name: releaseJob.Name(),
Version: releaseJob.Fingerprint(),
}
}
renderedJobListArchiveBlobRef := BlobRef{
BlobstoreID: renderedJobTemplates.BlobstoreID,
SHA1: renderedJobTemplates.Archive.SHA1(),
}
return &state{
deploymentName: deploymentManifest.Name,
name: jobName,
id: instanceID,
networks: initialState.NetworkInterfaces(),
compiledPackages: compiledDeploymentPackageRefs,
renderedJobs: renderedJobRefs,
renderedJobListArchive: renderedJobListArchiveBlobRef,
hash: renderedJobTemplates.Archive.Fingerprint(),
}, nil
}
func (b *builder) BuildInitialState(jobName string, instanceID int, deploymentManifest bideplmanifest.Manifest) (State, error) {
deploymentJob, found := deploymentManifest.FindJobByName(jobName)
if !found {
return nil, bosherr.Errorf("Job '%s' not found in deployment manifest", jobName)
}
networkInterfaces, err := deploymentManifest.NetworkInterfaces(deploymentJob.Name)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Finding networks for job '%s", jobName)
}
// convert map to array
networkRefs := make([]NetworkRef, 0, len(networkInterfaces))
for networkName, networkInterface := range networkInterfaces {
genericMap := make(map[string]interface{}, len(networkInterface))
for k, v := range networkInterface {
genericMap[k] = v
}
networkRefs = append(networkRefs, NetworkRef{
Name: networkName,
Interface: genericMap,
})
}
return &state{
deploymentName: deploymentManifest.Name,
name: jobName,
id: instanceID,
networks: networkRefs,
}, nil
}
// FIXME: why do i exist here and in installation/state/builder.go??
func (b *builder) resolveJobs(jobRefs []bideplmanifest.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.Errorf("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,
releaseJobProperties map[string]*biproperty.Map,
jobProperties biproperty.Map,
globalProperties biproperty.Map,
deploymentName string,
address string,
stage biui.Stage,
) (renderedJobs, error) {
var (
renderedJobListArchive bitemplate.RenderedJobListArchive
blobID string
)
err := stage.Perform("Rendering job templates", func() error {
renderedJobList, err := b.jobListRenderer.Render(releaseJobs, releaseJobProperties, jobProperties, globalProperties, deploymentName, address)
if err != nil {
return err
}
defer renderedJobList.DeleteSilently()
renderedJobListArchive, err = b.renderedJobListCompressor.Compress(renderedJobList)
if err != nil {
return bosherr.WrapError(err, "Compressing rendered job templates")
}
defer renderedJobListArchive.DeleteSilently()
blobID, err = b.blobstore.Add(renderedJobListArchive.Path())
if err != nil {
return bosherr.WrapErrorf(err, "Uploading rendered job template archive '%s' to the blobstore", renderedJobListArchive.Path())
}
return nil
})
if err != nil {
return renderedJobs{}, err
}
return renderedJobs{
BlobstoreID: blobID,
Archive: renderedJobListArchive,
}, nil
}
func (b *builder) defaultAddress(networkRefs []NetworkRef, agentState agentclient.AgentState) (string, error) {
if (networkRefs == nil) || (len(networkRefs) == 0) {
return "", errors.New("Must specify network")
}
if len(networkRefs) == 1 {
return networkIp(networkRefs[0], agentState), nil
}
for _, ref := range networkRefs {
if ref.Interface["default"] == nil {
continue
}
for _, val := range ref.Interface["default"].([]bideplmanifest.NetworkDefault) {
if val == "gateway" {
return networkIp(ref, agentState), nil
}
}
}
return "", errors.New("Must specify default network")
}
func networkIp(networkRef NetworkRef, agentState agentclient.AgentState) string {
if "dynamic" == networkRef.Interface["type"].(string) {
return agentState.NetworkSpecs[networkRef.Name].IP
}
return networkRef.Interface["ip"].(string)
}