mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-09-04 02:24:42 +00:00
[WIP] Try to pass command line options for upgrade as kairos config
because it's actually configuration and it's better than using viper to pass things around. The `upgrade:` block seems to be ignored early in the process so even if the user specifies an image there, the latest release will be looked up. Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
@@ -5,8 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"github.com/spf13/viper"
|
|
||||||
|
|
||||||
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
|
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
|
||||||
|
|
||||||
@@ -55,6 +54,10 @@ func Upgrade(
|
|||||||
version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error {
|
version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error {
|
||||||
bus.Manager.Initialize()
|
bus.Manager.Initialize()
|
||||||
|
|
||||||
|
// TODO: Before we check for empy source,
|
||||||
|
// shouldn't we read the `upgrade:` block from the config and check if something
|
||||||
|
// is defined there?
|
||||||
|
|
||||||
if version == "" && source == "" {
|
if version == "" && source == "" {
|
||||||
fmt.Println("Searching for releases")
|
fmt.Println("Searching for releases")
|
||||||
if preReleases {
|
if preReleases {
|
||||||
@@ -93,13 +96,11 @@ func Upgrade(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set this here with viper help so we can use it while creating the upgrade spec
|
upgradeConf := generateUpgradeConf(img, upgradeRecovery)
|
||||||
// And its properly set since creation without having to modify it later
|
|
||||||
// This should be binded somehow but the current cli doesnt allow us to bind flags to values
|
|
||||||
viper.Set("upgradeSource", img)
|
|
||||||
viper.Set("upgradeRecovery", upgradeRecovery)
|
|
||||||
|
|
||||||
c, err := config.Scan(collector.Directories(dirs...), collector.StrictValidation(strictValidations))
|
c, err := config.Scan(collector.Directories(dirs...),
|
||||||
|
collector.Readers(strings.NewReader(upgradeConf)),
|
||||||
|
collector.StrictValidation(strictValidations))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -162,3 +163,48 @@ func determineUpgradeImage(version string) (string, error) {
|
|||||||
|
|
||||||
return fmt.Sprintf("%s:%s", registry, version), nil
|
return fmt.Sprintf("%s:%s", registry, version), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generateUpgradeConf creates a kairos configuration for upgrade to be
|
||||||
|
// added to the rest of the configurations.
|
||||||
|
func generateUpgradeConf(source string, upgradeRecovery bool) string {
|
||||||
|
conf := ""
|
||||||
|
|
||||||
|
if source == "" {
|
||||||
|
return conf
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Do the same for recovery upgrade
|
||||||
|
conf = fmt.Sprintf(`
|
||||||
|
upgrade:
|
||||||
|
recovery-system:
|
||||||
|
uri: %s`, source)
|
||||||
|
|
||||||
|
// source := viper.GetString("upgradeSource")
|
||||||
|
// recoveryUpgrade := viper.GetBool("upgradeRecovery")
|
||||||
|
// if source != "" {
|
||||||
|
// imgSource, err := v1.NewSrcFromURI(source)
|
||||||
|
// // TODO: Don't hide the error here!
|
||||||
|
// if err == nil {
|
||||||
|
// if recoveryUpgrade {
|
||||||
|
// spec.RecoveryUpgrade = recoveryUpgrade
|
||||||
|
// spec.Recovery.Source = imgSource
|
||||||
|
// } else {
|
||||||
|
// spec.Active.Source = imgSource
|
||||||
|
// }
|
||||||
|
// size, err := GetSourceSize(cfg, imgSource)
|
||||||
|
// if err != nil {
|
||||||
|
// cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
|
||||||
|
// } else {
|
||||||
|
// cfg.Logger.Infof("Setting image size to %dMb", size)
|
||||||
|
// // On upgrade only the active or recovery will be upgraded, so we dont need to override passive
|
||||||
|
// if recoveryUpgrade {
|
||||||
|
// spec.Recovery.Size = uint(size)
|
||||||
|
// } else {
|
||||||
|
// spec.Active.Size = uint(size)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
return conf
|
||||||
|
}
|
||||||
|
@@ -27,7 +27,7 @@ import (
|
|||||||
"github.com/kairos-io/kairos-agent/v2/internal/common"
|
"github.com/kairos-io/kairos-agent/v2/internal/common"
|
||||||
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
|
||||||
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
|
||||||
"github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
|
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
|
||||||
"github.com/kairos-io/kairos-agent/v2/pkg/utils/partitions"
|
"github.com/kairos-io/kairos-agent/v2/pkg/utils/partitions"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
"github.com/sanity-io/litter"
|
"github.com/sanity-io/litter"
|
||||||
@@ -265,31 +265,32 @@ func NewUpgradeSpec(cfg *Config) (*v1.UpgradeSpec, error) {
|
|||||||
State: installState,
|
State: installState,
|
||||||
}
|
}
|
||||||
|
|
||||||
source := viper.GetString("upgradeSource")
|
// source := viper.GetString("upgradeSource")
|
||||||
recoveryUpgrade := viper.GetBool("upgradeRecovery")
|
// recoveryUpgrade := viper.GetBool("upgradeRecovery")
|
||||||
if source != "" {
|
// if source != "" {
|
||||||
imgSource, err := v1.NewSrcFromURI(source)
|
// imgSource, err := v1.NewSrcFromURI(source)
|
||||||
if err == nil {
|
// // TODO: Don't hide the error here!
|
||||||
if recoveryUpgrade {
|
// if err == nil {
|
||||||
spec.RecoveryUpgrade = recoveryUpgrade
|
// if recoveryUpgrade {
|
||||||
spec.Recovery.Source = imgSource
|
// spec.RecoveryUpgrade = recoveryUpgrade
|
||||||
} else {
|
// spec.Recovery.Source = imgSource
|
||||||
spec.Active.Source = imgSource
|
// } else {
|
||||||
}
|
// spec.Active.Source = imgSource
|
||||||
size, err := GetSourceSize(cfg, imgSource)
|
// }
|
||||||
if err != nil {
|
// size, err := GetSourceSize(cfg, imgSource)
|
||||||
cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
|
// if err != nil {
|
||||||
} else {
|
// cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
|
||||||
cfg.Logger.Infof("Setting image size to %dMb", size)
|
// } else {
|
||||||
// On upgrade only the active or recovery will be upgraded, so we dont need to override passive
|
// cfg.Logger.Infof("Setting image size to %dMb", size)
|
||||||
if recoveryUpgrade {
|
// // On upgrade only the active or recovery will be upgraded, so we dont need to override passive
|
||||||
spec.Recovery.Size = uint(size)
|
// if recoveryUpgrade {
|
||||||
} else {
|
// spec.Recovery.Size = uint(size)
|
||||||
spec.Active.Size = uint(size)
|
// } else {
|
||||||
}
|
// spec.Active.Size = uint(size)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
return spec, nil
|
return spec, nil
|
||||||
}
|
}
|
||||||
@@ -543,6 +544,8 @@ func ReadUpgradeSpecFromConfig(c *Config) (*v1.UpgradeSpec, error) {
|
|||||||
|
|
||||||
// ReadSpecFromCloudConfig returns a v1.Spec for the given spec
|
// ReadSpecFromCloudConfig returns a v1.Spec for the given spec
|
||||||
func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
|
func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
|
||||||
|
fmt.Printf("litter.Sdump(r) before = %+v\n", litter.Sdump(r))
|
||||||
|
|
||||||
var sp v1.Spec
|
var sp v1.Spec
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@@ -560,6 +563,8 @@ func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
|
|||||||
return nil, fmt.Errorf("failed initializing spec: %v", err)
|
return nil, fmt.Errorf("failed initializing spec: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("litter.Sdump(sp) before = %+v\n", litter.Sdump(sp))
|
||||||
|
|
||||||
// Load the config into viper from the raw cloud config string
|
// Load the config into viper from the raw cloud config string
|
||||||
ccString, err := r.String()
|
ccString, err := r.String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -576,6 +581,11 @@ func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
r.Logger.Warnf("error unmarshalling %s Spec: %s", spec, err)
|
r.Logger.Warnf("error unmarshalling %s Spec: %s", spec, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("---------------------------------")
|
||||||
|
fmt.Printf("litter.Sdump(sp) after = %+v\n", litter.Sdump(sp))
|
||||||
|
fmt.Println("---------------------------------")
|
||||||
|
|
||||||
err = sp.Sanitize()
|
err = sp.Sanitize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Logger.Warnf("Error sanitizing the % spec: %s", spec, err)
|
r.Logger.Warnf("Error sanitizing the % spec: %s", spec, err)
|
||||||
|
@@ -35,31 +35,31 @@ const (
|
|||||||
|
|
||||||
// ImageSource represents the source from where an image is created for easy identification
|
// ImageSource represents the source from where an image is created for easy identification
|
||||||
type ImageSource struct {
|
type ImageSource struct {
|
||||||
source string
|
Source string
|
||||||
srcType string
|
SrcType string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ImageSource) Value() string {
|
func (i ImageSource) Value() string {
|
||||||
return i.source
|
return i.Source
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ImageSource) IsDocker() bool {
|
func (i ImageSource) IsDocker() bool {
|
||||||
return i.srcType == oci
|
return i.SrcType == oci
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ImageSource) IsDir() bool {
|
func (i ImageSource) IsDir() bool {
|
||||||
return i.srcType == dir
|
return i.SrcType == dir
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ImageSource) IsFile() bool {
|
func (i ImageSource) IsFile() bool {
|
||||||
return i.srcType == file
|
return i.SrcType == file
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ImageSource) IsEmpty() bool {
|
func (i ImageSource) IsEmpty() bool {
|
||||||
if i.srcType == "" {
|
if i.SrcType == "" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if i.source == "" {
|
if i.Source == "" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -69,7 +69,7 @@ func (i ImageSource) String() string {
|
|||||||
if i.IsEmpty() {
|
if i.IsEmpty() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s://%s", i.srcType, i.source)
|
return fmt.Sprintf("%s://%s", i.SrcType, i.Source)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ImageSource) MarshalYAML() (interface{}, error) {
|
func (i ImageSource) MarshalYAML() (interface{}, error) {
|
||||||
@@ -103,11 +103,11 @@ func (i *ImageSource) updateFromURI(uri string) error {
|
|||||||
case oci, docker:
|
case oci, docker:
|
||||||
return i.parseImageReference(value)
|
return i.parseImageReference(value)
|
||||||
case dir:
|
case dir:
|
||||||
i.srcType = dir
|
i.SrcType = dir
|
||||||
i.source = value
|
i.Source = value
|
||||||
case file:
|
case file:
|
||||||
i.srcType = file
|
i.SrcType = file
|
||||||
i.source = value
|
i.Source = value
|
||||||
default:
|
default:
|
||||||
return i.parseImageReference(uri)
|
return i.parseImageReference(uri)
|
||||||
}
|
}
|
||||||
@@ -121,8 +121,8 @@ func (i *ImageSource) parseImageReference(ref string) error {
|
|||||||
} else if reference.IsNameOnly(n) {
|
} else if reference.IsNameOnly(n) {
|
||||||
ref += ":latest"
|
ref += ":latest"
|
||||||
}
|
}
|
||||||
i.srcType = oci
|
i.SrcType = oci
|
||||||
i.source = ref
|
i.Source = ref
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,13 +137,13 @@ func NewEmptySrc() *ImageSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerSrc(src string) *ImageSource {
|
func NewDockerSrc(src string) *ImageSource {
|
||||||
return &ImageSource{source: src, srcType: oci}
|
return &ImageSource{Source: src, SrcType: oci}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileSrc(src string) *ImageSource {
|
func NewFileSrc(src string) *ImageSource {
|
||||||
return &ImageSource{source: src, srcType: file}
|
return &ImageSource{Source: src, SrcType: file}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDirSrc(src string) *ImageSource {
|
func NewDirSrc(src string) *ImageSource {
|
||||||
return &ImageSource{source: src, srcType: dir}
|
return &ImageSource{Source: src, SrcType: dir}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user