builder/vmware: redo host IP stuff for Windows

This commit is contained in:
Mitchell Hashimoto 2013-07-31 14:46:12 -07:00
parent 5343bc42a0
commit 1ff52979fe
7 changed files with 120 additions and 155 deletions

View File

@ -2,44 +2,8 @@
package vmware
import (
"bytes"
"errors"
"os/exec"
"regexp"
)
// Interface to help find the host IP that is available from within
// the VMware virtual machines.
type HostIPFinder interface {
HostIP() (string, error)
}
// IfconfigIPFinder finds the host IP based on the output of `ifconfig`.
type IfconfigIPFinder struct {
Device string
}
func (f *IfconfigIPFinder) HostIP() (string, error) {
ifconfigPath, err := exec.LookPath("ifconfig")
if err != nil {
return "", err
}
stdout := new(bytes.Buffer)
cmd := exec.Command(ifconfigPath, f.Device)
cmd.Stdout = stdout
cmd.Stderr = new(bytes.Buffer)
if err := cmd.Run(); err != nil {
return "", err
}
re := regexp.MustCompile(`inet\s*(?:addr:)?(.+?)\s`)
matches := re.FindStringSubmatch(stdout.String())
if matches == nil {
return "", errors.New("IP not found in ifconfig output...")
}
return matches[1], nil
}

View File

@ -0,0 +1,37 @@
package vmware
import (
"bytes"
"errors"
"os/exec"
"regexp"
)
// IfconfigIPFinder finds the host IP based on the output of `ifconfig`.
type IfconfigIPFinder struct {
Device string
}
func (f *IfconfigIPFinder) HostIP() (string, error) {
ifconfigPath, err := exec.LookPath("ifconfig")
if err != nil {
return "", err
}
stdout := new(bytes.Buffer)
cmd := exec.Command(ifconfigPath, f.Device)
cmd.Stdout = stdout
cmd.Stderr = new(bytes.Buffer)
if err := cmd.Run(); err != nil {
return "", err
}
re := regexp.MustCompile(`inet\s*(?:addr:)?(.+?)\s`)
matches := re.FindStringSubmatch(stdout.String())
if matches == nil {
return "", errors.New("IP not found in ifconfig output...")
}
return matches[1], nil
}

View File

@ -0,0 +1,64 @@
package vmware
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
// VMnetNatConfIPFinder finds the IP address of the host machine by
// retrieving the IP from the vmnetnat.conf. This isn't a full proof
// technique but so far it has not failed.
type VMnetNatConfIPFinder struct{}
func (*VMnetNatConfIPFinder) HostIP() (string, error) {
programData := os.Getenv("ProgramData")
if programData == "" {
return "", errors.New("ProgramData directory not found.")
}
programData = strings.Replace(programData, "\\", "/", -1)
vmnetnat := filepath.Join(programData, "/VMware/vmnetnat.conf")
if _, err := os.Stat(vmnetnat); err != nil {
return "", fmt.Errorf("Error with vmnetnat.conf: %s", err)
}
f, err := os.Open(vmnetnat)
if err != nil {
return "", err
}
defer f.Close()
ipRe := regexp.MustCompile(`^\s*ip\s*=\s*(.+?)\s*$`)
r := bufio.NewReader(f)
for {
line, err := r.ReadString('\n')
if line != "" {
matches := ipRe.FindStringSubmatch(line)
if matches != nil {
ip := matches[1]
dotIndex := strings.LastIndex(ip, ".")
if dotIndex == -1 {
continue
}
ip = ip[0:dotIndex] + ".1"
return ip, nil
}
}
if err == io.EOF {
break
}
return "", err
}
return "", errors.New("host IP not found in NAT config")
}

View File

@ -0,0 +1,11 @@
package vmware
import "testing"
func TestVMnetNatConfIPFinder_Impl(t *testing.T) {
var raw interface{}
raw = &VMnetNatConfIPFinder{}
if _, ok := raw.(HostIPFinder); !ok {
t.Fatalf("VMnetNatConfIPFinder is not a host IP finder")
}
}

