mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-07-18 18:21:07 +00:00
Rework prepareConfiguration (#289)
This commit is contained in:
parent
5a983f6ae1
commit
59777eeb3e
@ -1,10 +1,11 @@
|
|||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -28,7 +29,6 @@ import (
|
|||||||
qr "github.com/mudler/go-nodepair/qrcode"
|
qr "github.com/mudler/go-nodepair/qrcode"
|
||||||
"github.com/mudler/go-pluggable"
|
"github.com/mudler/go-pluggable"
|
||||||
"github.com/pterm/pterm"
|
"github.com/pterm/pterm"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func displayInfo(agentConfig *Config) {
|
func displayInfo(agentConfig *Config) {
|
||||||
@ -54,10 +54,7 @@ func displayInfo(agentConfig *Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ManualInstall(c, sourceImgURL, device string, reboot, poweroff, strictValidations bool) error {
|
func ManualInstall(c, sourceImgURL, device string, reboot, poweroff, strictValidations bool) error {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
configSource, err := prepareConfiguration(c)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
configSource, err := prepareConfiguration(ctx, c)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -65,8 +62,8 @@ func ManualInstall(c, sourceImgURL, device string, reboot, poweroff, strictValid
|
|||||||
cliConf := generateInstallConfForCLIArgs(sourceImgURL)
|
cliConf := generateInstallConfForCLIArgs(sourceImgURL)
|
||||||
cliConfManualArgs := generateInstallConfForManualCLIArgs(device, reboot, poweroff)
|
cliConfManualArgs := generateInstallConfForManualCLIArgs(device, reboot, poweroff)
|
||||||
|
|
||||||
cc, err := config.Scan(collector.Directories(configSource),
|
cc, err := config.Scan(
|
||||||
collector.Readers(strings.NewReader(cliConf), strings.NewReader(cliConfManualArgs)),
|
collector.Readers(configSource, strings.NewReader(cliConf), strings.NewReader(cliConfManualArgs)),
|
||||||
collector.MergeBootLine,
|
collector.MergeBootLine,
|
||||||
collector.StrictValidation(strictValidations), collector.NoLogs)
|
collector.StrictValidation(strictValidations), collector.NoLogs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -324,32 +321,26 @@ func ensureDataSourceReady() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareConfiguration(ctx context.Context, source string) (string, error) {
|
func prepareConfiguration(source string) (io.Reader, error) {
|
||||||
|
var cfg io.Reader
|
||||||
|
// source can be either a file in the system or an url
|
||||||
|
// We need to differentiate between the two
|
||||||
|
// If its a local file, we just read it and return it
|
||||||
|
// If its a url, we need to create a configuration with the url and let the config.Scan handle it
|
||||||
// if the source is not an url it is already a configuration path
|
// if the source is not an url it is already a configuration path
|
||||||
if u, err := url.Parse(source); err != nil || u.Scheme == "" {
|
if u, err := url.Parse(source); err != nil || u.Scheme == "" {
|
||||||
return source, nil
|
file, err := os.ReadFile(source)
|
||||||
}
|
|
||||||
|
|
||||||
// create a configuration file with the source referenced
|
|
||||||
f, err := os.CreateTemp(os.TempDir(), "kairos-install-*.yaml")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return cfg, err
|
||||||
|
}
|
||||||
|
cfg = bytes.NewReader(file)
|
||||||
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// defer cleanup until after parent is done
|
cfgUrl := fmt.Sprintf(`config_url: %s`, source)
|
||||||
go func() {
|
cfg = strings.NewReader(cfgUrl)
|
||||||
<-ctx.Done()
|
|
||||||
_ = os.RemoveAll(f.Name())
|
|
||||||
}()
|
|
||||||
|
|
||||||
cfg := config.Config{
|
return cfg, nil
|
||||||
ConfigURL: source,
|
|
||||||
}
|
|
||||||
if err = yaml.NewEncoder(f).Encode(cfg); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return f.Name(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateInstallConfForCLIArgs(sourceImageURL string) string {
|
func generateInstallConfForCLIArgs(sourceImageURL string) string {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
||||||
"os"
|
"os"
|
||||||
@ -26,41 +25,43 @@ const partTmpl = `
|
|||||||
%d:%ss:%ss:2048s:ext4::type=83;`
|
%d:%ss:%ss:2048s:ext4::type=83;`
|
||||||
|
|
||||||
var _ = Describe("prepareConfiguration", func() {
|
var _ = Describe("prepareConfiguration", func() {
|
||||||
path := "/foo/bar"
|
|
||||||
url := "https://example.com"
|
url := "https://example.com"
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
|
|
||||||
It("returns a file path with no modifications", func() {
|
|
||||||
source, err := prepareConfiguration(ctx, path)
|
|
||||||
|
|
||||||
|
It("loads the content from a file path", func() {
|
||||||
|
temp, err := os.MkdirTemp("", "")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(source).To(Equal(path))
|
defer os.RemoveAll(temp)
|
||||||
|
|
||||||
|
content, err := yaml.Marshal(config.Config{
|
||||||
|
Debug: true,
|
||||||
|
Install: &config.Install{
|
||||||
|
Device: "fake",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
It("creates a configuration file containing the given url", func() {
|
|
||||||
source, err := prepareConfiguration(ctx, url)
|
|
||||||
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(source).ToNot(Equal(path))
|
err = os.WriteFile(filepath.Join(temp, "config.yaml"), content, 0644)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
f, err := os.Open(source)
|
source, err := prepareConfiguration(filepath.Join(temp, "config.yaml"))
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
var cfg config.Config
|
var cfg config.Config
|
||||||
err = yaml.NewDecoder(f).Decode(&cfg)
|
err = yaml.NewDecoder(source).Decode(&cfg)
|
||||||
|
Expect(cfg.ConfigURL).To(BeEmpty())
|
||||||
|
Expect(cfg.Debug).To(BeTrue())
|
||||||
|
Expect(cfg.Install.Device).To(Equal("fake"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("creates a configuration file containing the given url", func() {
|
||||||
|
source, err := prepareConfiguration(url)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
var cfg config.Config
|
||||||
|
err = yaml.NewDecoder(source).Decode(&cfg)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
Expect(cfg.ConfigURL).To(Equal(url))
|
Expect(cfg.ConfigURL).To(Equal(url))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("cleans up the configuration file after context is done", func() {
|
|
||||||
source, err := prepareConfiguration(ctx, url)
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
_, err = os.Stat(source)
|
|
||||||
Expect(os.IsNotExist(err))
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
var _ = Describe("RunInstall", func() {
|
var _ = Describe("RunInstall", func() {
|
||||||
|
Loading…
Reference in New Issue
Block a user