Skip to content

Commit

Permalink
feat: auto reboot node on reset
Browse files Browse the repository at this point in the history
now we reboot the node after finishing the reset command.
  • Loading branch information
ricardomaraschini committed Sep 24, 2024
1 parent 7d54e53 commit 4d04007
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
17 changes: 3 additions & 14 deletions cmd/embedded-cluster/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,6 @@ var resetCommand = &cli.Command{
Usage: "Disable interactive prompts",
Value: false,
},
&cli.BoolFlag{
Name: "reboot",
Usage: "Reboot system after resetting the node",
Value: false,
},
},
Usage: fmt.Sprintf("Remove %s from the current node", binName),
Action: func(c *cli.Context) error {
Expand All @@ -378,7 +373,7 @@ var resetCommand = &cli.Command{
}

logrus.Info("This will remove this node from the cluster and completely reset it, removing all data stored on the node.")
logrus.Info("Do not reset another node until this is complete.")
logrus.Info("This action will cause the node to reboot. Do not reset another node until this is complete.")
if !c.Bool("force") && !c.Bool("no-prompt") && !prompts.New().Confirm("Do you want to continue?", false) {
return fmt.Errorf("Aborting")
}
Expand Down Expand Up @@ -449,10 +444,6 @@ var resetCommand = &cli.Command{
return err
}

if !c.Bool("reboot") {
logrus.Infof("Node has been reset. Please reboot to ensure transient configuration is also reset.")
}

if err := helpers.RemoveAll(defaults.PathToK0sConfig()); err != nil {
return fmt.Errorf("failed to remove k0s config: %w", err)
}
Expand Down Expand Up @@ -501,10 +492,8 @@ var resetCommand = &cli.Command{
return fmt.Errorf("failed to remove k0s binary: %w", err)
}

if c.Bool("reboot") {
if _, err := exec.Command("reboot").Output(); err != nil {
return err
}
if _, err := exec.Command("reboot").Output(); err != nil {
return err
}

return nil
Expand Down
21 changes: 21 additions & 0 deletions e2e/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
lxd "github.com/canonical/lxd/client"
"github.com/canonical/lxd/shared/api"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

var networkaddr chan string
Expand Down Expand Up @@ -131,6 +132,26 @@ func (o *Output) Destroy() {
networkaddr <- o.network
}

// WaitForNodeRunning waits until the node is in "running" state.
func (o *Output) WaitForNodeRunning(node int) {
client, err := lxd.ConnectLXDUnix(lxdSocket, nil)
require.NoError(o.T, err, "failed to connect to lxd")

var counter int
state := &api.InstanceState{}
for state.Status != "Running" {
time.Sleep(5 * time.Second)
o.T.Logf("waiting for node %d to start (running)", node)

state, _, err = client.GetInstanceState(o.Nodes[node])
require.NoError(o.T, err, "failed to get node state %d", node)
o.T.Logf("node %d is in state %q", node, state.Status)

require.True(o.T, counter < 10, "node %d failed to start", node)
counter++
}
}

// Command is a command to be run in a node.
type Command struct {
Node string
Expand Down
8 changes: 8 additions & 0 deletions e2e/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ func TestResetAndReinstall(t *testing.T) {
t.Fatalf("fail to reset the installation: %v", err)
}

// reset will reboot the node, this waits until the node reports back
// as running.
tc.WaitForNodeRunning(0)

t.Logf("%s: installing embedded-cluster on node 0 after reset", time.Now().Format(time.RFC3339))
line = []string{"single-node-install.sh", "ui"}
if _, _, err := RunCommandOnNode(t, tc, 0, line); err != nil {
Expand Down Expand Up @@ -698,6 +702,10 @@ func TestResetAndReinstallAirgap(t *testing.T) {
t.Fatalf("fail to reset the installation: %v", err)
}

// reset will reboot the node, this waits until the node reports back
// as running.
tc.WaitForNodeRunning(0)

t.Logf("%s: installing embedded-cluster on node 0", time.Now().Format(time.RFC3339))
line = []string{"single-node-airgap-install.sh"}
if _, _, err := RunCommandOnNode(t, tc, 0, line); err != nil {
Expand Down

0 comments on commit 4d04007

Please sign in to comment.