-
Notifications
You must be signed in to change notification settings - Fork 694
Feature: Registry for drivers and expose driver info in CLI #3692
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,183 @@ | ||||||||||||||||||||
// SPDX-FileCopyrightText: Copyright The Lima Authors | ||||||||||||||||||||
// SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||||||
|
||||||||||||||||||||
package registry | ||||||||||||||||||||
|
||||||||||||||||||||
import ( | ||||||||||||||||||||
"context" | ||||||||||||||||||||
"fmt" | ||||||||||||||||||||
"os" | ||||||||||||||||||||
"os/exec" | ||||||||||||||||||||
"path/filepath" | ||||||||||||||||||||
"strings" | ||||||||||||||||||||
|
||||||||||||||||||||
"github.com/sirupsen/logrus" | ||||||||||||||||||||
|
||||||||||||||||||||
"github.com/lima-vm/lima/pkg/driver" | ||||||||||||||||||||
"github.com/lima-vm/lima/pkg/driver/vz" | ||||||||||||||||||||
"github.com/lima-vm/lima/pkg/driver/wsl2" | ||||||||||||||||||||
"github.com/lima-vm/lima/pkg/limayaml" | ||||||||||||||||||||
"github.com/lima-vm/lima/pkg/usrlocalsharelima" | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
const ( | ||||||||||||||||||||
Internal = "internal" | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
type ExternalDriver struct { | ||||||||||||||||||||
Name string | ||||||||||||||||||||
InstanceName string | ||||||||||||||||||||
Command *exec.Cmd | ||||||||||||||||||||
SocketPath string | ||||||||||||||||||||
Path string | ||||||||||||||||||||
Ctx context.Context | ||||||||||||||||||||
Logger *logrus.Logger | ||||||||||||||||||||
CancelFunc context.CancelFunc | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
var ( | ||||||||||||||||||||
internalDrivers = make(map[string]driver.Driver) | ||||||||||||||||||||
ExternalDrivers = make(map[string]*ExternalDriver) | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
func List() map[string]string { | ||||||||||||||||||||
if err := discoverDrivers(); err != nil { | ||||||||||||||||||||
logrus.Warnf("Error discovering drivers: %v", err) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
vmTypes := make(map[string]string) | ||||||||||||||||||||
for name := range internalDrivers { | ||||||||||||||||||||
vmTypes[name] = Internal | ||||||||||||||||||||
} | ||||||||||||||||||||
for name, d := range ExternalDrivers { | ||||||||||||||||||||
vmTypes[name] = d.Path | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// This block will be removed while merging the internal driver pull request(#3693). | ||||||||||||||||||||
if len(vmTypes) == 0 { | ||||||||||||||||||||
vmTypes[limayaml.QEMU] = Internal | ||||||||||||||||||||
if vz.Enabled { | ||||||||||||||||||||
vmTypes[limayaml.VZ] = Internal | ||||||||||||||||||||
} | ||||||||||||||||||||
if wsl2.Enabled { | ||||||||||||||||||||
vmTypes[limayaml.WSL2] = Internal | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return vmTypes | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func Get(name string) (*ExternalDriver, driver.Driver, bool) { | ||||||||||||||||||||
if err := discoverDrivers(); err != nil { | ||||||||||||||||||||
logrus.Warnf("Error discovering drivers: %v", err) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
internalDriver, exists := internalDrivers[name] | ||||||||||||||||||||
if !exists { | ||||||||||||||||||||
externalDriver, exists := ExternalDrivers[name] | ||||||||||||||||||||
if exists { | ||||||||||||||||||||
return externalDriver, nil, exists | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
return nil, internalDriver, exists | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func registerExternalDriver(name, path string) { | ||||||||||||||||||||
if _, exists := ExternalDrivers[name]; exists { | ||||||||||||||||||||
return | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if _, exists := internalDrivers[name]; exists { | ||||||||||||||||||||
logrus.Debugf("Driver %q is already registered as an internal driver, skipping external registration", name) | ||||||||||||||||||||
return | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
ExternalDrivers[name] = &ExternalDriver{ | ||||||||||||||||||||
Name: name, | ||||||||||||||||||||
Path: path, | ||||||||||||||||||||
Logger: logrus.New(), | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func discoverDrivers() error { | ||||||||||||||||||||
prefix, err := usrlocalsharelima.Prefix() | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return err | ||||||||||||||||||||
} | ||||||||||||||||||||
stdDriverDir := filepath.Join(prefix, "libexec", "lima") | ||||||||||||||||||||
|
||||||||||||||||||||
logrus.Debugf("Discovering external drivers in %s", stdDriverDir) | ||||||||||||||||||||
if _, err := os.Stat(stdDriverDir); err == nil { | ||||||||||||||||||||
if err := discoverDriversInDir(stdDriverDir); err != nil { | ||||||||||||||||||||
logrus.Warnf("Error discovering external drivers in %q: %v", stdDriverDir, err) | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if driverPaths := os.Getenv("LIMA_DRIVERS_PATH"); driverPaths != "" { | ||||||||||||||||||||
paths := filepath.SplitList(driverPaths) | ||||||||||||||||||||
for _, path := range paths { | ||||||||||||||||||||
if path == "" { | ||||||||||||||||||||
continue | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
info, err := os.Stat(path) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
logrus.Warnf("Error accessing external driver path %q: %v", path, err) | ||||||||||||||||||||
continue | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if info.IsDir() { | ||||||||||||||||||||
if err := discoverDriversInDir(path); err != nil { | ||||||||||||||||||||
logrus.Warnf("Error discovering external drivers in %q: %v", path, err) | ||||||||||||||||||||
} | ||||||||||||||||||||
} else if isExecutable(info.Mode()) { | ||||||||||||||||||||
registerDriverFile(path) | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return nil | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func discoverDriversInDir(dir string) error { | ||||||||||||||||||||
entries, err := os.ReadDir(dir) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return fmt.Errorf("failed to read driver directory %q: %w", dir, err) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
for _, entry := range entries { | ||||||||||||||||||||
if entry.IsDir() { | ||||||||||||||||||||
continue | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
info, err := entry.Info() | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
logrus.Warnf("Failed to get info for %q: %v", entry.Name(), err) | ||||||||||||||||||||
continue | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if !isExecutable(info.Mode()) { | ||||||||||||||||||||
continue | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
driverPath := filepath.Join(dir, entry.Name()) | ||||||||||||||||||||
registerDriverFile(driverPath) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return nil | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func registerDriverFile(path string) { | ||||||||||||||||||||
base := filepath.Base(path) | ||||||||||||||||||||
if !strings.HasPrefix(base, "lima-driver-") { | ||||||||||||||||||||
return | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
name := strings.TrimPrefix(base, "lima-driver-") | ||||||||||||||||||||
Comment on lines
+172
to
+176
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we can simplify (but I'm not sure without any tests):
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
registerExternalDriver(name, path) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func isExecutable(mode os.FileMode) bool { | ||||||||||||||||||||
return mode&0o111 != 0 | ||||||||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add unit tests for this newly created package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I add it under #3554 and another PR because it will require a mock diver and
Register()
function(see this) in order to test functions likeGet()
,registerDriverFile()
etc?