Run stages directly from code instead of shelling out (#23)

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka 2023-05-16 18:06:49 +02:00 committed by GitHub
parent 7f703ae588
commit 1afe3c2dbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 14 deletions

View File

@ -1,8 +1,9 @@
package hook
import (
"github.com/kairos-io/kairos-sdk/utils"
config "github.com/kairos-io/kairos/v2/pkg/config"
"github.com/kairos-io/kairos/v2/pkg/config"
"github.com/kairos-io/kairos/v2/pkg/elementalConfig"
"github.com/kairos-io/kairos/v2/pkg/utils"
events "github.com/kairos-io/kairos-sdk/bus"
)
@ -10,7 +11,11 @@ import (
type RunStage struct{}
func (r RunStage) Run(_ config.Config) error {
utils.SH("elemental run-stage kairos-install.after") //nolint:errcheck
cfg, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil {
cfg.Logger.Errorf("Error reading config: %s\n", err)
}
_ = utils.RunStage(&cfg.Config, "kairos-install.after", cfg.Strict, cfg.CloudInitPaths...)
events.RunHookScript("/usr/bin/kairos-agent.install.after.hook") //nolint:errcheck
return nil
}

View File

@ -22,6 +22,7 @@ import (
"github.com/kairos-io/kairos/v2/pkg/config/collector"
"github.com/kairos-io/kairos/v2/pkg/elementalConfig"
v1 "github.com/kairos-io/kairos/v2/pkg/types/v1"
elementalUtils "github.com/kairos-io/kairos/v2/pkg/utils"
qr "github.com/mudler/go-nodepair/qrcode"
"github.com/mudler/go-pluggable"
"github.com/pterm/pterm"
@ -251,8 +252,14 @@ func Install(dir ...string) error {
}
func RunInstall(options map[string]string) error {
// TODO: run yip directly
utils.SH("elemental run-stage kairos-install.pre") //nolint:errcheck
// Load the installation Config from the system
installConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil {
return err
}
// Run pre-install stage
_ = elementalUtils.RunStage(&installConfig.Config, "kairos-install.pre", installConfig.Strict, installConfig.CloudInitPaths...)
events.RunHookScript("/usr/bin/kairos-agent.install.pre.hook") //nolint:errcheck
f, _ := os.CreateTemp("", "xxxx")
@ -277,20 +284,12 @@ func RunInstall(options map[string]string) error {
env := append(c.Install.Env, c.Env...)
utils.SetEnv(env)
err := os.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
err = os.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
if err != nil {
fmt.Printf("could not write cloud init: %s\n", err.Error())
os.Exit(1)
}
// Load the installation Config from the system
// TODO: This uses the default mounter, logger, fs, etc...
// Make it configurable?
installConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil {
return err
}
_, reboot := options["reboot"]
_, poweroff := options["poweroff"]
installConfig.Reboot = reboot

32
main.go
View File

@ -6,6 +6,7 @@ import (
"fmt"
"github.com/kairos-io/kairos/v2/pkg/elementalConfig"
v1 "github.com/kairos-io/kairos/v2/pkg/types/v1"
"github.com/kairos-io/kairos/v2/pkg/utils"
"path/filepath"
"runtime"
@ -483,9 +484,40 @@ The validate command expects a configuration file as its only argument. Local fi
Usage: "Print out Kairos' Cloud Configuration JSON Schema",
Description: `Prints out Kairos' Cloud Configuration JSON Schema`,
},
{
Name: "run-stage",
Description: "Run stage from cloud-init",
Usage: "Run stage from cloud-init",
UsageText: "run-stage STAGE",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "strict",
Usage: "Enable strict mode. Fails and exits on stage errors",
},
},
Before: func(c *cli.Context) error {
if c.Args().Len() != 1 {
cli.HelpPrinter(c.App.Writer, "Stage to run missing\n\n", c.Command)
_ = cli.ShowSubcommandHelp(c)
return fmt.Errorf("")
}
return nil
},
Action: func(c *cli.Context) error {
stage := c.Args().First()
cfg, err := elementalConfig.ReadConfigRun("/etc/elemental")
cfg.Strict = c.Bool("strict")
if err != nil {
cfg.Logger.Errorf("Error reading config: %s\n", err)
}
return utils.RunStage(&cfg.Config, stage, cfg.Strict, cfg.CloudInitPaths...)
},
},
{
Name: "pull-image",
Description: "Pull remote image to local file",
Usage: "Pull remote image to local file",
UsageText: "pull-image [-l] IMAGE TARGET",
Flags: []cli.Flag{
&cli.BoolFlag{