From 8d2b1e211540c1a39b7a511ca7d9cd662b34beda Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Fri, 9 Aug 2024 14:33:48 +0200 Subject: [PATCH] outofband: use rivets.Server instead of model.Asset This is based on the changes in the previous commit --- internal/outofband/action_handlers.go | 38 +++++++++++----------- internal/outofband/action_handlers_test.go | 1 + internal/outofband/actions.go | 2 +- internal/outofband/actions_test.go | 5 +-- internal/outofband/bmc.go | 9 ++--- internal/outofband/bmc_helpers.go | 10 +++--- internal/outofband/graph.go | 5 +-- 7 files changed, 37 insertions(+), 33 deletions(-) diff --git a/internal/outofband/action_handlers.go b/internal/outofband/action_handlers.go index ed2cfa54..e7c33bf8 100644 --- a/internal/outofband/action_handlers.go +++ b/internal/outofband/action_handlers.go @@ -109,7 +109,7 @@ func (h *handler) serverPoweredOff(ctx context.Context) (bool, error) { // init out of band device queryor - if one isn't already initialized // this is done conditionally to enable tests to pass in a device queryor if h.deviceQueryor == nil { - h.deviceQueryor = NewDeviceQueryor(ctx, h.task.Asset, h.logger) + h.deviceQueryor = NewDeviceQueryor(ctx, h.task.Server, h.logger) } if err := h.deviceQueryor.Open(ctx); err != nil { @@ -181,7 +181,7 @@ func (h *handler) installedEqualsExpected(ctx context.Context, component, expect return err } - found := components.BySlugModel(component, models) + found := components.ByNameModel(component, models) if found == nil { h.logger.WithFields( logrus.Fields{ @@ -201,22 +201,22 @@ func (h *handler) installedEqualsExpected(ctx context.Context, component, expect h.logger.WithFields( logrus.Fields{ - "component": found.Slug, + "component": found.Name, "vendor": found.Vendor, "model": found.Model, "serial": found.Serial, - "current": found.FirmwareInstalled, + "current": found.Firmware.Installed, "expected": expectedFirmware, }).Debug("component version check") - if strings.TrimSpace(found.FirmwareInstalled) == "" { + if strings.TrimSpace(found.Firmware.Installed) == "" { return ErrInstalledVersionUnknown } - if !strings.EqualFold(expectedFirmware, found.FirmwareInstalled) { + if !strings.EqualFold(expectedFirmware, found.Firmware.Installed) { return errors.Wrap( ErrInstalledFirmwareNotEqual, - fmt.Sprintf("expected: %s, current: %s", expectedFirmware, found.FirmwareInstalled), + fmt.Sprintf("expected: %s, current: %s", expectedFirmware, found.Firmware.Installed), ) } @@ -500,7 +500,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { logrus.Fields{ "component": h.action.Firmware.Component, "version": h.action.Firmware.Version, - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "step": h.action.FirmwareInstallStep, "installTask": installTask, }).Info("polling BMC for firmware task status") @@ -548,7 +548,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { case nil: h.logger.WithFields( logrus.Fields{ - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "component": h.firmware.Component, }).Debug("Installed firmware matches expected.") @@ -567,7 +567,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { attemptErrors = multierror.Append(attemptErrors, err) h.logger.WithFields( logrus.Fields{ - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "component": h.firmware.Component, "elapsed": time.Since(startTS).String(), "attempts": fmt.Sprintf("attempt %d/%d", attempts, maxPollStatusAttempts), @@ -592,7 +592,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { "component": h.firmware.Component, "update": h.firmware.FileName, "version": h.firmware.Version, - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "elapsed": time.Since(startTS).String(), "attempts": fmt.Sprintf("attempt %d/%d", attempts, maxPollStatusAttempts), "taskState": state, @@ -625,7 +625,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { if componentIsBMC(h.action.Firmware.Component) && installTask { h.logger.WithFields( logrus.Fields{ - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "delay": delayBMCReset.String(), "taskState": state, "bmcTaskID": h.action.BMCTaskID, @@ -691,7 +691,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { if err := h.powerCycleBMC(ctx); err != nil { h.logger.WithFields( logrus.Fields{ - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "component": h.firmware.Component, "err": err.Error(), }).Debug("install failure required a BMC reset, reset returned error") @@ -699,7 +699,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { h.logger.WithFields( logrus.Fields{ - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "component": h.firmware.Component, }).Debug("BMC reset for failed BMC firmware install") } @@ -719,7 +719,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { if errBmcReset := h.powerCycleBMC(ctx); errBmcReset != nil { h.logger.WithFields( logrus.Fields{ - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "component": h.firmware.Component, "err": errBmcReset.Error(), }).Debug("install success required a BMC reset, reset returned error") @@ -727,7 +727,7 @@ func (h *handler) pollFirmwareTaskStatus(ctx context.Context) error { h.logger.WithFields( logrus.Fields{ - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, "component": h.firmware.Component, }).Debug("BMC reset for successful BMC firmware install") } @@ -747,7 +747,7 @@ func (h *handler) resetBMC(ctx context.Context) error { h.logger.WithFields( logrus.Fields{ "component": h.firmware.Component, - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, }).Info("resetting BMC, delay introduced: " + delayBMCReset.String()) err := h.powerCycleBMC(ctx) @@ -778,7 +778,7 @@ func (h *handler) powerCycleServer(ctx context.Context) error { h.logger.WithFields( logrus.Fields{ "component": h.firmware.Component, - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, }).Info("resetting host for firmware install") return h.deviceQueryor.SetPowerState(ctx, "cycle") @@ -822,7 +822,7 @@ func (h *handler) powerOffServer(ctx context.Context) error { h.logger.WithFields( logrus.Fields{ "component": h.firmware.Component, - "bmc": h.task.Asset.BmcAddress, + "bmc": h.task.Server.BMCAddress, }).Debug("powering off device") if err := h.deviceQueryor.SetPowerState(ctx, "off"); err != nil { diff --git a/internal/outofband/action_handlers_test.go b/internal/outofband/action_handlers_test.go index b7c36e65..9d756b8d 100644 --- a/internal/outofband/action_handlers_test.go +++ b/internal/outofband/action_handlers_test.go @@ -11,6 +11,7 @@ import ( "github.com/metal-toolbox/flasher/internal/model" "github.com/metal-toolbox/flasher/internal/runner" rctypes "github.com/metal-toolbox/rivets/condition" + rtypes "github.com/metal-toolbox/rivets/types" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" diff --git a/internal/outofband/actions.go b/internal/outofband/actions.go index c2f30745..d432e4df 100644 --- a/internal/outofband/actions.go +++ b/internal/outofband/actions.go @@ -55,7 +55,7 @@ func initHandler(actionCtx *runner.ActionHandlerContext, queryor device.Queryor) func (o *ActionHandler) ComposeAction(ctx context.Context, actionCtx *runner.ActionHandlerContext) (*model.Action, error) { var deviceQueryor device.Queryor if actionCtx.DeviceQueryor == nil { - deviceQueryor = NewDeviceQueryor(ctx, actionCtx.Task.Asset, actionCtx.Logger) + deviceQueryor = NewDeviceQueryor(ctx, actionCtx.Task.Server, actionCtx.Logger) } else { deviceQueryor = actionCtx.DeviceQueryor } diff --git a/internal/outofband/actions_test.go b/internal/outofband/actions_test.go index 1035ba09..7af00c45 100644 --- a/internal/outofband/actions_test.go +++ b/internal/outofband/actions_test.go @@ -9,6 +9,7 @@ import ( "github.com/metal-toolbox/flasher/internal/model" "github.com/metal-toolbox/flasher/internal/runner" rctypes "github.com/metal-toolbox/rivets/condition" + rtypes "github.com/metal-toolbox/rivets/types" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -19,8 +20,8 @@ func TestComposeAction(t *testing.T) { return &runner.ActionHandlerContext{ TaskHandlerContext: &runner.TaskHandlerContext{ Task: &model.Task{ - Parameters: rctypes.FirmwareInstallTaskParameters{}, - Asset: &model.Asset{}, + Parameters: &rctypes.FirmwareInstallTaskParameters{}, + Server: &rtypes.Server{}, }, Logger: logrus.NewEntry(logrus.New()), }, diff --git a/internal/outofband/bmc.go b/internal/outofband/bmc.go index be5fd6d5..8c36366e 100644 --- a/internal/outofband/bmc.go +++ b/internal/outofband/bmc.go @@ -15,8 +15,9 @@ import ( "github.com/bmc-toolbox/common" "github.com/metal-toolbox/flasher/internal/device" - "github.com/metal-toolbox/flasher/internal/model" "github.com/sirupsen/logrus" + + rtypes "github.com/metal-toolbox/rivets/types" ) var ( @@ -48,17 +49,17 @@ var ( ErrFirmwareInstallProvider = errors.New("firmware install provider not identified") ) -// bmc wraps the bmclib client and implements the bmcQueryor interface +// bmc wraps the bmclib client and implements the device.Queryor interface type bmc struct { client *bmclib.Client logger *logrus.Entry - asset *model.Asset + asset *rtypes.Server installProvider string availableProviders []string } // NewDeviceQueryor returns a bmc queryor that implements the DeviceQueryor interface -func NewDeviceQueryor(ctx context.Context, asset *model.Asset, logger *logrus.Entry) device.Queryor { +func NewDeviceQueryor(ctx context.Context, asset *rtypes.Server, logger *logrus.Entry) device.Queryor { return &bmc{ client: newBmclibv2Client(ctx, asset, logger), logger: logger, diff --git a/internal/outofband/bmc_helpers.go b/internal/outofband/bmc_helpers.go index 1199b616..92726946 100644 --- a/internal/outofband/bmc_helpers.go +++ b/internal/outofband/bmc_helpers.go @@ -19,12 +19,12 @@ import ( logrusrv2 "github.com/bombsimon/logrusr/v2" "github.com/hashicorp/go-multierror" "github.com/jpillora/backoff" + rtypes "github.com/metal-toolbox/rivets/types" "github.com/pkg/errors" "go.opentelemetry.io/otel" "golang.org/x/exp/slices" "golang.org/x/net/publicsuffix" - "github.com/metal-toolbox/flasher/internal/model" "github.com/sirupsen/logrus" ) @@ -73,7 +73,7 @@ func newHTTPClient() *http.Client { } // newBmclibv2Client initializes a bmclib client with the given credentials -func newBmclibv2Client(_ context.Context, asset *model.Asset, l *logrus.Entry) *bmclib.Client { +func newBmclibv2Client(_ context.Context, asset *rtypes.Server, l *logrus.Entry) *bmclib.Client { logger := logrus.New() if l != nil { logger.Formatter = l.Logger.Formatter @@ -93,9 +93,9 @@ func newBmclibv2Client(_ context.Context, asset *model.Asset, l *logrus.Entry) * logruslogr := logrusrv2.New(logger) bmcClient := bmclib.NewClient( - asset.BmcAddress.String(), - asset.BmcUsername, - asset.BmcPassword, + asset.BMCAddress, + asset.BMCAddress, + asset.BMCPassword, bmclib.WithLogger(logruslogr), bmclib.WithHTTPClient(newHTTPClient()), bmclib.WithPerProviderTimeout(loginTimeout), diff --git a/internal/outofband/graph.go b/internal/outofband/graph.go index 9168a755..82937dd8 100644 --- a/internal/outofband/graph.go +++ b/internal/outofband/graph.go @@ -12,6 +12,7 @@ import ( bconsts "github.com/bmc-toolbox/bmclib/v2/constants" rctypes "github.com/metal-toolbox/rivets/condition" + rtypes "github.com/metal-toolbox/rivets/types" ) func GraphSteps(ctx context.Context, g *dot.Graph) error { @@ -28,10 +29,10 @@ func GraphSteps(ctx context.Context, g *dot.Graph) error { testActionCtx := &runner.ActionHandlerContext{ TaskHandlerContext: &runner.TaskHandlerContext{ Task: &model.Task{ - Parameters: rctypes.FirmwareInstallTaskParameters{ + Parameters: &rctypes.FirmwareInstallTaskParameters{ ResetBMCBeforeInstall: true, }, - Asset: &model.Asset{}, + Server: &rtypes.Server{}, }, Logger: logrus.NewEntry(logrus.New()), DeviceQueryor: m,