Skip to content

Commit

Permalink
cli: Add exit-codes to upgrade cmd (rancher#371)
Browse files Browse the repository at this point in the history
cli: Add exit-codes to upgrade cmd

Uses the new elemental error to add exit-codes for error paths in the
upgrade command and upgrade action.

Fixes rancher#370

Signed-off-by: Fredrik Lönnegren <[email protected]>
  • Loading branch information
frelon authored Nov 14, 2022
1 parent bc06830 commit 3b2e929
Show file tree
Hide file tree
Showing 17 changed files with 515 additions and 214 deletions.
31 changes: 19 additions & 12 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/sanity-io/litter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"k8s.io/mount-utils"
Expand Down Expand Up @@ -142,15 +141,15 @@ func ReadConfigRun(configDir string, flags *pflag.FlagSet, mounter mount.Interfa

configLogger(cfg.Logger, cfg.Fs)

// TODO: is this really needed? It feels quite wrong, shouldn't it be loaded
// as regular environment variables?
// IMHO loading os-release as env variables should be sufficient here
cfgDefault := []string{"/etc/os-release"}
for _, c := range cfgDefault {
if exists, _ := utils.Exists(cfg.Fs, c); exists {
viper.SetConfigFile(c)
viper.SetConfigType("env")
cobra.CheckErr(viper.MergeInConfig())
const cfgDefault = "/etc/os-release"
if exists, _ := utils.Exists(cfg.Fs, cfgDefault); exists {
viper.SetConfigFile(cfgDefault)
viper.SetConfigType("env")

err := viper.MergeInConfig()
if err != nil {
cfg.Logger.Warnf("error merging os-release file: %s", err)
return cfg, err
}
}

Expand All @@ -163,21 +162,28 @@ func ReadConfigRun(configDir string, flags *pflag.FlagSet, mounter mount.Interfa
err := viper.MergeInConfig()
if err != nil {
cfg.Logger.Warnf("error merging config files: %s", err)
return cfg, err
}
}

// Load extra config files on configdir/config.d/ so we can override config values
cfgExtra := fmt.Sprintf("%s/config.d/", strings.TrimSuffix(configDir, "/"))
if exists, _ := utils.Exists(cfg.Fs, cfgExtra); exists {
viper.AddConfigPath(cfgExtra)
_ = filepath.WalkDir(cfgExtra, func(path string, d fs.DirEntry, err error) error {
err := filepath.WalkDir(cfgExtra, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && filepath.Ext(d.Name()) == ".yaml" {
viper.SetConfigType("yaml")
viper.SetConfigName(strings.TrimSuffix(d.Name(), ".yaml"))
cobra.CheckErr(viper.MergeInConfig())

return viper.MergeInConfig()
}
return nil
})

if err != nil {
cfg.Logger.Warnf("error merging extra config files: %s", err)
return cfg, err
}
}

// Bind runconfig flags
Expand Down Expand Up @@ -256,6 +262,7 @@ func ReadUpgradeSpec(r *v1.RunConfig, flags *pflag.FlagSet) (*v1.UpgradeSpec, er
err = vp.Unmarshal(upgrade, setDecoder, decodeHook)
if err != nil {
r.Logger.Warnf("error unmarshalling UpgradeSpec: %s", err)
return nil, err
}
err = upgrade.Sanitize()
r.Logger.Debugf("Loaded upgrade UpgradeSpec: %s", litter.Sdump(upgrade))
Expand Down
9 changes: 6 additions & 3 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/rancher/elemental-cli/cmd/config"
"github.com/rancher/elemental-cli/pkg/action"
elementalError "github.com/rancher/elemental-cli/pkg/error"
)

// NewUpgradeCmd returns a new instance of the upgrade subcommand and appends it to
Expand Down Expand Up @@ -52,10 +53,12 @@ func NewUpgradeCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command {
cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), mounter)
if err != nil {
cfg.Logger.Errorf("Error reading config: %s\n", err)
return elementalError.NewFromError(err, elementalError.ReadingRunConfig)
}

if err := validateInstallUpgradeFlags(cfg.Logger, cmd.Flags()); err != nil {
return err
cfg.Logger.Errorf("Error reading install/upgrade flags: %s\n", err)
return elementalError.NewFromError(err, elementalError.ReadingInstallUpgradeFlags)
}

// Adapt 'docker-image' and 'directory' deprecated flags to 'system' syntax
Expand All @@ -67,8 +70,8 @@ func NewUpgradeCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command {

spec, err := config.ReadUpgradeSpec(cfg, cmd.Flags())
if err != nil {
cfg.Logger.Errorf("invalid upgrade command setup %v", err)
return err
cfg.Logger.Errorf("Invalid upgrade command setup %v", err)
return elementalError.NewFromError(err, elementalError.ReadingRunConfig)
}

cfg.Logger.Infof("Upgrade called")
Expand Down
17 changes: 17 additions & 0 deletions docs/elemental_exit-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,21 @@
| 28 | Error running stat on a file|
| 29 | Error creating a tar archive|
| 30 | Error truncating a file|
| 31 | Error reading the run config|
| 32 | Error reading the install/upgrade flags|
| 33 | Error reading the upgrade config|
| 34 | Error mounting state partition|
| 35 | Error mounting recovery partition|
| 36 | Error during before-upgrade hook|
| 37 | Error during before-upgrade-chroot hook|
| 38 | Error during after-upgrade hook|
| 39 | Error during after-upgrade-chroot hook|
| 40 | Error moving file|
| 41 | Error occurred during cleanup|
| 42 | Error occurred trying to reboot|
| 43 | Error occurred trying to shutdown|
| 44 | Error occurred when unmounting image|
| 44 | Error occurred when labeling partition|
| 45 | Error setting default grub entry|
| 46 | Error occurred during selinux relabeling|
| 255 | Unknown error|
9 changes: 3 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,13 @@ require (
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/godbus/dbus v4.1.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-containerregistry v0.7.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/renameio v1.0.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down Expand Up @@ -198,13 +196,12 @@ require (
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/tools v0.2.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.62.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading

0 comments on commit 3b2e929

Please sign in to comment.