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 with log/slog #139

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 @@ -2,6 +2,7 @@ package actions

import (
"context"
"log/slog"

"github.com/bmc-toolbox/common"
"github.com/metal-toolbox/ironlib/model"
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 *slog.Logger, logicalName string) error
}
37 changes: 19 additions & 18 deletions actions/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import (
"context"
"encoding/json"
"log/slog"
"runtime/debug"
"strings"

Expand All @@ -13,7 +14,6 @@
"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 @@
collectors Collectors

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

// device is the model in which the collected inventory is recorded.
device *common.Device
Expand Down Expand Up @@ -130,9 +130,10 @@
}

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

Check failure on line 136 in actions/inventory.go

View workflow job for this annotation

GitHub Actions / lint-test

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
}

// set options to override
Expand Down Expand Up @@ -205,71 +206,71 @@
// 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")
a.log.Debug("collect initial done", "error", err)
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")
a.log.Debug("collect drives done", "error", err)
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")
a.log.Debug("collect nics done", "error", err)
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")
a.log.Debug("collect bios done", "error", err)
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")
a.log.Debug("collect cpld done", "error", err)
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")
a.log.Debug("collect bmc done", "error", err)
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")
a.log.Debug("collect tpm done", "error", err)
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")
a.log.Debug("collect firmware checksum done", "error", err)
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")
a.log.Debug("collect uefi variables done", "error", err)
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving UEFI variables")
}
Expand All @@ -286,7 +287,7 @@
// Collect StorageController info
a.log.Debug("collect storage controller")
err = a.CollectStorageControllers(ctx)
a.log.WithError(err).Debug("collect storage controller done")
a.log.Debug("collect storage controller done", "error", err)
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving StorageController inventory")
}
Expand All @@ -302,7 +303,7 @@
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")
a.log.Debug("dynamic collect drive done", "error", err)

if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving drive inventory")
Expand All @@ -316,7 +317,7 @@
if err != nil && a.failOnError {
return errors.Wrap(err, "error retrieving DriveCapabilities")
}
a.log.WithError(err).Debug("collect drive capabilities done")
a.log.Debug("collect drive capabilities done", "error", err)

a.setDefaultAttributes()

Expand Down Expand Up @@ -731,13 +732,13 @@

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

if a.device.BIOS == nil {
// XXX: how did we get here?
a.log.Error("nil device bios data")
a.log.Error("expected bios data", "error", errors.New("nil device 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 @@ -11,7 +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/neilotoole/slogt"
"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(slogt.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(slogt.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(slogt.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 @@
import (
"context"
"fmt"
"log"
"log/slog"
"strings"

"github.com/bmc-toolbox/common"
"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 *slog.Logger
trace bool
}

func NewStorageControllerAction(logger *logrus.Logger) *StorageControllerAction {
return &StorageControllerAction{logger}
func NewStorageControllerAction(logger *slog.Logger) *StorageControllerAction {
return &StorageControllerAction{
Logger: logger,
trace: logger.Enabled(nil, -5),

Check failure on line 25 in actions/storage_controller.go

View workflow job for this annotation

GitHub Actions / lint-test

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
}
}

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

// 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.Debug("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 *slog.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 @@ -2,6 +2,7 @@ package ironlib

import (
"fmt"
"log/slog"

"github.com/bmc-toolbox/common"
"github.com/metal-toolbox/ironlib/actions"
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 *slog.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)
}
18 changes: 13 additions & 5 deletions examples/biosconfig/biosconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"

"github.com/metal-toolbox/ironlib"
"github.com/sirupsen/logrus"
)

// This example invokes ironlib and prints out the BIOS features on supported platforms
// a sample output can be seen in the biosconfig.json file

func main() {
logger := logrus.New()
trace := &slog.LevelVar{}
trace.Set(-5)
h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: trace})
logger := slog.New(h)

device, err := ironlib.New(logger)
if err != nil {
logger.Fatal(err)
logger.Error("creating ironlib manager", "error", err)
os.Exit(1)
}

features, err := device.GetBIOSConfiguration(context.TODO())
if err != nil {
logger.Fatal(err)
logger.Error("getting bios config", "error", err)
os.Exit(1)
}

j, err := json.MarshalIndent(features, " ", " ")
if err != nil {
logger.Fatal(err)
logger.Error("formatting json", "error", err)
os.Exit(1)
}

fmt.Println(string(j))
Expand Down
Loading
Loading