Handle --source in install/manual-install/interactive-install the same way

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis
2023-09-28 15:55:14 +03:00
parent c58efaa623
commit 15a926ad3b
3 changed files with 37 additions and 37 deletions

View File

@@ -62,7 +62,12 @@ func ManualInstall(c, sourceImg, device string, reboot, poweroff, strictValidati
return err 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 { if err != nil {
return err return err
} }
@@ -80,7 +85,7 @@ func ManualInstall(c, sourceImg, device string, reboot, poweroff, strictValidati
// Override from flags! // Override from flags!
cc.Install.Device = device cc.Install.Device = device
} }
return RunInstall(cc, sourceImg) return RunInstall(cc)
} }
func Install(sourceImg string, dir ...string) error { func Install(sourceImg string, dir ...string) error {
@@ -116,11 +121,14 @@ func Install(sourceImg string, dir ...string) error {
ensureDataSourceReady() ensureDataSourceReady()
// Reads config, and if present and offline is defined, cliConf := generateInstallConfForCLIArgs(sourceImg)
// runs the installation
cc, err = config.Scan(collector.Directories(dir...), collector.MergeBootLine) // 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 { if err == nil && cc.Install != nil && cc.Install.Auto {
err = RunInstall(cc, sourceImg) err = RunInstall(cc)
if err != nil { if err != nil {
return err return err
} }
@@ -184,7 +192,7 @@ func Install(sourceImg string, dir ...string) error {
pterm.Info.Println("Starting installation") pterm.Info.Println("Starting installation")
cc.Logger.Debugf("Runinstall with cc: %s\n", litter.Sdump(cc)) 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 return err
} }
@@ -213,7 +221,7 @@ func Install(sourceImg string, dir ...string) error {
return nil return nil
} }
func RunInstall(c *config.Config, sourceImg string) error { func RunInstall(c *config.Config) error {
utils.SetEnv(c.Env) utils.SetEnv(c.Env)
utils.SetEnv(c.Install.Env) utils.SetEnv(c.Install.Env)
@@ -250,28 +258,7 @@ func RunInstall(c *config.Config, sourceImg string) error {
// Set our cloud-init to the file we just created // Set our cloud-init to the file we just created
installSpec.CloudInit = append(installSpec.CloudInit, f.Name()) installSpec.CloudInit = append(installSpec.CloudInit, f.Name())
// Get the source of the installation if we are overriding it // Get the source of the installation if we are overriding it
if sourceImg != "" { if c.Install.Image != "" {
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 != "" {
imgSource, err := v1.NewSrcFromURI(c.Install.Image) imgSource, err := v1.NewSrcFromURI(c.Install.Image)
if err != nil { if err != nil {
return err return err
@@ -352,3 +339,14 @@ func prepareConfiguration(ctx context.Context, source string) (string, error) {
return f.Name(), nil return f.Name(), nil
} }
func generateInstallConfForCLIArgs(source string) string {
if source == "" {
return ""
}
return fmt.Sprintf(`install:
system:
uri: %s
`, source)
}

View File

@@ -282,9 +282,11 @@ func InteractiveInstall(debug, spawnShell bool, sourceImg string) error {
if err != nil { if err != nil {
fmt.Printf("could not write event cloud init: %s\n", err.Error()) 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 cliConf := generateInstallConfForCLIArgs(sourceImg)
cc, _ = config.Scan(collector.Directories(tmpdir), collector.MergeBootLine, collector.NoLogs) cc, _ = config.Scan(collector.Directories(tmpdir),
collector.Readers(strings.NewReader(cliConf)),
collector.MergeBootLine, collector.NoLogs)
} }
pterm.Info.Println("Starting installation") pterm.Info.Println("Starting installation")
@@ -292,7 +294,7 @@ func InteractiveInstall(debug, spawnShell bool, sourceImg string) error {
ccString, _ := cc.String() ccString, _ := cc.String()
pterm.Info.Println(ccString) pterm.Info.Println(ccString)
err = RunInstall(cc, sourceImg) err = RunInstall(cc)
if err != nil { if err != nil {
pterm.Error.Println(err.Error()) pterm.Error.Println(err.Error())
} }

View File

@@ -110,9 +110,9 @@ func determineUpgradeImage(version string) (*v1.ImageSource, error) {
return v1.NewSrcFromURI(fmt.Sprintf("%s:%s", registry, version)) 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. // 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 := map[string](map[string]interface{}){
"upgrade": {}, "upgrade": {},
} }
@@ -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) { 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 { if err != nil {
return nil, nil, err return nil, nil, err
} }