Merge pull request #9322 from arizvisa/GH-8528

Fix the VMware builders when the guest platform's dhcpcd switches the ip address in-between a build
pull/9368/head
Megan Marsh 6 years ago committed by GitHub
commit 093cfcfd9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -70,7 +70,7 @@ type Driver interface {
GuestAddress(multistep.StateBag) (string, error) GuestAddress(multistep.StateBag) (string, error)
// Get the guest ip address for the vm // Get the guest ip address for the vm
GuestIP(multistep.StateBag) (string, error) PotentialGuestIP(multistep.StateBag) ([]string, error)
// Get the host hw address for the vm // Get the host hw address for the vm
HostAddress(multistep.StateBag) (string, error) HostAddress(multistep.StateBag) (string, error)
@ -327,12 +327,12 @@ func (d *VmwareDriver) GuestAddress(state multistep.StateBag) (string, error) {
return res.String(), nil return res.String(), nil
} }
func (d *VmwareDriver) GuestIP(state multistep.StateBag) (string, error) { func (d *VmwareDriver) PotentialGuestIP(state multistep.StateBag) ([]string, error) {
// grab network mapper // grab network mapper
netmap, err := d.NetworkMapper() netmap, err := d.NetworkMapper()
if err != nil { if err != nil {
return "", err return []string{}, err
} }
// convert the stashed network to a device // convert the stashed network to a device
@ -350,14 +350,14 @@ func (d *VmwareDriver) GuestIP(state multistep.StateBag) (string, error) {
vmxPath := state.Get("vmx_path").(string) vmxPath := state.Get("vmx_path").(string)
vmxData, err := readVMXConfig(vmxPath) vmxData, err := readVMXConfig(vmxPath)
if err != nil { if err != nil {
return "", err return []string{}, err
} }
var device string var device string
device, err = readCustomDeviceName(vmxData) device, err = readCustomDeviceName(vmxData)
devices = append(devices, device) devices = append(devices, device)
if err != nil { if err != nil {
return "", err return []string{}, err
} }
log.Printf("GuestIP discovered custom device matching %s: %s", network, device) log.Printf("GuestIP discovered custom device matching %s: %s", network, device)
} }
@ -365,15 +365,19 @@ func (d *VmwareDriver) GuestIP(state multistep.StateBag) (string, error) {
// figure out our MAC address for looking up the guest address // figure out our MAC address for looking up the guest address
MACAddress, err := d.GuestAddress(state) MACAddress, err := d.GuestAddress(state)
if err != nil { if err != nil {
return "", err return []string{}, err
} }
// iterate through all of the devices and collect all the dhcp lease entries
// that we possibly cacn.
var available_lease_entries []dhcpLeaseEntry
for _, device := range devices { for _, device := range devices {
// figure out the correct dhcp leases // figure out the correct dhcp leases
dhcpLeasesPath := d.DhcpLeasesPath(device) dhcpLeasesPath := d.DhcpLeasesPath(device)
log.Printf("Trying DHCP leases path: %s", dhcpLeasesPath) log.Printf("Trying DHCP leases path: %s", dhcpLeasesPath)
if dhcpLeasesPath == "" { if dhcpLeasesPath == "" {
return "", fmt.Errorf("no DHCP leases path found for device %s", device) return []string{}, fmt.Errorf("no DHCP leases path found for device %s", device)
} }
// open up the path to the dhcpd leases // open up the path to the dhcpd leases
@ -387,39 +391,69 @@ func (d *VmwareDriver) GuestIP(state multistep.StateBag) (string, error) {
// and then read its contents // and then read its contents
leaseEntries, err := ReadDhcpdLeaseEntries(fh) leaseEntries, err := ReadDhcpdLeaseEntries(fh)
if err != nil { if err != nil {
return "", err return []string{}, err
} }
// Parse our MAC address again. There's no need to check for an // Parse our MAC address again. There's no need to check for an
// error because we've already parsed this successfully. // error because we've already parsed this successfully.
hwaddr, _ := net.ParseMAC(MACAddress) hwaddr, _ := net.ParseMAC(MACAddress)
// start grepping through the file looking for fields that we care about // Go through our available lease entries and see which ones are within
var lastIp string // scope, and that match to our hardware address.
var lastLeaseEnd time.Time results := make([]dhcpLeaseEntry, 0)
for _, entry := range leaseEntries {
var curIp string // First check for leases that are still valid. The timestamp for
var curLeaseEnd time.Time // each lease should be in UTC according to the documentation at
// the top of VMWare's dhcpd.leases file.
now := time.Now().UTC()
if !(now.After(entry.starts) && now.Before(entry.ends)) {
continue
}
for _, entry := range leaseEntries { // Next check for any where the hardware address matches.
if !bytes.Equal(hwaddr, entry.ether) {
continue
}
lastIp = entry.address // This entry fits within our constraints, so store it so we can
lastLeaseEnd = entry.ends // check it out later.
results = append(results, entry)
}
// If the mac address matches and this lease ends farther in the // If we weren't able to grab any results, then we'll do a "loose"-match
// future than the last match we might have, then choose it. // where we only look for anything where the hardware address matches.
if bytes.Equal(hwaddr, entry.ether) && curLeaseEnd.Before(lastLeaseEnd) { if len(results) == 0 {
curIp = lastIp log.Printf("Unable to find an exact match for DHCP lease. Falling back to a loose match for hw address %v", MACAddress)
curLeaseEnd = lastLeaseEnd for _, entry := range leaseEntries {
if bytes.Equal(hwaddr, entry.ether) {
results = append(results, entry)
}
} }
} }
if curIp != "" { // If we found something, then we need to add it to our current list
return curIp, nil // of lease entries.
if len(results) > 0 {
available_lease_entries = append(available_lease_entries, results...)
}
// Now we need to map our results to get the address so we can return it.iterate through our results and figure out which one
// is actually up...and should be relevant.
}
// Check if we found any lease entries that correspond to us. If so, then we
// need to map() them in order to extract the address field to return to the
// caller.
if len(available_lease_entries) > 0 {
addrs := make([]string, 0)
for _, entry := range available_lease_entries {
addrs = append(addrs, entry.address)
} }
return addrs, nil
} }
return "", fmt.Errorf("None of the found device(s) %v has a DHCP lease for MAC %s", devices, MACAddress) return []string{}, fmt.Errorf("None of the found device(s) %v has a DHCP lease for MAC %s", devices, MACAddress)
} }
func (d *VmwareDriver) HostAddress(state multistep.StateBag) (string, error) { func (d *VmwareDriver) HostAddress(state multistep.StateBag) (string, error) {

@ -266,16 +266,16 @@ func (d *ESX5Driver) HostIP(multistep.StateBag) (string, error) {
return host, err return host, err
} }
func (d *ESX5Driver) GuestIP(multistep.StateBag) (string, error) { func (d *ESX5Driver) PotentialGuestIP(multistep.StateBag) ([]string, error) {
// GuestIP is defined by the user as d.Host..but let's validate it just to be sure // GuestIP is defined by the user as d.Host..but let's validate it just to be sure
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", d.Host, d.Port)) conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", d.Host, d.Port))
if err != nil { if err != nil {
return "", err return []string{}, err
} }
defer conn.Close() defer conn.Close()
host, _, err := net.SplitHostPort(conn.RemoteAddr().String()) host, _, err := net.SplitHostPort(conn.RemoteAddr().String())
return host, err return []string{host}, err
} }
func (d *ESX5Driver) HostAddress(multistep.StateBag) (string, error) { func (d *ESX5Driver) HostAddress(multistep.StateBag) (string, error) {
@ -439,10 +439,7 @@ func (ESX5Driver) UpdateVMX(_, password string, port int, data map[string]string
func (d *ESX5Driver) CommHost(state multistep.StateBag) (string, error) { func (d *ESX5Driver) CommHost(state multistep.StateBag) (string, error) {
sshc := state.Get("sshConfig").(*SSHConfig).Comm sshc := state.Get("sshConfig").(*SSHConfig).Comm
port := sshc.SSHPort port := sshc.Port()
if sshc.Type == "winrm" {
port = sshc.WinRMPort
}
if address, ok := state.GetOk("vm_address"); ok { if address, ok := state.GetOk("vm_address"); ok {
return address.(string), nil return address.(string), nil

@ -52,10 +52,10 @@ type DriverMock struct {
GuestAddressResult string GuestAddressResult string
GuestAddressErr error GuestAddressErr error
GuestIPCalled bool PotentialGuestIPCalled bool
GuestIPState multistep.StateBag PotentialGuestIPState multistep.StateBag
GuestIPResult string PotentialGuestIPResult []string
GuestIPErr error PotentialGuestIPErr error
StartCalled bool StartCalled bool
StartPath string StartPath string
@ -192,10 +192,10 @@ func (d *DriverMock) GuestAddress(state multistep.StateBag) (string, error) {
return d.GuestAddressResult, d.GuestAddressErr return d.GuestAddressResult, d.GuestAddressErr
} }
func (d *DriverMock) GuestIP(state multistep.StateBag) (string, error) { func (d *DriverMock) PotentialGuestIP(state multistep.StateBag) ([]string, error) {
d.GuestIPCalled = true d.PotentialGuestIPCalled = true
d.GuestIPState = state d.PotentialGuestIPState = state
return d.GuestIPResult, d.GuestIPErr return d.PotentialGuestIPResult, d.PotentialGuestIPErr
} }
func (d *DriverMock) Start(path string, headless bool) error { func (d *DriverMock) Start(path string, headless bool) error {

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"net"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
) )
@ -11,23 +12,51 @@ import (
func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) { func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) { return func(state multistep.StateBag) (string, error) {
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
comm := config.Comm
if config.Comm.SSHHost != "" { host := comm.Host()
return config.Comm.SSHHost, nil if host != "" {
return host, nil
} }
ipAddress, err := driver.GuestIP(state) // Snag the port from the communicator config. This way we can use it
// to perform a 3-way handshake with all of the hosts we suspect in
// order to determine which one of the hosts is the correct one.
port := comm.Port()
// Get the list of potential addresses that the guest might use.
hosts, err := driver.PotentialGuestIP(state)
if err != nil { if err != nil {
log.Printf("IP lookup failed: %s", err) log.Printf("IP lookup failed: %s", err)
return "", fmt.Errorf("IP lookup failed: %s", err) return "", fmt.Errorf("IP lookup failed: %s", err)
} }
if ipAddress == "" { if len(hosts) == 0 {
log.Println("IP is blank, no IP yet.") log.Println("IP is blank, no IP yet.")
return "", errors.New("IP is blank") return "", errors.New("IP is blank")
} }
log.Printf("Detected IP: %s", ipAddress) // Iterate through our list of addresses and dial up each one similar to
return ipAddress, nil // a really inefficient port-scan. This way we can determine which of
// the leases that we've parsed was the correct one and actually has our
// target ssh/winrm service bound to a tcp port.
for index, host := range hosts {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
// If we got a connection, then we should be good to go. Return the
// address to the caller and pray that things work out.
if err == nil {
conn.Close()
log.Printf("Detected IP: %s", host)
return host, nil
}
// Otherwise we need to iterate to the next entry and keep hoping.
log.Printf("Skipping lease entry #%d due to being unable to connect to the host (%s) with tcp port (%d).", 1+index, host, port)
}
return "", errors.New("Host is not up")
} }
} }

Loading…
Cancel
Save