-
Notifications
You must be signed in to change notification settings - Fork 11
/
setup_chassis.go
714 lines (610 loc) · 17.9 KB
/
setup_chassis.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package configure
import (
"errors"
"fmt"
"strings"
"time"
"github.com/bmc-toolbox/bmcbutler/pkg/asset"
"github.com/bmc-toolbox/bmcbutler/pkg/config"
"github.com/bmc-toolbox/bmcbutler/pkg/inventory"
"github.com/bmc-toolbox/bmcbutler/pkg/metrics"
"github.com/bmc-toolbox/bmclib/cfgresources"
"github.com/bmc-toolbox/bmclib/devices"
"github.com/sirupsen/logrus"
)
// BmcChassisSetup struct holds various attributes for chassis setup methods.
type BmcChassisSetup struct {
asset *asset.Asset
chassis devices.BmcChassis
setup devices.BmcChassisSetup
config *cfgresources.SetupChassis
resources []string
butlerConfig *config.Params
metricsEmitter *metrics.Emitter
log *logrus.Logger
ip string
serial string
vendor string
model string
stopChan <-chan struct{}
}
// NewBmcChassisSetup returns a new struct to apply configuration.
func NewBmcChassisSetup(
chassis devices.BmcChassis,
asset *asset.Asset,
resources []string,
config *cfgresources.SetupChassis,
butlerConfig *config.Params,
metricsEmitter *metrics.Emitter,
stopChan <-chan struct{},
logger *logrus.Logger) *BmcChassisSetup {
return &BmcChassisSetup{
// asset to be setup
asset: asset,
// client is of type devices.Bmc
chassis: chassis,
// devices.Bmc is type asserted to apply one time setup configuration,
// this is possible since devices.Bmc embeds the BmcChassisSetup interface.
setup: chassis.(devices.BmcChassisSetup),
butlerConfig: butlerConfig,
// if --resources was passed, only these resources will be applied
resources: resources,
metricsEmitter: metricsEmitter,
config: config,
log: logger,
stopChan: stopChan,
}
}
// Apply applies one time setup configuration.
func (b *BmcChassisSetup) Apply() { //nolint: gocyclo
defer b.metricsEmitter.MeasureRuntime(
[]string{"butler", "setupChassis_runtime"},
time.Now(),
)
var interrupt bool
go func() { <-b.stopChan; interrupt = true }()
// slice of configuration resources to be applied.
var resources []string
// if any setup action fails, this is set to false
// if this finally is true, the post actions are invoked.
setupActionSuccess := true
// retrieve valid or known setup configuration resources for the chassis.
if len(b.resources) > 0 {
resources = b.resources
} else {
resources = b.setup.ResourcesSetup()
}
b.vendor = b.chassis.Vendor()
b.model, _ = b.chassis.Model()
b.serial, _ = b.chassis.Serial()
b.ip = b.asset.IPAddress
var failed, success []string
b.log.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"To apply": strings.Join(resources, ", "),
}).Trace("Configuration resources to be applied.")
for _, resource := range resources {
var err error
// check if an interrupt was received.
if interrupt == true {
b.log.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Debug("Received interrupt.")
break
}
err = b.ensurePoweredUp()
if err != nil {
b.log.WithFields(logrus.Fields{
"resource": resource,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn("Chassis power status")
return
}
b.log.WithFields(logrus.Fields{
"resource": resource,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Debug("Chassis is powered on, continuing setup.")
switch resource {
case "setipmioverlan":
if b.config.IpmiOverLan != nil {
err = b.setIpmiOverLan()
}
case "flexaddress":
if b.config.FlexAddress != nil {
err = b.setFlexAddressState()
}
case "dynamicpower":
if b.config.DynamicPower != nil {
err = b.setDynamicPower()
}
case "bladespower":
if b.config.BladesPower != nil {
err = b.setBladesPower()
}
case "add_blade_bmc_admins":
if len(b.config.AddBladeBmcAdmins) > 0 {
err = b.addBladeBmcAdmins()
}
case "remove_blade_bmc_users":
if len(b.config.RemoveBladeBmcUsers) > 0 {
err = b.removeBladeBmcUsers()
}
default:
b.log.WithFields(logrus.Fields{
"resource": resource,
}).Warn("Unknown setup resource.")
}
if err != nil {
setupActionSuccess = false
failed = append(failed, resource)
b.log.WithFields(logrus.Fields{
"resource": resource,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn("Setup resource returned errors.")
} else {
success = append(success, resource)
}
b.log.WithFields(logrus.Fields{
"resource": resource,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Trace("Resource configuration applied.")
}
//if chassis setup is done successfully invoke post action.
if setupActionSuccess {
b.Post()
}
b.log.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"applied": strings.Join(success, ", "),
"unsuccessful": strings.Join(failed, ", "),
}).Info("Chassis setup actions done.")
}
// Post method is when a chassis was setup successfully.
func (b *BmcChassisSetup) Post() {
enc := inventory.Enc{
Config: b.butlerConfig,
Log: b.log,
MetricsEmitter: b.metricsEmitter,
}
enc.SetChassisInstalled(b.asset.Serial)
return
}
// ensurePoweredUp method checks if a chassis is powered off
// and powers it back on.
func (b *BmcChassisSetup) ensurePoweredUp() (err error) {
status, _ := b.chassis.IsOn()
if status == false {
_, err := b.chassis.PowerOn()
if err != nil {
return err
}
return errors.New("Chassis power status was off, powering on.. retry in a few minutes")
}
return nil
}
func (b *BmcChassisSetup) addBladeBmcAdmins() (err error) {
component := "addBladeBmcAdmins"
cfg := b.config.AddBladeBmcAdmins
for _, user := range cfg {
if user.Name == "" {
return fmt.Errorf("AddbladeBmcAdmins resource expects parameter: Name")
}
if user.Password == "" {
return fmt.Errorf("AddbladeBmcAdmins resource expects parameter: Password")
}
err = b.setup.AddBladeBmcAdmin(user.Name, user.Password)
if err != nil {
return err
}
// in cases where the user may already exist, we modify the credentials
err = b.setup.ModBladeBmcUser(user.Name, user.Password)
if err != nil {
return err
}
b.log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"User": user.Name,
}).Debug("Blade BMC admin account added.")
}
return err
}
func (b *BmcChassisSetup) removeBladeBmcUsers() (err error) {
component := "removeBladeBmcUsers"
cfg := b.config.RemoveBladeBmcUsers
for _, user := range cfg {
if user.Name == "" {
return fmt.Errorf("RemoveBladeBmcUsers resource expects parameter: Name")
}
err = b.setup.RemoveBladeBmcUser(user.Name)
if err != nil {
return err
}
b.log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"User": user.Name,
}).Debug("Blade BMC user account removed.")
}
return err
}
func (b *BmcChassisSetup) setDynamicPower() (err error) {
log := b.log
component := "setDynamicPower"
_, err = b.setup.SetDynamicPower(b.config.DynamicPower.Enable)
if err != nil {
msg := "Unable to update Dynamic Power status."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Debug("Dynamic Power config applied successfully.")
return err
}
func (b *BmcChassisSetup) setIpmiOverLan() (err error) {
log := b.log
component := "setIpmiOverLan"
enable := b.config.IpmiOverLan.Enable
chassis := b.chassis
setup := b.setup
//retrive list of blades in chassis
blades, err := chassis.Blades()
if err != nil {
msg := "Unable to list blades for chassis."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Error(msg)
return errors.New(msg)
}
for _, blade := range blades {
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Enable": enable,
}).Debug("Updating IpmiOverLan config.")
//blade needs to be powered on to set this parameter
isPoweredOn, _ := chassis.IsOnBlade(blade.BladePosition)
if isPoweredOn == false {
_, err = chassis.PowerOnBlade(blade.BladePosition)
if err != nil {
msg := "Unable to power up blade to enable IpmiOverLan."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
//give it a few seconds to power on
time.Sleep(20 * time.Second)
}
_, err = setup.SetIpmiOverLan(blade.BladePosition, enable)
if err != nil {
msg := "Unable to update IpmiOverLan status."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
}
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Debug("IpmiOverLan config applied successfully.")
return err
}
// Enables/ Disables FlexAddress status for each blade in a chassis.
// Each blade is powered down, flex state updated, powered up
func (b *BmcChassisSetup) setFlexAddressState() (err error) { // nolint: gocyclo
component := "setFlexAddressState"
chassis := b.chassis
setup := b.setup
log := b.log
enable := b.config.FlexAddress.Enable
//retrive list of blades in chassis
blades, err := chassis.Blades()
if err != nil {
msg := "Unable to list blades for chassis."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Error(msg)
return errors.New(msg)
}
for _, blade := range blades {
//Flex addresses are enabled, disable them.
if blade.FlexAddressEnabled == true && enable == false {
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Current state": blade.FlexAddressEnabled,
"Expected state": enable,
}).Debug("Disabling FlexAddress on blade.")
isPoweredOn, _ := chassis.IsOnBlade(blade.BladePosition)
if isPoweredOn {
_, err = chassis.PowerOffBlade(blade.BladePosition)
if err != nil {
msg := "Unable to disable FlexAddress - blade power off failed."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
//generally 10 seconds is enough for the blade to power off
time.Sleep(10 * time.Second)
}
_, err = setup.SetFlexAddressState(blade.BladePosition, false)
if err != nil {
msg := "Unable to disable FlexAddress - action failed."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
//give it a few seconds to change the flex state
time.Sleep(10 * time.Second)
chassis.PowerOnBlade(blade.BladePosition)
if err != nil {
msg := "Unable to disable FlexAddress - blade power on failed."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
}
//flex addresses are disabled, enable them
if blade.FlexAddressEnabled == false && enable == true {
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Current state": blade.FlexAddressEnabled,
"Expected state": enable,
}).Info("Enabling FlexAddress on blade.")
isPoweredOn, _ := chassis.IsOnBlade(blade.BladePosition)
if isPoweredOn {
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
}).Info("Powering off blade, this takes a few seconds..")
_, err = chassis.PowerOffBlade(blade.BladePosition)
if err != nil {
msg := "Unable to enable FlexAddress - blade power off failed."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
//generally 10 seconds is enough for the blade to power off
time.Sleep(10 * time.Second)
}
_, err = setup.SetFlexAddressState(blade.BladePosition, true)
if err != nil {
msg := "Unable to enable FlexAddress - action failed."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Error": err,
}).Error(msg)
return errors.New(msg)
}
//give it a few seconds to change the flex state
time.Sleep(10 * time.Second)
_, err = chassis.PowerOnBlade(blade.BladePosition)
if err != nil {
msg := "Unable to enable FlexAddress - blade power on failed."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
}
}
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Debug("FlexAddress config applied successfully.")
return err
}
// Powers up/down blades as defined in config.
func (b *BmcChassisSetup) setBladesPower() (err error) {
log := b.log
component := "setBladesPower"
chassis := b.chassis
powerEnable := b.config.BladesPower.Enable
blades, err := chassis.Blades()
if err != nil {
msg := "Unable to list blades for chassis."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Error(msg)
return errors.New(msg)
}
for _, blade := range blades {
bladeIsPoweredOn, _ := chassis.IsOnBlade(blade.BladePosition)
if bladeIsPoweredOn != powerEnable {
if powerEnable == true {
_, err = chassis.PowerOnBlade(blade.BladePosition)
if err != nil {
msg := "Unable power up blade."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
}).Debug("Set blade power state on.")
}
if powerEnable == false {
chassis.PowerOffBlade(blade.BladePosition)
if err != nil {
msg := "Unable power down blade."
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
"Error": err,
}).Warn(msg)
return errors.New(msg)
}
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Blade Serial": blade.Serial,
"Blade Position": blade.BladePosition,
}).Info("Set blade power state off.")
}
}
}
log.WithFields(logrus.Fields{
"component": component,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Debug("BladesPower config applied successfully.")
return err
}