This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cppforlife/bosh-provisioner
/
deployment.go
222 lines (164 loc) · 4.7 KB
/
deployment.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
package deployment
import (
gonet "net"
boshaction "github.com/cloudfoundry/bosh-agent/agent/action"
bpdepman "github.com/cppforlife/bosh-provisioner/deployment/manifest"
)
type Deployment struct {
Manifest bpdepman.Manifest
Name string
Releases []Release
Networks []Network
Jobs []Job
CompilationInstance Instance
}
type Release struct {
Name string
Version string
// Not offical BOSH manifest construct
URL string
}
const (
NetworkTypeManual = bpdepman.NetworkTypeManual
NetworkTypeLocal = bpdepman.NetworkTypeLocal
NetworkTypeDynamic = bpdepman.NetworkTypeDynamic
NetworkTypeVip = bpdepman.NetworkTypeVip
)
var NetworkTypes = []string{NetworkTypeManual, NetworkTypeLocal, NetworkTypeDynamic, NetworkTypeVip}
type Network struct {
Name string
Type string
}
type Job struct {
Name string
Templates []Template
Instances []Instance
}
type Template struct {
Name string
Release *Release
}
type Instance struct {
Index int
// Denormalized values to avoid passing dep/job/instance tuple
JobName string
DeploymentName string
// Watch time will vary depending if an instance is a canary
WatchTime bpdepman.WatchTime
Properties Properties
NetworkAssociations []NetworkAssociation
// Represents current state of an associated VM
CurrentState boshaction.GetStateV1ApplySpec
}
type Properties map[string]interface{}
type NetworkAssociation struct {
Network *Network
StaticIP gonet.IP
// StaticIP might equal to nil, though that does not indicate
// that this instance does not need a static IP
MustHaveStaticIP bool
}
// populateFromManifest populates deployment information
// interpreted from deployment manifest.
func (d *Deployment) populateFromManifest(manifest bpdepman.Manifest) {
d.populateNetworks(manifest)
d.populateReleases(manifest)
d.populateCompilationInstances(manifest)
d.populateJobs(manifest)
d.Manifest = manifest
}
func (d *Deployment) populateNetworks(manifest bpdepman.Manifest) {
for _, manNet := range manifest.Deployment.Networks {
d.Networks = append(d.Networks, Network{
Name: manNet.Name,
Type: manNet.Type,
})
}
}
func (d *Deployment) populateReleases(manifest bpdepman.Manifest) {
for _, manRelease := range manifest.Deployment.Releases {
d.Releases = append(d.Releases, Release{
Name: manRelease.Name,
Version: manRelease.Version,
URL: manRelease.URL,
})
}
}
func (d *Deployment) populateCompilationInstances(manifest bpdepman.Manifest) {
network := d.findNetworkOrDefault(manifest.Deployment.Compilation.NetworkName)
d.CompilationInstance = Instance{
Index: 0,
JobName: "compilation",
DeploymentName: manifest.Deployment.Name,
NetworkAssociations: []NetworkAssociation{
NetworkAssociation{Network: network},
},
}
}
func (d *Deployment) populateJobs(manifest bpdepman.Manifest) {
for _, manJob := range manifest.Deployment.Jobs {
d.Jobs = append(d.Jobs, d.buildJob(manifest.Deployment, manJob))
}
}
func (d *Deployment) buildJob(manDep bpdepman.Deployment, manJob bpdepman.Job) Job {
job := Job{Name: manJob.Name}
for i := 0; i < manJob.Instances; i++ {
watchTime := manDep.InstanceWatchTime(manJob, i)
properties := manDep.InstanceProperties(manJob, i)
netAssocs := d.buildNetworkAssociations(manJob, i)
job.Instances = append(job.Instances, Instance{
Index: i,
JobName: manJob.Name,
DeploymentName: manDep.Name,
WatchTime: watchTime,
Properties: Properties(properties),
NetworkAssociations: netAssocs,
})
}
// todo check to make sure release is present
for _, manTemplate := range manJob.Templates {
release := d.findReleaseOrDefault(manTemplate.ReleaseName)
job.Templates = append(job.Templates, Template{
Name: manTemplate.Name,
Release: release,
})
}
return job
}
func (d *Deployment) buildNetworkAssociations(manJob bpdepman.Job, i int) []NetworkAssociation {
var netAssocs []NetworkAssociation
for _, manNa := range manJob.NetworkAssociations {
var staticIP gonet.IP
network := d.findNetworkOrDefault(manNa.NetworkName)
if i < len(manNa.StaticIPs) {
staticIP = manNa.StaticIPs[i]
}
netAssocs = append(netAssocs, NetworkAssociation{
Network: network,
StaticIP: staticIP,
MustHaveStaticIP: len(manNa.StaticIPs) > 0,
})
}
return netAssocs
}
func (d *Deployment) findReleaseOrDefault(name string) *Release {
// todo check against empty release name
for _, release := range d.Releases {
if release.Name == name {
return &release
}
}
// Assume if there is only one release it's default
if len(d.Releases) == 1 {
return &d.Releases[0]
}
return nil
}
func (d *Deployment) findNetworkOrDefault(name string) *Network {
for _, net := range d.Networks {
if net.Name == name {
return &net
}
}
return nil
}