Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace logrus for logr+logrus #138

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion actions/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/bmc-toolbox/common"
"github.com/go-logr/logr"
"github.com/metal-toolbox/ironlib/model"
"github.com/metal-toolbox/ironlib/utils"
)
Expand Down Expand Up @@ -202,5 +203,5 @@ type VirtualDiskManager interface {

// DiskWiper defines an interface to override disk data
type DiskWiper interface {
WipeDisk(ctx context.Context, logicalName string) error
WipeDisk(ctx context.Context, log logr.Logger, logicalName string) error
}
61 changes: 31 additions & 30 deletions actions/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"strings"

"github.com/bmc-toolbox/common"
"github.com/go-logr/logr"
"github.com/metal-toolbox/ironlib/firmware"
"github.com/metal-toolbox/ironlib/model"
"github.com/metal-toolbox/ironlib/utils"
"github.com/pkg/errors"
"github.com/r3labs/diff/v2"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)

Expand All @@ -28,7 +28,7 @@ type InventoryCollectorAction struct {
collectors Collectors

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

// device is the model in which the collected inventory is recorded.
device *common.Device
Expand Down Expand Up @@ -130,9 +130,10 @@ func WithDisabledCollectorUtilities(utilityNames []model.CollectorUtility) Optio
}

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

// set options to override
Expand Down Expand Up @@ -203,73 +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")
a.log.V(1).Info("collect initial inventory")
err := a.collectors.InventoryCollector.Collect(ctx, a.device)
a.log.WithError(err).Debug("collect initial done")
a.log.V(1).Error(err, "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")
a.log.V(1).Info("collect drives")
err = a.CollectDrives(ctx)
a.log.WithError(err).Debug("collect drives done")
a.log.V(1).Error(err, "collect drives done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving drive inventory")
}

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

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

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

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

// Collect TPM info
a.log.Debug("collect tpm")
a.log.V(1).Info("collect tpm")
err = a.CollectTPMs(ctx)
a.log.WithError(err).Debug("collect tpm done")
a.log.V(1).Error(err, "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")
a.log.V(1).Info("collect firmware checksum")
err = a.CollectFirmwareChecksums(ctx)
a.log.WithError(err).Debug("collect firmware checksum done")
a.log.V(1).Error(err, "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")
a.log.V(1).Info("collect uefi variables")
err = a.CollectUEFIVariables(ctx)
a.log.WithError(err).Debug("collect uefi variables done")
a.log.V(1).Error(err, "collect uefi variables done")
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving UEFI variables")
}
Expand All @@ -284,9 +285,9 @@ func (a *InventoryCollectorAction) Collect(ctx context.Context, device *common.D
}

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

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

