/
msecli.go
224 lines (176 loc) · 4.92 KB
/
msecli.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
package utils
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"github.com/bmc-toolbox/common"
"github.com/metal-toolbox/ironlib/model"
"github.com/pkg/errors"
)
var (
ErrMseCliDriveNotIdentified = errors.New("failed to identify drive for update")
)
const (
EnvMsecliUtility = "IRONLIB_UTIL_MSECLI"
)
// Msecli is an msecli executor
type Msecli struct {
Executor Executor
}
// MseclieDevice is a Micron disk device object
type MsecliDevice struct {
ModelNumber string // Micron_5200_MTFDDAK480TDN
SerialNumber string
FirmwareRevision string
}
// NewMsecli returns a Msecli object to run msecli commands
func NewMsecli(trace bool) *Msecli {
utility := "msecli"
// lookup env var for util
if eVar := os.Getenv(EnvMsecliUtility); eVar != "" {
utility = eVar
}
e := NewExecutor(utility)
e.SetEnv([]string{"LC_ALL=C.UTF-8"})
if !trace {
e.SetQuiet()
}
return &Msecli{Executor: e}
}
// Attributes implements the actions.UtilAttributeGetter interface
func (m *Msecli) Attributes() (utilName model.CollectorUtility, absolutePath string, err error) {
// Call CheckExecutable first so that the Executable CmdPath is resolved.
er := m.Executor.CheckExecutable()
return "msecli", m.Executor.CmdPath(), er
}
// Drives returns a slice of drive components identified
func (m *Msecli) Drives(_ context.Context) ([]*common.Drive, error) {
devices, err := m.Query()
if err != nil {
return nil, err
}
drives := []*common.Drive{}
for _, d := range devices {
item := &common.Drive{
Common: common.Common{
Model: d.ModelNumber,
Vendor: common.VendorFromString(d.ModelNumber),
Description: d.ModelNumber,
Serial: d.SerialNumber,
Firmware: &common.Firmware{Installed: d.FirmwareRevision},
Metadata: make(map[string]string),
},
Type: model.DriveTypeSlug(d.ModelNumber),
}
drives = append(drives, item)
}
return drives, nil
}
// UpdateDrive installs drive updates
func (m *Msecli) UpdateDrive(ctx context.Context, updateFile, modelNumber, serialNumber string) error {
// query list of drives
drives, err := m.Query()
if err != nil {
return err
}
// msecli expects the update file to be named 1.bin - don't ask
expectedFileName := "1.bin"
// rename update file
if filepath.Base(updateFile) != expectedFileName {
newName := filepath.Join(filepath.Dir(updateFile), expectedFileName)
err := os.Rename(updateFile, newName)
if err != nil {
return err
}
updateFile = newName
}
for _, d := range drives {
// filter by serial number
if serialNumber != "" {
if !strings.EqualFold(d.SerialNumber, serialNumber) {
continue
}
}
// filter by model number
if modelNumber != "" {
if !strings.EqualFold(d.ModelNumber, modelNumber) {
continue
}
}
// get the product name from the model number - msecli expects the product name
modelNForMsecli := model.FormatProductName(d.ModelNumber)
return m.updateDrive(ctx, modelNForMsecli, updateFile)
}
return ErrMseCliDriveNotIdentified
}
// updateDrive installs the given updatefile
func (m *Msecli) updateDrive(ctx context.Context, modelNumber, updateFile string) error {
// echo 'y'
m.Executor.SetStdin(bytes.NewReader([]byte("y\n")))
m.Executor.SetArgs([]string{
"-U", // update
"-m", // model
modelNumber,
"-i", // directory containing the update file
filepath.Dir(updateFile),
},
)
result, err := m.Executor.ExecWithContext(ctx)
if err != nil {
return newExecError(m.Executor.GetCmd(), result)
}
if result.ExitCode != 0 {
return newExecError(m.Executor.GetCmd(), result)
}
return nil
}
// Query parses the output of mseli -L and returns a slice of *MsecliDevice's
func (m *Msecli) Query() ([]*MsecliDevice, error) {
m.Executor.SetArgs([]string{"-L"})
result, err := m.Executor.ExecWithContext(context.Background())
if err != nil {
return nil, err
}
if len(result.Stdout) == 0 {
return nil, errors.Wrap(ErrNoCommandOutput, m.Executor.GetCmd())
}
return m.parseMsecliQueryOutput(result.Stdout), nil
}
// Parse msecli -L output into []*MsecliDevice
func (m *Msecli) parseMsecliQueryOutput(b []byte) []*MsecliDevice {
devices := []*MsecliDevice{}
// split
byteSlice := bytes.Split(b, []byte("\n\n"))
for _, sl := range byteSlice {
if !bytes.Contains(sl, []byte("Device Name")) {
continue
}
devices = append(devices, parseMsecliDeviceAttributes(sl))
}
return devices
}
// parse a Device information section into *MsecliDevice
func parseMsecliDeviceAttributes(bSlice []byte) *MsecliDevice {
device := &MsecliDevice{}
lines := bytes.Split(bSlice, []byte("\n"))
for _, line := range lines {
s := string(line)
cols := 2
parts := strings.Split(s, ":")
if len(parts) < cols {
continue
}
key, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
switch key {
case "Model No":
device.ModelNumber = value
case "FW-Rev":
device.FirmwareRevision = value
case "Serial No":
device.SerialNumber = value
}
}
return device
}