Skip to content

Commit 4a9489c

Browse files
authored
Merge pull request #104 from Parallels/103-ip-detection-fails-due-to-parallels_dhcp_leases-not-found
Resolve ip is not detected if dhcp_leases file is missing
2 parents be4b844 + bf4ecdf commit 4a9489c

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

builder/parallels/common/driver_9.go

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,12 @@ func (d *Parallels9Driver) MAC(vmName string) (string, error) {
461461
return mac, nil
462462
}
463463

464-
// IPAddress finds the IP address of a VM connected that uses DHCP by its MAC address
465-
//
466464
// Parses the file /Library/Preferences/Parallels/parallels_dhcp_leases
467465
// file contain a list of DHCP leases given by Parallels Desktop
468466
// Example line:
469467
// 10.211.55.181="1418921112,1800,001c42f593fb,ff42f593fb000100011c25b9ff001c42f593fb"
470468
// IP Address ="Lease expiry, Lease time, MAC, MAC or DUID"
471-
func (d *Parallels9Driver) IPAddress(mac string, vmName string) (string, error) {
472-
469+
func (d *Parallels9Driver) ipWithLeases(mac string, vmName string) (string, error) {
473470
if len(mac) != 12 {
474471
return "", fmt.Errorf("Not a valid MAC address: %s. It should be exactly 12 digits.", mac)
475472
}
@@ -493,34 +490,52 @@ func (d *Parallels9Driver) IPAddress(mac string, vmName string) (string, error)
493490
}
494491
}
495492

496-
if len(mostRecentIP) == 0 {
497-
log.Printf("IP lease not found for MAC address %s in: %s\n", mac, d.dhcpLeaseFile)
493+
return mostRecentIP, nil
494+
}
498495

499-
var stdout bytes.Buffer
500-
cmd := exec.Command(d.PrlctlPath, "list", vmName, "--full", "--no-header", "-o", "ip_configured")
501-
cmd.Stdout = &stdout
502-
if err := cmd.Run(); err != nil {
503-
log.Printf("Command run failed for Virtual Machine: %s\n", vmName)
504-
return "", err
505-
}
496+
func (d *Parallels9Driver) ipWithPrlctl(vmName string) (string, error) {
497+
ip := ""
498+
var stdout, stderr bytes.Buffer
499+
cmd := exec.Command(d.PrlctlPath, "list", vmName, "--full", "--no-header", "-o", "ip_configured")
500+
cmd.Stdout = &stdout
501+
cmd.Stderr = &stderr
502+
if err := cmd.Run(); err != nil {
503+
log.Printf("Command run failed for Virtual Machine: %s\n", vmName)
504+
return "", err
505+
}
506506

507-
stdoutString := strings.TrimSpace(stdout.String())
508-
re := regexp.MustCompile(`([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)`)
509-
macMatch := re.FindAllStringSubmatch(stdoutString, 1)
507+
stderrString := strings.TrimSpace(stderr.String())
508+
if len(stderrString) > 0 {
509+
log.Printf("stderr: %s", stderrString)
510+
}
510511

511-
if len(macMatch) != 1 {
512-
return "", fmt.Errorf("Unable to retrieve ip address of VM %s through tools\n", vmName)
513-
}
512+
stdoutString := strings.TrimSpace(stdout.String())
513+
re := regexp.MustCompile(`([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)`)
514+
macMatch := re.FindAllStringSubmatch(stdoutString, 1)
514515

515-
mostRecentIP = macMatch[0][1]
516+
if len(macMatch) != 1 {
517+
return "", fmt.Errorf("Unable to retrieve ip address of VM %s through tools\n", vmName)
516518
}
517519

518-
if len(mostRecentIP) == 0 {
519-
return "", fmt.Errorf("IP address not found for this VM: %s\n", vmName)
520+
ip = macMatch[0][1]
521+
return ip, nil
522+
}
523+
524+
// IPAddress finds the IP address of a VM connected that uses DHCP by its MAC address
525+
// If the MAC address is not found in the DHCP lease file, it will try to find ip using prlctl
526+
func (d *Parallels9Driver) IPAddress(mac string, vmName string) (string, error) {
527+
ip, err := d.ipWithLeases(mac, vmName)
528+
if (err != nil) || (len(ip) == 0) {
529+
log.Printf("IP lease not found for MAC address %s in: %s\n", mac, d.dhcpLeaseFile)
530+
531+
ip, err = d.ipWithPrlctl(vmName)
532+
if (err != nil) || (len(ip) == 0) {
533+
return "", fmt.Errorf("IP address not found for this VM using prlctl: %s\n", vmName)
534+
}
520535
}
521536

522-
log.Printf("Found IP lease: %s for MAC address %s\n", mostRecentIP, mac)
523-
return mostRecentIP, nil
537+
log.Printf("Found IP lease: %s for MAC address %s\n", ip, mac)
538+
return ip, nil
524539
}
525540

526541
// ToolsISOPath returns a full path to the Parallels Tools ISO for the specified guest

0 commit comments

Comments
 (0)