github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/parallels/common/driver.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os/exec"
     7  	"runtime"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  // A driver is able to talk to Parallels and perform certain
    13  // operations with it. Some of the operations on here may seem overly
    14  // specific, but they were built specifically in mind to handle features
    15  // of the Parallels builder for Packer, and to abstract differences in
    16  // versions out of the builder steps, so sometimes the methods are
    17  // extremely specific.
    18  type Driver interface {
    19  	// Compact a virtual disk image.
    20  	CompactDisk(string) error
    21  
    22  	// Adds new CD/DVD drive to the VM and returns name of this device
    23  	DeviceAddCdRom(string, string) (string, error)
    24  
    25  	// Get path to the first virtual disk image
    26  	DiskPath(string) (string, error)
    27  
    28  	// Import a VM
    29  	Import(string, string, string, bool) error
    30  
    31  	// Checks if the VM with the given name is running.
    32  	IsRunning(string) (bool, error)
    33  
    34  	// Stop stops a running machine, forcefully.
    35  	Stop(string) error
    36  
    37  	// Prlctl executes the given Prlctl command
    38  	Prlctl(...string) error
    39  
    40  	// Get the path to the Parallels Tools ISO for the given flavor.
    41  	ToolsIsoPath(string) (string, error)
    42  
    43  	// Verify checks to make sure that this driver should function
    44  	// properly. If there is any indication the driver can't function,
    45  	// this will return an error.
    46  	Verify() error
    47  
    48  	// Version reads the version of Parallels that is installed.
    49  	Version() (string, error)
    50  
    51  	// Send scancodes to the vm using the prltype python script.
    52  	SendKeyScanCodes(string, ...string) error
    53  
    54  	// Apply default сonfiguration settings to the virtual machine
    55  	SetDefaultConfiguration(string) error
    56  
    57  	// Finds the MAC address of the NIC nic0
    58  	Mac(string) (string, error)
    59  
    60  	// Finds the IP address of a VM connected that uses DHCP by its MAC address
    61  	IpAddress(string) (string, error)
    62  }
    63  
    64  func NewDriver() (Driver, error) {
    65  	var drivers map[string]Driver
    66  	var prlctlPath string
    67  	var prlsrvctlPath string
    68  	var supportedVersions []string
    69  	dhcp_lease_file := "/Library/Preferences/Parallels/parallels_dhcp_leases"
    70  
    71  	if runtime.GOOS != "darwin" {
    72  		return nil, fmt.Errorf(
    73  			"Parallels builder works only on \"darwin\" platform!")
    74  	}
    75  
    76  	if prlctlPath == "" {
    77  		var err error
    78  		prlctlPath, err = exec.LookPath("prlctl")
    79  		if err != nil {
    80  			return nil, err
    81  		}
    82  	}
    83  
    84  	log.Printf("prlctl path: %s", prlctlPath)
    85  
    86  	if prlsrvctlPath == "" {
    87  		var err error
    88  		prlsrvctlPath, err = exec.LookPath("prlsrvctl")
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  	}
    93  
    94  	log.Printf("prlsrvctl path: %s", prlsrvctlPath)
    95  
    96  	drivers = map[string]Driver{
    97  		"11": &Parallels11Driver{
    98  			Parallels9Driver: Parallels9Driver{
    99  				PrlctlPath:      prlctlPath,
   100  				PrlsrvctlPath:   prlsrvctlPath,
   101  				dhcp_lease_file: dhcp_lease_file,
   102  			},
   103  		},
   104  		"10": &Parallels10Driver{
   105  			Parallels9Driver: Parallels9Driver{
   106  				PrlctlPath:      prlctlPath,
   107  				PrlsrvctlPath:   prlsrvctlPath,
   108  				dhcp_lease_file: dhcp_lease_file,
   109  			},
   110  		},
   111  		"9": &Parallels9Driver{
   112  			PrlctlPath:      prlctlPath,
   113  			PrlsrvctlPath:   prlsrvctlPath,
   114  			dhcp_lease_file: dhcp_lease_file,
   115  		},
   116  	}
   117  
   118  	for v, d := range drivers {
   119  		version, _ := d.Version()
   120  		if strings.HasPrefix(version, v) {
   121  			if err := d.Verify(); err != nil {
   122  				return nil, err
   123  			}
   124  			return d, nil
   125  		}
   126  		supportedVersions = append(supportedVersions, v)
   127  	}
   128  
   129  	latestDriver := 11
   130  	version, _ := drivers[strconv.Itoa(latestDriver)].Version()
   131  	majVer, _ := strconv.Atoi(strings.SplitN(version, ".", 2)[0])
   132  	if majVer > latestDriver {
   133  		log.Printf("Your version of Parallels Desktop for Mac is %s, Packer will use driver for version %d.", version, latestDriver)
   134  		return drivers[strconv.Itoa(latestDriver)], nil
   135  	}
   136  
   137  	return nil, fmt.Errorf(
   138  		"Unable to initialize any driver. Supported Parallels Desktop versions: "+
   139  			"%s\n", strings.Join(supportedVersions, ", "))
   140  }