From 9489c83f0f23ba415765925c1bb05978f248af14 Mon Sep 17 00:00:00 2001 From: Jeremiah Roth Date: Fri, 19 Aug 2016 05:23:43 -0600 Subject: [PATCH] If the VM has more than one NIC, loop through until we find one that works (#3347) --- builder/vmware/iso/driver_esx5.go | 39 +++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/builder/vmware/iso/driver_esx5.go b/builder/vmware/iso/driver_esx5.go index a8b5d92cf..fa8c5df12 100644 --- a/builder/vmware/iso/driver_esx5.go +++ b/builder/vmware/iso/driver_esx5.go @@ -268,18 +268,37 @@ func (d *ESX5Driver) CommHost(state multistep.StateBag) (string, error) { return "", err } - record, err = r.read() - if err != nil { - return "", err - } + // Loop through interfaces + for { + record, err = r.read() + if err == io.EOF { + break + } + if err != nil { + return "", err + } - if record["IPAddress"] == "0.0.0.0" { - return "", errors.New("VM network port found, but no IP address") + if record["IPAddress"] == "0.0.0.0" { + continue + } + // When multiple NICs are connected to the same network, choose + // one that has a route back. This Dial should ensure that. + conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", record["IPAddress"], d.Port), 2*time.Second) + if err != nil { + if e, ok := err.(*net.OpError); ok { + if e.Timeout() { + log.Printf("Timeout connecting to %s", record["IPAddress"]) + continue + } + } + } else { + defer conn.Close() + address := record["IPAddress"] + state.Put("vm_address", address) + return address, nil + } } - - address := record["IPAddress"] - state.Put("vm_address", address) - return address, nil + return "", errors.New("No interface on the VM has an IP address ready") } //-------------------------------------------------------------------