-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement to upgrade-recovery separate command
Signed-off-by: Andrea Mazzotti <andrea.mazzotti@suse.com>
- Loading branch information
1 parent
9feacf3
commit 3246717
Showing
6 changed files
with
274 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright © 2022 - 2024 SUSE LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/rancher/elemental-toolkit/cmd/config" | ||
"github.com/rancher/elemental-toolkit/pkg/action" | ||
elementalError "github.com/rancher/elemental-toolkit/pkg/error" | ||
v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" | ||
) | ||
|
||
// NewUpgradeCmd returns a new instance of the upgrade subcommand and appends it to | ||
// the root command. requireRoot is to initiate it with or without the CheckRoot | ||
// pre-run check. This method is mostly used for testing purposes. | ||
func NewUpgradeRecoveryCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "upgrade-recovery", | ||
Short: "Upgrade the Recovery system", | ||
Args: cobra.ExactArgs(0), | ||
PreRunE: func(_ *cobra.Command, _ []string) error { | ||
if addCheckRoot { | ||
return CheckRoot() | ||
} | ||
return nil | ||
}, | ||
|
||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
path, err := exec.LookPath("mount") | ||
if err != nil { | ||
return err | ||
} | ||
mounter := v1.NewMounter(path) | ||
|
||
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) | ||
} | ||
|
||
// Set this after parsing of the flags, so it fails on parsing and prints usage properly | ||
cmd.SilenceUsage = true | ||
cmd.SilenceErrors = true // Do not propagate errors down the line, we control them | ||
|
||
spec, err := config.ReadUpgradeRecoverySpec(cfg, cmd.Flags()) | ||
if err != nil { | ||
cfg.Logger.Errorf("Invalid upgrade-recovery command setup %v", err) | ||
return elementalError.NewFromError(err, elementalError.ReadingSpecConfig) | ||
} | ||
|
||
cfg.Logger.Infof("Upgrade Recovery called") | ||
upgrade, err := action.NewUpgradeRecoveryAction(cfg, spec, action.WithUpdateInstallState(true)) | ||
if err != nil { | ||
cfg.Logger.Errorf("failed to initialize upgrade-recovery action: %v", err) | ||
return err | ||
} | ||
|
||
return upgrade.Run() | ||
}, | ||
} | ||
root.AddCommand(c) | ||
addRecoverySystemFlag(c) | ||
return c | ||
} | ||
|
||
// register the subcommand into rootCmd | ||
var _ = NewUpgradeRecoveryCmd(rootCmd, true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.