Skip to content

Commit

Permalink
Use utils nsenter
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <andrea.mazzotti@suse.com>
  • Loading branch information
anmazzotti committed Oct 22, 2024
1 parent 3098c28 commit 34e6e89
Show file tree
Hide file tree
Showing 16 changed files with 961 additions and 38 deletions.
57 changes: 34 additions & 23 deletions cmd/register/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"errors"
"fmt"
"os"
"os/exec"

"strings"

"github.com/rancher/elemental-operator/pkg/elementalcli"
Expand All @@ -29,6 +29,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/twpayne/go-vfs"

"k8s.io/utils/exec"
"k8s.io/utils/nsenter"
)

var (
Expand All @@ -42,16 +45,6 @@ func newUpgradeCommand() *cobra.Command {
Use: "upgrade",
Short: "Upgrades the machine",
RunE: func(_ *cobra.Command, _ []string) error {
// If the system is shutting down, return an error so we can try again on next reboot.
alreadyShuttingDown, err := isSystemShuttingDown()
if err != nil {
return fmt.Errorf("determining if system is running: %w", err)
}
if alreadyShuttingDown {
return ErrAlreadyShuttingDown
}

// If system is not shutting down we can proceed.
upgradeConfig := elementalcli.UpgradeConfig{
Debug: debug,
Recovery: viper.GetBool("recovery"),
Expand All @@ -66,6 +59,16 @@ func newUpgradeCommand() *cobra.Command {
CorrelationID: viper.GetString("correlation-id"),
}

// If the system is shutting down, return an error so we can try again on next reboot.
alreadyShuttingDown, err := isSystemShuttingDown(upgradeContext.HostDir)
if err != nil {
return fmt.Errorf("determining if system is running: %w", err)
}
if alreadyShuttingDown {
return ErrAlreadyShuttingDown
}

// If system is not shutting down we can proceed.
installer := install.NewInstaller(vfs.OSFS, nil, nil)

needsReboot, err := installer.UpgradeElemental(upgradeContext)
Expand All @@ -78,7 +81,7 @@ func newUpgradeCommand() *cobra.Command {
// so that consumers can try again after reboot to validate the upgrade has been applied successfully.
if needsReboot {
log.Infof("Rebooting machine after %s upgrade", upgradeContext.CorrelationID)
reboot()
reboot(upgradeContext.HostDir)
return ErrRebooting
}
// Upgrade has been applied successfully, nothing to do.
Expand Down Expand Up @@ -116,11 +119,15 @@ func newUpgradeCommand() *cobra.Command {
return cmd
}

func isSystemShuttingDown() (bool, error) {
cmd := exec.Command("nsenter")
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Args = []string{"-i", "-m", "-t", "1", "--", "systemctl is-system-running"}
func isSystemShuttingDown(hostDir string) (bool, error) {
ex := exec.New()
nsEnter, err := nsenter.NewNsenter(hostDir, ex)
if err != nil {
return false, fmt.Errorf("initializing nsenter: %w", err)
}
cmd := nsEnter.Command("systemctl", "is-system-running")
cmd.SetStdin(os.Stdin)
cmd.SetStderr(os.Stderr)
output, err := cmd.Output()
if err != nil {
return false, fmt.Errorf("running: systemctl is-system-running: %w", err)
Expand All @@ -131,12 +138,16 @@ func isSystemShuttingDown() (bool, error) {
return false, nil
}

func reboot() {
cmd := exec.Command("nsenter")
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Args = []string{"-i", "-m", "-t", "1", "--", "reboot"}
func reboot(hostDir string) {
ex := exec.New()
nsEnter, err := nsenter.NewNsenter(hostDir, ex)
if err != nil {
log.Errorf("Coult not initialize nsenter: %s", err.Error())
}
cmd := nsEnter.Command("reboot")
cmd.SetStdin(os.Stdin)
cmd.SetStderr(os.Stderr)
cmd.SetStdout(os.Stdout)
if err := cmd.Run(); err != nil {
log.Errorf("Could not reboot: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
k8s.io/apimachinery v0.29.2
k8s.io/client-go v12.0.0+incompatible
k8s.io/klog/v2 v2.110.1
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6
sigs.k8s.io/cluster-api v1.6.3
sigs.k8s.io/controller-runtime v0.17.2
sigs.k8s.io/yaml v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,8 @@ k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/A
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/kubernetes v1.27.9 h1:aN3arQtT15QHn3CGZ47egfySPKmSozkj+yZTEF2QLJs=
k8s.io/kubernetes v1.27.9/go.mod h1:bwTXvXbv/nV1D6JnLy4zAcPfChls2+aGL3an0inNbXM=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 h1:MDF6h2H/h4tbzmtIKTuctcwZmY0tY9mD9fNT47QO6HI=
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
pack.ag/amqp v0.11.2/go.mod h1:4/cbmt4EJXSKlG6LCfWHoqmN0uFdy5i/+YFz+fTfhV4=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
5 changes: 5 additions & 0 deletions vendor/k8s.io/utils/exec/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/k8s.io/utils/exec/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 34e6e89

Please sign in to comment.