Skip to content

Commit

Permalink
outofband/action_handlers: purge unused function parameters, objects …
Browse files Browse the repository at this point in the history
…renamed
  • Loading branch information
joelrebel committed May 4, 2023
1 parent 0c75132 commit d75aae6
Showing 1 changed file with 32 additions and 75 deletions.
107 changes: 32 additions & 75 deletions internal/outofband/action_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,14 @@ func sleepWithContext(ctx context.Context, t time.Duration) error {
}
}

func (h *actionHandler) conditionPowerOnDevice(action *model.Action, tctx *sm.HandlerContext) (bool, error) {
task, err := tctx.Store.TaskByID(tctx.Ctx, action.TaskID)
if err != nil {
return false, err
}

func (h *actionHandler) conditionPowerOnDevice(_ *model.Action, tctx *sm.HandlerContext) (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 tctx.DeviceQueryor == nil {
tctx.DeviceQueryor = NewDeviceQueryor(tctx.Ctx, &task.Parameters.Device, tctx.Logger)
tctx.DeviceQueryor = NewDeviceQueryor(tctx.Ctx, tctx.Asset, tctx.Logger)
}

if err = tctx.DeviceQueryor.Open(tctx.Ctx); err != nil {
if err := tctx.DeviceQueryor.Open(tctx.Ctx); err != nil {
return false, err
}

Expand Down Expand Up @@ -132,7 +127,7 @@ func (h *actionHandler) powerOnDevice(a sw.StateSwitch, c sw.TransitionArgs) err

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"component": action.Firmware.Component,
"version": action.Firmware.Version,
}).Info("device is currently powered off, powering on")

Expand Down Expand Up @@ -160,21 +155,16 @@ func (h *actionHandler) checkCurrentFirmware(a sw.StateSwitch, c sw.TransitionAr
if !action.VerifyCurrentFirmware {
tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"vendor": action.Firmware.Vendor,
"model": action.Firmware.Model,
"component": action.Firmware.Component,
}).Debug("Skipped installed version lookup - action.VerifyCurrentFirmware was disabled")

return nil
}

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"vendor": action.Firmware.Vendor,
"model": action.Firmware.Model,
"plannedVersion": action.Firmware.Version,
}).Debug("Querying device inventory from BMC for current component firmware - ")
"component": action.Firmware.Component,
}).Debug("Querying device inventory from BMC for current component firmware")

