From c7cbb37b24601f42bdf3b1d7ce4e4d68e5b2ed03 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Thu, 7 Jul 2022 16:57:38 +0000 Subject: [PATCH] gear: Extract netboot artifacts This changeset also adds a `config_url` and `options` keyword in the c3os config. Along with that the config logic is changed so the configuration is taken also from boot commands and merged in the final installed config file. --- cmd/agent/agent.go | 3 ++- cmd/agent/installer.go | 39 ++++++++++++++++++++++++++++++++++----- cmd/agent/main.go | 2 +- cmd/agent/rotate.go | 2 +- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go index 882bfee..b59fb5a 100644 --- a/cmd/agent/agent.go +++ b/cmd/agent/agent.go @@ -20,11 +20,12 @@ func agent(apiAddress string, dir []string, force bool) error { os.MkdirAll("/usr/local/.c3os", 0600) // Reads config - c, err := config.Scan(dir...) + c, err := config.Scan(config.Directories(dir...)) if err != nil { return err } + // TODO: Proper cleanup the log file f, err := ioutil.TempFile(os.TempDir(), "c3os") if err != nil { return err diff --git a/cmd/agent/installer.go b/cmd/agent/installer.go index 3e7b408..c94f82f 100644 --- a/cmd/agent/installer.go +++ b/cmd/agent/installer.go @@ -45,6 +45,17 @@ func install(dir ...string) error { tk := "" r := map[string]string{} + + mergeOption := func(cloudConfig string) { + c := &config.Config{} + yaml.Unmarshal([]byte(cloudConfig), c) + for k, v := range c.Options { + if k == "cc" { + continue + } + r[k] = v + } + } bus.Manager.Response(events.EventChallenge, func(p *pluggable.Plugin, r *pluggable.EventResponse) { tk = r.Data }) @@ -57,12 +68,13 @@ func install(dir ...string) error { // Reads config, and if present and offline is defined, // runs the installation - cc, err := config.Scan(dir...) + cc, err := config.Scan(config.Directories(dir...), config.MergeBootLine) if err == nil && cc.C3OS != nil && cc.C3OS.Offline { - runInstall(map[string]string{ - "device": cc.C3OS.Device, - "cc": cc.String(), - }) + r["cc"] = cc.String() + r["device"] = cc.C3OS.Device + mergeOption(cc.String()) + + runInstall(r) svc, err := machine.Getty(1) if err == nil { @@ -95,6 +107,22 @@ func install(dir ...string) error { return errors.New("no configuration, stopping installation") } + cloudConfig, exists := r["cc"] + mergeOption(cloudConfig) + + ccData := map[string]interface{}{} + yaml.Unmarshal([]byte(cc.String()), &ccData) + if exists { + yaml.Unmarshal([]byte(cloudConfig), &ccData) + } + + out, err := yaml.Marshal(ccData) + if err != nil { + return fmt.Errorf("failed marshalling cc: %w", err) + } + + r["cc"] = string(out) + pterm.Info.Println("Starting installation") utils.SH("elemental run-stage c3os-install.pre") bus.RunHookScript("/usr/bin/c3os-agent.install.pre.hook") @@ -116,6 +144,7 @@ func install(dir ...string) error { func runInstall(options map[string]string) error { f, _ := ioutil.TempFile("", "xxxx") + defer os.RemoveAll(f.Name()) device, ok := options["device"] if !ok { diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 80d910b..8109701 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -166,7 +166,7 @@ $ c3os rotate --network-token XXX if len(args) > 0 { dirs = args } - cc, err := config.Scan(dirs...) + cc, err := config.Scan(config.Directories(dirs...)) if err != nil { return err } diff --git a/cmd/agent/rotate.go b/cmd/agent/rotate.go index f98aa6f..1d1112d 100644 --- a/cmd/agent/rotate.go +++ b/cmd/agent/rotate.go @@ -11,7 +11,7 @@ func rotate(configDir []string, newToken, apiAddress, rootDir string, restart bo return err } - c, err := config.Scan(configDir...) + c, err := config.Scan(config.Directories(configDir...)) if err != nil { return err }