-
Notifications
You must be signed in to change notification settings - Fork 3
/
device.go
112 lines (95 loc) · 3.19 KB
/
device.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
package ironlib
import (
"fmt"
common "github.com/metal-toolbox/bmc-common"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/metal-toolbox/ironlib/actions"
"github.com/metal-toolbox/ironlib/errs"
"github.com/metal-toolbox/ironlib/providers/asrockrack"
"github.com/metal-toolbox/ironlib/providers/dell"
"github.com/metal-toolbox/ironlib/providers/generic"
"github.com/metal-toolbox/ironlib/providers/supermicro"
"github.com/metal-toolbox/ironlib/utils"
)
// New returns a device Manager interface based on the hardware deviceVendor, model attributes
// by default returns a Generic device instance that only returns the device inventory
func New(logger *logrus.Logger) (m actions.DeviceManager, err error) {
dmidecode, err := utils.NewDmidecode()
if err != nil {
return nil, errors.Wrap(errs.ErrDmiDecodeRun, err.Error())
}
deviceVendor, err := dmidecode.Manufacturer()
if err != nil {
return nil, errors.Wrap(errs.NewDmidecodeValueError("system manufacturer", "", 0), err.Error())
}
if deviceVendor == "" || deviceVendor == common.SystemManufacturerUndefined {
deviceVendor, err = dmidecode.BaseBoardManufacturer()
if err != nil {
return nil, errors.Wrap(errs.NewDmidecodeValueError("baseboard manufacturer", "", 0), err.Error())
}
}
deviceVendor = common.FormatVendorName(deviceVendor)
switch deviceVendor {
case common.VendorDell:
return dell.New(dmidecode, logger)
case common.VendorSupermicro:
return supermicro.New(dmidecode, logger)
case common.VendorPacket, common.VendorAsrockrack:
return asrockrack.New(dmidecode, logger)
default:
return generic.New(dmidecode, logger)
}
}
// CheckDependencies checks and lists available utilities.
func CheckDependencies() {
utilities := []actions.UtilAttributeGetter{
utils.NewAsrrBioscontrol(false),
utils.NewDellRacadm(false),
utils.NewDnf(false),
utils.NewDsu(false),
utils.NewHdparmCmd(false),
utils.NewLshwCmd(false),
utils.NewLsblkCmd(false),
utils.NewMlxupCmd(false),
utils.NewMsecli(false),
utils.NewMvcliCmd(false),
utils.NewNvmeCmd(false),
utils.NewSmartctlCmd(false),
utils.NewIpmicfgCmd(false),
utils.NewSupermicroSUM(false),
utils.NewStoreCLICmd(false),
utils.NewFlashromCmd(false),
utils.NewUefiFirmwareParserCmd(false),
}
red := "\033[31m"
green := "\033[32m"
reset := "\033[0m"
dmi, err := utils.NewDmidecode()
dmiName, dmiPath, dmiErr := dmi.Attributes()
if err != nil || dmiErr != nil {
fmt.Printf("util: %s, path: %s %s[err]%s - %s\n", dmiName, dmiPath, red, reset, dmiErr.Error())
} else {
fmt.Printf("util: %s, path: %s %s[ok]%s\n", dmiName, dmiPath, green, reset)
}
for _, utility := range utilities {
name, uPath, err := utility.Attributes()
if err != nil {
fmt.Printf("util: %s, path: %s %s[err]%s - %s\n", name, uPath, red, reset, err.Error())
continue
}
fmt.Printf("util: %s, path: %s %s[ok]%s\n", name, uPath, green, reset)
}
}
// Logformat adds default fields to each log entry.
type LogFormat struct {
Fields logrus.Fields
Formatter logrus.Formatter
}
// Format satisfies the logrus.Formatter interface.
func (f *LogFormat) Format(e *logrus.Entry) ([]byte, error) {
for k, v := range f.Fields {
e.Data[k] = v
}
return f.Formatter.Format(e)
}