if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving drive inventory")
Expand All @@ -311,12 +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")
a.log.V(1).Info("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.log.V(1).Error(err, "collect drive capabilities done")

a.setDefaultAttributes()

Expand Down Expand Up @@ -731,13 +732,13 @@ func (a *InventoryCollectorAction) CollectFirmwareChecksums(ctx context.Context)

sumStr, err := a.collectors.FirmwareChecksumCollector.BIOSLogoChecksum(ctx)
if err != nil {
a.log.WithError(err).Warn("error collecting BIOS Logo checksum")
a.log.V(3).Error(err, "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")
a.log.Error(errors.New("nil device bios data"), "expected bios data")
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions actions/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"testing"

"github.com/bmc-toolbox/common"
"github.com/go-logr/logr/testr"
dellFixtures "github.com/metal-toolbox/ironlib/fixtures/dell"
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 @@ -47,7 +47,7 @@ func Test_Inventory_dell(t *testing.T) {
WithDisabledCollectorUtilities([]model.CollectorUtility{"dmidecode"}),
}

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

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

switch tt.name {
case "trace-enabled":
Expand Down
52 changes: 17 additions & 35 deletions actions/storage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ package actions
import (
"context"
"fmt"
"log"
"strings"

"github.com/bmc-toolbox/common"
"github.com/go-logr/logr"
"github.com/metal-toolbox/ironlib/model"
"github.com/metal-toolbox/ironlib/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

var ErrVirtualDiskManagerUtilNotIdentified = errors.New("virtual disk management utility not identifed")

type StorageControllerAction struct {
Logger *logrus.Logger
Logger logr.Logger
trace bool
}

func NewStorageControllerAction(logger *logrus.Logger) *StorageControllerAction {
return &StorageControllerAction{logger}
func NewStorageControllerAction(logger logr.Logger) *StorageControllerAction {
return &StorageControllerAction{
Logger: logger,
trace: logger.GetV() >= 2,
}
}

func (s *StorageControllerAction) CreateVirtualDisk(ctx context.Context, hba *common.StorageController, options *model.CreateVirtualDiskOptions) error {
Expand Down Expand Up @@ -67,58 +70,37 @@ func (s *StorageControllerAction) ListVirtualDisks(ctx context.Context, hba *com

// GetControllerUtility returns the utility command for the given vendor
func (s *StorageControllerAction) GetControllerUtility(vendorName, modelName string) (VirtualDiskManager, error) {
var trace bool

if s.Logger.GetLevel().String() == "trace" {
trace = true
}

if strings.EqualFold(vendorName, common.VendorMarvell) {
return utils.NewMvcliCmd(trace), nil
return utils.NewMvcliCmd(s.trace), nil
}

return nil, errors.Wrap(ErrVirtualDiskManagerUtilNotIdentified, "vendor: "+vendorName+" model: "+modelName)
}

// GetWipeUtility returns the wipe utility based on the disk wipping features
func (s *StorageControllerAction) GetWipeUtility(logicalName string) (DiskWiper, error) {
var trace bool

if s.Logger.GetLevel().String() == "trace" {
trace = true
}
// TODO: use disk wipping features to return the best wipe utility, currently only one available
if trace {
log.Printf("%s | Detecting wipe utility", logicalName)
}

return utils.NewFillZeroCmd(trace), nil
s.Logger.V(6).Info("Detecting wipe utility", "device", logicalName)
return utils.NewFillZeroCmd(s.trace), nil
}

func (s *StorageControllerAction) WipeDisk(ctx context.Context, logicalName string) error {
func (s *StorageControllerAction) WipeDisk(ctx context.Context, log logr.Logger, logicalName string) error {
util, err := s.GetWipeUtility(logicalName)
if err != nil {
return err
}

// Watermark disk
// Before wiping the disk, we apply watermarks to later verify successful deletion
log.Printf("%s | Initiating watermarking process", logicalName)
check, err := utils.ApplyWatermarks(logicalName)
if err != nil {
return err
}

// Wipe the disk
err = util.WipeDisk(ctx, logicalName)
err = util.WipeDisk(ctx, log, logicalName)
if err != nil {
return err
}
// Check if the watermark has been removed after wiping
log.Printf("%s | Checking if the watermark has been removed", logicalName)
err = check()
if err != nil {
return err
}
// Watermarks have been successfully removed, indicating successful deletion
log.Printf("%s | Watermarks has been removed", logicalName)
return nil

return check()
}
19 changes: 2 additions & 17 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/bmc-toolbox/common"
"github.com/go-logr/logr"
"github.com/metal-toolbox/ironlib/actions"
"github.com/metal-toolbox/ironlib/errs"
"github.com/metal-toolbox/ironlib/providers/asrockrack"
Expand All @@ -12,12 +13,11 @@ import (
"github.com/metal-toolbox/ironlib/providers/supermicro"
"github.com/metal-toolbox/ironlib/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// 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) {
func New(logger logr.Logger) (m actions.DeviceManager, err error) {
dmidecode, err := utils.NewDmidecode()
if err != nil {
return nil, errors.Wrap(errs.ErrDmiDecodeRun, err.Error())
Expand Down Expand Up @@ -94,18 +94,3 @@ func CheckDependencies() {
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)
}
Loading
Loading