-
Notifications
You must be signed in to change notification settings - Fork 10
/
chassis.go
178 lines (157 loc) · 4.26 KB
/
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
package configure
import (
"strings"
"github.com/bmc-toolbox/bmcbutler/pkg/asset"
"github.com/bmc-toolbox/bmclib/cfgresources"
"github.com/bmc-toolbox/bmclib/devices"
"github.com/sirupsen/logrus"
)
// Cmc struct declares attributes required to apply configuration.
type Cmc struct {
asset *asset.Asset
bmc devices.Cmc
resources []string
configure devices.Configure
config *cfgresources.ResourcesConfig
logger *logrus.Logger
ip string
serial string
vendor string
model string
stopChan <-chan struct{}
}
// NewCmcConfigurator returns a new configure struct to apply configuration.
func NewCmcConfigurator(bmc devices.Cmc,
asset *asset.Asset,
resources []string,
config *cfgresources.ResourcesConfig,
stopChan <-chan struct{},
logger *logrus.Logger) *Cmc {
return &Cmc{
// asset to be setup
asset: asset,
// client is of type devices.Bmc
bmc: bmc,
// if --resources was passed, only these resources will be applied
resources: resources,
// devices.Cmc is type asserted to apply configuration,
// this is possible since devices.Bmc embeds the Configure interface.
configure: bmc.(devices.Configure),
config: config,
logger: logger,
stopChan: stopChan,
ip: asset.IPAddress,
serial: asset.Serial,
vendor: asset.Vendor,
model: asset.Model,
}
}
// Apply applies configuration.
func (b *Cmc) Apply() { //nolint: gocyclo
var interrupt bool
go func() { <-b.stopChan; interrupt = true }()
// slice of configuration resources to be applied.
var resources []string
// retrieve valid or known configuration resources for the bmc.
if len(b.resources) > 0 {
resources = b.resources
} else {
resources = b.configure.Resources()
}
b.ip = b.asset.IPAddress
var failed, success []string
b.logger.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.logger.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Debug("Received interrupt.")
break
}
switch resource {
case "user":
if b.config.User != nil {
err = b.configure.User(b.config.User)
}
case "syslog":
if b.config.Syslog != nil {
err = b.configure.Syslog(b.config.Syslog)
}
case "ntp":
if b.config.Ntp != nil {
err = b.configure.Ntp(b.config.Ntp)
}
case "ldap":
if b.config.Ldap != nil {
err = b.configure.Ldap(b.config.Ldap)
}
case "ldap_group":
if b.config.LdapGroup != nil && b.config.Ldap != nil {
err = b.configure.LdapGroup(b.config.LdapGroup, b.config.Ldap)
}
case "license":
if b.config.License != nil {
err = b.configure.SetLicense(b.config.License)
}
case "network":
if b.config.Network != nil {
_, err = b.configure.Network(b.config.Network)
}
default:
b.logger.WithFields(logrus.Fields{
"resource": resource,
}).Warn("Unknown resource.")
}
if err != nil {
failed = append(failed, resource)
b.logger.WithFields(logrus.Fields{
"resource": resource,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn("Resource configuration returned errors.")
} else {
success = append(success, resource)
}
b.logger.WithFields(logrus.Fields{
"resource": resource,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Trace("Resource configuration applied.")
}
if len(failed) > 0 {
b.logger.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"success": false,
"applied": strings.Join(success, ", "),
"failed": strings.Join(failed, ", "),
}).Warn("One or more resources failed to apply.")
return
}
b.logger.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"success": true,
"applied": strings.Join(success, ", "),
}).Info("BMC configuration actions successful.")
}