-
Notifications
You must be signed in to change notification settings - Fork 161
/
parser.go
362 lines (300 loc) · 11.1 KB
/
parser.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package manifest
import (
biutil "github.com/cloudfoundry/bosh-cli/common/util"
bidepltpl "github.com/cloudfoundry/bosh-cli/deployment/template"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"gopkg.in/yaml.v2"
)
type Parser interface {
Parse(interpolatedTemplate bidepltpl.InterpolatedTemplate, path string) (Manifest, error)
}
type parser struct {
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
type manifest struct {
Name string
Update UpdateSpec
Networks []network
ResourcePools []resourcePool `yaml:"resource_pools"`
DiskPools []diskPool `yaml:"disk_pools"`
Jobs []job
InstanceGroups []job `yaml:"instance_groups"`
Properties map[interface{}]interface{}
Tags map[string]string
}
type UpdateSpec struct {
UpdateWatchTime *string `yaml:"update_watch_time"`
}
type network struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
CloudProperties map[interface{}]interface{} `yaml:"cloud_properties"`
Subnets []subnet `yaml:"subnets"`
IP string `yaml:"ip"`
Netmask string `yaml:"netmask"`
Gateway string `yaml:"gateway"`
DNS []string `yaml:"dns"`
}
type subnet struct {
Range string `yaml:"range"`
Gateway string `yaml:"gateway"`
DNS []string `yaml:"dns"`
CloudProperties map[interface{}]interface{} `yaml:"cloud_properties"`
}
type resourcePool struct {
Name string `yaml:"name"`
Network string `yaml:"network"`
CloudProperties map[interface{}]interface{} `yaml:"cloud_properties"`
Env map[interface{}]interface{} `yaml:"env"`
Stemcell stemcellRef `yaml:"stemcell"`
}
type diskPool struct {
Name string `yaml:"name"`
DiskSize int `yaml:"disk_size"`
CloudProperties map[interface{}]interface{} `yaml:"cloud_properties"`
}
type job struct {
Name string
Instances int
Lifecycle string
Templates []releaseJobRef
Jobs []releaseJobRef `yaml:"jobs"`
Networks []jobNetwork
PersistentDisk int `yaml:"persistent_disk"`
PersistentDiskPool string `yaml:"persistent_disk_pool"`
ResourcePool string `yaml:"resource_pool"`
Properties map[interface{}]interface{}
}
type releaseJobRef struct {
Name string
Release string
// This is a pointer so we can differentiate between `properties: {}`
// and not specifying the key at all.
Properties *map[interface{}]interface{}
}
type stemcellRef struct {
URL string
SHA1 string
}
type jobNetwork struct {
Name string
Defaults []string `yaml:"default"`
StaticIPs []string `yaml:"static_ips"`
}
var boshDeploymentDefaults = Manifest{
Update: Update{
UpdateWatchTime: WatchTime{
Start: 0,
End: 300000,
},
},
}
func NewParser(fs boshsys.FileSystem, logger boshlog.Logger) Parser {
return &parser{
fs: fs,
logger: logger,
logTag: "deploymentParser",
}
}
func (p *parser) Parse(interpolatedTemplate bidepltpl.InterpolatedTemplate, path string) (Manifest, error) {
bytes := interpolatedTemplate.Content()
comboManifest := manifest{}
err := yaml.Unmarshal(bytes, &comboManifest)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Unmarshalling BOSH deployment manifest")
}
p.logger.Debug(p.logTag, "Parsed BOSH deployment manifest: %#v", comboManifest)
deploymentManifest, err := p.parseDeploymentManifest(comboManifest, path)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Unmarshalling BOSH deployment manifest")
}
return deploymentManifest, nil
}
func (p *parser) parseDeploymentManifest(depManifest manifest, path string) (Manifest, error) {
deployment := boshDeploymentDefaults
deployment.Name = depManifest.Name
deployment.Tags = depManifest.Tags
networks, err := p.parseNetworkManifests(depManifest.Networks)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Parsing networks: %#v", depManifest.Networks)
}
deployment.Networks = networks
resourcePools, err := p.parseResourcePoolManifests(depManifest.ResourcePools, path)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Parsing resource_pools: %#v", depManifest.ResourcePools)
}
deployment.ResourcePools = resourcePools
diskPools, err := p.parseDiskPoolManifests(depManifest.DiskPools)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Parsing disk_pools: %#v", depManifest.DiskPools)
}
deployment.DiskPools = diskPools
if len(depManifest.Jobs) > 0 && len(depManifest.InstanceGroups) > 0 {
return Manifest{}, bosherr.Error("Deployment specifies both jobs and instance_groups keys, only one is allowed")
}
rawJobs := depManifest.Jobs
if len(depManifest.InstanceGroups) > 0 {
rawJobs = depManifest.InstanceGroups
}
jobs, err := p.parseJobManifests(rawJobs)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Parsing jobs: %#v", depManifest.Jobs)
}
deployment.Jobs = jobs
properties, err := biproperty.BuildMap(depManifest.Properties)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Parsing global manifest properties: %#v", depManifest.Properties)
}
deployment.Properties = properties
if depManifest.Update.UpdateWatchTime != nil {
updateWatchTime, err := NewWatchTime(*depManifest.Update.UpdateWatchTime)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Parsing update watch time")
}
deployment.Update = Update{
UpdateWatchTime: updateWatchTime,
}
}
return deployment, nil
}
func (p *parser) parseJobManifests(rawJobs []job) ([]Job, error) {
jobs := make([]Job, len(rawJobs), len(rawJobs))
for i, rawJob := range rawJobs {
job := Job{
Name: rawJob.Name,
Instances: rawJob.Instances,
Lifecycle: JobLifecycle(rawJob.Lifecycle),
PersistentDisk: rawJob.PersistentDisk,
PersistentDiskPool: rawJob.PersistentDiskPool,
ResourcePool: rawJob.ResourcePool,
}
if len(rawJob.Templates) > 0 && len(rawJob.Jobs) > 0 {
return jobs, bosherr.Error("Deployment specifies both templates and jobs keys for instance_group " + job.Name + ", only one is allowed")
}
templates := rawJob.Templates
if len(rawJob.Jobs) > 0 {
templates = rawJob.Jobs
}
if templates != nil {
releaseJobRefs := make([]ReleaseJobRef, len(templates), len(templates))
for i, rawJobRef := range templates {
ref := ReleaseJobRef{
Name: rawJobRef.Name,
Release: rawJobRef.Release,
}
if rawJobRef.Properties != nil {
properties, err := biproperty.BuildMap(*rawJobRef.Properties)
if err != nil {
return []Job{}, bosherr.WrapErrorf(err, "Parsing release job properties: %#v", rawJobRef.Properties)
}
ref.Properties = &properties
}
releaseJobRefs[i] = ref
}
job.Templates = releaseJobRefs
}
if rawJob.Networks != nil {
jobNetworks := make([]JobNetwork, len(rawJob.Networks), len(rawJob.Networks))
for i, rawJobNetwork := range rawJob.Networks {
jobNetwork := JobNetwork{
Name: rawJobNetwork.Name,
StaticIPs: rawJobNetwork.StaticIPs,
}
if rawJobNetwork.Defaults != nil {
networkDefaults := make([]NetworkDefault, len(rawJobNetwork.Defaults), len(rawJobNetwork.Defaults))
for i, rawDefaults := range rawJobNetwork.Defaults {
networkDefaults[i] = NetworkDefault(rawDefaults)
}
jobNetwork.Defaults = networkDefaults
}
jobNetworks[i] = jobNetwork
}
job.Networks = jobNetworks
}
if rawJob.Properties != nil {
properties, err := biproperty.BuildMap(rawJob.Properties)
if err != nil {
return jobs, bosherr.WrapErrorf(err, "Parsing job '%s' properties: %#v", rawJob.Name, rawJob.Properties)
}
job.Properties = properties
}
jobs[i] = job
}
return jobs, nil
}
func (p *parser) parseNetworkManifests(rawNetworks []network) ([]Network, error) {
networks := make([]Network, len(rawNetworks), len(rawNetworks))
for i, rawNetwork := range rawNetworks {
network := Network{
Name: rawNetwork.Name,
Type: NetworkType(rawNetwork.Type),
DNS: rawNetwork.DNS,
}
cloudProperties, err := biproperty.BuildMap(rawNetwork.CloudProperties)
if err != nil {
return networks, bosherr.WrapErrorf(err, "Parsing network '%s' cloud_properties: %#v", rawNetwork.Name, rawNetwork.CloudProperties)
}
network.CloudProperties = cloudProperties
for _, subnet := range rawNetwork.Subnets {
cloudProperties, err := biproperty.BuildMap(subnet.CloudProperties)
if err != nil {
return networks, bosherr.WrapErrorf(err, "Parsing network subnet '%s' cloud_properties: %#v", rawNetwork.Name, subnet.CloudProperties)
}
network.Subnets = append(network.Subnets, Subnet{
Range: subnet.Range,
Gateway: subnet.Gateway,
DNS: subnet.DNS,
CloudProperties: cloudProperties,
})
}
networks[i] = network
}
return networks, nil
}
func (p *parser) parseResourcePoolManifests(rawResourcePools []resourcePool, path string) ([]ResourcePool, error) {
resourcePools := make([]ResourcePool, len(rawResourcePools), len(rawResourcePools))
for i, rawResourcePool := range rawResourcePools {
resourcePool := ResourcePool{
Name: rawResourcePool.Name,
Network: rawResourcePool.Network,
Stemcell: StemcellRef(rawResourcePool.Stemcell),
}
cloudProperties, err := biproperty.BuildMap(rawResourcePool.CloudProperties)
if err != nil {
return resourcePools, bosherr.WrapErrorf(err, "Parsing resource_pool '%s' cloud_properties: %#v", rawResourcePool.Name, rawResourcePool.CloudProperties)
}
resourcePool.CloudProperties = cloudProperties
env, err := biproperty.BuildMap(rawResourcePool.Env)
if err != nil {
return resourcePools, bosherr.WrapErrorf(err, "Parsing resource_pool '%s' env: %#v", rawResourcePool.Name, rawResourcePool.Env)
}
resourcePool.Env = env
resourcePool.Stemcell.URL, err = biutil.AbsolutifyPath(path, resourcePool.Stemcell.URL, p.fs)
if err != nil {
return resourcePools, bosherr.WrapErrorf(err, "Resolving stemcell path '%s", resourcePool.Stemcell.URL)
}
resourcePools[i] = resourcePool
}
return resourcePools, nil
}
func (p *parser) parseDiskPoolManifests(rawDiskPools []diskPool) ([]DiskPool, error) {
diskPools := make([]DiskPool, len(rawDiskPools), len(rawDiskPools))
for i, rawDiskPool := range rawDiskPools {
diskPool := DiskPool{
Name: rawDiskPool.Name,
DiskSize: rawDiskPool.DiskSize,
}
cloudProperties, err := biproperty.BuildMap(rawDiskPool.CloudProperties)
if err != nil {
return diskPools, bosherr.WrapErrorf(err, "Parsing disk_pool '%s' cloud_properties: %#v", rawDiskPool.Name, rawDiskPool.CloudProperties)
}
diskPool.CloudProperties = cloudProperties
diskPools[i] = diskPool
}
return diskPools, nil
}