Skip to content

refactor: move driver specific code to drivers #3770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cmd/lima-driver-qemu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package main

import (
"context"

"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/driver/qemu"
)

// To be used as an external driver for Lima.
func main() {
server.Serve(context.Background(), qemu.New())
server.Serve(qemu.New())
}
4 changes: 1 addition & 3 deletions cmd/lima-driver-vz/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package main

import (
"context"

"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/driver/vz"
)

// To be used as an external driver for Lima.
func main() {
server.Serve(context.Background(), vz.New())
server.Serve(vz.New())
}
4 changes: 1 addition & 3 deletions cmd/lima-driver-wsl2/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package main

import (
"context"

"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/driver/wsl2"
)

// To be used as an external driver for Lima.
func main() {
server.Serve(context.Background(), wsl2.New())
server.Serve(wsl2.New())
}
6 changes: 5 additions & 1 deletion cmd/limactl/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
"github.com/lima-vm/lima/v2/pkg/driverutil"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/limayaml"
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
"github.com/lima-vm/lima/v2/pkg/yqutil"
)

Expand Down Expand Up @@ -79,6 +80,9 @@ func cloneAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err := driverutil.ResolveVMType(y, filePath); err != nil {
return fmt.Errorf("failed to accept config for %q: %w", filePath, err)
}
if err := limayaml.Validate(y, true); err != nil {
return saveRejectedYAML(yBytes, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/ioutilx"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/sshutil"
"github.com/lima-vm/lima/v2/pkg/store"
)
Expand Down Expand Up @@ -63,7 +64,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
instances := make(map[string]*store.Instance)
instances := make(map[string]*limatype.Instance)
scpFlags := []string{}
scpArgs := []string{}
debug, err := cmd.Flags().GetBool("debug")
Expand Down Expand Up @@ -114,7 +115,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
}
return err
}
if inst.Status == store.StatusStopped {
if inst.Status == limatype.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
if legacySSH {
Expand Down
9 changes: 5 additions & 4 deletions cmd/limactl/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/imgutil/proxyimgutil"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
)

func newDiskCommand() *cobra.Command {
Expand Down Expand Up @@ -243,7 +244,7 @@ func diskDeleteAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
var instances []*store.Instance
var instances []*limatype.Instance
for _, instName := range instNames {
inst, err := store.Inspect(ctx, instName)
if err != nil {
Expand Down Expand Up @@ -344,7 +345,7 @@ func diskUnlockAction(cmd *cobra.Command, args []string) error {
diskName, disk.Instance, inst.Errors)
continue
}
if inst.Status == store.StatusRunning {
if inst.Status == limatype.StatusRunning {
logrus.Warnf("Cannot unlock disk %q used by running instance %q", diskName, disk.Instance)
continue
}
Expand Down Expand Up @@ -402,7 +403,7 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
if disk.Instance != "" {
inst, err := store.Inspect(ctx, disk.Instance)
if err == nil {
if inst.Status == store.StatusRunning {
if inst.Status == limatype.StatusRunning {
return fmt.Errorf("cannot resize disk %q used by running instance %q. Please stop the VM instance", diskName, disk.Instance)
}
}
Expand Down
14 changes: 10 additions & 4 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
"github.com/lima-vm/lima/v2/pkg/driverutil"
"github.com/lima-vm/lima/v2/pkg/editutil"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/limayaml"
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
"github.com/lima-vm/lima/v2/pkg/uiutil"
"github.com/lima-vm/lima/v2/pkg/yqutil"
)
Expand All @@ -46,20 +49,20 @@ func editAction(cmd *cobra.Command, args []string) error {

var filePath string
var err error
var inst *store.Instance
var inst *limatype.Instance

if arg == "" {
arg = DefaultInstanceName
}
if err := store.ValidateInstName(arg); err == nil {
if err := dirnames.ValidateInstName(arg); err == nil {
inst, err = store.Inspect(ctx, arg)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q not found", arg)
}
return err
}
if inst.Status == store.StatusRunning {
if inst.Status == limatype.StatusRunning {
return errors.New("cannot edit a running instance")
}
filePath = filepath.Join(inst.Dir, filenames.LimaYAML)
Expand Down Expand Up @@ -118,6 +121,9 @@ func editAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err := driverutil.ResolveVMType(y, filePath); err != nil {
return fmt.Errorf("failed to accept config for %q: %w", filePath, err)
}
if err := limayaml.Validate(y, true); err != nil {
return saveRejectedYAML(yBytes, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/factory-reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/lima-vm/lima/v2/pkg/cidata"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
)

func newFactoryResetCommand() *cobra.Command {
Expand Down
12 changes: 6 additions & 6 deletions cmd/limactl/genschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
orderedmap "github.com/wk8/go-ordered-map/v2"

"github.com/lima-vm/lima/v2/pkg/jsonschemautil"
"github.com/lima-vm/lima/v2/pkg/limayaml"
"github.com/lima-vm/lima/v2/pkg/limatype"
)

func newGenSchemaCommand() *cobra.Command {
Expand Down Expand Up @@ -52,7 +52,7 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
return err
}

schema := jsonschema.Reflect(&limayaml.LimaYAML{})
schema := jsonschema.Reflect(&limatype.LimaYAML{})
// allow Disk to be either string (name) or object (struct)
schema.Definitions["Disk"].Type = "" // was: "object"
schema.Definitions["Disk"].OneOf = []*jsonschema.Schema{
Expand All @@ -72,10 +72,10 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
{Type: "object"},
}
properties := schema.Definitions["LimaYAML"].Properties
getProp(properties, "os").Enum = toAny(limayaml.OSTypes)
getProp(properties, "arch").Enum = toAny(limayaml.ArchTypes)
getProp(properties, "mountType").Enum = toAny(limayaml.MountTypes)
getProp(properties, "vmType").Enum = toAny(limayaml.VMTypes)
getProp(properties, "os").Enum = toAny(limatype.OSTypes)
getProp(properties, "arch").Enum = toAny(limatype.ArchTypes)
getProp(properties, "mountType").Enum = toAny(limatype.MountTypes)
getProp(properties, "vmType").Enum = toAny(limatype.VMTypes)
j, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

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

Expand Down Expand Up @@ -172,7 +173,7 @@ func listAction(cmd *cobra.Command, args []string) error {
}

// get the state and config for all the requested instances
var instances []*store.Instance
var instances []*limatype.Instance
for _, instanceName := range instanceNames {
instance, err := store.Inspect(ctx, instanceName)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/limactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/lima-vm/lima/v2/pkg/debugutil"
"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/fsutil"
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/osutil"
"github.com/lima-vm/lima/v2/pkg/store/dirnames"
"github.com/lima-vm/lima/v2/pkg/version"
)

Expand All @@ -44,6 +44,7 @@ func main() {
}
rootCmd := newApp()
if err := executeWithPluginSupport(rootCmd, os.Args[1:]); err != nil {
server.StopAllExternalDrivers()
handleExitCoder(err)
logrus.Fatal(err)
}
Expand Down
14 changes: 10 additions & 4 deletions cmd/limactl/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ package main

import (
"context"
"fmt"
"maps"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/downloader"
"github.com/lima-vm/lima/v2/pkg/driverutil"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limayaml"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/templatestore"
Expand Down Expand Up @@ -64,8 +67,8 @@ func pruneAction(cmd *cobra.Command, _ []string) error {
return nil
}

func knownLocations(ctx context.Context) (map[string]limayaml.File, error) {
locations := make(map[string]limayaml.File)
func knownLocations(ctx context.Context) (map[string]limatype.File, error) {
locations := make(map[string]limatype.File)

// Collect locations from instances
instances, err := store.Instances()
Expand Down Expand Up @@ -94,13 +97,16 @@ func knownLocations(ctx context.Context) (map[string]limayaml.File, error) {
if err != nil {
return nil, err
}
if err := driverutil.ResolveVMType(y, t.Name); err != nil {
return nil, fmt.Errorf("failed to accept config for %q: %w", t.Name, err)
}
maps.Copy(locations, locationsFromLimaYAML(y))
}
return locations, nil
}

func locationsFromLimaYAML(y *limayaml.LimaYAML) map[string]limayaml.File {
locations := make(map[string]limayaml.File)
func locationsFromLimaYAML(y *limatype.LimaYAML) map[string]limatype.File {
locations := make(map[string]limatype.File)
for _, f := range y.Images {
locations[downloader.CacheKey(f.Location)] = f.File
if f.Kernel != nil {
Expand Down
10 changes: 5 additions & 5 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/lima-vm/lima/v2/pkg/envutil"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/ioutilx"
"github.com/lima-vm/lima/v2/pkg/limayaml"
"github.com/lima-vm/lima/v2/pkg/limatype"
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
"github.com/lima-vm/lima/v2/pkg/sshutil"
"github.com/lima-vm/lima/v2/pkg/store"
Expand Down Expand Up @@ -86,7 +86,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
}
return err
}
if inst.Status == store.StatusStopped {
if inst.Status == limatype.StatusStopped {
startNow, err := askWhetherToStart()
if err != nil {
return err
Expand Down Expand Up @@ -142,7 +142,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
if workDir != "" {
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
// FIXME: check whether y.Mounts contains the home, not just len > 0
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limayaml.WSL2 {
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limatype.WSL2 {
hostCurrentDir, err := os.Getwd()
if err == nil && runtime.GOOS == "windows" {
hostCurrentDir, err = mountDirFromWindowsDir(ctx, inst, hostCurrentDir)
Expand Down Expand Up @@ -265,8 +265,8 @@ func shellAction(cmd *cobra.Command, args []string) error {
return sshCmd.Run()
}

func mountDirFromWindowsDir(ctx context.Context, inst *store.Instance, dir string) (string, error) {
if inst.VMType == limayaml.WSL2 {
func mountDirFromWindowsDir(ctx context.Context, inst *limatype.Instance, dir string) (string, error) {
if inst.VMType == limatype.WSL2 {
distroName := "lima-" + inst.Name
return ioutilx.WindowsSubsystemPathForLinux(ctx, dir, distroName)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/limactl/show-ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/sshutil"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/dirnames"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
)

const showSSHExample = `
Expand Down
Loading
Loading