inv, err := tctx.DeviceQueryor.Inventory(tctx.Ctx)
if err != nil {
Expand All @@ -186,7 +176,7 @@ func (h *actionHandler) checkCurrentFirmware(a sw.StateSwitch, c sw.TransitionAr
return err
}

component := components.BySlugVendorModel(action.Firmware.ComponentSlug, action.Firmware.Vendor, action.Firmware.Model)
component := components.BySlugVendorModel(action.Firmware.Component, action.Firmware.Vendor, action.Firmware.Models)
if component == nil {
return errors.Wrap(ErrComponentNotFound, "no match based on given slug, vendor, model attributes")
}
Expand All @@ -199,9 +189,9 @@ func (h *actionHandler) checkCurrentFirmware(a sw.StateSwitch, c sw.TransitionAr
if equal {
tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"component": action.Firmware.Component,
"vendor": action.Firmware.Vendor,
"model": action.Firmware.Model,
"models": action.Firmware.Models,
"plannedVersion": action.Firmware.Version,
"installedVersion": component.FirmwareInstalled,
"err": err.Error(),
Expand Down Expand Up @@ -246,7 +236,7 @@ func (h *actionHandler) downloadFirmware(a sw.StateSwitch, c sw.TransitionArgs)

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"component": action.Firmware.Component,
"version": action.Firmware.Version,
"url": action.Firmware.URL,
"file": file,
Expand All @@ -266,11 +256,6 @@ func (h *actionHandler) initiateInstallFirmware(a sw.StateSwitch, c sw.Transitio
return errors.Wrap(ErrFirmwareTempFile, "expected FirmwareTempFile to be declared")
}

task, err := tctx.Store.TaskByID(tctx.Ctx, tctx.TaskID)
if err != nil {
return err
}

// open firmware file handle
fileHandle, err := os.Open(action.FirmwareTempFile)
if err != nil {
Expand All @@ -284,8 +269,8 @@ func (h *actionHandler) initiateInstallFirmware(a sw.StateSwitch, c sw.Transitio
// initiate firmware install
bmcTaskID, err := tctx.DeviceQueryor.FirmwareInstall(
tctx.Ctx,
action.Firmware.ComponentSlug,
task.Parameters.ForceInstall,
action.Firmware.Component,
tctx.Task.Parameters.ForceInstall,
fileHandle,
)
if err != nil {
Expand All @@ -303,7 +288,7 @@ func (h *actionHandler) initiateInstallFirmware(a sw.StateSwitch, c sw.Transitio

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"component": action.Firmware.Component,
"update": action.Firmware.FileName,
"version": action.Firmware.Version,
"bmcTaskID": action.BMCTaskID,
Expand All @@ -312,6 +297,9 @@ func (h *actionHandler) initiateInstallFirmware(a sw.StateSwitch, c sw.Transitio
return nil
}

// polls firmware install status from the BMC
//
// nolint:gocyclo // for now this is best kept in the same method
func (h *actionHandler) pollFirmwareInstallStatus(a sw.StateSwitch, c sw.TransitionArgs) error {
action, tctx, err := actionTaskCtxFromInterfaces(a, c)
if err != nil {
Expand All @@ -322,11 +310,6 @@ func (h *actionHandler) pollFirmwareInstallStatus(a sw.StateSwitch, c sw.Transit
return nil
}

task, err := tctx.Store.TaskByID(tctx.Ctx, tctx.TaskID)
if err != nil {
return err
}

delay := &backoff.Backoff{
Min: backoffMin,
Max: backoffMax,
Expand All @@ -347,9 +330,9 @@ func (h *actionHandler) pollFirmwareInstallStatus(a sw.StateSwitch, c sw.Transit

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"component": action.Firmware.Component,
"version": action.Firmware.Version,
"bmc": task.Parameters.Device.BmcAddress,
"bmc": tctx.Asset.BmcAddress,
}).Info("polling BMC for firmware install status")

for {
Expand Down Expand Up @@ -379,16 +362,16 @@ func (h *actionHandler) pollFirmwareInstallStatus(a sw.StateSwitch, c sw.Transit
status, err := tctx.DeviceQueryor.FirmwareInstallStatus(
tctx.Ctx,
action.Firmware.Version,
action.Firmware.ComponentSlug,
action.Firmware.Component,
action.BMCTaskID,
)

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"component": action.Firmware.Component,
"update": action.Firmware.FileName,
"version": action.Firmware.Version,
"bmc": task.Parameters.Device.BmcAddress,
"bmc": tctx.Asset.BmcAddress,
"elapsed": time.Since(startTS).String(),
"attempts": fmt.Sprintf("attempt %d/%d", attempts, maxAttempts),
"taskState": status,
Expand Down Expand Up @@ -450,15 +433,10 @@ func (h *actionHandler) resetBMC(a sw.StateSwitch, c sw.TransitionArgs) error {
return nil
}

task, err := tctx.Store.TaskByID(tctx.Ctx, tctx.TaskID)
if err != nil {
return err
}

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"bmc": task.Parameters.Device.BmcAddress,
"component": action.Firmware.Component,
"bmc": tctx.Asset.BmcAddress,
}).Info("resetting BMC for firmware install")

if !tctx.Dryrun {
Expand All @@ -484,15 +462,10 @@ func (h *actionHandler) resetDevice(a sw.StateSwitch, c sw.TransitionArgs) error
return nil
}

task, err := tctx.Store.TaskByID(tctx.Ctx, tctx.TaskID)
if err != nil {
return err
}

tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"bmc": task.Parameters.Device.BmcAddress,
"component": action.Firmware.Component,
"bmc": tctx.Asset.BmcAddress,
}).Info("resetting host for firmware install")

if !tctx.Dryrun {
Expand Down Expand Up @@ -542,16 +515,11 @@ func (h *actionHandler) powerOffDevice(a sw.StateSwitch, c sw.TransitionArgs) er
return nil
}

task, err := tctx.Store.TaskByID(tctx.Ctx, tctx.TaskID)
if err != nil {
return err
}

if !tctx.Dryrun {
tctx.Logger.WithFields(
logrus.Fields{
"component": action.Firmware.ComponentSlug,
"bmc": task.Parameters.Device.BmcAddress,
"component": action.Firmware.Component,
"bmc": tctx.Asset.BmcAddress,
}).Debug("powering off device")

if err := tctx.DeviceQueryor.SetPowerState(tctx.Ctx, "off"); err != nil {
Expand All @@ -562,32 +530,21 @@ func (h *actionHandler) powerOffDevice(a sw.StateSwitch, c sw.TransitionArgs) er
return nil
}

func (h *actionHandler) PersistState(a sw.StateSwitch, args sw.TransitionArgs) error {
func (h *actionHandler) PublishStatus(_ sw.StateSwitch, args sw.TransitionArgs) error {
tctx, ok := args.(*sm.HandlerContext)
if !ok {
return sm.ErrInvalidTransitionHandler
}

action, ok := a.(*model.Action)
if !ok {
return errors.Wrap(ErrSaveAction, ErrActionTypeAssertion.Error())
}

if err := tctx.Store.UpdateTaskAction(tctx.Ctx, tctx.TaskID, *action); err != nil {
return errors.Wrap(ErrSaveAction, err.Error())
}

return nil
}
tctx.Publisher.Publish(tctx.Ctx, tctx.Task)

func (h *actionHandler) actionFailed(a sw.StateSwitch, c sw.TransitionArgs) error {
return nil
}

func (h *actionHandler) actionSuccessful(a sw.StateSwitch, c sw.TransitionArgs) error {
func (h *actionHandler) actionFailed(_ sw.StateSwitch, _ sw.TransitionArgs) error {
return nil
}

func (h *actionHandler) actionSkipped(a sw.StateSwitch, c sw.TransitionArgs) error {
func (h *actionHandler) actionSuccessful(_ sw.StateSwitch, _ sw.TransitionArgs) error {
return nil
}

0 comments on commit d75aae6

Please sign in to comment.