Skip to content

Commit c6ba1db

Browse files
pendo324afbjorklund
andcommitted
feat: add Register/Unregister to Driver interface
Co-authored-by: Anders F Björklund <[email protected]> Signed-off-by: Justin Alvarez <[email protected]>
1 parent da1260d commit c6ba1db

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

cmd/limactl/delete.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
78

89
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
10+
"github.com/lima-vm/lima/pkg/stop"
911
"github.com/lima-vm/lima/pkg/store"
1012
"github.com/sirupsen/logrus"
1113
"github.com/spf13/cobra"
@@ -38,21 +40,25 @@ func deleteAction(cmd *cobra.Command, args []string) error {
3840
}
3941
return err
4042
}
41-
if err := deleteInstance(inst, force); err != nil {
43+
if err := deleteInstance(cmd.Context(), inst, force); err != nil {
4244
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
4345
}
4446
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
4547
}
4648
return networks.Reconcile(cmd.Context(), "")
4749
}
4850

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

5456
stopInstanceForcibly(inst)
5557

58+
if err := stop.Unregister(ctx, inst); err != nil {
59+
return fmt.Errorf("failed to unregister %q: %w", inst.Dir, err)
60+
}
61+
5662
if err := os.RemoveAll(inst.Dir); err != nil {
5763
return fmt.Errorf("failed to remove %q: %w", inst.Dir, err)
5864
}

cmd/limactl/start.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io"
@@ -266,7 +267,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
266267
}
267268
}
268269
saveBrokenEditorBuffer := tty
269-
return createInstance(st, saveBrokenEditorBuffer)
270+
return createInstance(cmd.Context(), st, saveBrokenEditorBuffer)
270271
}
271272

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

293-
func createInstance(st *creatorState, saveBrokenEditorBuffer bool) (*store.Instance, error) {
294+
func createInstance(ctx context.Context, st *creatorState, saveBrokenEditorBuffer bool) (*store.Instance, error) {
294295
if st.instName == "" {
295296
return nil, errors.New("got empty st.instName")
296297
}
@@ -334,7 +335,17 @@ func createInstance(st *creatorState, saveBrokenEditorBuffer bool) (*store.Insta
334335
if err := os.WriteFile(filePath, st.yBytes, 0644); err != nil {
335336
return nil, err
336337
}
337-
return store.Inspect(st.instName)
338+
339+
inst, err := store.Inspect(st.instName)
340+
if err != nil {
341+
return nil, err
342+
}
343+
344+
if err := start.Register(ctx, inst); err != nil {
345+
return nil, err
346+
}
347+
348+
return inst, nil
338349
}
339350

340351
type creatorState struct {

pkg/driver/driver.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ type Driver interface {
3535
// It returns error if there are any errors during Stop
3636
Stop(_ context.Context) error
3737

38+
// Register will add an instance to a registry.
39+
// It returns error if there are any errors during Register
40+
Register(_ context.Context) error
41+
42+
// Unregister will perform any cleanup related to the vm instance.
43+
// It returns error if there are any errors during Unregister
44+
Unregister(_ context.Context) error
45+
3846
ChangeDisplayPassword(_ context.Context, password string) error
3947

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

66+
var _ Driver = (*BaseDriver)(nil)
67+
5868
func (d *BaseDriver) Validate() error {
5969
return nil
6070
}
@@ -79,6 +89,14 @@ func (d *BaseDriver) Stop(_ context.Context) error {
7989
return nil
8090
}
8191

92+
func (d *BaseDriver) Register(_ context.Context) error {
93+
return nil
94+
}
95+
96+
func (d *BaseDriver) Unregister(_ context.Context) error {
97+
return nil
98+
}
99+
82100
func (d *BaseDriver) ChangeDisplayPassword(_ context.Context, _ string) error {
83101
return nil
84102
}

pkg/hostagent/hostagent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
285285
}
286286
defer dnsServer.Shutdown()
287287
}
288+
288289
errCh, err := a.driver.Start(ctx)
289290
if err != nil {
290291
return err

pkg/start/start.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,17 @@ func ShowMessage(inst *store.Instance) error {
311311
}
312312
return scanner.Err()
313313
}
314+
315+
func Register(ctx context.Context, inst *store.Instance) error {
316+
y, err := inst.LoadYAML()
317+
if err != nil {
318+
return err
319+
}
320+
321+
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
322+
Instance: inst,
323+
Yaml: y,
324+
})
325+
326+
return limaDriver.Register(ctx)
327+
}

pkg/stop/stop.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package stop
2+
3+
import (
4+
"context"
5+
6+
"github.com/lima-vm/lima/pkg/driver"
7+
"github.com/lima-vm/lima/pkg/driverutil"
8+
9+
"github.com/lima-vm/lima/pkg/store"
10+
)
11+
12+
func Unregister(ctx context.Context, inst *store.Instance) error {
13+
y, err := inst.LoadYAML()
14+
if err != nil {
15+
return err
16+
}
17+
18+
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
19+
Instance: inst,
20+
Yaml: y,
21+
})
22+
23+
return limaDriver.Unregister(ctx)
24+
}

0 commit comments

Comments
 (0)