Skip to content

Commit

Permalink
✨ Minor enhancements (kairos-io#239)
Browse files Browse the repository at this point in the history
* 🐛 Fixup grub option quoting

* ⚙️ Copy discovery to oem if found

* ✨ Add environment block to install

* ⚙️ Use /oem for mount in kcrypt post-hook

* 📝 Update docs with installer env reference

* 🤖 Add test deps

* ⚙️ Be consistent and set env also for post-hooks

* ⚙️ propagate env in post-hooks
  • Loading branch information
mudler authored Oct 18, 2022
1 parent 1a8e246 commit 9d5f7e3
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/src/pages/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ install:
extra_passive_cmdline: ""
# Change GRUB menu entry
default_menu_entry: ""
# Environmental variable to set to the installer calls
env:
foo: "bar"

vpn:
# EdgeVPN environment options
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/nxadm/tail v1.4.8
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.20.0
github.com/otiai10/copy v1.7.0
github.com/pterm/pterm v0.12.41
github.com/qeesung/image2ascii v1.0.1
github.com/spectrocloud/peg v0.0.0-20221005172954-aa887438fafc
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q=
github.com/onsi/gomega v1.20.0/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/packethost/packngo v0.1.0/go.mod h1:otzZQXgoO96RTzDB/Hycg0qZcXZsWJGJRSXbmEIJ+4M=
github.com/packethost/packngo v0.25.0/go.mod h1:/UHguFdPs6Lf6FOkkSEPnRY5tgS0fsVM+Zv/bvBrmt0=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/hooks/gruboptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (b GrubOptions) Run(c config.Config) error {
machine.Umount("/tmp/oem")
}()
for k, v := range c.Install.GrubOptions {
out, err := utils.SH(fmt.Sprintf("grub2-editenv /tmp/oem/grubenv set %s=%s", k, v))
out, err := utils.SH(fmt.Sprintf(`grub2-editenv /tmp/oem/grubenv set "%s=%s"`, k, v))
if err != nil {
fmt.Printf("could not set boot option: %s\n", out+err.Error())
return nil // do not error out
Expand Down
28 changes: 28 additions & 0 deletions internal/agent/hooks/kcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@ package hook

import (
"fmt"
"os"
"time"

config "github.com/kairos-io/kairos/pkg/config"
"github.com/kairos-io/kairos/pkg/machine"
"github.com/kairos-io/kairos/pkg/utils"
cp "github.com/otiai10/copy"
)

type Kcrypt struct{}

func (k Kcrypt) Run(c config.Config) error {

if len(c.Install.Encrypt) == 0 {
return nil
}

machine.Mount("COS_OEM", "/oem") //nolint:errcheck
defer func() {
machine.Umount("/oem")
}()

_ = os.MkdirAll("/oem/system/discovery", 0650)

for _, p := range c.Install.Encrypt {
out, err := utils.SH(fmt.Sprintf("kcrypt encrypt %s", p))
if err != nil {
Expand All @@ -24,5 +39,18 @@ func (k Kcrypt) Run(c config.Config) error {
}
}

if c.Install.SkipEncryptCopyPlugins {
fmt.Println("Skip discovery plugin copy")
return nil
}

err := cp.Copy("/system/discovery", "/oem/system/discovery")
if err != nil {
fmt.Println("Failed during copying discovery plugins: ", err.Error())
if c.FailOnBundleErrors {
return err
}
}

return nil
}
12 changes: 11 additions & 1 deletion internal/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -226,6 +227,15 @@ func RunInstall(options map[string]string) error {
c.Install.Reboot = true
}

for _, e := range c.Install.Env {
pair := strings.SplitN(e, "=", 2)
if len(pair) >= 2 {
os.Setenv(pair[0], pair[1])
}
}

envs := os.Environ()

err := ioutil.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
if err != nil {
fmt.Printf("could not write cloud init: %s\n", err.Error())
Expand All @@ -236,7 +246,7 @@ func RunInstall(options map[string]string) error {
args = append(args, "-c", f.Name(), device)

cmd := exec.Command("elemental", args...)
cmd.Env = os.Environ()
cmd.Env = envs
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
Expand Down
14 changes: 14 additions & 0 deletions overlay/files/system/oem/21_kcrypt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "Update discovery plugins"
stages:
after-upgrade:
- name: "Update plugins"
commands:
- |
STATEDIR=/tmp/mnt/OEM
OEM=$(blkid -L COS_OEM || true)
mkdir -p $STATEDIR || true
mount ${OEM} $STATEDIR
if [ -d "$STATEDIR/system/discovery" ]; then
cp -rfv /system/discovery/* $STATEDIR/system/discovery
fi
umount $STATEDIR
16 changes: 9 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (
)

type Install struct {
Auto bool `yaml:"auto,omitempty"`
Reboot bool `yaml:"reboot,omitempty"`
Device string `yaml:"device,omitempty"`
Poweroff bool `yaml:"poweroff,omitempty"`
GrubOptions map[string]string `yaml:"grub_options,omitempty"`
Bundles Bundles `yaml:"bundles,omitempty"`
Encrypt []string `yaml:"encrypted_partitions,omitempty"`
Auto bool `yaml:"auto,omitempty"`
Reboot bool `yaml:"reboot,omitempty"`
Device string `yaml:"device,omitempty"`
Poweroff bool `yaml:"poweroff,omitempty"`
GrubOptions map[string]string `yaml:"grub_options,omitempty"`
Bundles Bundles `yaml:"bundles,omitempty"`
Encrypt []string `yaml:"encrypted_partitions,omitempty"`
SkipEncryptCopyPlugins bool `yaml:"skip_copy_kcrypt_plugin,omitempty"`
Env []string `yaml:"env,omitempty"`
}

type Config struct {
Expand Down
4 changes: 3 additions & 1 deletion pkg/utils/sh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
)

func SH(c string) (string, error) {
o, err := exec.Command("/bin/sh", "-c", c).CombinedOutput()
cmd := exec.Command("/bin/sh", "-c", c)
cmd.Env = os.Environ()
o, err := cmd.CombinedOutput()
return string(o), err
}

Expand Down

0 comments on commit 9d5f7e3

Please sign in to comment.