Skip to content

Commit

Permalink
Vc/instrument firmware (#123)
Browse files Browse the repository at this point in the history
* add some logging

* default to using trace level logging

* wsl has stupid opinions on spacing
  • Loading branch information
DoctorVin authored Jan 18, 2024
1 parent 60a43e8 commit dd1e5bd
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ linters-settings:
- wrapperFunc
gofumpt:
extra-rules: true
wsl:
auto-fix: true

linters:
enable:
Expand All @@ -72,7 +70,6 @@ linters:
- noctx
- stylecheck
- whitespace
- wsl
enable-all: false
disable-all: true

Expand Down
38 changes: 36 additions & 2 deletions actions/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bmc-toolbox/common"
"github.com/pkg/errors"
"github.com/r3labs/diff/v2"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"

"github.com/metal-toolbox/ironlib/firmware"
Expand All @@ -27,6 +28,9 @@ type InventoryCollectorAction struct {
// collectors registered for inventory collection.
collectors Collectors

// something to track our execution
log *logrus.Logger

// device is the model in which the collected inventory is recorded.
device *common.Device

Expand Down Expand Up @@ -127,8 +131,10 @@ func WithDisabledCollectorUtilities(utilityNames []model.CollectorUtility) Optio
}

// NewActionrunner returns an Actions runner that is capable of collecting inventory.
func NewInventoryCollectorAction(options ...Option) *InventoryCollectorAction {
a := &InventoryCollectorAction{}
func NewInventoryCollectorAction(ll *logrus.Logger, options ...Option) *InventoryCollectorAction {
a := &InventoryCollectorAction{
log: ll,
}

// set options to override
for _, opt := range options {
Expand Down Expand Up @@ -198,55 +204,73 @@ func (a *InventoryCollectorAction) Collect(ctx context.Context, device *common.D
// one drive/nic/storagecontroller/psu component returns an error.

// Collect initial device inventory
a.log.Debug("collect initial inventory")
err := a.collectors.InventoryCollector.Collect(ctx, a.device)
a.log.WithError(err).Debug("collect initial done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving device inventory")
}

// Collect drive smart data
a.log.Debug("collect drives")
err = a.CollectDrives(ctx)
a.log.WithError(err).Debug("collect drives done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving drive inventory")
}

// Collect NIC info
a.log.Debug("collect nics")
err = a.CollectNICs(ctx)
a.log.WithError(err).Debug("collect nics done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving NIC inventory")
}

// Collect BIOS info
a.log.Debug("collect bios")
err = a.CollectBIOS(ctx)
a.log.WithError(err).Debug("collect bios done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving BIOS inventory")
}

// Collect CPLD info
a.log.Debug("collect cpld")
err = a.CollectCPLDs(ctx)
a.log.WithError(err).Debug("collect cpld done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving CPLD inventory")
}

// Collect BMC info
a.log.Debug("collect bmc")
err = a.CollectBMC(ctx)
a.log.WithError(err).Debug("collect bmc done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving BMC inventory")
}

// Collect TPM info
a.log.Debug("collect tpm")
err = a.CollectTPMs(ctx)
a.log.WithError(err).Debug("collect tpm done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving TPM inventory")
}

// Collect Firmware checksums
a.log.Debug("collect firmware checksum")
err = a.CollectFirmwareChecksums(ctx)
a.log.WithError(err).Debug("collect firmware checksum done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving Firmware checksums")
}

// Collect UEFI variables
a.log.Debug("collect uefi variables")
err = a.CollectUEFIVariables(ctx)
a.log.WithError(err).Debug("collect uefi variables done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving UEFI variables")
}
Expand All @@ -261,7 +285,9 @@ func (a *InventoryCollectorAction) Collect(ctx context.Context, device *common.D
}

// Collect StorageController info
a.log.Debug("collect storage controller")
err = a.CollectStorageControllers(ctx)
a.log.WithError(err).Debug("collect storage controller done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving StorageController inventory")
}
Expand All @@ -275,7 +301,9 @@ func (a *InventoryCollectorAction) Collect(ctx context.Context, device *common.D
}

if len(a.collectors.DriveCollectors) > 0 {
a.log.Debug("dynamic collect drive")
err = a.CollectDrives(ctx)
a.log.WithError(err).Debug("dynamic collect drive done")

if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving drive inventory")
Expand All @@ -284,10 +312,12 @@ func (a *InventoryCollectorAction) Collect(ctx context.Context, device *common.D
}

// CollectDriveCapabilities is to be invoked after Drives()
a.log.Debug("collect drive capabilities")
err = a.CollectDriveCapabilities(ctx)
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving DriveCapabilities")
}
a.log.WithError(err).Debug("collect drive capabilities done")

