-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
245 lines (216 loc) · 5.93 KB
/
server.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
package configure
import (
"context"
"strings"
"github.com/bmc-toolbox/bmcbutler/pkg/asset"
"github.com/bmc-toolbox/bmcbutler/pkg/config"
"github.com/bmc-toolbox/bmclib/cfgresources"
"github.com/bmc-toolbox/bmclib/devices"
"github.com/sirupsen/logrus"
)
// Bmc struct declares attributes required to apply configuration.
type Bmc struct {
bmc devices.Bmc
asset *asset.Asset
resources []string
configure devices.Configure
config *cfgresources.ResourcesConfig
butlerConfig *config.Params
logger *logrus.Logger
ip string
serial string
vendor string
model string
stopChan <-chan struct{}
}
// NewBmcConfigurator returns a new configure struct to apply configuration.
func NewBmcConfigurator(bmc devices.Bmc,
asset *asset.Asset,
resources []string,
config *cfgresources.ResourcesConfig,
butlerConfig *config.Params,
stopChan <-chan struct{},
logger *logrus.Logger) *Bmc {
return &Bmc{
// asset to be setup
asset: asset,
// client is of type devices.Bmc
bmc: bmc,
// devices.Bmc is type asserted to apply configuration,
// this is possible since devices.Bmc embeds the Configure interface.
configure: bmc.(devices.Configure),
// if --resources was passed, only these resources will be applied
resources: resources,
config: config,
butlerConfig: butlerConfig,
logger: logger,
stopChan: stopChan,
ip: asset.IPAddress,
serial: asset.Serial,
vendor: asset.Vendor,
model: asset.Model,
}
}
// Apply applies configuration.
// nolint: gocyclo
func (b *Bmc) Apply() {
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
// reset causes are appended here
var resetCause []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
var reset bool
// check if an interrupt was received.
if interrupt {
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.LdapGroups != nil && b.config.Ldap != nil {
k, err := b.config.LdapGroups.GetExtraGroups(b.asset.Serial, b.asset.Vendor)
if err != nil {
b.logger.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
"K": k,
"Groups": b.config.LdapGroups.Groups,
}).Warn("Trying to fetch more LDAP groups has failed.")
}
err = b.configure.LdapGroups(b.config.LdapGroups.Groups, b.config.Ldap)
}
case "license":
if b.config.License != nil {
err = b.configure.SetLicense(b.config.License)
}
case "network":
if b.config.Network != nil {
reset, err = b.configure.Network(b.config.Network)
}
case "bios":
if b.config.Bios != nil {
err = b.configure.Bios(b.config.Bios)
}
case "https_cert":
if b.config.HTTPSCert != nil {
reset, err = b.certificateSetup()
}
case "power":
if b.config.Power != nil {
err = b.configure.Power(b.config.Power)
}
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)
}
if reset {
resetCause = append(resetCause, resource)
}
b.logger.WithFields(logrus.Fields{
"resource": resource,
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
}).Trace("Resource configuration applied.")
}
//// Reset BMC if needed.
if len(resetCause) > 0 {
b.logger.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"cause": strings.Join(resetCause, ", "),
}).Info("BMC to be reset.")
// Close the current connection - so we don't leave connections hanging.
b.bmc.Close(context.TODO())
//// reset BMC using SSH.
_, err := b.bmc.PowerCycleBmc()
if err != nil {
b.logger.WithFields(logrus.Fields{
"Vendor": b.vendor,
"Model": b.model,
"Serial": b.serial,
"IPAddress": b.ip,
"Error": err,
}).Warn("BMC reset failed.")
}
}
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.")
}