github.com/rothwerx/packer@v0.9.0/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 // Compact a virtual disk image. 19 CompactDisk(string) error 20 21 // Adds new CD/DVD drive to the VM and returns name of this device 22 DeviceAddCdRom(string, string) (string, error) 23 24 // Get path to the first virtual disk image 25 DiskPath(string) (string, error) 26 27 // Import a VM 28 Import(string, string, string, bool) error 29 30 // Checks if the VM with the given name is running. 31 IsRunning(string) (bool, error) 32 33 // Stop stops a running machine, forcefully. 34 Stop(string) error 35 36 // Prlctl executes the given Prlctl command 37 Prlctl(...string) error 38 39 // Get the path to the Parallels Tools ISO for the given flavor. 40 ToolsIsoPath(string) (string, error) 41 42 // Verify checks to make sure that this driver should function 43 // properly. If there is any indication the driver can't function, 44 // this will return an error. 45 Verify() error 46 47 // Version reads the version of Parallels that is installed. 48 Version() (string, error) 49 50 // Send scancodes to the vm using the prltype python script. 51 SendKeyScanCodes(string, ...string) error 52 53 // Apply default Ńonfiguration settings to the virtual machine 54 SetDefaultConfiguration(string) error 55 56 // Finds the MAC address of the NIC nic0 57 Mac(string) (string, error) 58 59 // Finds the IP address of a VM connected that uses DHCP by its MAC address 60 IpAddress(string) (string, error) 61 } 62 63 func NewDriver() (Driver, error) { 64 var drivers map[string]Driver 65 var prlctlPath string 66 var prlsrvctlPath string 67 var supportedVersions []string 68 dhcp_lease_file := "/Library/Preferences/Parallels/parallels_dhcp_leases" 69 70 if runtime.GOOS != "darwin" { 71 return nil, fmt.Errorf( 72 "Parallels builder works only on \"darwin\" platform!") 73 } 74 75 if prlctlPath == "" { 76 var err error 77 prlctlPath, err = exec.LookPath("prlctl") 78 if err != nil { 79 return nil, err 80 } 81 } 82 83 log.Printf("prlctl path: %s", prlctlPath) 84 85 if prlsrvctlPath == "" { 86 var err error 87 prlsrvctlPath, err = exec.LookPath("prlsrvctl") 88 if err != nil { 89 return nil, err 90 } 91 } 92 93 log.Printf("prlsrvctl path: %s", prlsrvctlPath) 94 95 drivers = map[string]Driver{ 96 "11": &Parallels11Driver{ 97 Parallels9Driver: Parallels9Driver{ 98 PrlctlPath: prlctlPath, 99 PrlsrvctlPath: prlsrvctlPath, 100 dhcp_lease_file: dhcp_lease_file, 101 }, 102 }, 103 "10": &Parallels10Driver{ 104 Parallels9Driver: Parallels9Driver{ 105 PrlctlPath: prlctlPath, 106 PrlsrvctlPath: prlsrvctlPath, 107 dhcp_lease_file: dhcp_lease_file, 108 }, 109 }, 110 "9": &Parallels9Driver{ 111 PrlctlPath: prlctlPath, 112 PrlsrvctlPath: prlsrvctlPath, 113 dhcp_lease_file: dhcp_lease_file, 114 }, 115 } 116 117 for v, d := range drivers { 118 version, _ := d.Version() 119 if strings.HasPrefix(version, v) { 120 if err := d.Verify(); err != nil { 121 return nil, err 122 } 123 return d, nil 124 } 125 supportedVersions = append(supportedVersions, v) 126 } 127 128 return nil, fmt.Errorf( 129 "Unable to initialize any driver. Supported Parallels Desktop versions: "+ 130 "%s\n", strings.Join(supportedVersions, ", ")) 131 }