Commit d44a42b0 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Merge pull request #884 from pmyjavec/vmware-iso-driver-fix

builder/vmware: get host IP by creating & inspecting connection, allows hypervisor...
parents 5f01415f aedef060
...@@ -126,27 +126,14 @@ func (d *ESX5Driver) Verify() error { ...@@ -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) {
......
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) { ...@@ -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))
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment