2022-07-18 22:02:49 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-18 13:12:05 +00:00
|
|
|
"encoding/json"
|
2022-07-18 22:02:49 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-08-17 08:31:39 +00:00
|
|
|
"strings"
|
2022-07-18 22:02:49 +00:00
|
|
|
|
2023-04-20 07:57:58 +00:00
|
|
|
"github.com/Masterminds/semver/v3"
|
2023-03-15 14:45:00 +00:00
|
|
|
events "github.com/kairos-io/kairos-sdk/bus"
|
2023-03-18 09:27:18 +00:00
|
|
|
"github.com/kairos-io/kairos-sdk/utils"
|
2023-03-30 11:18:53 +00:00
|
|
|
"github.com/kairos-io/kairos/v2/internal/bus"
|
2023-04-20 07:57:58 +00:00
|
|
|
"github.com/kairos-io/kairos/v2/pkg/config"
|
|
|
|
"github.com/kairos-io/kairos/v2/pkg/config/collector"
|
2023-03-30 11:18:53 +00:00
|
|
|
"github.com/kairos-io/kairos/v2/pkg/github"
|
2022-08-18 13:12:05 +00:00
|
|
|
"github.com/mudler/go-pluggable"
|
2022-07-18 22:02:49 +00:00
|
|
|
)
|
|
|
|
|
2023-04-20 07:57:58 +00:00
|
|
|
func ListReleases() semver.Collection {
|
|
|
|
var releases semver.Collection
|
2022-08-18 13:12:05 +00:00
|
|
|
|
|
|
|
bus.Manager.Response(events.EventAvailableReleases, func(p *pluggable.Plugin, r *pluggable.EventResponse) {
|
2022-08-18 15:19:15 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2022-08-18 15:19:15 +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 {
|
2022-07-18 22:02:49 +00:00
|
|
|
githubRepo, err := utils.OSRelease("GITHUB_REPO")
|
|
|
|
if err != nil {
|
2022-08-18 13:12:05 +00:00
|
|
|
return releases
|
2022-07-18 22:02:49 +00:00
|
|
|
}
|
2022-08-18 13:12:05 +00:00
|
|
|
releases, _ = github.FindReleases(context.Background(), "", githubRepo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return releases
|
|
|
|
}
|
|
|
|
|
2023-03-24 13:00:33 +00:00
|
|
|
func Upgrade(
|
|
|
|
version, image string, force, debug, strictValidations bool, dirs []string,
|
|
|
|
authUser string, authPass string, authServer string, authType string, registryToken string, identityToken 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")
|
|
|
|
}
|
|
|
|
|
2023-04-20 07:57:58 +00:00
|
|
|
// Using Original here because the parsing removes the v as its a semver. But it stores the original full version there
|
|
|
|
version = releases[0].Original()
|
|
|
|
|
|
|
|
if utils.Version() == version && !force {
|
|
|
|
fmt.Printf("version %s already installed. use --force to force upgrade\n", version)
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-07 18:28:33 +00:00
|
|
|
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
|
|
|
|
}
|
2022-07-18 22:02:49 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-18 22:02:49 +00:00
|
|
|
registry, err := utils.OSRelease("IMAGE_REPO")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-18 13:12:05 +00:00
|
|
|
|
2022-08-17 08:02:18 +00:00
|
|
|
img := fmt.Sprintf("%s:%s", registry, version)
|
2022-08-18 13:12:05 +00:00
|
|
|
if discoveredImage != "" {
|
|
|
|
img = discoveredImage
|
|
|
|
}
|
2022-07-18 22:02:49 +00:00
|
|
|
if image != "" {
|
|
|
|
img = image
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:31:39 +00:00
|
|
|
if debug {
|
|
|
|
fmt.Printf("Upgrading to image: '%s'\n", img)
|
|
|
|
}
|
|
|
|
|
2023-03-29 14:25:38 +00:00
|
|
|
c, err := config.Scan(collector.Directories(dirs...), collector.StrictValidation(strictValidations))
|
2022-10-24 11:13:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.SetEnv(c.Env)
|
|
|
|
|
2022-07-18 22:02:49 +00:00
|
|
|
args := []string{"upgrade", "--system.uri", fmt.Sprintf("docker:%s", img)}
|
2023-03-24 13:00:33 +00:00
|
|
|
args = append(args,
|
|
|
|
"--auth-username", authUser,
|
|
|
|
"--auth-password", authPass,
|
|
|
|
"--auth-server-address", authServer,
|
|
|
|
"--auth-type", authType,
|
|
|
|
"--auth-registry-token", registryToken,
|
|
|
|
"--auth-identity-token", identityToken,
|
|
|
|
)
|
2022-08-17 08:31:39 +00:00
|
|
|
|
|
|
|
if debug {
|
|
|
|
fmt.Printf("Running command: 'elemental %s'", strings.Join(args, " "))
|
|
|
|
}
|
|
|
|
|
2022-07-18 22:02:49 +00:00
|
|
|
cmd := exec.Command("elemental", args...)
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
|
|
|
}
|