From d02ed94e66d199c437041d89c655b3afe0844817 Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Mon, 4 Dec 2023 16:07:35 +0100 Subject: [PATCH] cmd/install: Add a --plan-only flag to print the firmware install plan --- cmd/install.go | 3 +++ internal/install/install.go | 2 ++ internal/install/task_handler.go | 29 ++++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cmd/install.go b/cmd/install.go index cc401050..35a16392 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -28,6 +28,7 @@ var ( user string pass string force bool + onlyPlan bool ) func runInstall(ctx context.Context) { @@ -63,6 +64,7 @@ func runInstall(ctx context.Context) { Pass: pass, BmcAddr: addr, Force: force, + OnlyPlan: onlyPlan, } installer := install.New(flasher.Logger) @@ -71,6 +73,7 @@ func runInstall(ctx context.Context) { } func init() { + cmdInstall.Flags().BoolVarP(&onlyPlan, "only-plan", "", false, "only plan and list the install plan") cmdInstall.Flags().BoolVarP(&dryrun, "dry-run", "", false, "dry run install") cmdInstall.Flags().BoolVarP(&force, "force", "", false, "force install, skip checking existing version") cmdInstall.Flags().StringVar(&fwversion, "version", "", "The version of the firmware being installed") diff --git a/internal/install/install.go b/internal/install/install.go index 063a2c9c..620ac8bd 100644 --- a/internal/install/install.go +++ b/internal/install/install.go @@ -34,6 +34,7 @@ type Params struct { Model string DryRun bool Force bool + OnlyPlan bool } func (i *Installer) Install(ctx context.Context, params *Params) { @@ -58,6 +59,7 @@ func (i *Installer) Install(ctx context.Context, params *Params) { fwComponent: params.Component, model: params.Model, vendor: params.Vendor, + onlyPlan: params.OnlyPlan, } le := i.logger.WithFields( diff --git a/internal/install/task_handler.go b/internal/install/task_handler.go index 91623968..dfb23eea 100644 --- a/internal/install/task_handler.go +++ b/internal/install/task_handler.go @@ -1,6 +1,8 @@ package install import ( + "fmt" + "github.com/bmc-toolbox/common" sw "github.com/filanov/stateswitch" "github.com/metal-toolbox/flasher/internal/model" @@ -18,11 +20,13 @@ var ( // taskHandler implements the taskTransitionHandler methods type taskHandler struct { - fwFile string - fwComponent string - fwVersion string - model string - vendor string + fwFile string + fwComponent string + fwVersion string + model string + vendor string + onlyPlan bool + bmcResetPreInstall bool } func (h *taskHandler) Init(_ sw.StateSwitch, args sw.TransitionArgs) error { @@ -92,6 +96,17 @@ func (h *taskHandler) Plan(t sw.StateSwitch, args sw.TransitionArgs) error { return nil } +func (h *taskHandler) listPlan(tctx *sm.HandlerContext) error { + tctx.Logger.WithField("plan.actions", len(tctx.ActionStateMachines)).Info("only listing the plan") + for _, actionSM := range tctx.ActionStateMachines { + for _, tx := range actionSM.TransitionOrder() { + fmt.Println(tx) + } + } + + return nil +} + func (h *taskHandler) Run(t sw.StateSwitch, args sw.TransitionArgs) error { tctx, ok := args.(*sm.HandlerContext) if !ok { @@ -103,6 +118,10 @@ func (h *taskHandler) Run(t sw.StateSwitch, args sw.TransitionArgs) error { return errors.Wrap(ErrSaveTask, ErrTaskTypeAssertion.Error()) } + if h.onlyPlan { + return h.listPlan(tctx) + } + tctx.Logger.WithField("plan.actions", len(tctx.ActionStateMachines)).Debug("running the plan") // each actionSM (state machine) corresponds to a firmware to be installed