2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-09-11 18:44:22 -04:00
|
|
|
"io/ioutil"
|
2014-04-06 13:21:22 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-08-22 05:06:55 -04:00
|
|
|
"path/filepath"
|
2014-04-06 13:21:22 -04:00
|
|
|
"regexp"
|
2014-12-11 16:09:04 -05:00
|
|
|
"strconv"
|
2014-04-06 13:21:22 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2014-08-23 14:23:13 -04:00
|
|
|
|
2015-08-21 20:17:56 -04:00
|
|
|
"gopkg.in/xmlpath.v2"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Parallels9Driver struct {
|
|
|
|
// This is the path to the "prlctl" application.
|
|
|
|
PrlctlPath string
|
2015-08-22 07:15:59 -04:00
|
|
|
|
|
|
|
// This is the path to the "prlsrvctl" application.
|
|
|
|
PrlsrvctlPath string
|
|
|
|
|
2014-12-11 16:09:04 -05:00
|
|
|
// The path to the parallels_dhcp_leases file
|
2016-12-11 13:49:54 -05:00
|
|
|
dhcpLeaseFile string
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
func (d *Parallels9Driver) Import(name, srcPath, dstDir string, reassignMAC bool) error {
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
err := d.Prlctl("register", srcPath, "--preserve-uuid")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
srcID, err := getVMID(srcPath)
|
2014-04-06 13:21:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
srcMAC := "auto"
|
|
|
|
if !reassignMAC {
|
|
|
|
srcMAC, err = getFirtsMACAddress(srcPath)
|
2014-08-12 08:52:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
err = d.Prlctl("clone", srcID, "--name", name, "--dst", dstDir)
|
2014-04-06 13:21:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
err = d.Prlctl("unregister", srcID)
|
2014-04-06 13:21:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
err = d.Prlctl("set", name, "--device-set", "net0", "--mac", srcMAC)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-06 13:21:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
func getVMID(path string) (string, error) {
|
2014-04-06 13:21:22 -04:00
|
|
|
return getConfigValueFromXpath(path, "/ParallelsVirtualMachine/Identification/VmUuid")
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
func getFirtsMACAddress(path string) (string, error) {
|
2014-04-06 13:21:22 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
xpathComp := xmlpath.MustCompile(xpath)
|
|
|
|
root, err := xmlpath.Parse(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
value, _ := xpathComp.String(root)
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2014-08-20 07:49:29 -04:00
|
|
|
// Finds an application bundle by identifier (for "darwin" platform only)
|
2016-12-11 13:49:54 -05:00
|
|
|
func getAppPath(bundleID string) (string, error) {
|
2014-08-22 05:06:55 -04:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
cmd := exec.Command("mdfind", "kMDItemCFBundleIdentifier ==", bundleID)
|
2014-08-22 05:06:55 -04:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
2014-08-20 07:49:29 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2014-08-22 05:06:55 -04:00
|
|
|
|
|
|
|
pathOutput := strings.TrimSpace(stdout.String())
|
|
|
|
if pathOutput == "" {
|
2015-10-16 13:57:39 -04:00
|
|
|
if fi, err := os.Stat("/Applications/Parallels Desktop.app"); err == nil {
|
|
|
|
if fi.IsDir() {
|
|
|
|
return "/Applications/Parallels Desktop.app", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 07:49:29 -04:00
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Could not detect Parallels Desktop! Make sure it is properly installed.")
|
|
|
|
}
|
2014-08-22 05:06:55 -04:00
|
|
|
|
|
|
|
return pathOutput, nil
|
2014-08-20 07:49:29 -04:00
|
|
|
}
|
|
|
|
|
2015-06-21 06:34:35 -04:00
|
|
|
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
|
2014-08-20 07:49:29 -04:00
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
func (d *Parallels9Driver) DeviceAddCDROM(name string, image string) (string, error) {
|
2014-09-17 07:01:54 -04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
deviceName := matches[1]
|
|
|
|
return deviceName, nil
|
2014-09-17 07:01:54 -04:00
|
|
|
}
|
|
|
|
|
2015-06-21 05:17:06 -04:00
|
|
|
func (d *Parallels9Driver) DiskPath(name string) (string, error) {
|
|
|
|
out, err := exec.Command(d.PrlctlPath, "list", "-i", name).Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
HDDRe := regexp.MustCompile("hdd0.* image='(.*)' type=*")
|
|
|
|
matches := HDDRe.FindStringSubmatch(string(out))
|
2015-06-21 05:17:06 -04:00
|
|
|
if matches == nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Could not determine hdd image path in the output:\n%s", string(out))
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
HDDPath := matches[1]
|
|
|
|
return HDDPath, nil
|
2015-06-21 05:17:06 -04:00
|
|
|
}
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:59:27 -04:00
|
|
|
log.Printf("Checking VM state: %s\n", strings.TrimSpace(stdout.String()))
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
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
|
|
|
|
}
|
2014-06-06 09:59:27 -04:00
|
|
|
if line == "stopping" {
|
|
|
|
return true, nil
|
|
|
|
}
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Parallels9Driver) Verify() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Parallels9Driver) Version() (string, error) {
|
2014-08-20 07:49:29 -04:00
|
|
|
out, err := exec.Command(d.PrlctlPath, "--version").Output()
|
|
|
|
if err != nil {
|
2014-04-06 13:21:22 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-08-20 07:49:29 -04:00
|
|
|
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))
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2014-08-20 07:49:29 -04:00
|
|
|
version := matches[1]
|
|
|
|
log.Printf("Parallels Desktop version: %s", version)
|
2014-04-06 13:21:22 -04:00
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Parallels9Driver) SendKeyScanCodes(vmName string, codes ...string) error {
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
2014-09-11 18:44:22 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
args := prepend(vmName, codes)
|
2014-09-11 18:44:22 -04:00
|
|
|
args = prepend(f.Name(), args)
|
|
|
|
cmd := exec.Command("/usr/bin/python", args...)
|
2014-04-06 13:21:22 -04:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2014-09-11 18:44:22 -04:00
|
|
|
err = cmd.Run()
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-18 05:04:02 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
func (d *Parallels9Driver) MAC(vmName string) (string, error) {
|
2014-04-06 13:21:22 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
// IPAddress finds the IP address of a VM connected that uses DHCP by its MAC address
|
2014-12-11 16:09:04 -05:00
|
|
|
//
|
|
|
|
// 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"
|
2016-12-11 13:49:54 -05:00
|
|
|
func (d *Parallels9Driver) IPAddress(mac string) (string, error) {
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
if len(mac) != 12 {
|
|
|
|
return "", fmt.Errorf("Not a valid MAC address: %s. It should be exactly 12 digits.", mac)
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
leases, err := ioutil.ReadFile(d.dhcpLeaseFile)
|
2014-12-11 16:09:04 -05:00
|
|
|
if err != nil {
|
2014-04-06 13:21:22 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:09:04 -05:00
|
|
|
re := regexp.MustCompile("(.*)=\"(.*),(.*)," + strings.ToLower(mac) + ",.*\"")
|
2016-12-11 13:49:54 -05:00
|
|
|
mostRecentIP := ""
|
2014-12-11 16:09:04 -05:00
|
|
|
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 {
|
2016-12-11 13:49:54 -05:00
|
|
|
mostRecentIP = ip
|
2014-12-11 16:09:04 -05:00
|
|
|
mostRecentLease = expiry - leaseTime
|
|
|
|
}
|
|
|
|
}
|
2014-04-06 13:21:22 -04:00
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
if len(mostRecentIP) == 0 {
|
|
|
|
return "", fmt.Errorf("IP lease not found for MAC address %s in: %s\n", mac, d.dhcpLeaseFile)
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
log.Printf("Found IP lease: %s for MAC address %s\n", mostRecentIP, mac)
|
|
|
|
return mostRecentIP, nil
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
2014-08-22 05:06:55 -04:00
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
func (d *Parallels9Driver) ToolsISOPath(k string) (string, error) {
|
2014-08-22 05:06:55 -04:00
|
|
|
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
|
|
|
|
}
|