github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/builder/parallels/common/driver.go (about)

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