kairos-agent/internal/agent/upgrade.go

119 lines
2.7 KiB
Go
Raw Normal View History

package agent
import (
"context"
2022-08-18 13:12:05 +00:00
"encoding/json"
"fmt"
"os"
"os/exec"
2022-08-17 08:31:39 +00:00
"strings"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos/pkg/config"
2022-08-18 13:12:05 +00:00
"github.com/kairos-io/kairos/internal/bus"
"github.com/kairos-io/kairos/pkg/github"
"github.com/kairos-io/kairos/pkg/utils"
2022-08-18 13:12:05 +00:00
"github.com/mudler/go-pluggable"
)
2022-08-18 13:12:05 +00:00
func ListReleases() []string {
releases := []string{}
bus.Manager.Response(events.EventAvailableReleases, func(p *pluggable.Plugin, r *pluggable.EventResponse) {
if err := json.Unmarshal([]byte(r.Data), &releases); err != nil {
fmt.Printf("warn: failed unmarshalling data: '%s'\n", err.Error())
}
2022-08-18 13:12:05 +00:00
})
if _, err := bus.Manager.Publish(events.EventAvailableReleases, events.EventPayload{}); err != nil {
fmt.Printf("warn: failed publishing event: '%s'\n", err.Error())
}
2022-08-18 13:12:05 +00:00
if len(releases) == 0 {
githubRepo, err := utils.OSRelease("GITHUB_REPO")
if err != nil {
2022-08-18 13:12:05 +00:00
return releases
}
2022-08-18 13:12:05 +00:00
releases, _ = github.FindReleases(context.Background(), "", githubRepo)
}
return releases
}
sparkles: Integrate schema validation (#853) * Change ValidationError to return the actual error Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add validate command Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Warn validation errors when scanning configs Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add schema command to print config json schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add strict-validations flag Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint and remove focus Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Rename command schema to print-schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Fix issue by reading originalData Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Remove test from removed feature Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add comments to exported functions Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add test for validate.go Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Remove focus Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add more tests for root schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add more tests Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> --------- Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> Co-authored-by: Itxaka <itxaka.garcia@spectrocloud.com>
2023-02-14 15:15:13 +00:00
func Upgrade(version, image string, force, debug, strictValidations bool, dirs []string) error {
2022-08-18 13:12:05 +00:00
bus.Manager.Initialize()
if version == "" && image == "" {
releases := ListReleases()
if len(releases) == 0 {
return fmt.Errorf("no releases found")
}
version = releases[len(releases)-1]
msg := fmt.Sprintf("Latest release is %s\nAre you sure you want to upgrade to this release? (y/n)", version)
reply, err := promptBool(events.YAMLPrompt{Prompt: msg, Default: "y"})
if err != nil {
return err
}
if reply == "false" {
return nil
}
}
if utils.Version() == version && !force {
2022-08-18 13:12:05 +00:00
fmt.Println("version already installed. use --force to force upgrade")
return nil
}
2022-08-18 13:12:05 +00:00
discoveredImage := ""
bus.Manager.Response(events.EventVersionImage, func(p *pluggable.Plugin, r *pluggable.EventResponse) {
discoveredImage = r.Data
})
_, err := bus.Manager.Publish(events.EventVersionImage, &events.VersionImagePayload{
Version: version,
})
if err != nil {
return err
}
registry, err := utils.OSRelease("IMAGE_REPO")
if err != nil {
return err
}
2022-08-18 13:12:05 +00:00
img := fmt.Sprintf("%s:%s", registry, version)
2022-08-18 13:12:05 +00:00
if discoveredImage != "" {
img = discoveredImage
}
if image != "" {
img = image
}
2022-08-17 08:31:39 +00:00
if debug {
fmt.Printf("Upgrading to image: '%s'\n", img)
}
sparkles: Integrate schema validation (#853) * Change ValidationError to return the actual error Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add validate command Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Warn validation errors when scanning configs Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add schema command to print config json schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add strict-validations flag Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint and remove focus Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Rename command schema to print-schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Fix issue by reading originalData Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Remove test from removed feature Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add comments to exported functions Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add test for validate.go Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Remove focus Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add more tests for root schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add more tests Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> --------- Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> Co-authored-by: Itxaka <itxaka.garcia@spectrocloud.com>
2023-02-14 15:15:13 +00:00
c, err := config.Scan(config.Directories(dirs...), config.StrictValidation(strictValidations))
if err != nil {
return err
}
utils.SetEnv(c.Env)
args := []string{"upgrade", "--system.uri", fmt.Sprintf("docker:%s", img)}
2022-08-17 08:31:39 +00:00
if debug {
fmt.Printf("Running command: 'elemental %s'", strings.Join(args, " "))
}
cmd := exec.Command("elemental", args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
return cmd.Run()
}