This changeset performs the following: - Updates `masterzen/winrm` vendor to include change from (https://github.com/masterzen/winrm/pull/73). - Removes `masterzen/xmlpath` dependency, as it is licensed under the LGPL license. - Updates `dylanmei/winrmtest` vendor to include change from (https://github.com/dylanmei/winrmtest/pull/4). - Updates `packer-community/winrmcp` vendor to include the removal of the `masterzen/winrm/winrm` sub-class as a result of the `winrm` CLI tool being removed from the `masterzen/winrm` repository. - Updates nested dependencies as a result of the above vendor changes. - Refactors `parallels/driver_9` to use `goxpath` instead of `xmlpath`, as `xmlpath` is licensed under LGPL. - Adds a basic unit test for the refactor work in `parallels/driver_9` to confirm functionality. This should completely remove any LGPL licensed dependencies inside of the Packer project. ``` $ make test /home/jake/src/go/src/github.com/hashicorp/packer/scripts/gofmtcheck.sh $(find . -not -path "./vendor/*" -name "*.go") ==> Checking that code complies with gofmt requirements... Check passed. ok github.com/hashicorp/packer 0.044s ok github.com/hashicorp/packer/builder/alicloud/ecs 0.055s ok github.com/hashicorp/packer/builder/amazon/chroot 0.040s ok github.com/hashicorp/packer/builder/amazon/common 0.021s ok github.com/hashicorp/packer/builder/amazon/ebs 0.016s ok github.com/hashicorp/packer/builder/amazon/ebssurrogate 0.015s ok github.com/hashicorp/packer/builder/amazon/ebsvolume 0.014s ok github.com/hashicorp/packer/builder/amazon/instance 0.058s ok github.com/hashicorp/packer/builder/azure/arm 2.833s ok github.com/hashicorp/packer/builder/azure/common 0.018s ? github.com/hashicorp/packer/builder/azure/common/constants [no test files] ? github.com/hashicorp/packer/builder/azure/common/lin [no test files] ? github.com/hashicorp/packer/builder/azure/common/logutil [no test files] ok github.com/hashicorp/packer/builder/azure/common/template 0.038s ok github.com/hashicorp/packer/builder/azure/pkcs12 0.120s ok github.com/hashicorp/packer/builder/azure/pkcs12/rc2 0.020s ok github.com/hashicorp/packer/builder/cloudstack 0.038s ok github.com/hashicorp/packer/builder/digitalocean 0.015s ok github.com/hashicorp/packer/builder/docker 0.028s ok github.com/hashicorp/packer/builder/file 0.029s ok github.com/hashicorp/packer/builder/googlecompute 3.162s ok github.com/hashicorp/packer/builder/hyperv/common 0.006s ok github.com/hashicorp/packer/builder/hyperv/iso 0.042s ok github.com/hashicorp/packer/builder/null 0.018s ok github.com/hashicorp/packer/builder/oneandone 0.022s ok github.com/hashicorp/packer/builder/openstack 0.052s ok github.com/hashicorp/packer/builder/parallels/common 2.549s ok github.com/hashicorp/packer/builder/parallels/iso 0.050s ok github.com/hashicorp/packer/builder/parallels/pvm 0.033s ok github.com/hashicorp/packer/builder/profitbricks 0.023s ok github.com/hashicorp/packer/builder/qemu 0.039s ok github.com/hashicorp/packer/builder/triton 0.018s ok github.com/hashicorp/packer/builder/virtualbox/common 5.533s ok github.com/hashicorp/packer/builder/virtualbox/iso 0.044s ok github.com/hashicorp/packer/builder/virtualbox/ovf 0.012s ok github.com/hashicorp/packer/builder/vmware/common 5.325s ok github.com/hashicorp/packer/builder/vmware/iso 0.076s ok github.com/hashicorp/packer/builder/vmware/vmx 0.010s ok github.com/hashicorp/packer/command 0.062s ok github.com/hashicorp/packer/common 0.053s ok github.com/hashicorp/packer/common/json 0.003s [no tests to run] ok github.com/hashicorp/packer/common/powershell 0.015s ? github.com/hashicorp/packer/common/powershell/hyperv [no test files] ? github.com/hashicorp/packer/common/ssh [no test files] ok github.com/hashicorp/packer/common/uuid 0.010s ok github.com/hashicorp/packer/communicator/none 0.008s ok github.com/hashicorp/packer/communicator/ssh 0.068s ok github.com/hashicorp/packer/communicator/winrm 0.046s ok github.com/hashicorp/packer/fix 0.005s ok github.com/hashicorp/packer/helper/builder/testing 0.007s ok github.com/hashicorp/packer/helper/communicator 0.013s ok github.com/hashicorp/packer/helper/config 0.004s ? github.com/hashicorp/packer/helper/enumflag [no test files] ok github.com/hashicorp/packer/helper/flag-kv 0.009s ok github.com/hashicorp/packer/helper/flag-slice 0.006s ok github.com/hashicorp/packer/packer 0.219s ok github.com/hashicorp/packer/packer/plugin 0.233s ok github.com/hashicorp/packer/packer/rpc 0.141s ok github.com/hashicorp/packer/plugin/example 0.008s [no tests to run] ? github.com/hashicorp/packer/post-processor/alicloud-import [no test files] ? github.com/hashicorp/packer/post-processor/amazon-import [no test files] ok github.com/hashicorp/packer/post-processor/artifice 0.003s [no tests to run] ok github.com/hashicorp/packer/post-processor/atlas 0.018s ok github.com/hashicorp/packer/post-processor/checksum 0.015s ok github.com/hashicorp/packer/post-processor/compress 0.047s ok github.com/hashicorp/packer/post-processor/docker-import 0.012s ok github.com/hashicorp/packer/post-processor/docker-push 0.012s ok github.com/hashicorp/packer/post-processor/docker-save 0.008s ok github.com/hashicorp/packer/post-processor/docker-tag 0.008s ok github.com/hashicorp/packer/post-processor/googlecompute-export 0.015s [no tests to run] ? github.com/hashicorp/packer/post-processor/manifest [no test files] ok github.com/hashicorp/packer/post-processor/shell-local 0.028s ok github.com/hashicorp/packer/post-processor/vagrant 0.030s ok github.com/hashicorp/packer/post-processor/vagrant-cloud 0.028s ok github.com/hashicorp/packer/post-processor/vsphere 0.014s ok github.com/hashicorp/packer/provisioner 0.011s ok github.com/hashicorp/packer/provisioner/ansible 0.140s ok github.com/hashicorp/packer/provisioner/ansible-local 0.056s ok github.com/hashicorp/packer/provisioner/chef-client 0.050s ok github.com/hashicorp/packer/provisioner/chef-solo 0.061s ok github.com/hashicorp/packer/provisioner/converge 0.018s ok github.com/hashicorp/packer/provisioner/file 0.015s ok github.com/hashicorp/packer/provisioner/powershell 0.168s ok github.com/hashicorp/packer/provisioner/puppet-masterless 0.008s ok github.com/hashicorp/packer/provisioner/puppet-server 0.031s ok github.com/hashicorp/packer/provisioner/salt-masterless 0.010s ok github.com/hashicorp/packer/provisioner/shell 0.010s ok github.com/hashicorp/packer/provisioner/shell-local 0.037s ok github.com/hashicorp/packer/provisioner/windows-restart 0.180s ok github.com/hashicorp/packer/provisioner/windows-shell 0.159s ? github.com/hashicorp/packer/scripts [no test files] ok github.com/hashicorp/packer/template 0.008s ok github.com/hashicorp/packer/template/interpolate 0.004s ? github.com/hashicorp/packer/version [no test files] ```
429 lines
11 KiB
Go
429 lines
11 KiB
Go
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/ChrisTrenkamp/goxpath"
|
|
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
|
|
)
|
|
|
|
// Parallels9Driver is a base type for Parallels builders.
|
|
type Parallels9Driver struct {
|
|
// This is the path to the "prlctl" application.
|
|
PrlctlPath string
|
|
|
|
// This is the path to the "prlsrvctl" application.
|
|
PrlsrvctlPath string
|
|
|
|
// The path to the parallels_dhcp_leases file
|
|
dhcpLeaseFile string
|
|
}
|
|
|
|
// Import creates a clone of the source VM and reassigns the MAC address if needed.
|
|
func (d *Parallels9Driver) Import(name, srcPath, dstDir string, reassignMAC bool) error {
|
|
err := d.Prlctl("register", srcPath, "--preserve-uuid")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
srcID, err := getVMID(srcPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
srcMAC := "auto"
|
|
if !reassignMAC {
|
|
srcMAC, err = getFirtsMACAddress(srcPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
err = d.Prlctl("clone", srcID, "--name", name, "--dst", dstDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = d.Prlctl("unregister", srcID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = d.Prlctl("set", name, "--device-set", "net0", "--mac", srcMAC)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getVMID(path string) (string, error) {
|
|
return getConfigValueFromXpath(path, "/ParallelsVirtualMachine/Identification/VmUuid")
|
|
}
|
|
|
|
func getFirtsMACAddress(path string) (string, error) {
|
|
return getConfigValueFromXpath(path, "/ParallelsVirtualMachine/Hardware/NetworkAdapter[@id='0']/MAC")
|
|
}
|
|
|
|
func getConfigValueFromXpath(path, xpath string) (string, error) {
|
|
file, err := os.Open(path + "/config.pvs")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
doc, err := xmltree.ParseXML(file)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
xpExec := goxpath.MustParse(xpath)
|
|
node, err := xpExec.Exec(doc)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return node.String(), nil
|
|
}
|
|
|
|
// Finds an application bundle by identifier (for "darwin" platform only)
|
|
func getAppPath(bundleID string) (string, error) {
|
|
var stdout bytes.Buffer
|
|
|
|
cmd := exec.Command("mdfind", "kMDItemCFBundleIdentifier ==", bundleID)
|
|
cmd.Stdout = &stdout
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
pathOutput := strings.TrimSpace(stdout.String())
|
|
if pathOutput == "" {
|
|
if fi, err := os.Stat("/Applications/Parallels Desktop.app"); err == nil {
|
|
if fi.IsDir() {
|
|
return "/Applications/Parallels Desktop.app", nil
|
|
}
|
|
}
|
|
|
|
return "", fmt.Errorf(
|
|
"Could not detect Parallels Desktop! Make sure it is properly installed.")
|
|
}
|
|
|
|
return pathOutput, nil
|
|
}
|
|
|
|
// CompactDisk performs the compation of the specified virtual disk image.
|
|
func (d *Parallels9Driver) CompactDisk(diskPath string) error {
|
|
prlDiskToolPath, err := exec.LookPath("prl_disk_tool")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Analyze the disk content and remove unused blocks
|
|
command := []string{
|
|
"compact",
|
|
"--hdd", diskPath,
|
|
}
|
|
if err := exec.Command(prlDiskToolPath, command...).Run(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Remove null blocks
|
|
command = []string{
|
|
"compact", "--buildmap",
|
|
"--hdd", diskPath,
|
|
}
|
|
if err := exec.Command(prlDiskToolPath, command...).Run(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeviceAddCDROM adds a virtual CDROM device and attaches the specified image.
|
|
func (d *Parallels9Driver) DeviceAddCDROM(name string, image string) (string, error) {
|
|
command := []string{
|
|
"set", name,
|
|
"--device-add", "cdrom",
|
|
"--image", image,
|
|
}
|
|
|
|
out, err := exec.Command(d.PrlctlPath, command...).Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
deviceRe := regexp.MustCompile(`\s+(cdrom\d+)\s+`)
|
|
matches := deviceRe.FindStringSubmatch(string(out))
|
|
if matches == nil {
|
|
return "", fmt.Errorf(
|
|
"Could not determine cdrom device name in the output:\n%s", string(out))
|
|
}
|
|
|
|
deviceName := matches[1]
|
|
return deviceName, nil
|
|
}
|
|
|
|
// DiskPath returns a full path to the first virtual disk drive.
|
|
func (d *Parallels9Driver) DiskPath(name string) (string, error) {
|
|
out, err := exec.Command(d.PrlctlPath, "list", "-i", name).Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
HDDRe := regexp.MustCompile("hdd0.* image='(.*)' type=*")
|
|
matches := HDDRe.FindStringSubmatch(string(out))
|
|
if matches == nil {
|
|
return "", fmt.Errorf(
|
|
"Could not determine hdd image path in the output:\n%s", string(out))
|
|
}
|
|
|
|
HDDPath := matches[1]
|
|
return HDDPath, nil
|
|
}
|
|
|
|
// IsRunning determines whether the VM is running or not.
|
|
func (d *Parallels9Driver) IsRunning(name string) (bool, error) {
|
|
var stdout bytes.Buffer
|
|
|
|
cmd := exec.Command(d.PrlctlPath, "list", name, "--no-header", "--output", "status")
|
|
cmd.Stdout = &stdout
|
|
if err := cmd.Run(); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
log.Printf("Checking VM state: %s\n", strings.TrimSpace(stdout.String()))
|
|
|
|
for _, line := range strings.Split(stdout.String(), "\n") {
|
|
if line == "running" {
|
|
return true, nil
|
|
}
|
|
|
|
if line == "suspended" {
|
|
return true, nil
|
|
}
|
|
if line == "paused" {
|
|
return true, nil
|
|
}
|
|
if line == "stopping" {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// Stop forcibly stops the VM.
|
|
func (d *Parallels9Driver) Stop(name string) error {
|
|
if err := d.Prlctl("stop", name); err != nil {
|
|
return err
|
|
}
|
|
|
|
// We sleep here for a little bit to let the session "unlock"
|
|
time.Sleep(2 * time.Second)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Prlctl executes the specified "prlctl" command.
|
|
func (d *Parallels9Driver) Prlctl(args ...string) error {
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
log.Printf("Executing prlctl: %#v", args)
|
|
cmd := exec.Command(d.PrlctlPath, args...)
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
err := cmd.Run()
|
|
|
|
stdoutString := strings.TrimSpace(stdout.String())
|
|
stderrString := strings.TrimSpace(stderr.String())
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
err = fmt.Errorf("prlctl error: %s", stderrString)
|
|
}
|
|
|
|
log.Printf("stdout: %s", stdoutString)
|
|
log.Printf("stderr: %s", stderrString)
|
|
|
|
return err
|
|
}
|
|
|
|
// Verify raises an error if the builder could not be used on that host machine.
|
|
func (d *Parallels9Driver) Verify() error {
|
|
return nil
|
|
}
|
|
|
|
// Version returns the version of Parallels Desktop installed on that host.
|
|
func (d *Parallels9Driver) Version() (string, error) {
|
|
out, err := exec.Command(d.PrlctlPath, "--version").Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
versionRe := regexp.MustCompile(`prlctl version (\d+\.\d+.\d+)`)
|
|
matches := versionRe.FindStringSubmatch(string(out))
|
|
if matches == nil {
|
|
return "", fmt.Errorf(
|
|
"Could not find Parallels Desktop version in output:\n%s", string(out))
|
|
}
|
|
|
|
version := matches[1]
|
|
log.Printf("Parallels Desktop version: %s", version)
|
|
return version, nil
|
|
}
|
|
|
|
// SendKeyScanCodes sends the specified scancodes as key events to the VM.
|
|
// It is performed using "Prltype" script (refer to "prltype.go").
|
|
func (d *Parallels9Driver) SendKeyScanCodes(vmName string, codes ...string) error {
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
if codes == nil || len(codes) == 0 {
|
|
log.Printf("No scan codes to send")
|
|
return nil
|
|
}
|
|
|
|
f, err := ioutil.TempFile("", "prltype")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(f.Name())
|
|
|
|
script := []byte(Prltype)
|
|
_, err = f.Write(script)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
args := prepend(vmName, codes)
|
|
args = prepend(f.Name(), args)
|
|
cmd := exec.Command("/usr/bin/python", args...)
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
err = cmd.Run()
|
|
|
|
stdoutString := strings.TrimSpace(stdout.String())
|
|
stderrString := strings.TrimSpace(stderr.String())
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
err = fmt.Errorf("prltype error: %s", stderrString)
|
|
}
|
|
|
|
log.Printf("stdout: %s", stdoutString)
|
|
log.Printf("stderr: %s", stderrString)
|
|
|
|
return err
|
|
}
|
|
|
|
func prepend(head string, tail []string) []string {
|
|
tmp := make([]string, len(tail)+1)
|
|
for i := 0; i < len(tail); i++ {
|
|
tmp[i+1] = tail[i]
|
|
}
|
|
tmp[0] = head
|
|
return tmp
|
|
}
|
|
|
|
// SetDefaultConfiguration applies pre-defined default settings to the VM config.
|
|
func (d *Parallels9Driver) SetDefaultConfiguration(vmName string) error {
|
|
commands := make([][]string, 7)
|
|
commands[0] = []string{"set", vmName, "--cpus", "1"}
|
|
commands[1] = []string{"set", vmName, "--memsize", "512"}
|
|
commands[2] = []string{"set", vmName, "--startup-view", "same"}
|
|
commands[3] = []string{"set", vmName, "--on-shutdown", "close"}
|
|
commands[4] = []string{"set", vmName, "--on-window-close", "keep-running"}
|
|
commands[5] = []string{"set", vmName, "--auto-share-camera", "off"}
|
|
commands[6] = []string{"set", vmName, "--smart-guard", "off"}
|
|
|
|
for _, command := range commands {
|
|
err := d.Prlctl(command...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MAC returns the MAC address of the VM's first network interface.
|
|
func (d *Parallels9Driver) MAC(vmName string) (string, error) {
|
|
var stdout bytes.Buffer
|
|
|
|
cmd := exec.Command(d.PrlctlPath, "list", "-i", vmName)
|
|
cmd.Stdout = &stdout
|
|
if err := cmd.Run(); err != nil {
|
|
log.Printf("MAC address for NIC: nic0 on Virtual Machine: %s not found!\n", vmName)
|
|
return "", err
|
|
}
|
|
|
|
stdoutString := strings.TrimSpace(stdout.String())
|
|
re := regexp.MustCompile("net0.* mac=([0-9A-F]{12}) card=.*")
|
|
macMatch := re.FindAllStringSubmatch(stdoutString, 1)
|
|
|
|
if len(macMatch) != 1 {
|
|
return "", fmt.Errorf("MAC address for NIC: nic0 on Virtual Machine: %s not found!\n", vmName)
|
|
}
|
|
|
|
mac := macMatch[0][1]
|
|
log.Printf("Found MAC address for NIC: net0 - %s\n", mac)
|
|
return mac, nil
|
|
}
|
|
|
|
// IPAddress finds the IP address of a VM connected that uses DHCP by its MAC address
|
|
//
|
|
// Parses the file /Library/Preferences/Parallels/parallels_dhcp_leases
|
|
// file contain a list of DHCP leases given by Parallels Desktop
|
|
// Example line:
|
|
// 10.211.55.181="1418921112,1800,001c42f593fb,ff42f593fb000100011c25b9ff001c42f593fb"
|
|
// IP Address ="Lease expiry, Lease time, MAC, MAC or DUID"
|
|
func (d *Parallels9Driver) IPAddress(mac string) (string, error) {
|
|
|
|
if len(mac) != 12 {
|
|
return "", fmt.Errorf("Not a valid MAC address: %s. It should be exactly 12 digits.", mac)
|
|
}
|
|
|
|
leases, err := ioutil.ReadFile(d.dhcpLeaseFile)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
re := regexp.MustCompile("(.*)=\"(.*),(.*)," + strings.ToLower(mac) + ",.*\"")
|
|
mostRecentIP := ""
|
|
mostRecentLease := uint64(0)
|
|
for _, l := range re.FindAllStringSubmatch(string(leases), -1) {
|
|
ip := l[1]
|
|
expiry, _ := strconv.ParseUint(l[2], 10, 64)
|
|
leaseTime, _ := strconv.ParseUint(l[3], 10, 32)
|
|
log.Printf("Found lease: %s for MAC: %s, expiring at %d, leased for %d s.\n", ip, mac, expiry, leaseTime)
|
|
if mostRecentLease <= expiry-leaseTime {
|
|
mostRecentIP = ip
|
|
mostRecentLease = expiry - leaseTime
|
|
}
|
|
}
|
|
|
|
if len(mostRecentIP) == 0 {
|
|
return "", fmt.Errorf("IP lease not found for MAC address %s in: %s\n", mac, d.dhcpLeaseFile)
|
|
}
|
|
|
|
log.Printf("Found IP lease: %s for MAC address %s\n", mostRecentIP, mac)
|
|
return mostRecentIP, nil
|
|
}
|
|
|
|
// ToolsISOPath returns a full path to the Parallels Tools ISO for the specified guest
|
|
// OS type. The following OS types are supported: "win", "lin", "mac", "other".
|
|
func (d *Parallels9Driver) ToolsISOPath(k string) (string, error) {
|
|
appPath, err := getAppPath("com.parallels.desktop.console")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
toolsPath := filepath.Join(appPath, "Contents", "Resources", "Tools", "prl-tools-"+k+".iso")
|
|
log.Printf("Parallels Tools path: '%s'", toolsPath)
|
|
return toolsPath, nil
|
|
}
|