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
3 changed files with 50 additions and 14 deletions

View File

@@ -1,8 +1,9 @@
package hook package hook
import ( import (
"github.com/kairos-io/kairos-sdk/utils" "github.com/kairos-io/kairos/v2/pkg/config"
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" events "github.com/kairos-io/kairos-sdk/bus"
) )
@@ -10,7 +11,11 @@ import (
type RunStage struct{} type RunStage struct{}
func (r RunStage) Run(_ config.Config) error { 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 events.RunHookScript("/usr/bin/kairos-agent.install.after.hook") //nolint:errcheck
return nil 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/config/collector"
"github.com/kairos-io/kairos/v2/pkg/elementalConfig" "github.com/kairos-io/kairos/v2/pkg/elementalConfig"
v1 "github.com/kairos-io/kairos/v2/pkg/types/v1" 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" qr "github.com/mudler/go-nodepair/qrcode"
"github.com/mudler/go-pluggable" "github.com/mudler/go-pluggable"
"github.com/pterm/pterm" "github.com/pterm/pterm"
@@ -251,8 +252,14 @@ func Install(dir ...string) error {
} }
func RunInstall(options map[string]string) error { func RunInstall(options map[string]string) error {
// TODO: run yip directly // Load the installation Config from the system
utils.SH("elemental run-stage kairos-install.pre") //nolint:errcheck 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 events.RunHookScript("/usr/bin/kairos-agent.install.pre.hook") //nolint:errcheck
f, _ := os.CreateTemp("", "xxxx") f, _ := os.CreateTemp("", "xxxx")
@@ -277,20 +284,12 @@ func RunInstall(options map[string]string) error {
env := append(c.Install.Env, c.Env...) env := append(c.Install.Env, c.Env...)
utils.SetEnv(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 { if err != nil {
fmt.Printf("could not write cloud init: %s\n", err.Error()) fmt.Printf("could not write cloud init: %s\n", err.Error())
os.Exit(1) 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"] _, reboot := options["reboot"]
_, poweroff := options["poweroff"] _, poweroff := options["poweroff"]
installConfig.Reboot = reboot installConfig.Reboot = reboot

32
main.go
View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"github.com/kairos-io/kairos/v2/pkg/elementalConfig" "github.com/kairos-io/kairos/v2/pkg/elementalConfig"
v1 "github.com/kairos-io/kairos/v2/pkg/types/v1" v1 "github.com/kairos-io/kairos/v2/pkg/types/v1"
"github.com/kairos-io/kairos/v2/pkg/utils"
"path/filepath" "path/filepath"
"runtime" "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", Usage: "Print out Kairos' Cloud Configuration JSON Schema",
Description: `Prints 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", Name: "pull-image",
Description: "Pull remote image to local file", Description: "Pull remote image to local file",
Usage: "Pull remote image to local file",
UsageText: "pull-image [-l] IMAGE TARGET", UsageText: "pull-image [-l] IMAGE TARGET",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{