gofmt fixes, improved/added log messages, fixes #221/#222
This commit is contained in:
parent
1cc36f11bc
commit
2e4585bc9c
|
@ -11,8 +11,8 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
"syscall"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Workstation9Driver is a driver that can run VMware Workstation 9
|
// Workstation9Driver is a driver that can run VMware Workstation 9
|
||||||
|
@ -104,16 +104,16 @@ func (d *Workstation9Driver) Verify() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to see if it APPEARS to be licensed.
|
// Check to see if it APPEARS to be licensed.
|
||||||
/*
|
/*
|
||||||
matches, err := filepath.Glob("/etc/vmware/license-*")
|
matches, err := filepath.Glob("/etc/vmware/license-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error looking for VMware license: %s", err)
|
return fmt.Errorf("Error looking for VMware license: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(matches) == 0 {
|
if len(matches) == 0 {
|
||||||
return errors.New("Workstation does not appear to be licensed. Please license it.")
|
return errors.New("Workstation does not appear to be licensed. Please license it.")
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,6 +126,7 @@ func (d *Workstation9Driver) findApp() error {
|
||||||
}
|
}
|
||||||
path += "vmware.exe"
|
path += "vmware.exe"
|
||||||
}
|
}
|
||||||
|
path = strings.Replace(path, "\\", "/", -1)
|
||||||
log.Printf("Using '%s' for vmware path", path)
|
log.Printf("Using '%s' for vmware path", path)
|
||||||
d.AppPath = path
|
d.AppPath = path
|
||||||
|
|
||||||
|
@ -141,6 +142,7 @@ func (d *Workstation9Driver) findVdiskManager() error {
|
||||||
}
|
}
|
||||||
path += "vmware-vdiskmanager.exe"
|
path += "vmware-vdiskmanager.exe"
|
||||||
}
|
}
|
||||||
|
path = strings.Replace(path, "\\", "/", -1)
|
||||||
log.Printf("Using '%s' for vmware-vdiskmanager path", path)
|
log.Printf("Using '%s' for vmware-vdiskmanager path", path)
|
||||||
d.VdiskManagerPath = path
|
d.VdiskManagerPath = path
|
||||||
return nil
|
return nil
|
||||||
|
@ -155,6 +157,7 @@ func (d *Workstation9Driver) findVmrun() error {
|
||||||
}
|
}
|
||||||
path += "vmrun.exe"
|
path += "vmrun.exe"
|
||||||
}
|
}
|
||||||
|
path = strings.Replace(path, "\\", "/", -1)
|
||||||
log.Printf("Using '%s' for vmrun path", path)
|
log.Printf("Using '%s' for vmrun path", path)
|
||||||
d.VmrunPath = path
|
d.VmrunPath = path
|
||||||
return nil
|
return nil
|
||||||
|
@ -203,41 +206,41 @@ func (d *Workstation9Driver) runAndLog(cmd *exec.Cmd) (string, string, error) {
|
||||||
// see http://blog.natefinch.com/2012/11/go-win-stuff.html
|
// see http://blog.natefinch.com/2012/11/go-win-stuff.html
|
||||||
|
|
||||||
func readRegString(hive syscall.Handle, subKeyPath, valueName string) (value string, err error) {
|
func readRegString(hive syscall.Handle, subKeyPath, valueName string) (value string, err error) {
|
||||||
var h syscall.Handle
|
var h syscall.Handle
|
||||||
err = syscall.RegOpenKeyEx(hive, syscall.StringToUTF16Ptr(subKeyPath), 0, syscall.KEY_READ, &h)
|
err = syscall.RegOpenKeyEx(hive, syscall.StringToUTF16Ptr(subKeyPath), 0, syscall.KEY_READ, &h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer syscall.RegCloseKey(h)
|
defer syscall.RegCloseKey(h)
|
||||||
|
|
||||||
var typ uint32
|
var typ uint32
|
||||||
var bufSize uint32
|
var bufSize uint32
|
||||||
|
|
||||||
err = syscall.RegQueryValueEx(
|
err = syscall.RegQueryValueEx(
|
||||||
h,
|
h,
|
||||||
syscall.StringToUTF16Ptr(valueName),
|
syscall.StringToUTF16Ptr(valueName),
|
||||||
nil,
|
nil,
|
||||||
&typ,
|
&typ,
|
||||||
nil,
|
nil,
|
||||||
&bufSize)
|
&bufSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := make([]uint16, bufSize/2+1)
|
data := make([]uint16, bufSize/2+1)
|
||||||
|
|
||||||
err = syscall.RegQueryValueEx(
|
err = syscall.RegQueryValueEx(
|
||||||
h,
|
h,
|
||||||
syscall.StringToUTF16Ptr(valueName),
|
syscall.StringToUTF16Ptr(valueName),
|
||||||
nil,
|
nil,
|
||||||
&typ,
|
&typ,
|
||||||
(*byte)(unsafe.Pointer(&data[0])),
|
(*byte)(unsafe.Pointer(&data[0])),
|
||||||
&bufSize)
|
&bufSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return syscall.UTF16ToString(data), nil
|
return syscall.UTF16ToString(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVmwarePath() (s string, e error) {
|
func getVmwarePath() (s string, e error) {
|
||||||
|
|
|
@ -51,7 +51,7 @@ func (f *DHCPLeaseGuestLookup) GuestIP() (string, error) {
|
||||||
|
|
||||||
for _, line := range strings.Split(string(dhcpBytes), "\n") {
|
for _, line := range strings.Split(string(dhcpBytes), "\n") {
|
||||||
// Need to trim off CR character when running in windows
|
// Need to trim off CR character when running in windows
|
||||||
line = strings.TrimRight(line, "\r");
|
line = strings.TrimRight(line, "\r")
|
||||||
|
|
||||||
matches := ipLineRe.FindStringSubmatch(line)
|
matches := ipLineRe.FindStringSubmatch(line)
|
||||||
if matches != nil {
|
if matches != nil {
|
||||||
|
|
|
@ -5,12 +5,12 @@ package vmware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"regexp"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"io/ioutil"
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Interface to help find the host IP that is available from within
|
// Interface to help find the host IP that is available from within
|
||||||
|
@ -38,12 +38,13 @@ func (f *IfconfigIPFinder) HostIP() (string, error) {
|
||||||
log.Printf("Searching for MAC %s", vmwareMac)
|
log.Printf("Searching for MAC %s", vmwareMac)
|
||||||
re := regexp.MustCompile("(?i)^" + vmwareMac)
|
re := regexp.MustCompile("(?i)^" + vmwareMac)
|
||||||
|
|
||||||
var rv string
|
ip := ""
|
||||||
|
|
||||||
for _, ifi := range ift {
|
for _, ifi := range ift {
|
||||||
log.Printf("Found interface %s", ifi.HardwareAddr.String())
|
mac := ifi.HardwareAddr.String()
|
||||||
|
log.Printf("Found MAC %s", mac)
|
||||||
|
|
||||||
matches := re.FindStringSubmatch(ifi.HardwareAddr.String())
|
matches := re.FindStringSubmatch(mac)
|
||||||
|
|
||||||
if matches == nil {
|
if matches == nil {
|
||||||
continue
|
continue
|
||||||
|
@ -51,23 +52,25 @@ func (f *IfconfigIPFinder) HostIP() (string, error) {
|
||||||
|
|
||||||
addrs, err := ifi.Addrs()
|
addrs, err := ifi.Addrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("No IP addresses found for %s", ifi.HardwareAddr.String())
|
log.Printf("No IP addresses found for MAC %s", mac)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, address := range addrs {
|
for _, address := range addrs {
|
||||||
rv = address.String()
|
ip = address.String()
|
||||||
log.Printf("Found VMWare IP address %s", 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)
|
// continue looping as VMNet8 comes after VMNet1 (at least on my system)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rv > "" {
|
if ip == "" {
|
||||||
return rv, nil
|
return "", errors.New("No MACs found matching " + vmwareMac)
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("No VMWare MAC addresses found")
|
log.Printf("Returning IP address %s", ip)
|
||||||
|
|
||||||
|
return ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVMWareMAC() (string, error) {
|
func getVMWareMAC() (string, error) {
|
||||||
|
@ -75,12 +78,15 @@ func getVMWareMAC() (string, error) {
|
||||||
const defaultMacRe = "00:50:56"
|
const defaultMacRe = "00:50:56"
|
||||||
|
|
||||||
programData := os.Getenv("ProgramData")
|
programData := os.Getenv("ProgramData")
|
||||||
|
programData = strings.Replace(programData, "\\", "/", -1)
|
||||||
vmnetnat := programData + "/VMware/vmnetnat.conf"
|
vmnetnat := programData + "/VMware/vmnetnat.conf"
|
||||||
if _, err := os.Stat(vmnetnat); os.IsNotExist(err) {
|
if _, err := os.Stat(vmnetnat); os.IsNotExist(err) {
|
||||||
log.Printf("File not found: '%s' (found '%s' in %%ProgramData%%)", vmnetnat, programData)
|
log.Printf("File not found: '%s' (found '%s' in %%ProgramData%%)", vmnetnat, programData)
|
||||||
return defaultMacRe, err
|
return defaultMacRe, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Searching for key hostMAC in '%s'", vmnetnat)
|
||||||
|
|
||||||
fh, err := os.Open(vmnetnat)
|
fh, err := os.Open(vmnetnat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultMacRe, err
|
return defaultMacRe, err
|
||||||
|
@ -96,14 +102,17 @@ func getVMWareMAC() (string, error) {
|
||||||
|
|
||||||
for _, line := range strings.Split(string(bytes), "\n") {
|
for _, line := range strings.Split(string(bytes), "\n") {
|
||||||
// Need to trim off CR character when running in windows
|
// Need to trim off CR character when running in windows
|
||||||
line = strings.TrimRight(line, "\r");
|
line = strings.TrimRight(line, "\r")
|
||||||
|
|
||||||
matches := hostMacRe.FindStringSubmatch(line)
|
matches := hostMacRe.FindStringSubmatch(line)
|
||||||
if matches != nil {
|
if matches != nil {
|
||||||
|
log.Printf("Found MAC '%s' in '%s'", matches[1], vmnetnat)
|
||||||
return matches[1], nil
|
return matches[1], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Did not find key hostMAC in '%s', using %s instead", vmnetnat, defaultMacRe)
|
||||||
|
|
||||||
return defaultMacRe, nil
|
return defaultMacRe, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue