From 09b41735f42c22f53fa34a6d5750c7ccc6b098d8 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto <mudler@users.noreply.github.com> Date: Thu, 21 Jul 2022 21:38:07 +0000 Subject: [PATCH] gear: Be sure to run datasource before install Seems in certain condition the datasource is kicking late and serving the datasource not fast as wanted. We make sure to pull it before install, so we also give chance to convoluted setups to pull configs. Also wires up autoinstall tests to CI --- internal/agent/install.go | 7 +++++++ internal/machine/machine.go | 10 ++++++++++ internal/utils/sh.go | 10 ++++++++++ 3 files changed, 27 insertions(+) diff --git a/internal/agent/install.go b/internal/agent/install.go index 4895704..c9317d0 100644 --- a/internal/agent/install.go +++ b/internal/agent/install.go @@ -65,6 +65,13 @@ func Install(dir ...string) error { } }) + // Try to pull userdata once more. best-effort + if _, err := os.Stat("/oem/userdata"); err != nil { + if err := machine.ExecuteCloudConfig("/system/oem/00_datasource.yaml", "rootfs.before"); err != nil { + fmt.Println("Warning: Failed pulling from datasources") + } + } + // Reads config, and if present and offline is defined, // runs the installation cc, err := config.Scan(config.Directories(dir...), config.MergeBootLine) diff --git a/internal/machine/machine.go b/internal/machine/machine.go index 67e6433..7d6d81e 100644 --- a/internal/machine/machine.go +++ b/internal/machine/machine.go @@ -145,3 +145,13 @@ func SentinelExist(f string) bool { } return false } + +func ExecuteInlineCloudConfig(cloudConfig, stage string) error { + _, err := utils.ShellSTDIN(cloudConfig, fmt.Sprintf("elemental run-stage -s %s -", stage)) + return err +} + +func ExecuteCloudConfig(file, stage string) error { + _, err := utils.SH(fmt.Sprintf("elemental run-stage -s %s %s", stage, file)) + return err +} diff --git a/internal/utils/sh.go b/internal/utils/sh.go index 4f0e619..25758ec 100644 --- a/internal/utils/sh.go +++ b/internal/utils/sh.go @@ -1,6 +1,7 @@ package utils import ( + "bytes" "io/ioutil" "os" "os/exec" @@ -31,3 +32,12 @@ func Shell() *exec.Cmd { cmd.Stdin = os.Stdin return cmd } + +func ShellSTDIN(s, c string) (string, error) { + cmd := exec.Command("/bin/sh", "-c", c) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = bytes.NewBuffer([]byte(s)) + o, err := cmd.CombinedOutput() + return string(o), err +}