Rewrote the GuestIP implementation in the vmware builder to use the new dhcpd lease parsers instead of the old regexp-based logic.
This commit is contained in:
parent
020b005522
commit
ad7194c920
|
@ -376,7 +376,7 @@ func (d *VmwareDriver) GuestIP(state multistep.StateBag) (string, error) {
|
||||||
return "", fmt.Errorf("no DHCP leases path found for device %s", device)
|
return "", fmt.Errorf("no DHCP leases path found for device %s", device)
|
||||||
}
|
}
|
||||||
|
|
||||||
// open up the lease and read its contents
|
// open up the path to the dhcpd leases
|
||||||
fh, err := os.Open(dhcpLeasesPath)
|
fh, err := os.Open(dhcpLeasesPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while reading DHCP lease path file %s: %s", dhcpLeasesPath, err.Error())
|
log.Printf("Error while reading DHCP lease path file %s: %s", dhcpLeasesPath, err.Error())
|
||||||
|
@ -384,11 +384,16 @@ func (d *VmwareDriver) GuestIP(state multistep.StateBag) (string, error) {
|
||||||
}
|
}
|
||||||
defer fh.Close()
|
defer fh.Close()
|
||||||
|
|
||||||
dhcpBytes, err := ioutil.ReadAll(fh)
|
// and then read its contents
|
||||||
|
leaseEntries, err := ReadDhcpdLeaseEntries(fh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse our MAC address again. There's no need to check for an
|
||||||
|
// error because we've already parsed this successfully.
|
||||||
|
hwaddr, _ := net.ParseMAC(MACAddress)
|
||||||
|
|
||||||
// start grepping through the file looking for fields that we care about
|
// start grepping through the file looking for fields that we care about
|
||||||
var lastIp string
|
var lastIp string
|
||||||
var lastLeaseEnd time.Time
|
var lastLeaseEnd time.Time
|
||||||
|
@ -396,34 +401,19 @@ func (d *VmwareDriver) GuestIP(state multistep.StateBag) (string, error) {
|
||||||
var curIp string
|
var curIp string
|
||||||
var curLeaseEnd time.Time
|
var curLeaseEnd time.Time
|
||||||
|
|
||||||
ipLineRe := regexp.MustCompile(`^lease (.+?) {$`)
|
for _, entry := range leaseEntries {
|
||||||
endTimeLineRe := regexp.MustCompile(`^\s*ends \d (.+?);$`)
|
|
||||||
macLineRe := regexp.MustCompile(`^\s*hardware ethernet (.+?);$`)
|
|
||||||
|
|
||||||
for _, line := range strings.Split(string(dhcpBytes), "\n") {
|
lastIp = entry.address
|
||||||
// Need to trim off CR character when running in windows
|
lastLeaseEnd = entry.ends
|
||||||
line = strings.TrimRight(line, "\r")
|
|
||||||
|
|
||||||
matches := ipLineRe.FindStringSubmatch(line)
|
|
||||||
if matches != nil {
|
|
||||||
lastIp = matches[1]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
matches = endTimeLineRe.FindStringSubmatch(line)
|
|
||||||
if matches != nil {
|
|
||||||
lastLeaseEnd, _ = time.Parse("2006/01/02 15:04:05", matches[1])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the mac address matches and this lease ends farther in the
|
// If the mac address matches and this lease ends farther in the
|
||||||
// future than the last match we might have, then choose it.
|
// future than the last match we might have, then choose it.
|
||||||
matches = macLineRe.FindStringSubmatch(line)
|
if bytes.Compare(hwaddr, entry.ether) == 0 && curLeaseEnd.Before(lastLeaseEnd) {
|
||||||
if matches != nil && strings.EqualFold(matches[1], MACAddress) && curLeaseEnd.Before(lastLeaseEnd) {
|
|
||||||
curIp = lastIp
|
curIp = lastIp
|
||||||
curLeaseEnd = lastLeaseEnd
|
curLeaseEnd = lastLeaseEnd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if curIp != "" {
|
if curIp != "" {
|
||||||
return curIp, nil
|
return curIp, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue