2013-12-25 17:50:12 -05:00
|
|
|
package common
|
2013-07-16 17:23:01 -04:00
|
|
|
|
|
|
|
import (
|
2013-12-26 16:39:41 -05:00
|
|
|
"errors"
|
2013-07-16 17:23:01 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2013-07-31 15:39:49 -04:00
|
|
|
"os"
|
2013-07-16 17:23:01 -04:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2013-12-24 13:31:57 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2013-07-16 17:23:01 -04:00
|
|
|
)
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
// Workstation9Driver is a driver that can run VMware Workstation 9
|
|
|
|
type Workstation9Driver struct {
|
2015-11-11 07:39:06 -05:00
|
|
|
VmwareDriver
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
AppPath string
|
|
|
|
VdiskManagerPath string
|
|
|
|
VmrunPath string
|
2013-12-24 13:31:57 -05:00
|
|
|
|
|
|
|
// SSHConfig are the SSH settings for the Fusion VM
|
2013-12-25 17:50:12 -05:00
|
|
|
SSHConfig *SSHConfig
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
2018-06-16 20:38:42 -04:00
|
|
|
func (d *Workstation9Driver) Clone(dst, src string, linked bool) error {
|
2014-08-22 04:07:36 -04:00
|
|
|
return errors.New("Cloning is not supported with VMware WS version 9. Please use VMware WS version 10, or greater.")
|
2013-12-26 16:39:41 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) CompactDisk(diskPath string) error {
|
2013-07-16 17:23:01 -04:00
|
|
|
defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath)
|
2013-07-16 18:02:27 -04:00
|
|
|
if _, _, err := runAndLog(defragCmd); err != nil {
|
2013-07-16 17:23:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath)
|
2013-07-16 18:02:27 -04:00
|
|
|
if _, _, err := runAndLog(shrinkCmd); err != nil {
|
2013-07-16 17:23:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-25 15:08:08 -05:00
|
|
|
func (d *Workstation9Driver) CreateDisk(output string, size string, adapter_type string, type_id string) error {
|
|
|
|
cmd := exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", adapter_type, "-t", type_id, output)
|
2013-07-16 18:02:27 -04:00
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
2013-07-16 17:23:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) {
|
2013-07-16 17:23:01 -04:00
|
|
|
vmxPath, err := filepath.Abs(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(d.VmrunPath, "-T", "ws", "list")
|
2013-07-16 18:02:27 -04:00
|
|
|
stdout, _, err := runAndLog(cmd)
|
2013-07-16 17:23:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range strings.Split(stdout, "\n") {
|
|
|
|
if line == vmxPath {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
func (d *Workstation9Driver) CommHost(state multistep.StateBag) (string, error) {
|
|
|
|
return CommHost(d.SSHConfig)(state)
|
2013-11-07 15:00:27 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
|
2013-07-16 17:23:01 -04:00
|
|
|
guiArgument := "gui"
|
|
|
|
if headless {
|
|
|
|
guiArgument = "nogui"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(d.VmrunPath, "-T", "ws", "start", vmxPath, guiArgument)
|
2013-07-16 18:02:27 -04:00
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
2013-07-16 17:23:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) Stop(vmxPath string) error {
|
2013-07-16 17:23:01 -04:00
|
|
|
cmd := exec.Command(d.VmrunPath, "-T", "ws", "stop", vmxPath, "hard")
|
2013-07-16 18:02:27 -04:00
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
2013-07-16 17:23:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 15:19:09 -05:00
|
|
|
func (d *Workstation9Driver) SuppressMessages(vmxPath string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) Verify() error {
|
2013-07-31 15:36:17 -04:00
|
|
|
var err error
|
|
|
|
if d.AppPath == "" {
|
|
|
|
if d.AppPath, err = workstationFindVMware(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
if d.VmrunPath == "" {
|
|
|
|
if d.VmrunPath, err = workstationFindVmrun(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:36:17 -04:00
|
|
|
if d.VdiskManagerPath == "" {
|
|
|
|
if d.VdiskManagerPath, err = workstationFindVdiskManager(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 16:50:49 -04:00
|
|
|
log.Printf("VMware app path: %s", d.AppPath)
|
|
|
|
log.Printf("vmrun path: %s", d.VmrunPath)
|
|
|
|
log.Printf("vdisk-manager path: %s", d.VdiskManagerPath)
|
|
|
|
|
2013-07-31 15:39:49 -04:00
|
|
|
if _, err := os.Stat(d.AppPath); err != nil {
|
|
|
|
return fmt.Errorf("VMware application not found: %s", d.AppPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(d.VmrunPath); err != nil {
|
|
|
|
return fmt.Errorf("'vmrun' application not found: %s", d.VmrunPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(d.VdiskManagerPath); err != nil {
|
2014-05-11 11:46:19 -04:00
|
|
|
return fmt.Errorf("'vmware-vdiskmanager' application not found: %s", d.VdiskManagerPath)
|
2013-07-31 15:39:49 -04:00
|
|
|
}
|
|
|
|
|
2013-07-16 17:58:16 -04:00
|
|
|
// Check to see if it APPEARS to be licensed.
|
2013-07-31 15:36:17 -04:00
|
|
|
if err := workstationCheckLicense(); err != nil {
|
2013-07-16 17:23:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
// Assigning the path callbacks to VmwareDriver
|
|
|
|
d.VmwareDriver.DhcpLeasesPath = func(device string) string {
|
|
|
|
return workstationDhcpLeasesPath(device)
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:45:58 -04:00
|
|
|
d.VmwareDriver.DhcpConfPath = func(device string) string {
|
|
|
|
return workstationDhcpConfPath(device)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-05 21:45:58 -04:00
|
|
|
d.VmwareDriver.VmnetnatConfPath = func(device string) string {
|
|
|
|
return workstationVmnetnatConfPath(device)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:26:16 -04:00
|
|
|
d.VmwareDriver.NetworkMapper = func() (NetworkNameMapper, error) {
|
|
|
|
pathNetmap := workstationNetmapConfPath()
|
|
|
|
if _, err := os.Stat(pathNetmap); err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not find netmap conf file: %s", pathNetmap)
|
|
|
|
}
|
2018-03-29 14:04:25 -04:00
|
|
|
log.Printf("Located networkmapper configuration file using Workstation: %s", pathNetmap)
|
2017-10-23 21:26:16 -04:00
|
|
|
|
2018-03-29 14:04:25 -04:00
|
|
|
return ReadNetmapConfig(pathNetmap)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2013-07-16 17:23:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) ToolsIsoPath(flavor string) string {
|
2013-07-31 15:36:17 -04:00
|
|
|
return workstationToolsIsoPath(flavor)
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
func (d *Workstation9Driver) ToolsInstall() error {
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-13 18:39:55 -04:00
|
|
|
|
|
|
|
func (d *Workstation9Driver) GetVmwareDriver() VmwareDriver {
|
|
|
|
return d.VmwareDriver
|
|
|
|
}
|