Skip to content

Commit 93a8909

Browse files
committed
Add support for restarting a instance
Signed-off-by: Ansuman Sahoo <anshumansahoo500@gmail.com>
1 parent 710e900 commit 93a8909

File tree

5 files changed

+145
-3
lines changed

5 files changed

+145
-3
lines changed

cmd/limactl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func newApp() *cobra.Command {
159159
newUnprotectCommand(),
160160
newTunnelCommand(),
161161
newTemplateCommand(),
162+
newRestartCommand(),
162163
)
163164
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
164165
rootCmd.AddCommand(startAtLoginCommand())

cmd/limactl/restart.go

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

cmd/limactl/stop.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ func stopAction(cmd *cobra.Command, args []string) error {
3939
if err != nil {
4040
return err
4141
}
42+
ctx := cmd.Context()
4243
if force {
4344
instance.StopForcibly(inst)
4445
} else {
45-
err = instance.StopGracefully(inst)
46+
err = instance.StopGracefully(ctx, inst)
4647
}
4748
// TODO: should we also reconcile networks if graceful stop returned an error?
4849
if err == nil {

pkg/instance/restart.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
logrus.Info("Restarting the instance")
18+
if err := StopGracefully(ctx, inst); err != nil {
19+
return err
20+
}
21+
22+
if err := networks.Reconcile(ctx, inst.Name); err != nil {
23+
return err
24+
}
25+
26+
if err := Start(ctx, inst, "", launchHostAgentForeground); err != nil {
27+
return err
28+
}
29+
30+
return nil
31+
}
32+
33+
func RestartForcibly(ctx context.Context, inst *store.Instance) error {
34+
logrus.Info("Restarting the instance forcibly")
35+
StopForcibly(inst)
36+
37+
if err := networks.Reconcile(ctx, inst.Name); err != nil {
38+
return err
39+
}
40+
41+
if err := Start(ctx, inst, "", launchHostAgentForeground); err != nil {
42+
return err
43+
}
44+
45+
return nil
46+
}

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(ctx context.Context, inst *store.Instance) error {
2323
if inst.Status != store.StatusRunning {
24+
if isRestart, ok := ctx.Value("restart").(bool); ok && 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(ctx, inst, begin)
39+
if err != nil {
40+
return err
41+
}
42+
43+
logrus.Info("Waiting for the instance to shut down")
44+
return waitForInstanceShutdown(ctx, 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)