github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/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  	// Import a VM
    19  	Import(string, string, string, bool) error
    20  
    21  	// Checks if the VM with the given name is running.
    22  	IsRunning(string) (bool, error)
    23  
    24  	// Stop stops a running machine, forcefully.
    25  	Stop(string) error
    26  
    27  	// Prlctl executes the given Prlctl command
    28  	Prlctl(...string) error
    29  
    30  	// Get the path to the Parallels Tools ISO for the given flavor.
    31  	ToolsIsoPath(string) (string, error)
    32  
    33  	// Verify checks to make sure that this driver should function
    34  	// properly. If there is any indication the driver can't function,
    35  	// this will return an error.
    36  	Verify() error
    37  
    38  	// Version reads the version of Parallels that is installed.
    39  	Version() (string, error)
    40  
    41  	// Send scancodes to the vm using the prltype tool.
    42  	SendKeyScanCodes(string, ...string) error
    43  
    44  	// Finds the MAC address of the NIC nic0
    45  	Mac(string) (string, error)
    46  
    47  	// Finds the IP address of a VM connected that uses DHCP by its MAC address
    48  	IpAddress(string) (string, error)
    49  }
    50  
    51  func NewDriver() (Driver, error) {
    52  	var drivers map[string]Driver
    53  	var prlctlPath string
    54  	var supportedVersions []string
    55  
    56  	if runtime.GOOS != "darwin" {
    57  		return nil, fmt.Errorf(
    58  			"Parallels builder works only on \"darwin\" platform!")
    59  	}
    60  
    61  	if prlctlPath == "" {
    62  		var err error
    63  		prlctlPath, err = exec.LookPath("prlctl")
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  	}
    68  
    69  	log.Printf("prlctl path: %s", prlctlPath)
    70  
    71  	drivers = map[string]Driver{
    72  		"10": &Parallels10Driver{
    73  			Parallels9Driver: Parallels9Driver{
    74  				PrlctlPath: prlctlPath,
    75  			},
    76  		},
    77  		"9": &Parallels9Driver{
    78  			PrlctlPath: prlctlPath,
    79  		},
    80  	}
    81  
    82  	for v, d := range drivers {
    83  		version, _ := d.Version()
    84  		if strings.HasPrefix(version, v) {
    85  			return d, nil
    86  		}
    87  		supportedVersions = append(supportedVersions, v)
    88  	}
    89  
    90  	return nil, fmt.Errorf(
    91  		"Unable to initialize any driver. Supported Parallels Desktop versions: "+
    92  			"%s\n", strings.Join(supportedVersions, ", "))
    93  }