2013-12-26 16:54:26 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-09-10 13:14:50 -04:00
|
|
|
const VMWARE_FUSION_VERSION = "6"
|
|
|
|
|
|
|
|
// Fusion6Driver is a driver that can run VMware Fusion 6.
|
2013-12-26 16:54:26 -05:00
|
|
|
type Fusion6Driver struct {
|
|
|
|
Fusion5Driver
|
|
|
|
}
|
|
|
|
|
2020-09-17 04:38:33 -04:00
|
|
|
func NewFusion6Driver(dconfig *DriverConfig, config *SSHConfig) Driver {
|
|
|
|
return &Fusion6Driver{
|
|
|
|
Fusion5Driver: Fusion5Driver{
|
|
|
|
AppPath: dconfig.FusionAppPath,
|
|
|
|
SSHConfig: config,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 20:38:42 -04:00
|
|
|
func (d *Fusion6Driver) Clone(dst, src string, linked bool) error {
|
2018-06-30 11:31:42 -04:00
|
|
|
|
|
|
|
var cloneType string
|
|
|
|
if linked {
|
|
|
|
cloneType = "linked"
|
|
|
|
} else {
|
|
|
|
cloneType = "full"
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:03:12 -05:00
|
|
|
cmd := exec.Command(d.vmrunPath(),
|
|
|
|
"-T", "fusion",
|
|
|
|
"clone", src, dst,
|
2018-06-30 11:31:42 -04:00
|
|
|
cloneType)
|
2013-12-26 17:03:12 -05:00
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
2014-09-05 14:28:43 -04:00
|
|
|
if strings.Contains(err.Error(), "parameters was invalid") {
|
|
|
|
return fmt.Errorf(
|
2014-10-28 11:35:21 -04:00
|
|
|
"Clone is not supported with your version of Fusion. Packer "+
|
2014-09-10 13:14:50 -04:00
|
|
|
"only works with Fusion %s Professional or above. Please verify your version.", VMWARE_FUSION_VERSION)
|
2014-09-05 14:28:43 -04:00
|
|
|
}
|
|
|
|
|
2013-12-26 17:03:12 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-12-26 16:54:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion6Driver) Verify() error {
|
|
|
|
if err := d.Fusion5Driver.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxpath := filepath.Join(d.AppPath, "Contents", "Library", "vmware-vmx")
|
|
|
|
if _, err := os.Stat(vmxpath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("vmware-vmx could not be found at path: %s",
|
|
|
|
vmxpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd := exec.Command(vmxpath, "-v")
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-17 03:32:15 -05:00
|
|
|
// Example: VMware Fusion e.x.p build-6048684 Release
|
|
|
|
techPreviewRe := regexp.MustCompile(`(?i)VMware [a-z0-9-]+ e\.x\.p `)
|
|
|
|
matches := techPreviewRe.FindStringSubmatch(stderr.String())
|
|
|
|
if matches != nil {
|
|
|
|
log.Printf("Detected VMware version: e.x.p (Tech Preview)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Example: VMware Fusion 7.1.3 build-3204469 Release
|
2014-09-10 13:14:50 -04:00
|
|
|
versionRe := regexp.MustCompile(`(?i)VMware [a-z0-9-]+ (\d+)\.`)
|
2018-11-17 03:32:15 -05:00
|
|
|
matches = versionRe.FindStringSubmatch(stderr.String())
|
2013-12-26 16:54:26 -05:00
|
|
|
if matches == nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Couldn't find VMware version in output: %s", stderr.String())
|
|
|
|
}
|
|
|
|
log.Printf("Detected VMware version: %s", matches[1])
|
|
|
|
|
2017-11-17 11:26:40 -05:00
|
|
|
libpath := filepath.Join("/", "Library", "Preferences", "VMware Fusion")
|
2017-10-16 12:59:10 -04:00
|
|
|
|
|
|
|
d.VmwareDriver.DhcpLeasesPath = func(device string) string {
|
|
|
|
return "/var/db/vmware/vmnet-dhcpd-" + device + ".leases"
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2018-03-29 14:06:41 -04:00
|
|
|
log.Printf("Located networkmapper configuration file using Fusion6: %s", pathNetworking)
|
2017-10-23 21:26:16 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-10 13:14:50 -04:00
|
|
|
return compareVersions(matches[1], VMWARE_FUSION_VERSION, "Fusion Professional")
|
2013-12-26 16:54:26 -05:00
|
|
|
}
|
2017-04-13 18:39:55 -04:00
|
|
|
|
|
|
|
func (d *Fusion6Driver) GetVmwareDriver() VmwareDriver {
|
|
|
|
return d.Fusion5Driver.VmwareDriver
|
|
|
|
}
|