esx5, Get host IP by creating & inspecting connection, allows hypervisor to reside in other networks

This commit is contained in:
Paul Myjavec 2014-02-13 21:53:47 +11:00
parent b4a57bd373
commit aedef06016
2 changed files with 21 additions and 17 deletions

View File

@ -126,27 +126,14 @@ func (d *ESX5Driver) Verify() error {
} }
func (d *ESX5Driver) HostIP() (string, error) { func (d *ESX5Driver) HostIP() (string, error) {
ip := net.ParseIP(d.Host) conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", d.Host, d.Port))
interfaces, err := net.Interfaces() defer conn.Close()
if err != nil { if err != nil {
return "", err return "", err
} }
for _, dev := range interfaces { host, _, err := net.SplitHostPort(conn.LocalAddr().String())
addrs, err := dev.Addrs() return host, err
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")
} }
func (d *ESX5Driver) VNCAddress(portMin, portMax uint) (string, uint) { func (d *ESX5Driver) VNCAddress(portMin, portMax uint) (string, uint) {

View File

@ -1,7 +1,9 @@
package iso package iso
import ( import (
"fmt"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common" vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
"net"
"testing" "testing"
) )
@ -16,3 +18,18 @@ func TestESX5Driver_implOutputDir(t *testing.T) {
func TestESX5Driver_implRemoteDriver(t *testing.T) { func TestESX5Driver_implRemoteDriver(t *testing.T) {
var _ RemoteDriver = new(ESX5Driver) 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))
}
}