github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/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  	// Finds the MAC address of the NIC nic0
    48  	Mac(string) (string, error)
    49  
    50  	// Finds the IP address of a VM connected that uses DHCP by its MAC address
    51  	IpAddress(string) (string, error)
    52  }
    53  
    54  func NewDriver() (Driver, error) {
    55  	var drivers map[string]Driver
    56  	var prlctlPath string
    57  	var supportedVersions []string
    58  
    59  	if runtime.GOOS != "darwin" {
    60  		return nil, fmt.Errorf(
    61  			"Parallels builder works only on \"darwin\" platform!")
    62  	}
    63  
    64  	if prlctlPath == "" {
    65  		var err error
    66  		prlctlPath, err = exec.LookPath("prlctl")
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  	}
    71  
    72  	log.Printf("prlctl path: %s", prlctlPath)
    73  
    74  	drivers = map[string]Driver{
    75  		"10": &Parallels10Driver{
    76  			Parallels9Driver: Parallels9Driver{
    77  				PrlctlPath: prlctlPath,
    78  			},
    79  		},
    80  		"9": &Parallels9Driver{
    81  			PrlctlPath: prlctlPath,
    82  		},
    83  	}
    84  
    85  	for v, d := range drivers {
    86  		version, _ := d.Version()
    87  		if strings.HasPrefix(version, v) {
    88  			return d, nil
    89  		}
    90  		supportedVersions = append(supportedVersions, v)
    91  	}
    92  
    93  	return nil, fmt.Errorf(
    94  		"Unable to initialize any driver. Supported Parallels Desktop versions: "+
    95  			"%s\n", strings.Join(supportedVersions, ", "))
    96  }