From aedef0601660f2dbcd5a8e8e2cbe751ca18e2376 Mon Sep 17 00:00:00 2001 From: Paul Myjavec Date: Thu, 13 Feb 2014 21:53:47 +1100 Subject: [PATCH] esx5, Get host IP by creating & inspecting connection, allows hypervisor to reside in other networks --- builder/vmware/iso/driver_esx5.go | 21 ++++----------------- builder/vmware/iso/driver_esx5_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/builder/vmware/iso/driver_esx5.go b/builder/vmware/iso/driver_esx5.go index 4226652ad..3314a7a9c 100644 --- a/builder/vmware/iso/driver_esx5.go +++ b/builder/vmware/iso/driver_esx5.go @@ -126,27 +126,14 @@ func (d *ESX5Driver) Verify() error { } func (d *ESX5Driver) HostIP() (string, error) { - ip := net.ParseIP(d.Host) - interfaces, err := net.Interfaces() + conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", d.Host, d.Port)) + defer conn.Close() if err != nil { return "", err } - for _, dev := range interfaces { - addrs, err := dev.Addrs() - if err != nil { - continue - } - for _, addr := range addrs { - if ipnet, ok := addr.(*net.IPNet); ok { - if ipnet.Contains(ip) { - return ipnet.IP.String(), nil - } - } - } - } - - return "", errors.New("Unable to determine Host IP") + host, _, err := net.SplitHostPort(conn.LocalAddr().String()) + return host, err } func (d *ESX5Driver) VNCAddress(portMin, portMax uint) (string, uint) { diff --git a/builder/vmware/iso/driver_esx5_test.go b/builder/vmware/iso/driver_esx5_test.go index 6c3cd23e2..f3f9df3c6 100644 --- a/builder/vmware/iso/driver_esx5_test.go +++ b/builder/vmware/iso/driver_esx5_test.go @@ -1,7 +1,9 @@ package iso import ( + "fmt" vmwcommon "github.com/mitchellh/packer/builder/vmware/common" + "net" "testing" ) @@ -16,3 +18,18 @@ func TestESX5Driver_implOutputDir(t *testing.T) { func TestESX5Driver_implRemoteDriver(t *testing.T) { var _ RemoteDriver = new(ESX5Driver) } + +func TestESX5Driver_HostIP(t *testing.T) { + expected_host := "127.0.0.1" + + //create mock SSH server + listen, _ := net.Listen("tcp", fmt.Sprintf("%s:0", expected_host)) + port := listen.Addr().(*net.TCPAddr).Port + defer listen.Close() + + driver := ESX5Driver{Host: "localhost", Port: uint(port)} + + if host, _ := driver.HostIP(); host != expected_host { + t.Error(fmt.Sprintf("Expected string, %s but got %s", expected_host, host)) + } +}