Skip to content

Commit d56c844

Browse files
authored
Merge pull request #3323 from unsuman/fix/exit-ssh-session
cmd: add support for restarting a running instance
2 parents f0771b5 + ea40c79 commit d56c844

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed

cmd/limactl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ func newApp() *cobra.Command {
186186
newUnprotectCommand(),
187187
newTunnelCommand(),
188188
newTemplateCommand(),
189+
newRestartCommand(),
189190
)
190191
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
191192
rootCmd.AddCommand(startAtLoginCommand())

cmd/limactl/restart.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"github.com/lima-vm/lima/pkg/instance"
8+
"github.com/lima-vm/lima/pkg/store"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func newRestartCommand() *cobra.Command {
13+
restartCmd := &cobra.Command{
14+
Use: "restart INSTANCE",
15+
Short: "Restart a running instance",
16+
Args: WrapArgsError(cobra.MaximumNArgs(1)),
17+
RunE: restartAction,
18+
ValidArgsFunction: restartBashComplete,
19+
GroupID: basicCommand,
20+
}
21+
22+
restartCmd.Flags().BoolP("force", "f", false, "force stop and restart the instance")
23+
return restartCmd
24+
}
25+
26+
func restartAction(cmd *cobra.Command, args []string) error {
27+
instName := DefaultInstanceName
28+
if len(args) > 0 {
29+
instName = args[0]
30+
}
31+
32+
inst, err := store.Inspect(instName)
33+
if err != nil {
34+
return err
35+
}
36+
37+
force, err := cmd.Flags().GetBool("force")
38+
if err != nil {
39+
return err
40+
}
41+
42+
ctx := cmd.Context()
43+
if force {
44+
return instance.RestartForcibly(ctx, inst)
45+
}
46+
47+
return instance.Restart(ctx, inst)
48+
}
49+
50+
func restartBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
51+
return bashCompleteInstanceNames(cmd)
52+
}

cmd/limactl/stop.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func stopAction(cmd *cobra.Command, args []string) error {
4242
if force {
4343
instance.StopForcibly(inst)
4444
} else {
45-
err = instance.StopGracefully(inst)
45+
err = instance.StopGracefully(inst, false)
4646
}
4747
// TODO: should we also reconcile networks if graceful stop returned an error?
4848
if err == nil {

pkg/instance/restart.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package instance
5+
6+
import (
7+
"context"
8+
9+
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
10+
"github.com/lima-vm/lima/pkg/store"
11+
"github.com/sirupsen/logrus"
12+
)
13+
14+
const launchHostAgentForeground = false
15+
16+
func Restart(ctx context.Context, inst *store.Instance) error {
17+
if err := StopGracefully(inst, true); err != nil {
18+
return err
19+
}
20+
21+
if err := networks.Reconcile(ctx, inst.Name); err != nil {
22+
return err
23+
}
24+
25+
if err := Start(ctx, inst, "", launchHostAgentForeground); err != nil {
26+
return err
27+
}
28+
29+
return nil
30+
}
31+
32+
func RestartForcibly(ctx context.Context, inst *store.Instance) error {
33+
logrus.Info("Restarting the instance forcibly")
34+
StopForcibly(inst)
35+
36+
if err := networks.Reconcile(ctx, inst.Name); err != nil {
37+
return err
38+
}
39+
40+
if err := Start(ctx, inst, "", launchHostAgentForeground); err != nil {
41+
return err
42+
}
43+
44+
return nil
45+
}

pkg/instance/stop.go

+37-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ import (
1919
"github.com/sirupsen/logrus"
2020
)
2121

22-
func StopGracefully(inst *store.Instance) error {
22+
func StopGracefully(inst *store.Instance, isRestart bool) error {
2323
if inst.Status != store.StatusRunning {
24+
if isRestart {
25+
logrus.Warn("The instance is not running, continuing with the restart")
26+
return nil
27+
}
2428
return fmt.Errorf("expected status %q, got %q (maybe use `limactl stop -f`?)", store.StatusRunning, inst.Status)
2529
}
2630

@@ -31,7 +35,13 @@ func StopGracefully(inst *store.Instance) error {
3135
}
3236

3337
logrus.Info("Waiting for the host agent and the driver processes to shut down")
34-
return waitForHostAgentTermination(context.TODO(), inst, begin)
38+
err := waitForHostAgentTermination(context.TODO(), inst, begin)
39+
if err != nil {
40+
return err
41+
}
42+
43+
logrus.Info("Waiting for the instance to shut down")
44+
return waitForInstanceShutdown(context.TODO(), inst)
3545
}
3646

3747
func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begin time.Time) error {
@@ -64,6 +74,31 @@ func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begi
6474
return nil
6575
}
6676

77+
func waitForInstanceShutdown(ctx context.Context, inst *store.Instance) error {
78+
ctx2, cancel := context.WithTimeout(ctx, 3*time.Minute)
79+
defer cancel()
80+
81+
ticker := time.NewTicker(500 * time.Millisecond)
82+
defer ticker.Stop()
83+
84+
for {
85+
select {
86+
case <-ticker.C:
87+
updatedInst, err := store.Inspect(inst.Name)
88+
if err != nil {
89+
return errors.New("failed to inspect instance status: " + err.Error())
90+
}
91+
92+
if updatedInst.Status == store.StatusStopped {
93+
logrus.Infof("The instance %s has shut down", updatedInst.Name)
94+
return nil
95+
}
96+
case <-ctx2.Done():
97+
return errors.New("timed out waiting for instance to shut down after 3 minutes")
98+
}
99+
}
100+
}
101+
67102
func StopForcibly(inst *store.Instance) {
68103
if inst.DriverPID > 0 {
69104
logrus.Infof("Sending SIGKILL to the %s driver process %d", inst.VMType, inst.DriverPID)

0 commit comments

Comments
 (0)