This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
dell.go
233 lines (225 loc) · 6.15 KB
/
dell.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
package main
import (
"bufio"
"encoding/xml"
"io"
"log"
"math/big"
"os"
"os/exec"
"strings"
)
type StrValid struct {
Val string `xml:",chardata"`
IsNull string `xml:"isnull,attr"`
}
// This struct has to encode the pertinent information from several
// different blobs of XML. Because Reasons, that is why.
type dellBiosOnlyConfigEnt struct {
// We will need the name of the element decoded to figure out how to handle it.
// These appear to be:
//
// HIIFormObj, which we will ignore.
//
// HIIEnumObj, which specifies a BIOS setting that can take on
// a small set of values.
//
// HIIEnumValueObj, which specifies a value that the preceeding
// HIIEnumObj can take.
//
// HIIStringObj, which specifies an arbitrary string a user can set
//
// HIIOrderedListObj, which specifies a BIOS setting that can have multiple values
//
// HIIOrderedListEntryObj, which specifies an entry in an OrderedListObj
XMLName xml.Name
// Valid for HIIEnumObj, HIIStringObj, and HIIIntegerObj
ReadOnly bool `xml:"hdr>bReadOnly"`
Name string `xml:"hdr>Name"`
// Valid for HIIEnumObj
PendingValid bool `xml:"bPendingValid"`
DefaultValid bool `xml:"bDefaultValid"`
CurrentState int `xml:"currentState"`
PendingState int `xml:"pendingState"`
DefaultState int `xml:"defaultState"`
PossibleStates int `xml:"numPossibleStates"`
// Valid for HIIIntegerObj
MinVal *big.Int `xml:"minValue"`
MaxVal *big.Int `xml:"maxVal"`
CurrentValue *big.Int `xml:"currentValue"`
PendingValue *big.Int `xml:"pendingValue"`
DefaultValue *big.Int `xml:"defaultValue"`
// Valid for HIIStringObj
MinLength *big.Int `xml:"minLength"`
MaxLength *big.Int `xml:"maxLength"`
CurrentStr StrValid `xml:"Current"`
PendingStr StrValid `xml:"Pending"`
DefaultStr StrValid `xml:"Default"`
// valid for HIIEnumValueObj
ValIdx int `xml:"stateNumber"`
ValName string `xml:"Name"`
// valid for HIIOrderedListObj
ListLen int `xml:"numOrdListEntries"`
// valid for HIIOrderedListEntryObj
ListIdx int `xml:"currentIndex"`
}
type dellBiosOnlyConfig struct {
source io.Reader
XMLName xml.Name `xml:"OMA"`
Ents []dellBiosOnlyConfigEnt `xml:",any"`
}
func (d *dellBiosOnlyConfig) Source(src io.Reader) {
d.source = src
}
// Need the following packages installed:
//
// srvadmin-omacore srvadmin-idrac srvadmin-storage-cli
//
// openmanage services need to start:
//
// /opt/dell/srvadmin/sbin/srvadmin-services.sh start
func (d *dellBiosOnlyConfig) Current() (res map[string]Entry, err error) {
res = map[string]Entry{}
if d.source == nil {
cmd := exec.Command("/opt/dell/srvadmin/bin/omreport", "chassis", "biossetup", "-fmt", "xml")
var w io.ReadCloser
w, err = cmd.StdoutPipe()
if err != nil {
return
}
buf := bufio.NewReader(w)
if err = cmd.Start(); err != nil {
return
}
defer cmd.Wait()
defer w.Close()
_, err = buf.ReadSlice('<')
if err != nil {
return
}
d.source = buf
}
// We should be at the start of the XML now.
dec := xml.NewDecoder(d.source)
cfg := &dellBiosOnlyConfig{}
if err = dec.Decode(cfg); err != nil {
return
}
for i := 0; i < len(cfg.Ents); i++ {
ent := &cfg.Ents[i]
var working Entry
switch ent.XMLName.Local {
case "HIIIntegerObj":
working = Entry{
Name: ent.Name,
Type: "Number",
Current: ent.CurrentValue.String(),
}
if ent.PendingValid {
working.PendingValid = ent.PendingValid
working.Pending = ent.PendingValue.String()
}
if ent.DefaultValid {
working.Default = ent.DefaultValue.String()
}
working.Checker.Int.Valid = true
working.Checker.Int.Max = ent.MaxVal
working.Checker.Int.Min = ent.MinVal
case "HIIStringObj":
working = Entry{
Name: ent.Name,
Type: "String",
Current: ent.CurrentStr.Val,
Pending: ent.PendingStr.Val,
Default: ent.DefaultStr.Val,
}
working.PendingValid = ent.PendingStr.IsNull != "true"
working.Checker.String.Valid = true
working.Checker.String.MaxLen = ent.MaxLength
working.Checker.String.MinLen = ent.MinLength
case "HIIEnumObj":
working = Entry{
Name: ent.Name,
Type: "Option",
}
working.Checker.Enum.Valid = true
j := i + 1
for j < len(cfg.Ents) && cfg.Ents[j].XMLName.Local == "HIIEnumValueObj" {
ev := &cfg.Ents[j]
working.Checker.Enum.Values = append(working.Checker.Enum.Values, ev.ValName)
if ent.CurrentState == ev.ValIdx {
working.Current = ev.ValName
}
if ent.PendingState == ev.ValIdx && ent.PendingValid {
working.PendingValid = ent.PendingValid
working.Pending = ev.ValName
}
if ent.DefaultState == ev.ValIdx && ent.DefaultValid {
working.Default = ev.ValName
}
j++
}
i = j - 1
case "HIIEnumValueObj":
log.Panicf("Should not happen")
return
case "HIIOrderedListObj":
working = Entry{
Type: "Seq",
Name: ent.Name,
}
working.Checker.Seq.Valid = true
vals := make([]string, ent.ListLen)
j := i + 1
for j < len(cfg.Ents) && cfg.Ents[j].XMLName.Local == "HIIOrderedListEntryObj" {
ev := &cfg.Ents[j]
vals[ev.ListIdx] = ev.Name
j++
}
working.Current = strings.Join(vals, ",")
i = j - 1
case "HIIOrderedListEntryObj":
log.Panicf("Should not happen")
return
case "OMAUserRights", "HIIFormObj", "HIIFormReferenceObj", "ObjCount", "SMStatus":
continue
default:
log.Fatalf("Unknown object type %s from omreport", ent.XMLName.Local)
continue
}
working.ReadOnly = ent.ReadOnly
res[working.Name] = working
}
return
}
func (d *dellBiosOnlyConfig) FixWanted(wanted map[string]string) map[string]string {
return wanted
}
func (c *dellBiosOnlyConfig) Apply(current map[string]Entry, trimmed map[string]string, dryRun bool) (needReboot bool, err error) {
for {
for k, v := range trimmed {
args := []string{
"chassis",
"biossetup",
"attribute=" + k,
}
if current[k].Checker.Seq.Valid {
args = append(args, "sequence="+v)
} else {
args = append(args, "setting="+v)
}
if dryRun {
return
}
cmd := exec.Command("/opt/dell/srvadmin/bin/omconfig", args...)
var out []byte
out, err = cmd.CombinedOutput()
os.Stderr.Write(out)
if err == nil {
needReboot = true
continue
}
return
}
}
}