mirror of https://github.com/hashicorp/packer
Also added VMWare Player support in Windows Fixes #972pull/1168/head
parent
38d1d7fd3c
commit
750ffc8a54
@ -0,0 +1,175 @@
|
||||
// +build windows
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func playerFindVdiskManager() (string, error) {
|
||||
path, err := exec.LookPath("vmware-vdiskmanager.exe")
|
||||
if err == nil {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
return findFile("vmware-vdiskmanager.exe", playerProgramFilePaths()), nil
|
||||
}
|
||||
|
||||
func playerFindQemuImg() (string, error) {
|
||||
path, err := exec.LookPath("qemu-img.exe")
|
||||
if err == nil {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
return findFile("qemu-img.exe", playerProgramFilePaths()), nil
|
||||
}
|
||||
|
||||
func playerFindVMware() (string, error) {
|
||||
path, err := exec.LookPath("vmplayer.exe")
|
||||
if err == nil {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
return findFile("vmplayer.exe", playerProgramFilePaths()), nil
|
||||
}
|
||||
|
||||
func playerFindVmrun() (string, error) {
|
||||
path, err := exec.LookPath("vmrun.exe")
|
||||
if err == nil {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
return findFile("vmrun.exe", playerProgramFilePaths()), nil
|
||||
}
|
||||
|
||||
func playerToolsIsoPath(flavor string) string {
|
||||
return findFile(flavor+".iso", playerProgramFilePaths())
|
||||
}
|
||||
|
||||
func playerDhcpLeasesPath(device string) string {
|
||||
path, err := playerDhcpLeasesPathRegistry()
|
||||
if err != nil {
|
||||
log.Printf("Error finding leases in registry: %s", err)
|
||||
} else if _, err := os.Stat(path); err == nil {
|
||||
return path
|
||||
}
|
||||
|
||||
return findFile("vmnetdhcp.leases", playerDataFilePaths())
|
||||
}
|
||||
|
||||
func playerVmnetnatConfPath() string {
|
||||
return findFile("vmnetnat.conf", playerDataFilePaths())
|
||||
}
|
||||
|
||||
// This reads the VMware installation path from the Windows registry.
|
||||
func playerVMwareRoot() (s string, err error) {
|
||||
key := `SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmplayer.exe`
|
||||
subkey := "Path"
|
||||
s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
|
||||
if err != nil {
|
||||
log.Printf(`Unable to read registry key %s\%s`, key, subkey)
|
||||
return
|
||||
}
|
||||
|
||||
return normalizePath(s), nil
|
||||
}
|
||||
|
||||
// This reads the VMware DHCP leases path from the Windows registry.
|
||||
func playerDhcpLeasesPathRegistry() (s string, err error) {
|
||||
key := "SYSTEM\\CurrentControlSet\\services\\VMnetDHCP\\Parameters"
|
||||
subkey := "LeaseFile"
|
||||
s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
|
||||
if err != nil {
|
||||
log.Printf(`Unable to read registry key %s\%s`, key, subkey)
|
||||
return
|
||||
}
|
||||
|
||||
return normalizePath(s), nil
|
||||
}
|
||||
|
||||
// playerProgramFilesPaths returns a list of paths that are eligible
|
||||
// to contain program files we may want just as vmware.exe.
|
||||
func playerProgramFilePaths() []string {
|
||||
path, err := playerVMwareRoot()
|
||||
if err != nil {
|
||||
log.Printf("Error finding VMware root: %s", err)
|
||||
}
|
||||
|
||||
paths := make([]string, 0, 5)
|
||||
if os.Getenv("VMWARE_HOME") != "" {
|
||||
paths = append(paths, os.Getenv("VMWARE_HOME"))
|
||||
}
|
||||
|
||||
if path != "" {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
|
||||
if os.Getenv("ProgramFiles(x86)") != "" {
|
||||
paths = append(paths,
|
||||
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Player"))
|
||||
}
|
||||
|
||||
if os.Getenv("ProgramFiles") != "" {
|
||||
paths = append(paths,
|
||||
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Player"))
|
||||
}
|
||||
|
||||
if os.Getenv("QEMU_HOME") != "" {
|
||||
paths = append(paths, os.Getenv("QEMU_HOME"))
|
||||
}
|
||||
|
||||
if os.Getenv("ProgramFiles(x86)") != "" {
|
||||
paths = append(paths,
|
||||
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/QEMU"))
|
||||
}
|
||||
|
||||
if os.Getenv("ProgramFiles") != "" {
|
||||
paths = append(paths,
|
||||
filepath.Join(os.Getenv("ProgramFiles"), "/QEMU"))
|
||||
}
|
||||
|
||||
if os.Getenv("SystemDrive") != "" {
|
||||
paths = append(paths,
|
||||
filepath.Join(os.Getenv("SystemDrive"), "/QEMU"))
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
// playerDataFilePaths returns a list of paths that are eligible
|
||||
// to contain data files we may want such as vmnet NAT configuration files.
|
||||
func playerDataFilePaths() []string {
|
||||
leasesPath, err := playerDhcpLeasesPathRegistry()
|
||||
if err != nil {
|
||||
log.Printf("Error getting DHCP leases path: %s", err)
|
||||
}
|
||||
|
||||
if leasesPath != "" {
|
||||
leasesPath = filepath.Dir(leasesPath)
|
||||
}
|
||||
|
||||
paths := make([]string, 0, 5)
|
||||
if os.Getenv("VMWARE_DATA") != "" {
|
||||
paths = append(paths, os.Getenv("VMWARE_DATA"))
|
||||
}
|
||||
|
||||
if leasesPath != "" {
|
||||
paths = append(paths, leasesPath)
|
||||
}
|
||||
|
||||
if os.Getenv("ProgramData") != "" {
|
||||
paths = append(paths,
|
||||
filepath.Join(os.Getenv("ProgramData"), "/VMware"))
|
||||
}
|
||||
|
||||
if os.Getenv("ALLUSERSPROFILE") != "" {
|
||||
paths = append(paths,
|
||||
filepath.Join(os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMware"))
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const VMWARE_PLAYER_VERSION = "6"
|
||||
|
||||
// Player6Driver is a driver that can run VMware Player 6
|
||||
// installations.
|
||||
|
||||
type Player6Driver struct {
|
||||
Player5Driver
|
||||
}
|
||||
|
||||
func (d *Player6Driver) Clone(dst, src string) error {
|
||||
// TODO(rasa) check if running player+, not just player
|
||||
|
||||
cmd := exec.Command(d.Player5Driver.VmrunPath,
|
||||
"-T", "ws",
|
||||
"clone", src, dst,
|
||||
"full")
|
||||
|
||||
if _, _, err := runAndLog(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Player6Driver) Verify() error {
|
||||
if err := d.Player5Driver.Verify(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return playerVerifyVersion(VMWARE_PLAYER_VERSION)
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
// +build windows
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func playerVerifyVersion(version string) error {
|
||||
key := `SOFTWARE\Wow6432Node\VMware, Inc.\VMware Player`
|
||||
subkey := "ProductVersion"
|
||||
productVersion, err := readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
|
||||
if err != nil {
|
||||
log.Printf(`Unable to read registry key %s\%s`, key, subkey)
|
||||
key = `SOFTWARE\VMware, Inc.\VMware Player`
|
||||
productVersion, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
|
||||
if err != nil {
|
||||
log.Printf(`Unable to read registry key %s\%s`, key, subkey)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
versionRe := regexp.MustCompile(`^(\d+)\.`)
|
||||
matches := versionRe.FindStringSubmatch(productVersion)
|
||||
if matches == nil {
|
||||
return fmt.Errorf(
|
||||
`Could not find a VMware Player version in registry key %s\%s: '%s'`, key, subkey, productVersion)
|
||||
}
|
||||
log.Printf("Detected VMware Player version: %s", matches[1])
|
||||
|
||||
return compareVersions(matches[1], version)
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
// +build !windows
|
||||
|
||||
// These functions are compatible with WS 9 and 10 on *NIX
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func playerFindVdiskManager() (string, error) {
|
||||
return exec.LookPath("vmware-vdiskmanager")
|
||||
}
|
||||
|
||||
func playerFindQemuImg() (string, error) {
|
||||
return exec.LookPath("qemu-img")
|
||||
}
|
||||
|
||||
func playerFindVMware() (string, error) {
|
||||
return exec.LookPath("vmplayer")
|
||||
}
|
||||
|
||||
func playerFindVmrun() (string, error) {
|
||||
return exec.LookPath("vmrun")
|
||||
}
|
||||
|
||||
func playerDhcpLeasesPath(device string) string {
|
||||
return "/etc/vmware/" + device + "/dhcpd/dhcpd.leases"
|
||||
}
|
||||
|
||||
func playerToolsIsoPath(flavor string) string {
|
||||
return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
|
||||
}
|
||||
|
||||
func playerVmnetnatConfPath() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func playerVerifyVersion(version string) error {
|
||||
if runtime.GOOS != "linux" {
|
||||
return fmt.Errorf("The VMWare Player version %s driver is only supported on Linux, and Windows, at the moment. Your OS: %s", version, runtime.GOOS)
|
||||
}
|
||||
|
||||
//TODO(pmyjavec) there is a better way to find this, how?
|
||||
//the default will suffice for now.
|
||||
vmxpath := "/usr/lib/vmware/bin/vmware-vmx"
|
||||
|
||||
var stderr bytes.Buffer
|
||||
cmd := exec.Command(vmxpath, "-v")
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
versionRe := regexp.MustCompile(`(?i)VMware Player (\d+)\.`)
|
||||
matches := versionRe.FindStringSubmatch(stderr.String())
|
||||
if matches == nil {
|
||||
return fmt.Errorf(
|
||||
"Could not find VMWare Player version in output: %s", stderr.String())
|
||||
}
|
||||
log.Printf("Detected VMWare Player version: %s", matches[1])
|
||||
|
||||
return compareVersions(matches[1], version)
|
||||
}
|
||||
Loading…
Reference in new issue