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

🐛 Fix manual install not supporting configuration url #963

Merged
merged 3 commits into from
Mar 8, 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
51 changes: 38 additions & 13 deletions internal/agent/install.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package agent

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -63,28 +64,24 @@ func displayInfo(agentConfig *Config) {
}

func ManualInstall(c string, options map[string]string, strictValidations bool) error {
dat, err := os.ReadFile(c)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

dir, err := os.MkdirTemp("", "kairos-install")
source, err := prepareConfiguration(ctx, c)
if err != nil {
return err
}
defer os.RemoveAll(dir)

if err := os.WriteFile(filepath.Join(dir, "config.yaml"), dat, 0600); err != nil {
return err
}

cc, err := config.Scan(config.Directories(dir), config.MergeBootLine, config.StrictValidation(strictValidations))
cc, err := config.Scan(config.Directories(source), config.MergeBootLine, config.StrictValidation(strictValidations))
if err != nil {
return err
}

options["cc"] = cc.String()

if options["device"] == "" {
options["device"] = cc.Install.Device
}

return RunInstall(options)
}

Expand Down Expand Up @@ -325,3 +322,31 @@ func ensureDataSourceReady() {
}
}
}

func prepareConfiguration(ctx context.Context, source string) (string, error) {
// if the source is not an url it is already a configuration path
if u, err := url.Parse(source); err != nil || u.Scheme == "" {
return source, nil
}

// create a configuration file with the source referenced
f, err := os.CreateTemp(os.TempDir(), "kairos-install-*.yaml")
if err != nil {
return "", err
}

// defer cleanup until after parent is done
go func() {
<-ctx.Done()
_ = os.RemoveAll(f.Name())
}()

cfg := config.Config{
ConfigURL: source,
}
if err = yaml.NewEncoder(f).Encode(cfg); err != nil {
return "", err
}

return f.Name(), nil
}
54 changes: 54 additions & 0 deletions internal/agent/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package agent

import (
"context"
"github.com/kairos-io/kairos/pkg/config"
"gopkg.in/yaml.v3"
"os"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("prepareConfiguration", func() {
path := "/foo/bar"
url := "https://example.com"
ctx, cancel := context.WithCancel(context.Background())

It("returns a file path with no modifications", func() {
source, err := prepareConfiguration(ctx, path)

Expect(err).ToNot(HaveOccurred())
Expect(source).To(Equal(path))
})

It("creates a configuration file containing the given url", func() {
source, err := prepareConfiguration(ctx, url)

Expect(err).ToNot(HaveOccurred())
Expect(source).ToNot(Equal(path))

f, err := os.Open(source)
Expect(err).ToNot(HaveOccurred())

var cfg config.Config
err = yaml.NewDecoder(f).Decode(&cfg)
Expect(err).ToNot(HaveOccurred())

Expect(cfg.ConfigURL).To(Equal(url))
})

It("cleans up the configuration file after context is done", func() {
source, err := prepareConfiguration(ctx, url)
cancel()

_, err = os.Stat(source)
Expect(os.IsNotExist(err))
})
})

func TestPrepareConfiguration(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "prepareConfiguration Suite")
}