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 <itxaka@kairos.io>
This commit is contained in:
Itxaka 2024-09-20 10:36:09 +02:00 committed by GitHub
parent 970eb1aa72
commit bd4dce015f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 49 additions and 9 deletions

View File

@ -667,6 +667,11 @@ The validate command expects a configuration file as its only argument. Local fi
Name: "cloud-init-paths", Name: "cloud-init-paths",
Usage: "Extra paths to add to the run stage", 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 { Before: func(c *cli.Context) error {
if c.Args().Len() != 1 { 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 { if err != nil {
config.Logger.Errorf("Error reading config: %s\n", err) config.Logger.Errorf("Error reading config: %s\n", err)
} }
if c.Bool("analyze") {
return utils.RunStageAnalyze(config, stage)
}
return utils.RunStage(config, stage) return utils.RunStage(config, stage)
}, },
}, },

View File

@ -81,3 +81,7 @@ func (ci *YipCloudInitRunner) SetModifier(m schema.Modifier) {
func (ci *YipCloudInitRunner) SetFs(fs vfs.FS) { func (ci *YipCloudInitRunner) SetFs(fs vfs.FS) {
ci.fs = fs ci.fs = fs
} }
func (ci *YipCloudInitRunner) Analyze(stage string, args ...string) {
ci.exec.Analyze(stage, vfs.OSFS, ci.console, args...)
}

View File

@ -22,5 +22,6 @@ import (
type CloudInitRunner interface { type CloudInitRunner interface {
Run(string, ...string) error Run(string, ...string) error
Analyze(string, ...string)
SetModifier(schema.Modifier) SetModifier(schema.Modifier)
} }

View File

@ -57,14 +57,26 @@ func checkYAMLError(cfg *agentConfig.Config, allErrors, err error) error {
return allErrors return allErrors
} }
// RunstageAnalyze
func RunStageAnalyze(cfg *agentConfig.Config, stage string) error {
return runstage(cfg, stage, true)
}
// RunStage will run yip // RunStage will run yip
func RunStage(cfg *agentConfig.Config, stage string) error { 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 cmdLineYipURI string
var allErrors error var allErrors error
var cloudInitPaths []string var cloudInitPaths []string
cloudInitPaths = append(constants.GetCloudInitPaths(), cfg.CloudInitPaths...) cloudInitPaths = append(constants.GetCloudInitPaths(), cfg.CloudInitPaths...)
cfg.Logger.Debugf("Cloud-init paths set to %v", 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 // Make sure cloud init path specified are existing in the system
for _, cp := range cloudInitPaths { for _, cp := range cloudInitPaths {
@ -96,33 +108,46 @@ func RunStage(cfg *agentConfig.Config, stage string) error {
// Run all stages for each of the default cloud config paths + extra cloud config paths // Run all stages for each of the default cloud config paths + extra cloud config paths
for _, s := range []string{stageBefore, stage, stageAfter} { for _, s := range []string{stageBefore, stage, stageAfter} {
if analyze {
cfg.CloudInitRunner.Analyze(s, cloudInitPaths...)
} else {
err = cfg.CloudInitRunner.Run(s, cloudInitPaths...) err = cfg.CloudInitRunner.Run(s, cloudInitPaths...)
if err != nil { if err != nil {
allErrors = multierror.Append(allErrors, err) allErrors = multierror.Append(allErrors, err)
} }
} }
}
// Run the stages if cmdline contains the cos.setup stanza // Run the stages if cmdline contains the cos.setup stanza
if cmdLineYipURI != "" { if cmdLineYipURI != "" {
cmdLineArgs := []string{cmdLineYipURI} cmdLineArgs := []string{cmdLineYipURI}
for _, s := range []string{stageBefore, stage, stageAfter} { for _, s := range []string{stageBefore, stage, stageAfter} {
if analyze {
cfg.CloudInitRunner.Analyze(s, cloudInitPaths...)
} else {
err = cfg.CloudInitRunner.Run(s, cmdLineArgs...) err = cfg.CloudInitRunner.Run(s, cmdLineArgs...)
if err != nil { if err != nil {
allErrors = multierror.Append(allErrors, err) allErrors = multierror.Append(allErrors, err)
} }
} }
} }
}
// Run stages encoded from /proc/cmdlines // Run stages encoded from /proc/cmdlines
cfg.CloudInitRunner.SetModifier(schema.DotNotationModifier) cfg.CloudInitRunner.SetModifier(schema.DotNotationModifier)
for _, s := range []string{stageBefore, stage, stageAfter} { for _, s := range []string{stageBefore, stage, stageAfter} {
if analyze {
cfg.CloudInitRunner.Analyze(s, cloudInitPaths...)
} else {
err = cfg.CloudInitRunner.Run(s, string(cmdLineOut)) err = cfg.CloudInitRunner.Run(s, string(cmdLineOut))
if err != nil { if err != nil {
allErrors = checkYAMLError(cfg, allErrors, err) allErrors = checkYAMLError(cfg, allErrors, err)
} }
} }
}
cfg.CloudInitRunner.SetModifier(nil) cfg.CloudInitRunner.SetModifier(nil)
// We return error here only if we have been running in strict mode. // We return error here only if we have been running in strict mode.

View File

@ -37,3 +37,5 @@ func (ci *FakeCloudInitRunner) Run(stage string, args ...string) error {
func (ci *FakeCloudInitRunner) SetModifier(modifier schema.Modifier) { func (ci *FakeCloudInitRunner) SetModifier(modifier schema.Modifier) {
} }
func (ci *FakeCloudInitRunner) Analyze(stage string, args ...string) {}