Skip to content

Commit

Permalink
Handle --source in install/manual-install/interactive-install the s…
Browse files Browse the repository at this point in the history
…ame way

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
  • Loading branch information
jimmykarily committed Sep 28, 2023
1 parent 638c6a1 commit 46dc067
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
58 changes: 28 additions & 30 deletions internal/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ func ManualInstall(c, sourceImg, device string, reboot, poweroff, strictValidati
return err
}

cc, err := config.Scan(collector.Directories(ConfigSource), collector.MergeBootLine, collector.StrictValidation(strictValidations), collector.NoLogs)
cliConf := generateInstallConfForCLIArgs(sourceImg)

cc, err := config.Scan(collector.Directories(ConfigSource),
collector.Readers(strings.NewReader(cliConf)),
collector.MergeBootLine,
collector.StrictValidation(strictValidations), collector.NoLogs)
if err != nil {
return err
}
Expand All @@ -80,7 +85,7 @@ func ManualInstall(c, sourceImg, device string, reboot, poweroff, strictValidati
// Override from flags!
cc.Install.Device = device
}
return RunInstall(cc, sourceImg)
return RunInstall(cc)
}

func Install(sourceImg string, dir ...string) error {
Expand Down Expand Up @@ -116,11 +121,14 @@ func Install(sourceImg string, dir ...string) error {

ensureDataSourceReady()

// Reads config, and if present and offline is defined,
// runs the installation
cc, err = config.Scan(collector.Directories(dir...), collector.MergeBootLine)
cliConf := generateInstallConfForCLIArgs(sourceImg)

// Reads config, and if present and offline is defined, runs the installation
cc, err = config.Scan(collector.Directories(dir...),
collector.Readers(strings.NewReader(cliConf)),
collector.MergeBootLine)
if err == nil && cc.Install != nil && cc.Install.Auto {
err = RunInstall(cc, sourceImg)
err = RunInstall(cc)
if err != nil {
return err
}
Expand Down Expand Up @@ -184,7 +192,7 @@ func Install(sourceImg string, dir ...string) error {
pterm.Info.Println("Starting installation")

cc.Logger.Debugf("Runinstall with cc: %s\n", litter.Sdump(cc))
if err := RunInstall(cc, sourceImg); err != nil {
if err := RunInstall(cc); err != nil {
return err
}

Expand Down Expand Up @@ -213,7 +221,7 @@ func Install(sourceImg string, dir ...string) error {
return nil
}

func RunInstall(c *config.Config, sourceImg string) error {
func RunInstall(c *config.Config) error {
utils.SetEnv(c.Env)
utils.SetEnv(c.Install.Env)

Expand Down Expand Up @@ -250,28 +258,7 @@ func RunInstall(c *config.Config, sourceImg string) error {
// Set our cloud-init to the file we just created
installSpec.CloudInit = append(installSpec.CloudInit, f.Name())
// Get the source of the installation if we are overriding it
if sourceImg != "" {
imgSource, err := v1.NewSrcFromURI(sourceImg)
if err != nil {
return err
}
installSpec.Active.Source = imgSource

// TODO: Why only setting active source above? What about size?
// TODO: These 2 blocks are identical, DRY them.

// size, err := GetSourceSize(cfg, imgSource)
// if err != nil {
// c.Logger.Warnf("Failed to infer size for images: %s", err.Error())
// }

// installSpec.Active.Source = imgSource
// installSpec.Passive.Source = imgSource
// installSpec.Recovery.Source = imgSource
// installSpec.Active.Size = uint(size)
// installSpec.Passive.Size = uint(size)
// installSpec.Recovery.Size = uint(size)
} else if c.Install.Image != "" {
if c.Install.Image != "" {
imgSource, err := v1.NewSrcFromURI(c.Install.Image)
if err != nil {
return err
Expand Down Expand Up @@ -352,3 +339,14 @@ func prepareConfiguration(ctx context.Context, source string) (string, error) {

return f.Name(), nil
}

func generateInstallConfForCLIArgs(source string) string {
if source == "" {
return ""
}

return fmt.Sprintf(`install:
system:
uri: %s
`, source)
}
10 changes: 6 additions & 4 deletions internal/agent/interactive_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,19 @@ func InteractiveInstall(debug, spawnShell bool, sourceImg string) error {
if err != nil {
fmt.Printf("could not write event cloud init: %s\n", err.Error())
}
// override cc with our new config object from the scan, so it's updated for the RunInstall function
// TODO: Alternative solution: pass a reader here (the new feature) and add the image source
cc, _ = config.Scan(collector.Directories(tmpdir), collector.MergeBootLine, collector.NoLogs)

cliConf := generateInstallConfForCLIArgs(sourceImg)
cc, _ = config.Scan(collector.Directories(tmpdir),
collector.Readers(strings.NewReader(cliConf)),
collector.MergeBootLine, collector.NoLogs)
}

pterm.Info.Println("Starting installation")
// Generate final config
ccString, _ := cc.String()
pterm.Info.Println(ccString)

err = RunInstall(cc, sourceImg)
err = RunInstall(cc)
if err != nil {
pterm.Error.Println(err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions internal/agent/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ func determineUpgradeImage(version string) (*v1.ImageSource, error) {
return v1.NewSrcFromURI(fmt.Sprintf("%s:%s", registry, version))
}

// generateConfForCLIArgs creates a kairos configuration for `--source` and `--recovery`
// generateUpgradeConfForCLIArgs creates a kairos configuration for `--source` and `--recovery`
// command line arguments. It will be added to the rest of the configurations.
func generateConfForCLIArgs(source string, upgradeRecovery bool) (string, error) {
func generateUpgradeConfForCLIArgs(source string, upgradeRecovery bool) (string, error) {
upgrade := map[string](map[string]interface{}){
"upgrade": {},
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func findLatestVersion(preReleases, force bool) (string, error) {
}

func generateUpgradeSpec(version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) (*v1.UpgradeSpec, *config.Config, error) {
cliConf, err := generateConfForCLIArgs(source, upgradeRecovery)
cliConf, err := generateUpgradeConfForCLIArgs(source, upgradeRecovery)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 46dc067

Please sign in to comment.