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"
|
2013-11-08 15:19:09 -05:00
|
|
|
"io/ioutil"
|
2013-07-16 17:23:01 -04:00
|
|
|
"os"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2014-08-22 04:07:36 -04:00
|
|
|
// Fusion5Driver is a driver that can run VMware Fusion 5.
|
2013-07-16 17:23:01 -04:00
|
|
|
type Fusion5Driver struct {
|
2015-11-11 07:39:06 -05:00
|
|
|
VmwareDriver
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
// This is the path to the "VMware Fusion.app"
|
|
|
|
AppPath 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
|
|
|
}
|
|
|
|
|
2013-12-26 16:39:41 -05:00
|
|
|
func (d *Fusion5Driver) Clone(dst, src string) error {
|
|
|
|
return errors.New("Cloning is not supported with Fusion 5. Please use Fusion 6+.")
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
func (d *Fusion5Driver) CompactDisk(diskPath string) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-08-22 14:40:56 -04:00
|
|
|
func (d *Fusion5Driver) CreateDisk(output string, size string, type_id string) error {
|
|
|
|
cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) {
|
|
|
|
vmxPath, err := filepath.Abs(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "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 *Fusion5Driver) CommHost(state multistep.StateBag) (string, error) {
|
|
|
|
return CommHost(d.SSHConfig)(state)
|
2013-11-07 15:00:27 -05:00
|
|
|
}
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
func (d *Fusion5Driver) Start(vmxPath string, headless bool) error {
|
|
|
|
guiArgument := "gui"
|
|
|
|
if headless == true {
|
|
|
|
guiArgument = "nogui"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion5Driver) Stop(vmxPath string) error {
|
|
|
|
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "stop", vmxPath, "hard")
|
2013-07-16 18:02:27 -04:00
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
2014-09-05 13:29:35 -04:00
|
|
|
// Check if the VM is running. If its not, it was already stopped
|
|
|
|
running, rerr := d.IsRunning(vmxPath)
|
|
|
|
if rerr == nil && !running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 15:19:09 -05:00
|
|
|
func (d *Fusion5Driver) SuppressMessages(vmxPath string) error {
|
|
|
|
dir := filepath.Dir(vmxPath)
|
|
|
|
base := filepath.Base(vmxPath)
|
|
|
|
base = strings.Replace(base, ".vmx", "", -1)
|
|
|
|
|
|
|
|
plistPath := filepath.Join(dir, base+".plist")
|
|
|
|
return ioutil.WriteFile(plistPath, []byte(fusionSuppressPlist), 0644)
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
func (d *Fusion5Driver) Verify() error {
|
|
|
|
if _, err := os.Stat(d.AppPath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("Fusion application not found at path: %s", d.AppPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(d.vmrunPath()); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2013-12-26 16:39:41 -05:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Critical application 'vmrun' not found at path: %s", d.vmrunPath())
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(d.vdiskManagerPath()); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2013-12-26 16:39:41 -05:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Critical application vdisk manager not found at path: %s",
|
|
|
|
d.vdiskManagerPath())
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-16 12:59:10 -04:00
|
|
|
libpath := filepath.Join("Library", "Preferences", "VMware Fusion")
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
d.VmwareDriver.DhcpLeasesPath = func(device string) string {
|
|
|
|
return "/var/db/vmware/vmnet-dhcpd-" + device + ".leases"
|
|
|
|
}
|
2017-10-16 12:59:10 -04:00
|
|
|
d.VmwareDriver.DhcpConfPath = func(device string) string {
|
|
|
|
return filepath.Join(libpath, device, "dhcpd.conf")
|
|
|
|
}
|
|
|
|
d.VmwareDriver.VmnetnatConfPath = func(device string) string {
|
|
|
|
return filepath.Join(libpath, device, "nat.conf")
|
|
|
|
}
|
2017-10-23 21:26:16 -04:00
|
|
|
d.VmwareDriver.NetworkMapper = func() (NetworkNameMapper, error) {
|
|
|
|
pathNetworking := filepath.Join(libpath, "networking")
|
|
|
|
if _, err := os.Stat(pathNetworking); err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not find networking conf file: %s", pathNetworking)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(pathNetworking)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
return ReadNetworkingConfig(fd)
|
2017-10-16 12:59:10 -04:00
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion5Driver) vdiskManagerPath() string {
|
|
|
|
return filepath.Join(d.AppPath, "Contents", "Library", "vmware-vdiskmanager")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion5Driver) vmrunPath() string {
|
|
|
|
return filepath.Join(d.AppPath, "Contents", "Library", "vmrun")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion5Driver) ToolsIsoPath(k string) string {
|
|
|
|
return filepath.Join(d.AppPath, "Contents", "Library", "isoimages", k+".iso")
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
func (d *Fusion5Driver) ToolsInstall() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 15:19:09 -05:00
|
|
|
const fusionSuppressPlist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
|
|
|
<key>disallowUpgrade</key>
|
|
|
|
<true/>
|
|
|
|
</dict>
|
|
|
|
</plist>`
|
2017-04-13 18:39:55 -04:00
|
|
|
|
|
|
|
func (d *Fusion5Driver) GetVmwareDriver() VmwareDriver {
|
|
|
|
return d.VmwareDriver
|
|
|
|
}
|