| 1 | |
| 2 | |
| 3 | |
| 4 | package instance |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | |
| 11 | "github.com/sirupsen/logrus" |
| 12 | |
| 13 | "github.com/lima-vm/lima/v2/pkg/autostart" |
| 14 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 15 | "github.com/lima-vm/lima/v2/pkg/networks/reconcile" |
| 16 | ) |
| 17 | |
| 18 | const ( |
| 19 | launchHostAgentForeground = false |
| 20 | ) |
| 21 | |
| 22 | func Restart(ctx context.Context, inst *limatype.Instance, showProgress bool) error { |
| 23 | if err := StopGracefully(ctx, inst, true); err != nil { |
| 24 | return err |
| 25 | } |
| 26 | |
| 27 | |
| 28 | if registered, err := autostart.IsRegistered(ctx, inst); err != nil && !errors.Is(err, autostart.ErrNotSupported) { |
| 29 | return fmt.Errorf("failed to check if the autostart entry for instance %#q is registered: %w", inst.Name, err) |
| 30 | } else if !registered { |
| 31 | if err := reconcile.Reconcile(ctx, inst.Name); err != nil { |
| 32 | return err |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if err := Start(ctx, inst, launchHostAgentForeground, showProgress); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | func RestartForcibly(ctx context.Context, inst *limatype.Instance, showProgress bool) error { |
| 44 | logrus.Info("Restarting the instance forcibly") |
| 45 | StopForcibly(inst) |
| 46 | |
| 47 | if registered, err := autostart.IsRegistered(ctx, inst); err != nil && !errors.Is(err, autostart.ErrNotSupported) { |
| 48 | return fmt.Errorf("failed to check if the autostart entry for instance %#q is registered: %w", inst.Name, err) |
| 49 | } else if !registered { |
| 50 | if err := reconcile.Reconcile(ctx, inst.Name); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if err := Start(ctx, inst, launchHostAgentForeground, showProgress); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | return nil |
| 60 | } |