Skip to content

Commit cc54d5c

Browse files
committed
driver(external): create driver logs in instance directory
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent a4d2fcd commit cc54d5c

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

pkg/driverutil/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// CreateTargetDriverInstance creates the appropriate driver for an instance.
1515
func CreateTargetDriverInstance(inst *store.Instance, sshLocalPort int) (driver.Driver, error) {
1616
limaDriver := inst.Config.VMType
17-
driver, exists := registry.DefaultRegistry.Get(string(*limaDriver))
17+
driver, exists := registry.DefaultRegistry.Get(string(*limaDriver), inst.Name)
1818
if !exists {
1919
return nil, fmt.Errorf("unknown or unsupported VM type: %s", *limaDriver)
2020
}

pkg/registry/registry.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ import (
1616

1717
"github.com/lima-vm/lima/pkg/driver"
1818
"github.com/lima-vm/lima/pkg/driver/external/client"
19+
"github.com/lima-vm/lima/pkg/store"
1920
"github.com/lima-vm/lima/pkg/store/filenames"
2021
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
2122
"github.com/sirupsen/logrus"
2223
)
2324

2425
type ExternalDriver struct {
25-
Name string
26-
Command *exec.Cmd
27-
Stdin io.WriteCloser
28-
Stdout io.ReadCloser
29-
Client *client.DriverClient // Client is the gRPC client for the external driver
30-
Path string
31-
ctx context.Context
32-
logger *logrus.Logger
33-
cancelFunc context.CancelFunc
26+
Name string
27+
InstanceName string
28+
Command *exec.Cmd
29+
Stdin io.WriteCloser
30+
Stdout io.ReadCloser
31+
Client *client.DriverClient // Client is the gRPC client for the external driver
32+
Path string
33+
ctx context.Context
34+
logger *logrus.Logger
35+
cancelFunc context.CancelFunc
3436
}
3537

3638
type Registry struct {
@@ -46,8 +48,12 @@ func NewRegistry() *Registry {
4648
}
4749
}
4850

49-
func (e *ExternalDriver) Start() error {
51+
func (e *ExternalDriver) Start(instName string) error {
5052
e.logger.Infof("Starting external driver at %s", e.Path)
53+
if instName == "" {
54+
return fmt.Errorf("instance name cannot be empty")
55+
}
56+
e.InstanceName = instName
5157

5258
ctx, cancel := context.WithCancel(context.Background())
5359
cmd := exec.CommandContext(ctx, e.Path)
@@ -64,12 +70,12 @@ func (e *ExternalDriver) Start() error {
6470
return fmt.Errorf("failed to create stdout pipe: %w", err)
6571
}
6672

67-
sharedDir, err := usrlocalsharelima.Dir()
73+
instanceDir, err := store.InstanceDir(e.InstanceName)
6874
if err != nil {
6975
cancel()
70-
return fmt.Errorf("failed to determine Lima share directory: %w", err)
76+
return fmt.Errorf("failed to determine instance directory: %w", err)
7177
}
72-
logPath := filepath.Join(sharedDir, filenames.ExternalDriverStderrLog)
78+
logPath := filepath.Join(instanceDir, filenames.ExternalDriverStderrLog)
7379
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
7480
if err != nil {
7581
cancel()
@@ -167,7 +173,7 @@ func (r *Registry) List() []string {
167173
return names
168174
}
169175

170-
func (r *Registry) Get(name string) (driver.Driver, bool) {
176+
func (r *Registry) Get(name, instName string) (driver.Driver, bool) {
171177
r.mu.RLock()
172178
defer r.mu.RUnlock()
173179

@@ -178,12 +184,13 @@ func (r *Registry) Get(name string) (driver.Driver, bool) {
178184
externalDriver.logger.Debugf("Using external driver %q", name)
179185
if externalDriver.Client == nil || externalDriver.Command == nil || externalDriver.Command.Process == nil {
180186
logrus.Infof("Starting new instance of external driver %q", name)
181-
if err := externalDriver.Start(); err != nil {
187+
if err := externalDriver.Start(instName); err != nil {
182188
externalDriver.logger.Errorf("Failed to start external driver %q: %v", name, err)
183189
return nil, false
184190
}
185191
} else {
186192
logrus.Infof("Reusing existing external driver %q instance", name)
193+
r.externalDrivers[name].InstanceName = instName
187194
}
188195

189196
return externalDriver.Client, true

0 commit comments

Comments
 (0)