@@ -25,44 +25,46 @@ import (
25
25
// FindMachineByPort finds a running machine that matches the given connection port.
26
26
// It returns the machine configuration and provider, or an error if not found.
27
27
func FindMachineByPort (connectionURI string , parsedConnection * url.URL ) (* vmconfigs.MachineConfig , vmconfigs.VMProvider , error ) {
28
- machineProvider , err := provider .Get ()
29
- if err != nil {
30
- return nil , nil , fmt .Errorf ("getting machine provider: %w" , err )
31
- }
32
-
33
- dirs , err := env .GetMachineDirs (machineProvider .VMType ())
34
- if err != nil {
35
- return nil , nil , err
36
- }
37
-
38
- machineList , err := vmconfigs .LoadMachinesInDir (dirs )
39
- if err != nil {
40
- return nil , nil , fmt .Errorf ("listing machines: %w" , err )
41
- }
42
-
43
- // Now we know that the connection points to a machine and we
44
- // can find the machine by looking for the one with the
45
- // matching port.
46
- connectionPort , err := strconv .Atoi (parsedConnection .Port ())
47
- if err != nil {
48
- return nil , nil , fmt .Errorf ("parsing connection port: %w" , err )
49
- }
50
-
51
- for _ , mc := range machineList {
52
- if connectionPort != mc .SSH .Port {
28
+ for _ , machineProvider := range provider .GetAll () {
29
+ logrus .Debugf ("Checking provider: %s" , machineProvider .VMType ())
30
+ dirs , err := env .GetMachineDirs (machineProvider .VMType ())
31
+ if err != nil {
32
+ logrus .Debugf ("Failed to get machine dirs for provider %s: %v" , machineProvider .VMType (), err )
53
33
continue
54
34
}
55
35
56
- state , err := machineProvider . State ( mc , false )
36
+ machineList , err := vmconfigs . LoadMachinesInDir ( dirs )
57
37
if err != nil {
58
- return nil , nil , err
38
+ logrus .Debugf ("Failed to list machines: %v" , err )
39
+ continue
59
40
}
60
41
61
- if state != define .Running {
62
- return nil , nil , fmt .Errorf ("machine %s is not running but in state %s" , mc .Name , state )
42
+ // Now we know that the connection points to a machine and we
43
+ // can find the machine by looking for the one with the
44
+ // matching port.
45
+ connectionPort , err := strconv .Atoi (parsedConnection .Port ())
46
+ if err != nil {
47
+ logrus .Debugf ("Failed to parse connection port: %v" , err )
48
+ continue
63
49
}
64
50
65
- return mc , machineProvider , nil
51
+ for _ , mc := range machineList {
52
+ if connectionPort != mc .SSH .Port {
53
+ continue
54
+ }
55
+
56
+ state , err := machineProvider .State (mc , false )
57
+ if err != nil {
58
+ logrus .Debugf ("Failed to get machine state for %s: %v" , mc .Name , err )
59
+ continue
60
+ }
61
+
62
+ if state != define .Running {
63
+ return nil , nil , fmt .Errorf ("machine %s is not running but in state %s" , mc .Name , state )
64
+ }
65
+
66
+ return mc , machineProvider , nil
67
+ }
66
68
}
67
69
68
70
return nil , nil , fmt .Errorf ("could not find a matching machine for connection %q" , connectionURI )
@@ -201,13 +203,13 @@ func IsHyperVProvider(ctx context.Context) (bool, error) {
201
203
return false , err
202
204
}
203
205
204
- _ , vmType , err := getMachineMountsAndVMType (conn .URI .String (), conn .URI )
206
+ _ , vmProvider , err := FindMachineByPort (conn .URI .String (), conn .URI )
205
207
if err != nil {
206
- logrus .Debugf ("Failed to get machine mounts : %v" , err )
208
+ logrus .Debugf ("Failed to get machine hypervisor type : %v" , err )
207
209
return false , err
208
210
}
209
211
210
- return vmType == define .HyperVVirt , nil
212
+ return vmProvider . VMType () == define .HyperVVirt , nil
211
213
}
212
214
213
215
// ValidatePathForLocalAPI checks if the provided path satisfies requirements for local API usage.
0 commit comments