Skip to content

Commit c09421d

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

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
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

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.MinimumNArgs(1)),
17+
RunE: restartAction,
18+
Aliases: []string{"reboot"},
19+
ValidArgsFunction: restartBashComplete,
20+
GroupID: basicCommand,
21+
}
22+
23+
restartCmd.Flags().BoolP("force", "f", false, "force stop and restart the instance")
24+
return restartCmd
25+
}
26+
27+
func restartAction(cmd *cobra.Command, args []string) error {
28+
instName := args[0]
29+
30+
inst, err := store.Inspect(instName)
31+
if err != nil {
32+
return err
33+
}
34+
35+
force, err := cmd.Flags().GetBool("force")
36+
if err != nil {
37+
return err
38+
}
39+
if force {
40+
err = instance.RestartForcibly(inst)
41+
} else {
42+
err = instance.Restart(inst)
43+
}
44+
return err
45+
}
46+
47+
func restartBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
48+
return bashCompleteInstanceNames(cmd)
49+
}

pkg/instance/restart.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package instance
5+
6+
import (
7+
"context"
8+
"errors"
9+
"time"
10+
11+
"github.com/lima-vm/lima/pkg/store"
12+
)
13+
14+
const launchHostAgentForeground = false
15+
16+
func Restart(inst *store.Instance) error {
17+
ctx := context.Background()
18+
if err := StopGracefully(inst); err != nil {
19+
return err
20+
}
21+
22+
if err := waitForInstanceShutdown(inst); 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(inst *store.Instance) error {
34+
ctx := context.Background()
35+
StopForcibly(inst)
36+
37+
if err := Start(ctx, inst, "", launchHostAgentForeground); err != nil {
38+
return err
39+
}
40+
41+
return nil
42+
}
43+
44+
func waitForInstanceShutdown(inst *store.Instance) error {
45+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
46+
defer cancel()
47+
48+
ticker := time.NewTicker(500 * time.Millisecond)
49+
defer ticker.Stop()
50+
51+
for {
52+
select {
53+
case <-ticker.C:
54+
updatedInst, err := store.Inspect(inst.Name)
55+
if err != nil {
56+
return errors.New("failed to inspect instance status: " + err.Error())
57+
}
58+
59+
if updatedInst.Status == store.StatusStopped {
60+
return nil
61+
}
62+
case <-ctx.Done():
63+
return errors.New("timed out waiting for instance to stop")
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)