-
Notifications
You must be signed in to change notification settings - Fork 104
/
configure_director.go
538 lines (444 loc) · 16 KB
/
configure_director.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package commands
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"github.com/pivotal-cf/om/interpolate"
"github.com/pivotal-cf/om/api"
"gopkg.in/yaml.v2"
)
type ConfigureDirector struct {
environFunc func() []string
service configureDirectorService
logger logger
Options struct {
IgnoreVerifierWarnings bool `long:"ignore-verifier-warnings" description:"option to ignore verifier warnings. NOT RECOMMENDED UNLESS DISABLED IN OPS MANAGER"`
ConfigFile string `long:"config" short:"c" description:"path to yml file containing all config fields (see docs/configure-director/README.md for format)" required:"true"`
VarsFile []string `long:"vars-file" short:"l" description:"load variables from a YAML file"`
VarsEnv []string `long:"vars-env" env:"OM_VARS_ENV" description:"load variables from environment variables (e.g.: 'MY' to load MY_var=value)"`
Vars []string `long:"var" short:"v" description:"load variable from the command line. Format: VAR=VAL"`
OpsFile []string `long:"ops-file" description:"YAML operations file"`
}
}
type VMTypesConfiguration struct {
CustomTypesOnly bool `yaml:"custom_only,omitempty" json:"custom_only,omitempty"`
VMTypes []api.CreateVMType `yaml:"vm_types,omitempty" json:"vm_types,omitempty"`
}
type directorConfig struct {
NetworkAssignment interface{} `yaml:"network-assignment"`
AZConfiguration interface{} `yaml:"az-configuration"`
NetworksConfiguration interface{} `yaml:"networks-configuration"`
PropertiesConfiguration interface{} `yaml:"properties-configuration"`
IAASConfigurations interface{} `yaml:"iaas-configurations"`
ResourceConfiguration map[string]interface{} `yaml:"resource-configuration"`
VMExtensions interface{} `yaml:"vmextensions-configuration"`
VMTypes VMTypesConfiguration `yaml:"vmtypes-configuration"`
Field map[string]interface{} `yaml:",inline"`
}
//counterfeiter:generate -o ./fakes/configure_director_service.go --fake-name ConfigureDirectorService . configureDirectorService
type configureDirectorService interface {
ConfigureJobResourceConfig(productGUID string, config map[string]interface{}) error
CreateCustomVMTypes(api.CreateVMTypes) error
CreateStagedVMExtension(api.CreateVMExtension) error
DeleteCustomVMTypes() error
DeleteVMExtension(name string) error
GetStagedProductByName(name string) (api.StagedProductsFindOutput, error)
GetStagedProductManifest(guid string) (manifest string, err error)
Info() (api.Info, error)
ListInstallations() ([]api.InstallationsServiceOutput, error)
ListStagedVMExtensions() ([]api.VMExtension, error)
ListVMTypes() ([]api.VMType, error)
UpdateStagedDirectorIAASConfigurations(api.IAASConfigurationsInput, bool) error
UpdateStagedDirectorAvailabilityZones(api.AvailabilityZoneInput, bool) error
UpdateStagedDirectorNetworkAndAZ(api.NetworkAndAZConfiguration) error
UpdateStagedDirectorNetworks(api.NetworkInput) error
UpdateStagedDirectorProperties(api.DirectorProperties) error
}
func NewConfigureDirector(environFunc func() []string, service configureDirectorService, logger logger) *ConfigureDirector {
return &ConfigureDirector{
environFunc: environFunc,
service: service,
logger: logger,
}
}
func (c ConfigureDirector) Execute(args []string) error {
err := checkRunningInstallation(c.service.ListInstallations)
if err != nil {
return err
}
config, err := c.interpolateConfig()
if err != nil {
return err
}
err = c.validateConfig(config)
if err != nil {
return err
}
err = c.updateIAASConfigurations(config)
if err != nil {
return err
}
err = c.updateStagedDirectorProperties(config)
if err != nil {
return err
}
err = c.configureAvailabilityZones(config)
if err != nil {
return err
}
err = c.configureNetworksConfiguration(config)
if err != nil {
return err
}
err = c.configureNetworkAssignment(config)
if err != nil {
return err
}
err = c.configureVMTypes(config)
if err != nil {
return err
}
err = c.configureVMExtensions(config)
if err != nil {
return err
}
productGUID, err := c.getProductGUID()
if err != nil {
return err
}
err = c.configureResourceConfiguration(config, productGUID)
if err != nil {
return err
}
return nil
}
func (c ConfigureDirector) interpolateConfig() (*directorConfig, error) {
configContents, err := interpolate.Execute(interpolate.Options{
TemplateFile: c.Options.ConfigFile,
VarsFiles: c.Options.VarsFile,
EnvironFunc: c.environFunc,
Vars: c.Options.Vars,
VarsEnvs: c.Options.VarsEnv,
OpsFiles: c.Options.OpsFile,
ExpectAllKeys: true,
})
if err != nil {
return nil, err
}
var config directorConfig
err = yaml.UnmarshalStrict(configContents, &config)
if err != nil {
return nil, fmt.Errorf("could not be parsed as valid configuration: %s: %s", c.Options.ConfigFile, err)
}
return &config, nil
}
func (c ConfigureDirector) validateConfig(config *directorConfig) error {
err := c.checkForDeprecatedKeys(config)
if err != nil {
return err
}
err = c.checkIAASConfigurationIsOnlySetOnce(config)
if err != nil {
return err
}
return nil
}
func (c ConfigureDirector) checkForDeprecatedKeys(config *directorConfig) error {
if len(config.Field) > 0 {
var unrecognizedKeys []string
for key := range config.Field {
unrecognizedKeys = append(unrecognizedKeys, key)
}
sort.Strings(unrecognizedKeys)
deprecatedKeys := []string{"director-configuration", "iaas-configuration", "security-configuration", "syslog-configuration"}
errorMessage := `The following keys have recently been removed from the top level configuration: director-configuration, iaas-configuration, security-configuration, syslog-configuration
To fix this error, move the above keys under 'properties-configuration' and change their dashes to underscores.
The old configuration file would contain the keys at the top level.
director-configuration: {}
iaas-configuration: {}
network-assignment: {}
networks-configuration: {}
resource-configuration: {}
security-configuration: {}
syslog-configuration: {}
vmextensions-configuration: {}
They'll need to be moved to the new 'properties-configuration', with their dashes turn to underscore.
For example, 'director-configuration' becomes 'director_configuration'.
The new configuration file will look like.
az-configuration: {}
network-assignment: {}
networks-configuration: {}
properties-configuration:
director_configuration: {}
security_configuration: {}
syslog_configuration: {}
iaas_configuration: {}
resource-configuration: {}
vmextensions-configuration: {}
`
for _, depKey := range deprecatedKeys {
for _, unrecKey := range unrecognizedKeys {
if depKey == unrecKey {
return errors.New(errorMessage)
}
}
}
return fmt.Errorf("the config file contains unrecognized keys: \"%s\"", strings.Join(unrecognizedKeys, "\", \""))
}
return nil
}
func (c ConfigureDirector) checkIAASConfigurationIsOnlySetOnce(config *directorConfig) error {
iaasConfigurations := config.IAASConfigurations
properties, ok := config.PropertiesConfiguration.(map[interface{}]interface{})
if !ok {
return nil
}
iaasProperties := properties["iaas-configuration"]
if iaasConfigurations != nil && iaasProperties != nil {
return errors.New("iaas-configurations cannot be used with properties-configuration.iaas-configurations\n" +
"Please only use one implementation.")
}
return nil
}
func (c ConfigureDirector) updateIAASConfigurations(config *directorConfig) error {
if config.IAASConfigurations != nil {
c.logger.Printf("started setting iaas configurations for bosh tile")
info, err := c.service.Info()
if err != nil {
return fmt.Errorf("could not retrieve info from targetted ops manager: %v", err)
}
if ok, err := info.VersionAtLeast(2, 2); !ok {
return fmt.Errorf("\"iaas-configurations\" is only available with Ops Manager 2.2 or later: you are running %s. Error: %w", info.Version, err)
}
configurations, err := getJSONProperties(config.IAASConfigurations)
if err != nil {
return err
}
err = c.service.UpdateStagedDirectorIAASConfigurations(api.IAASConfigurationsInput(configurations), c.Options.IgnoreVerifierWarnings)
if err != nil {
return fmt.Errorf("iaas configurations could not be completed: %s", err)
}
c.logger.Printf("finished setting iaas configurations for bosh tile")
}
return nil
}
func (c ConfigureDirector) updateStagedDirectorProperties(config *directorConfig) error {
if config.PropertiesConfiguration != nil {
c.logger.Printf("started configuring director options for bosh tile")
properties, err := getJSONProperties(config.PropertiesConfiguration)
if err != nil {
return err
}
err = c.service.UpdateStagedDirectorProperties(api.DirectorProperties(properties))
if err != nil {
return fmt.Errorf("properties could not be applied: %s", err)
}
c.logger.Printf("finished configuring director options for bosh tile")
}
return nil
}
func (c ConfigureDirector) configureAvailabilityZones(config *directorConfig) error {
if config.AZConfiguration != nil {
c.logger.Printf("started configuring availability zone options for bosh tile")
azs, err := getJSONProperties(config.AZConfiguration)
if err != nil {
return err
}
err = c.service.UpdateStagedDirectorAvailabilityZones(api.AvailabilityZoneInput{
AvailabilityZones: json.RawMessage(azs),
}, c.Options.IgnoreVerifierWarnings)
if err != nil {
return fmt.Errorf("availability zones configuration could not be applied: %s", err)
}
c.logger.Printf("finished configuring availability zone options for bosh tile")
}
return nil
}
func (c ConfigureDirector) configureNetworksConfiguration(config *directorConfig) error {
if config.NetworksConfiguration != nil {
c.logger.Printf("started configuring network options for bosh tile")
networksConfiguration, err := getJSONProperties(config.NetworksConfiguration)
if err != nil {
return err
}
err = c.service.UpdateStagedDirectorNetworks(api.NetworkInput{
Networks: json.RawMessage(networksConfiguration),
})
if err != nil {
return fmt.Errorf("networks configuration could not be applied: %s", err)
}
c.logger.Printf("finished configuring network options for bosh tile")
}
return nil
}
func (c ConfigureDirector) configureNetworkAssignment(config *directorConfig) error {
if config.NetworkAssignment != nil {
c.logger.Printf("started configuring network assignment options for bosh tile")
networkAssignment, err := getJSONProperties(config.NetworkAssignment)
if err != nil {
return err
}
err = c.service.UpdateStagedDirectorNetworkAndAZ(api.NetworkAndAZConfiguration{
NetworkAZ: json.RawMessage(networkAssignment),
})
if err != nil {
return fmt.Errorf("network and AZs could not be applied: %s", err)
}
c.logger.Printf("finished configuring network assignment options for bosh tile")
}
return nil
}
func (c ConfigureDirector) addNewExtensions(extensionsToDelete map[string]api.VMExtension, newExtensions interface{}) (map[string]api.VMExtension, error) {
var newVMExtensions []api.VMExtension
newExtensionBytes, err := getJSONProperties(newExtensions)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(newExtensionBytes), &newVMExtensions)
if err != nil {
return nil, fmt.Errorf("could not unmarshall vmextensions-configuration json: %s. Full Error: %s", newExtensions, err)
}
c.logger.Printf("applying vmextensions configuration for the following:")
for _, newExtension := range newVMExtensions {
c.logger.Printf("\t%s", newExtension.Name)
cloudProperties, err := json.Marshal(newExtension.CloudProperties)
if err != nil {
return nil, err
}
err = c.service.CreateStagedVMExtension(api.CreateVMExtension{
Name: newExtension.Name,
CloudProperties: cloudProperties,
})
if err != nil {
return nil, err
}
for name := range extensionsToDelete {
if name == newExtension.Name {
delete(extensionsToDelete, name)
}
}
}
return extensionsToDelete, nil
}
func (c ConfigureDirector) getExistingExtensions() (map[string]api.VMExtension, error) {
existingVMExtensions, err := c.service.ListStagedVMExtensions()
if err != nil {
return nil, err
}
extensionsToDelete := make(map[string]api.VMExtension)
for _, vmExtension := range existingVMExtensions {
extensionsToDelete[vmExtension.Name] = vmExtension
}
return extensionsToDelete, nil
}
func (c ConfigureDirector) deleteExtensions(extensionsToDelete map[string]api.VMExtension) error {
keys := []string{}
for k := range extensionsToDelete {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
extensionToDelete := extensionsToDelete[key]
c.logger.Printf("deleting vm extension %s", extensionToDelete.Name)
err := c.service.DeleteVMExtension(extensionToDelete.Name)
if err != nil {
return err
}
c.logger.Printf("done deleting vm extension %s", extensionToDelete.Name)
}
return nil
}
func (c ConfigureDirector) configureVMExtensions(config *directorConfig) error {
if config.VMExtensions != nil {
c.logger.Printf("started configuring vm extensions")
currentExtensions, err := c.getExistingExtensions()
if err != nil {
return err
}
extensionsToDelete, err := c.addNewExtensions(currentExtensions, config.VMExtensions)
if err != nil {
return err
}
err = c.deleteExtensions(extensionsToDelete)
if err != nil {
return err
}
c.logger.Printf("finished configuring vm extensions")
}
return nil
}
func (c ConfigureDirector) configureVMTypes(config *directorConfig) error {
if len(config.VMTypes.VMTypes) == 0 {
if config.VMTypes.CustomTypesOnly {
return errors.New("if custom_types = true, vm_types must not be empty")
}
return nil
}
c.logger.Printf("creating custom vm types")
vmTypesToCreate := make([]api.CreateVMType, 0)
existingVMTypes := make([]api.VMType, 0)
var err error
if !config.VMTypes.CustomTypesOnly {
// delete all custom VM types
if err = c.service.DeleteCustomVMTypes(); err != nil {
return err
}
existingVMTypes, err = c.service.ListVMTypes()
if err != nil {
return err
}
}
for i := range existingVMTypes {
vmTypesToCreate = append(vmTypesToCreate, existingVMTypes[i].CreateVMType)
}
for i := range config.VMTypes.VMTypes {
overwriting := false
for j := range vmTypesToCreate {
if config.VMTypes.VMTypes[i].Name == vmTypesToCreate[j].Name {
vmTypesToCreate[j] = config.VMTypes.VMTypes[i]
overwriting = true
break
}
}
if !overwriting {
vmTypesToCreate = append(vmTypesToCreate, config.VMTypes.VMTypes[i])
}
}
return c.service.CreateCustomVMTypes(api.CreateVMTypes{
VMTypes: vmTypesToCreate,
})
}
func (c ConfigureDirector) getProductGUID() (string, error) {
findOutput, err := c.service.GetStagedProductByName("p-bosh")
if err != nil {
return "", fmt.Errorf("could not find staged product with name 'p-bosh': %s", err)
}
return findOutput.Product.GUID, nil
}
func (c ConfigureDirector) configureResourceConfiguration(config *directorConfig, productGUID string) error {
if config.ResourceConfiguration == nil {
c.logger.Println("resource config property for bosh tile was not provided, nothing to do here")
return nil
}
c.logger.Printf("started configuring resource options for bosh tile")
err := c.service.ConfigureJobResourceConfig(productGUID, config.ResourceConfiguration)
if err != nil {
return fmt.Errorf("failed to configure resources: %s", err)
}
c.logger.Printf("finished configuring resource options for bosh tile")
return nil
}
func checkRunningInstallation(listInstallations func() ([]api.InstallationsServiceOutput, error)) error {
installations, err := listInstallations()
if err != nil {
return fmt.Errorf("could not list installations: %s", err)
}
if len(installations) > 0 {
if installations[0].Status == "running" {
return errors.New("OpsManager does not allow configuration or staging changes while apply changes are running to prevent data loss for configuration and/or staging changes")
}
}
return nil
}