Skip to content

Commit

Permalink
Merge pull request lima-vm#1726 from pendo324/unregister
Browse files Browse the repository at this point in the history
feat: add Register/Unregister to Driver interface
  • Loading branch information
AkihiroSuda authored Aug 25, 2023
2 parents 6f82baf + 93eab33 commit faf2aa6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
10 changes: 8 additions & 2 deletions cmd/limactl/delete.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"context"
"errors"
"fmt"
"os"

networks "github.com/lima-vm/lima/pkg/networks/reconcile"
"github.com/lima-vm/lima/pkg/stop"
"github.com/lima-vm/lima/pkg/store"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -38,21 +40,25 @@ func deleteAction(cmd *cobra.Command, args []string) error {
}
return err
}
if err := deleteInstance(inst, force); err != nil {
if err := deleteInstance(cmd.Context(), inst, force); err != nil {
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
}
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
}
return networks.Reconcile(cmd.Context(), "")
}

func deleteInstance(inst *store.Instance, force bool) error {
func deleteInstance(ctx context.Context, inst *store.Instance, force bool) error {
if !force && inst.Status != store.StatusStopped {
return fmt.Errorf("expected status %q, got %q", store.StatusStopped, inst.Status)
}

stopInstanceForcibly(inst)

if err := stop.Unregister(ctx, inst); err != nil {
return fmt.Errorf("failed to unregister %q: %w", inst.Dir, err)
}

if err := os.RemoveAll(inst.Dir); err != nil {
return fmt.Errorf("failed to remove %q: %w", inst.Dir, err)
}
Expand Down
17 changes: 14 additions & 3 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -266,7 +267,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
}
}
saveBrokenEditorBuffer := tty
return createInstance(st, saveBrokenEditorBuffer)
return createInstance(cmd.Context(), st, saveBrokenEditorBuffer)
}

func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*store.Instance, error) {
Expand All @@ -290,7 +291,7 @@ func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*stor
return store.Inspect(inst.Name)
}

func createInstance(st *creatorState, saveBrokenEditorBuffer bool) (*store.Instance, error) {
func createInstance(ctx context.Context, st *creatorState, saveBrokenEditorBuffer bool) (*store.Instance, error) {
if st.instName == "" {
return nil, errors.New("got empty st.instName")
}
Expand Down Expand Up @@ -334,7 +335,17 @@ func createInstance(st *creatorState, saveBrokenEditorBuffer bool) (*store.Insta
if err := os.WriteFile(filePath, st.yBytes, 0644); err != nil {
return nil, err
}
return store.Inspect(st.instName)

inst, err := store.Inspect(st.instName)
if err != nil {
return nil, err
}

if err := start.Register(ctx, inst); err != nil {
return nil, err
}

return inst, nil
}

type creatorState struct {
Expand Down
18 changes: 18 additions & 0 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ type Driver interface {
// It returns error if there are any errors during Stop
Stop(_ context.Context) error

// Register will add an instance to a registry.
// It returns error if there are any errors during Register
Register(_ context.Context) error

// Unregister will perform any cleanup related to the vm instance.
// It returns error if there are any errors during Unregister
Unregister(_ context.Context) error

ChangeDisplayPassword(_ context.Context, password string) error

GetDisplayConnection(_ context.Context) (string, error)
Expand All @@ -55,6 +63,8 @@ type BaseDriver struct {
SSHLocalPort int
}

var _ Driver = (*BaseDriver)(nil)

func (d *BaseDriver) Validate() error {
return nil
}
Expand All @@ -79,6 +89,14 @@ func (d *BaseDriver) Stop(_ context.Context) error {
return nil
}

func (d *BaseDriver) Register(_ context.Context) error {
return nil
}

func (d *BaseDriver) Unregister(_ context.Context) error {
return nil
}

func (d *BaseDriver) ChangeDisplayPassword(_ context.Context, _ string) error {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
}
defer dnsServer.Shutdown()
}

errCh, err := a.driver.Start(ctx)
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions pkg/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,17 @@ func ShowMessage(inst *store.Instance) error {
}
return scanner.Err()
}

func Register(ctx context.Context, inst *store.Instance) error {
y, err := inst.LoadYAML()
if err != nil {
return err
}

limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
Instance: inst,
Yaml: y,
})

return limaDriver.Register(ctx)
}
24 changes: 24 additions & 0 deletions pkg/stop/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package stop

import (
"context"

"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/driverutil"

"github.com/lima-vm/lima/pkg/store"
)

func Unregister(ctx context.Context, inst *store.Instance) error {
y, err := inst.LoadYAML()
if err != nil {
return err
}

limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
Instance: inst,
Yaml: y,
})

return limaDriver.Unregister(ctx)
}

0 comments on commit faf2aa6

Please sign in to comment.