Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load elemental config/spec from cloud config #82

Merged
merged 7 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Config struct {

func LoadConfig(path ...string) (*Config, error) {
if len(path) == 0 {
path = append(path, "/etc/kairos/agent.yaml", "/etc/elemental/config.yaml")
path = append(path, "/etc/kairos/agent.yaml")
}

cfg := &Config{}
Expand Down
4 changes: 2 additions & 2 deletions internal/agent/hooks/runstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

type RunStage struct{}

func (r RunStage) Run(_ config.Config) error {
cfg, err := elementalConfig.ReadConfigRun("/etc/elemental")
func (r RunStage) Run(c config.Config) error {
cfg, err := elementalConfig.ReadConfigRunFromAgentConfig(&c)
if err != nil {
cfg.Logger.Errorf("Error reading config: %s\n", err)
}
Expand Down
62 changes: 26 additions & 36 deletions internal/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"syscall"
"time"

events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/utils"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/internal/cmd"
Expand All @@ -23,6 +19,10 @@ import (
"github.com/kairos-io/kairos-agent/v2/pkg/elementalConfig"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
elementalUtils "github.com/kairos-io/kairos-agent/v2/pkg/utils"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/utils"
qr "github.com/mudler/go-nodepair/qrcode"
"github.com/mudler/go-pluggable"
"github.com/pterm/pterm"
Expand Down Expand Up @@ -96,14 +96,7 @@ func ManualInstall(c string, options map[string]string, strictValidations bool)
}
}
}

// Load the installation Config from the system
installConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil {
return err
}

return RunInstall(installConfig, options)
return RunInstall(options)
}

func Install(dir ...string) error {
Expand All @@ -130,12 +123,6 @@ func Install(dir ...string) error {

ensureDataSourceReady()

// Load the installation Config from the system
installConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil {
return err
}

// Reads config, and if present and offline is defined,
// runs the installation
cc, err := config.Scan(collector.Directories(dir...), collector.MergeBootLine, collector.NoLogs)
Expand All @@ -148,7 +135,7 @@ func Install(dir ...string) error {
r["device"] = cc.Install.Device
mergeOption(configStr, r)

err = RunInstall(installConfig, r)
err = RunInstall(r)
if err != nil {
return err
}
Expand Down Expand Up @@ -242,7 +229,7 @@ func Install(dir ...string) error {

pterm.Info.Println("Starting installation")

if err := RunInstall(installConfig, r); err != nil {
if err := RunInstall(r); err != nil {
return err
}

Expand All @@ -259,14 +246,7 @@ func Install(dir ...string) error {
return nil
}

func RunInstall(installConfig *v1.RunConfig, options map[string]string) error {
f, err := elementalUtils.TempFile(installConfig.Fs, "", "kairos-install-config-xxx.yaml")
if err != nil {
installConfig.Logger.Error("Error creating temporal file for install config: %s\n", err.Error())
return err
}
defer os.RemoveAll(f.Name())

func RunInstall(options map[string]string) error {
cloudInit, ok := options["cc"]
if !ok {
fmt.Println("cloudInit must be specified among options")
Expand All @@ -286,12 +266,6 @@ func RunInstall(installConfig *v1.RunConfig, options map[string]string) error {
env := append(c.Install.Env, c.Env...)
utils.SetEnv(env)

err = os.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
if err != nil {
fmt.Printf("could not write cloud init to %s: %s\n", f.Name(), err.Error())
return err
}

_, reboot := options["reboot"]
_, poweroff := options["poweroff"]
if poweroff {
Expand All @@ -301,8 +275,24 @@ func RunInstall(installConfig *v1.RunConfig, options map[string]string) error {
c.Install.Reboot = true
}

// Generate the installation spec
installSpec, _ := elementalConfig.ReadInstallSpec(installConfig)
// Load the installation Config from the system
mauromorales marked this conversation as resolved.
Show resolved Hide resolved
installConfig, installSpec, err := elementalConfig.ReadInstallConfigFromAgentConfig(c)
if err != nil {
return err
}

f, err := elementalUtils.TempFile(installConfig.Fs, "", "kairos-install-config-xxx.yaml")
if err != nil {
installConfig.Logger.Error("Error creating temporal file for install config: %s\n", err.Error())
return err
}
defer os.RemoveAll(f.Name())

err = os.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
if err != nil {
fmt.Printf("could not write cloud init to %s: %s\n", f.Name(), err.Error())
return err
}

installSpec.NoFormat = c.Install.NoFormat

Expand Down
26 changes: 1 addition & 25 deletions internal/agent/install_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package agent

import (
"bytes"
"context"
"fmt"
"github.com/jaypipes/ghw/pkg/block"
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
conf "github.com/kairos-io/kairos-agent/v2/pkg/elementalConfig"
"github.com/kairos-io/kairos-agent/v2/pkg/utils"
"os"
"path/filepath"

"github.com/kairos-io/kairos-agent/v2/pkg/config"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
v1mock "github.com/kairos-io/kairos-agent/v2/tests/mocks"
"github.com/twpayne/go-vfs"
"github.com/twpayne/go-vfs/vfst"
Expand Down Expand Up @@ -66,27 +63,17 @@ var _ = Describe("prepareConfiguration", func() {
})

var _ = Describe("RunInstall", func() {
var installConfig *v1.RunConfig
var options map[string]string
var err error
var fs vfs.FS
var cloudInit *v1mock.FakeCloudInitRunner
var cleanup func()
var memLog *bytes.Buffer
var ghwTest v1mock.GhwMock
var cmdline func() ([]byte, error)

BeforeEach(func() {
// Default mock objects
runner := v1mock.NewFakeRunner()
syscall := &v1mock.FakeSyscall{}
mounter := v1mock.NewErrorMounter()
memLog = &bytes.Buffer{}
logger := v1.NewBufferLogger(memLog)
logger = v1.NewLogger()
extractor := v1mock.NewFakeImageExtractor(logger)
//logger.SetLevel(v1.DebugLevel())
cloudInit = &v1mock.FakeCloudInitRunner{}
// Set default cmdline function so we dont panic :o
cmdline = func() ([]byte, error) {
return []byte{}, nil
Expand All @@ -105,17 +92,6 @@ var _ = Describe("RunInstall", func() {
_, err = fs.Create(grubCfg)
Expect(err).To(BeNil())

// Create new runconfig with all mocked objects
installConfig = conf.NewRunConfig(
conf.WithFs(fs),
conf.WithRunner(runner),
conf.WithLogger(logger),
conf.WithMounter(mounter),
conf.WithSyscall(syscall),
conf.WithCloudInitRunner(cloudInit),
conf.WithImageExtractor(extractor),
)

// Side effect of runners, hijack calls to commands and return our stuff
partNum := 0
partedOut := printOutput
Expand Down Expand Up @@ -225,7 +201,7 @@ install:

It("runs the install", func() {
Skip("Not ready yet")
err = RunInstall(installConfig, options)
err = RunInstall(options)
Expect(err).ToNot(HaveOccurred())
})
})
14 changes: 1 addition & 13 deletions internal/agent/interactive_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/internal/cmd"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-agent/v2/pkg/elementalConfig"

events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/unstructured"

Expand All @@ -19,7 +17,6 @@ import (
"github.com/mudler/go-pluggable"
"github.com/mudler/yip/pkg/schema"
"github.com/pterm/pterm"
"github.com/spf13/viper"
)

const (
Expand Down Expand Up @@ -277,16 +274,7 @@ func InteractiveInstall(debug, spawnShell bool) error {
pterm.Info.Println("Starting installation")
pterm.Info.Println(finalCloudConfig)

// Set debug from here already, so it's loaded by the ReadConfigRun
viper.Set("debug", debug)

// Load the installation Config from the system
installConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil {
return err
}

err = RunInstall(installConfig, map[string]string{
err = RunInstall(map[string]string{
"device": device,
"cc": finalCloudConfig,
})
Expand Down
25 changes: 8 additions & 17 deletions internal/agent/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ package agent
import (
"encoding/json"
"fmt"
"github.com/sanity-io/litter"
"github.com/sirupsen/logrus"
"os"
"sync"
"time"

sdk "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/utils"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/internal/cmd"
"github.com/kairos-io/kairos-agent/v2/pkg/action"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-agent/v2/pkg/elementalConfig"
sdk "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/utils"

"github.com/mudler/go-pluggable"
"github.com/pterm/pterm"
)

func Reset(debug bool, dir ...string) error {
func Reset(dir ...string) error {
// TODO: Enable args? No args for now so no possibility of reset persistent or overriding the source for the reset
// Nor the auto-reboot via cmd?
// This comment pertains calling reset via cmdline when wanting to override configs
Expand All @@ -43,7 +41,6 @@ func Reset(debug bool, dir ...string) error {

// This loads yet another config ¬_¬
// TODO: merge this somehow with the rest so there is no 5 places to configure stuff?
// Also this reads the elemental config.yaml
agentConfig, err := LoadConfig()
if err != nil {
return err
Expand Down Expand Up @@ -83,18 +80,12 @@ func Reset(debug bool, dir ...string) error {

utils.SetEnv(c.Env)

resetConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil {
return err
}
if debug {
resetConfig.Logger.SetLevel(logrus.DebugLevel)
}
resetConfig.Logger.Debugf("Full config: %s\n", litter.Sdump(resetConfig))
resetSpec, err := elementalConfig.ReadResetSpec(resetConfig)
// Load the installation Config from the cloud-config data
resetConfig, resetSpec, err := elementalConfig.ReadResetConfigFromAgentConfig(c)
if err != nil {
return err
}

// Not even sure what opts can come from here to be honest. Where is the struct that supports this options?
// Where is the docs to support this? This is generic af and not easily identifiable
if len(options) == 0 {
Expand Down
25 changes: 6 additions & 19 deletions internal/agent/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import (
"fmt"

"github.com/Masterminds/semver/v3"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/utils"
"github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/pkg/action"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-agent/v2/pkg/elementalConfig"
"github.com/kairos-io/kairos-agent/v2/pkg/github"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/utils"
"github.com/mudler/go-pluggable"
"github.com/sanity-io/litter"
log "github.com/sirupsen/logrus"
)

func ListReleases(includePrereleases bool) semver.Collection {
Expand Down Expand Up @@ -49,7 +47,7 @@ func ListReleases(includePrereleases bool) semver.Collection {
}

func Upgrade(
version, source string, force, debug, strictValidations bool, dirs []string, preReleases bool) error {
version, source string, force, strictValidations bool, dirs []string, preReleases bool) error {
bus.Manager.Initialize()

if version == "" && source == "" {
Expand Down Expand Up @@ -90,10 +88,6 @@ func Upgrade(
}
}

if debug {
fmt.Printf("Upgrading to source: '%s'\n", img)
}

c, err := config.Scan(collector.Directories(dirs...), collector.StrictValidation(strictValidations))
if err != nil {
return err
Expand All @@ -102,26 +96,19 @@ func Upgrade(
utils.SetEnv(c.Env)

// Load the upgrade Config from the system
upgradeConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
upgradeConfig, upgradeSpec, err := elementalConfig.ReadUpgradeConfigFromAgentConfig(c)
if err != nil {
return err
}
if debug {
upgradeConfig.Logger.SetLevel(log.DebugLevel)
}

upgradeConfig.Logger.Debugf("Full config: %s\n", litter.Sdump(upgradeConfig))

// Generate the upgrade spec
upgradeSpec, _ := elementalConfig.ReadUpgradeSpec(upgradeConfig)
// Add the image source
imgSource, err := v1.NewSrcFromURI(img)
if err != nil {
return err
}
upgradeSpec.Active.Source = imgSource

// Sanitize (this is not required but good to do
// Sanitize
err = upgradeSpec.Sanitize()
if err != nil {
return err
Expand Down
Loading