a.setDefaultAttributes()

Expand Down Expand Up @@ -687,6 +717,7 @@ func (a *InventoryCollectorAction) CollectFirmwareChecksums(ctx context.Context)
}()

if a.collectors.FirmwareChecksumCollector == nil {
a.log.Info("nil firmware checksum collector")
return nil
}

Expand All @@ -695,16 +726,19 @@ func (a *InventoryCollectorAction) CollectFirmwareChecksums(ctx context.Context)
if slices.Contains(a.disabledCollectorUtilities, collectorKind) ||
slices.Contains(a.disabledCollectorUtilities, firmware.FirmwareDumpUtility) ||
slices.Contains(a.disabledCollectorUtilities, firmware.UEFIParserUtility) {
a.log.Info("firmware checksum disabled")
return nil
}

sumStr, err := a.collectors.FirmwareChecksumCollector.BIOSLogoChecksum(ctx)
if err != nil {
a.log.WithError(err).Warn("error collecting BIOS Logo checksum")
return err
}

if a.device.BIOS == nil {
// XXX: how did we get here?
a.log.Error("nil device bios data")
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions actions/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
smcFixtures "github.com/metal-toolbox/ironlib/fixtures/supermicro"
"github.com/metal-toolbox/ironlib/model"
"github.com/metal-toolbox/ironlib/utils"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func Test_Inventory_dell(t *testing.T) {
WithDisabledCollectorUtilities([]model.CollectorUtility{"dmidecode"}),
}

collector := NewInventoryCollectorAction(options...)
collector := NewInventoryCollectorAction(logrus.New(), options...)
if err := collector.Collect(context.TODO(), &device); err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -123,7 +124,7 @@ func Test_Inventory_smc(t *testing.T) {
StorageControllerCollectors: []StorageControllerCollector{storecli},
}

collector := NewInventoryCollectorAction(WithCollectors(collectors), WithTraceLevel())
collector := NewInventoryCollectorAction(logrus.New(), WithCollectors(collectors), WithTraceLevel())
if err := collector.Collect(context.TODO(), &device); err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func TestNewInventoryCollectorAction(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewInventoryCollectorAction(tt.options...)
got := NewInventoryCollectorAction(logrus.New(), tt.options...)

switch tt.name {
case "trace-enabled":
Expand Down
2 changes: 2 additions & 0 deletions examples/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

func main() {
logger := logrus.New()
logger.Formatter = new(logrus.JSONFormatter)
logger.SetLevel(logrus.TraceLevel)
device, err := ironlib.New(logger)
if err != nil {
logger.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion providers/asrockrack/asrockrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (a *asrockrack) GetInventory(ctx context.Context, options ...actions.Option
deviceObj := common.NewDevice()
a.hw.Device = &deviceObj

collector := actions.NewInventoryCollectorAction(options...)
collector := actions.NewInventoryCollectorAction(a.logger, options...)
if err := collector.Collect(ctx, a.hw.Device); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion providers/dell/dell.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (d *dell) GetInventory(ctx context.Context, options ...actions.Option) (*co
// Collect device inventory
d.logger.Info("Collecting hardware inventory")

collector := actions.NewInventoryCollectorAction(options...)
collector := actions.NewInventoryCollectorAction(d.logger, options...)
if err := collector.Collect(ctx, d.hw.Device); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion providers/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (a *Generic) GetInventory(ctx context.Context, options ...actions.Option) (
// Collect device inventory
a.logger.Info("Collecting inventory")

collector := actions.NewInventoryCollectorAction(options...)
collector := actions.NewInventoryCollectorAction(a.logger, options...)
if err := collector.Collect(ctx, a.hw.Device); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion providers/supermicro/supermicro.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *supermicro) GetInventory(ctx context.Context, options ...actions.Option

options = append(options, actions.WithCollectors(collectors))

collector := actions.NewInventoryCollectorAction(options...)
collector := actions.NewInventoryCollectorAction(s.logger, options...)
if err := collector.Collect(ctx, s.hw.Device); err != nil {
return nil, err
}
Expand Down

0 comments on commit dd1e5bd

Please sign in to comment.