Skip to content

Commit 2b4b6c9

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

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-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

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

pkg/instance/restart.go

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

0 commit comments

Comments
 (0)