Skip to content

Commit

Permalink
Expose the Analize method of yip (#548)
Browse files Browse the repository at this point in the history
This only shows for a given stage what steps would be run and in which
order

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka authored Sep 20, 2024
1 parent 970eb1a commit bd4dce0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
},
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
1 change: 1 addition & 0 deletions pkg/types/v1/cloud-init-runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ import (

type CloudInitRunner interface {
Run(string, ...string) error
Analyze(string, ...string)
SetModifier(schema.Modifier)
}
43 changes: 34 additions & 9 deletions pkg/utils/runstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -96,19 +108,27 @@ 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)
}
}
}

// Run the stages if cmdline contains the cos.setup stanza
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)
}
}
}
}
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/mocks/cloud-init-runner_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

0 comments on commit bd4dce0

Please sign in to comment.