Skip to content

Commit

Permalink
install: update install command to use internal task runner
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Feb 1, 2024
1 parent c9f62ac commit 253d3f0
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 169 deletions.
74 changes: 32 additions & 42 deletions internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/google/uuid"
"github.com/metal-toolbox/flasher/internal/model"
"github.com/metal-toolbox/flasher/internal/runner"
sm "github.com/metal-toolbox/flasher/internal/statemachine"
rctypes "github.com/metal-toolbox/rivets/condition"
"github.com/pkg/errors"
Expand Down Expand Up @@ -52,66 +53,55 @@ func (i *Installer) Install(ctx context.Context, params *Params) {
Status: model.NewTaskStatusRecord("initialized task"),
}

// setup state machine task handler
handler := &taskHandler{
fwFile: params.File,
fwVersion: params.Version,
fwComponent: params.Component,
model: params.Model,
vendor: params.Vendor,
onlyPlan: params.OnlyPlan,
}

le := i.logger.WithFields(
logrus.Fields{
"dry-run": params.DryRun,
"bmc": params.BmcAddr,
"component": params.Component,
})

handlerCtx := &sm.HandlerContext{
Dryrun: params.DryRun,
Task: task,
Ctx: ctx,
Publisher: &publisher{},
Data: make(map[string]string),
Asset: &model.Asset{
BmcAddress: net.ParseIP(params.BmcAddr),
BmcUsername: params.User,
BmcPassword: params.Pass,
Model: params.Model,
Vendor: params.Vendor,
},
Logger: le,
}

i.runTaskStatemachine(handler, handlerCtx)
i.runTask(ctx, params, task, le)
}

type publisher struct{}

func (f *publisher) Publish(_ *sm.HandlerContext) {}

func (i *Installer) runTaskStatemachine(handler *taskHandler, handlerCtx *sm.HandlerContext) {
startTS := time.Now()

i.logger.Info("running task for device")
func (i *Installer) runTask(ctx context.Context, params *Params, task *model.Task, le *logrus.Entry) {
h := &handler{
fwFile: params.File,
fwComponent: params.Component,
fwVersion: params.Version,
model: params.Model,
vendor: params.Vendor,
onlyPlan: params.OnlyPlan,
ctx: &sm.HandlerContext{
Dryrun: params.DryRun,
Task: task,
Ctx: ctx,
Publisher: &publisher{},
Data: make(map[string]string),
Asset: &model.Asset{
BmcAddress: net.ParseIP(params.BmcAddr),
BmcUsername: params.User,
BmcPassword: params.Pass,
Model: params.Model,
Vendor: params.Vendor,
},
Logger: le,
},
}

// init state machine for task
stateMachine, err := sm.NewTaskStateMachine(handler)
if err != nil {
i.logger.Error(err)
r := runner.New(le)

return
}
startTS := time.Now()

_ = handlerCtx.Task.SetState(model.StatePending)
i.logger.Info("running task for device")

// run task state machine
if err := stateMachine.Run(handlerCtx.Task, handlerCtx); err != nil {
if err := r.RunTask(ctx, task, h); err != nil {
i.logger.WithFields(
logrus.Fields{
"bmc-ip": handlerCtx.Asset.BmcAddress.String(),
"bmc-ip": h.ctx.Asset.BmcAddress.String(),
"err": err.Error(),
},
).Warn("task for device failed")
Expand All @@ -120,7 +110,7 @@ func (i *Installer) runTaskStatemachine(handler *taskHandler, handlerCtx *sm.Han
}

i.logger.WithFields(logrus.Fields{
"bmc-ip": handlerCtx.Asset.BmcAddress,
"bmc-ip": h.ctx.Asset.BmcAddress,
"elapsed": time.Since(startTS).String(),
}).Info("task for device completed")
}
Loading

0 comments on commit 253d3f0

Please sign in to comment.