From bd4dce015f1a6893c27c9dc77a8599f3f0d09853 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Fri, 20 Sep 2024 10:36:09 +0200 Subject: [PATCH] Expose the Analize method of yip (#548) This only shows for a given stage what steps would be run and in which order Signed-off-by: Itxaka --- main.go | 8 +++++ pkg/cloudinit/cloudinit.go | 4 +++ pkg/types/v1/cloud-init-runner.go | 1 + pkg/utils/runstage.go | 43 +++++++++++++++++++++------ tests/mocks/cloud-init-runner_mock.go | 2 ++ 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 2716b28b..3a84a29c 100644 --- a/main.go +++ b/main.go @@ -667,6 +667,11 @@ The validate command expects a configuration file as its only argument. Local fi Name: "cloud-init-paths", Usage: "Extra paths to add to the run stage", }, + &cli.BoolFlag{ + Name: "analyze", + Usage: "Only print the modules that would run in the order they would run", + Aliases: []string{"a"}, + }, }, Before: func(c *cli.Context) error { if c.Args().Len() != 1 { @@ -692,6 +697,9 @@ The validate command expects a configuration file as its only argument. Local fi if err != nil { config.Logger.Errorf("Error reading config: %s\n", err) } + if c.Bool("analyze") { + return utils.RunStageAnalyze(config, stage) + } return utils.RunStage(config, stage) }, }, diff --git a/pkg/cloudinit/cloudinit.go b/pkg/cloudinit/cloudinit.go index 2b32a010..538c441f 100644 --- a/pkg/cloudinit/cloudinit.go +++ b/pkg/cloudinit/cloudinit.go @@ -81,3 +81,7 @@ func (ci *YipCloudInitRunner) SetModifier(m schema.Modifier) { func (ci *YipCloudInitRunner) SetFs(fs vfs.FS) { ci.fs = fs } + +func (ci *YipCloudInitRunner) Analyze(stage string, args ...string) { + ci.exec.Analyze(stage, vfs.OSFS, ci.console, args...) +} diff --git a/pkg/types/v1/cloud-init-runner.go b/pkg/types/v1/cloud-init-runner.go index bb43cb3e..b0de5591 100644 --- a/pkg/types/v1/cloud-init-runner.go +++ b/pkg/types/v1/cloud-init-runner.go @@ -22,5 +22,6 @@ import ( type CloudInitRunner interface { Run(string, ...string) error + Analyze(string, ...string) SetModifier(schema.Modifier) } diff --git a/pkg/utils/runstage.go b/pkg/utils/runstage.go index c8702ddb..69982b87 100644 --- a/pkg/utils/runstage.go +++ b/pkg/utils/runstage.go @@ -57,14 +57,26 @@ func checkYAMLError(cfg *agentConfig.Config, allErrors, err error) error { return allErrors } +// RunstageAnalyze +func RunStageAnalyze(cfg *agentConfig.Config, stage string) error { + return runstage(cfg, stage, true) +} + // RunStage will run yip func RunStage(cfg *agentConfig.Config, stage string) error { + return runstage(cfg, stage, false) +} + +func runstage(cfg *agentConfig.Config, stage string, analyze bool) error { var cmdLineYipURI string var allErrors error var cloudInitPaths []string cloudInitPaths = append(constants.GetCloudInitPaths(), cfg.CloudInitPaths...) cfg.Logger.Debugf("Cloud-init paths set to %v", cloudInitPaths) + if analyze { + cfg.Logger.Info("Analyze mode, showing DAG") + } // Make sure cloud init path specified are existing in the system for _, cp := range cloudInitPaths { @@ -96,9 +108,13 @@ func RunStage(cfg *agentConfig.Config, stage string) error { // Run all stages for each of the default cloud config paths + extra cloud config paths for _, s := range []string{stageBefore, stage, stageAfter} { - err = cfg.CloudInitRunner.Run(s, cloudInitPaths...) - if err != nil { - allErrors = multierror.Append(allErrors, err) + if analyze { + cfg.CloudInitRunner.Analyze(s, cloudInitPaths...) + } else { + err = cfg.CloudInitRunner.Run(s, cloudInitPaths...) + if err != nil { + allErrors = multierror.Append(allErrors, err) + } } } @@ -106,9 +122,13 @@ func RunStage(cfg *agentConfig.Config, stage string) error { if cmdLineYipURI != "" { cmdLineArgs := []string{cmdLineYipURI} for _, s := range []string{stageBefore, stage, stageAfter} { - err = cfg.CloudInitRunner.Run(s, cmdLineArgs...) - if err != nil { - allErrors = multierror.Append(allErrors, err) + if analyze { + cfg.CloudInitRunner.Analyze(s, cloudInitPaths...) + } else { + err = cfg.CloudInitRunner.Run(s, cmdLineArgs...) + if err != nil { + allErrors = multierror.Append(allErrors, err) + } } } } @@ -117,10 +137,15 @@ func RunStage(cfg *agentConfig.Config, stage string) error { cfg.CloudInitRunner.SetModifier(schema.DotNotationModifier) for _, s := range []string{stageBefore, stage, stageAfter} { - err = cfg.CloudInitRunner.Run(s, string(cmdLineOut)) - if err != nil { - allErrors = checkYAMLError(cfg, allErrors, err) + if analyze { + cfg.CloudInitRunner.Analyze(s, cloudInitPaths...) + } else { + err = cfg.CloudInitRunner.Run(s, string(cmdLineOut)) + if err != nil { + allErrors = checkYAMLError(cfg, allErrors, err) + } } + } cfg.CloudInitRunner.SetModifier(nil) diff --git a/tests/mocks/cloud-init-runner_mock.go b/tests/mocks/cloud-init-runner_mock.go index c769d24a..a5d97e52 100644 --- a/tests/mocks/cloud-init-runner_mock.go +++ b/tests/mocks/cloud-init-runner_mock.go @@ -37,3 +37,5 @@ func (ci *FakeCloudInitRunner) Run(stage string, args ...string) error { func (ci *FakeCloudInitRunner) SetModifier(modifier schema.Modifier) { } + +func (ci *FakeCloudInitRunner) Analyze(stage string, args ...string) {}