View File

@ -1,118 +0,0 @@
// +build windows
// Contributed by Ross Smith II (smithii.com)
package vmware
import (
"errors"
"io/ioutil"
"log"
"net"
"os"
"regexp"
"strings"
)
// Interface to help find the host IP that is available from within
// the VMware virtual machines.
type HostIPFinder interface {
HostIP() (string, error)
}
// IfconfigIPFinder finds the host IP based on the output of `ifconfig`.
type IfconfigIPFinder struct {
Device string
}
func (f *IfconfigIPFinder) HostIP() (string, error) {
ift, err := net.Interfaces()
if err != nil {
return "", errors.New("No network interfaces found")
}
vmwareMac, err := getVMWareMAC()
if err != nil {
log.Print(err)
}
log.Printf("Searching for MAC %s", vmwareMac)
re := regexp.MustCompile("(?i)^" + vmwareMac)
ip := ""
for _, ifi := range ift {
mac := ifi.HardwareAddr.String()
log.Printf("Found MAC %s", mac)
matches := re.FindStringSubmatch(mac)
if matches == nil {
continue
}
addrs, err := ifi.Addrs()
if err != nil {
log.Printf("No IP addresses found for MAC %s", mac)
continue
}
for _, address := range addrs {
ip = address.String()
log.Printf("Found IP address %s for MAC %s", ip, mac)
}
// continue looping as VMNet8 comes after VMNet1 (at least on my system)
}
if ip == "" {
return "", errors.New("No MACs found matching " + vmwareMac)
}
log.Printf("Returning IP address %s", ip)
return ip, nil
}
func getVMWareMAC() (string, error) {
// return the first three tuples, if the actual MAC cannot be found
const defaultMacRe = "00:50:56"
programData := os.Getenv("ProgramData")
programData = strings.Replace(programData, "\\", "/", -1)
vmnetnat := programData + "/VMware/vmnetnat.conf"
if _, err := os.Stat(vmnetnat); os.IsNotExist(err) {
log.Printf("File not found: '%s' (found '%s' in %%ProgramData%%)", vmnetnat, programData)
return defaultMacRe, err
}
log.Printf("Searching for key hostMAC in '%s'", vmnetnat)
fh, err := os.Open(vmnetnat)
if err != nil {
return defaultMacRe, err
}
defer fh.Close()
bytes, err := ioutil.ReadAll(fh)
if err != nil {
return defaultMacRe, err
}
hostMacRe := regexp.MustCompile(`(?i)^\s*hostMAC\s*=\s*(.+)\s*$`)
for _, line := range strings.Split(string(bytes), "\n") {
// Need to trim off CR character when running in windows
line = strings.TrimRight(line, "\r")
matches := hostMacRe.FindStringSubmatch(line)
if matches != nil {
log.Printf("Found MAC '%s' in '%s'", matches[1], vmnetnat)
return matches[1], nil
}
}
log.Printf("Did not find key hostMAC in '%s', using %s instead", vmnetnat, defaultMacRe)
return defaultMacRe, nil
}

View File

@ -8,6 +8,7 @@ import (
"github.com/mitchellh/packer/packer"
"log"
"net"
"runtime"
"strings"
"text/template"
"time"
@ -64,7 +65,13 @@ func (s *stepTypeBootCommand) Run(state map[string]interface{}) multistep.StepAc
log.Printf("Connected to VNC desktop: %s", c.DesktopName)
// Determine the host IP
ipFinder := &IfconfigIPFinder{"vmnet8"}
var ipFinder HostIPFinder
if runtime.GOOS == "windows" {
ipFinder = new(VMnetNatConfIPFinder)
} else {
ipFinder = &IfconfigIPFinder{Device: "vmnet8"}
}
hostIp, err := ipFinder.HostIP()
if err != nil {
err := fmt.Errorf("Error detecting host IP: %s", err)