2013-12-24 13:00:51 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-12-25 17:50:12 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
2015-06-10 14:31:20 -04:00
|
|
|
"regexp"
|
2013-12-25 17:50:12 -05:00
|
|
|
"runtime"
|
2014-04-06 18:31:49 -04:00
|
|
|
"strconv"
|
2013-12-25 17:50:12 -05:00
|
|
|
"strings"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2013-12-24 13:00:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// A driver is able to talk to VMware, control virtual machines, etc.
|
|
|
|
type Driver interface {
|
2013-12-26 16:39:41 -05:00
|
|
|
// Clone clones the VMX and the disk to the destination path. The
|
|
|
|
// destination is a path to the VMX file. The disk will be copied
|
|
|
|
// to that same directory.
|
|
|
|
Clone(dst string, src string) error
|
|
|
|
|
2013-12-24 13:00:51 -05:00
|
|
|
// CompactDisk compacts a virtual disk.
|
|
|
|
CompactDisk(string) error
|
|
|
|
|
|
|
|
// CreateDisk creates a virtual disk with the given size.
|
|
|
|
CreateDisk(string, string, string) error
|
|
|
|
|
|
|
|
// Checks if the VMX file at the given path is running.
|
|
|
|
IsRunning(string) (bool, error)
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
// CommHost returns the host address for the VM that is being
|
2013-12-24 13:00:51 -05:00
|
|
|
// managed by this driver.
|
2015-06-13 19:23:33 -04:00
|
|
|
CommHost(multistep.StateBag) (string, error)
|
2013-12-24 13:00:51 -05:00
|
|
|
|
|
|
|
// Start starts a VM specified by the path to the VMX given.
|
|
|
|
Start(string, bool) error
|
|
|
|
|
|
|
|
// Stop stops a VM specified by the path to the VMX given.
|
|
|
|
Stop(string) error
|
|
|
|
|
|
|
|
// SuppressMessages modifies the VMX or surrounding directory so that
|
|
|
|
// VMware doesn't show any annoying messages.
|
|
|
|
SuppressMessages(string) error
|
|
|
|
|
|
|
|
// Get the path to the VMware ISO for the given flavor.
|
|
|
|
ToolsIsoPath(string) string
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
// Attach the VMware tools ISO
|
|
|
|
ToolsInstall() error
|
|
|
|
|
2013-12-24 13:00:51 -05:00
|
|
|
// Get the path to the DHCP leases file for the given device.
|
|
|
|
DhcpLeasesPath(string) string
|
|
|
|
|
|
|
|
// Verify checks to make sure that this driver should function
|
|
|
|
// properly. This should check that all the files it will use
|
|
|
|
// appear to exist and so on. If everything is okay, this doesn't
|
|
|
|
// return an error. Otherwise, this returns an error.
|
|
|
|
Verify() error
|
|
|
|
}
|
2013-12-25 17:50:12 -05:00
|
|
|
|
|
|
|
// NewDriver returns a new driver implementation for this operating
|
|
|
|
// system, or an error if the driver couldn't be initialized.
|
2013-12-27 10:37:39 -05:00
|
|
|
func NewDriver(dconfig *DriverConfig, config *SSHConfig) (Driver, error) {
|
2013-12-25 17:50:12 -05:00
|
|
|
drivers := []Driver{}
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
drivers = []Driver{
|
2013-12-26 16:54:26 -05:00
|
|
|
&Fusion6Driver{
|
|
|
|
Fusion5Driver: Fusion5Driver{
|
2013-12-27 10:37:39 -05:00
|
|
|
AppPath: dconfig.FusionAppPath,
|
2013-12-26 16:54:26 -05:00
|
|
|
SSHConfig: config,
|
|
|
|
},
|
|
|
|
},
|
2013-12-25 17:50:12 -05:00
|
|
|
&Fusion5Driver{
|
2013-12-27 10:37:39 -05:00
|
|
|
AppPath: dconfig.FusionAppPath,
|
2013-12-25 17:50:12 -05:00
|
|
|
SSHConfig: config,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case "linux":
|
2014-05-12 21:35:37 -04:00
|
|
|
fallthrough
|
|
|
|
case "windows":
|
2013-12-25 17:50:12 -05:00
|
|
|
drivers = []Driver{
|
2014-02-24 11:26:51 -05:00
|
|
|
&Workstation10Driver{
|
|
|
|
Workstation9Driver: Workstation9Driver{
|
|
|
|
SSHConfig: config,
|
|
|
|
},
|
|
|
|
},
|
2013-12-25 17:50:12 -05:00
|
|
|
&Workstation9Driver{
|
|
|
|
SSHConfig: config,
|
|
|
|
},
|
2014-05-12 21:35:37 -04:00
|
|
|
&Player6Driver{
|
|
|
|
Player5Driver: Player5Driver{
|
2014-04-06 18:31:49 -04:00
|
|
|
SSHConfig: config,
|
|
|
|
},
|
|
|
|
},
|
2014-05-12 21:35:37 -04:00
|
|
|
&Player5Driver{
|
2013-12-25 17:50:12 -05:00
|
|
|
SSHConfig: config,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := ""
|
|
|
|
for _, driver := range drivers {
|
|
|
|
err := driver.Verify()
|
|
|
|
if err == nil {
|
|
|
|
return driver, nil
|
|
|
|
}
|
|
|
|
errs += "* " + err.Error() + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Unable to initialize any driver for this platform. The errors\n"+
|
|
|
|
"from each driver are shown below. Please fix at least one driver\n"+
|
|
|
|
"to continue:\n%s", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAndLog(cmd *exec.Cmd) (string, string, error) {
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
2017-11-02 13:45:54 -04:00
|
|
|
log.Printf("Executing: %s %s", cmd.Path, strings.Join(cmd.Args[1:], " "))
|
2013-12-25 17:50:12 -05:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
stdoutString := strings.TrimSpace(stdout.String())
|
|
|
|
stderrString := strings.TrimSpace(stderr.String())
|
|
|
|
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
2014-09-05 14:27:33 -04:00
|
|
|
message := stderrString
|
|
|
|
if message == "" {
|
|
|
|
message = stdoutString
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fmt.Errorf("VMware error: %s", message)
|
2015-06-10 14:31:20 -04:00
|
|
|
|
|
|
|
// If "unknown error" is in there, add some additional notes
|
|
|
|
re := regexp.MustCompile(`(?i)unknown error`)
|
|
|
|
if re.MatchString(message) {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"%s\n\n%s", err,
|
|
|
|
"Packer detected a VMware 'Unknown Error'. Unfortunately VMware\n"+
|
|
|
|
"often has extremely vague error messages such as this and Packer\n"+
|
|
|
|
"itself can't do much about that. Please check the vmware.log files\n"+
|
|
|
|
"created by VMware when a VM is started (in the directory of the\n"+
|
|
|
|
"vmx file), which often contains more detailed error information.")
|
|
|
|
}
|
2013-12-25 17:50:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("stdout: %s", stdoutString)
|
|
|
|
log.Printf("stderr: %s", stderrString)
|
|
|
|
|
|
|
|
// Replace these for Windows, we only want to deal with Unix
|
|
|
|
// style line endings.
|
|
|
|
returnStdout := strings.Replace(stdout.String(), "\r\n", "\n", -1)
|
|
|
|
returnStderr := strings.Replace(stderr.String(), "\r\n", "\n", -1)
|
|
|
|
|
|
|
|
return returnStdout, returnStderr, err
|
|
|
|
}
|
2014-04-06 18:31:49 -04:00
|
|
|
|
|
|
|
func normalizeVersion(version string) (string, error) {
|
|
|
|
i, err := strconv.Atoi(version)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(
|
2014-08-22 04:07:36 -04:00
|
|
|
"VMware version '%s' is not numeric", version)
|
2014-04-06 18:31:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%02d", i), nil
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:14:50 -04:00
|
|
|
func compareVersions(versionFound string, versionWanted string, product string) error {
|
2014-04-06 18:31:49 -04:00
|
|
|
found, err := normalizeVersion(versionFound)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wanted, err := normalizeVersion(versionWanted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found < wanted {
|
|
|
|
return fmt.Errorf(
|
2014-09-10 13:14:50 -04:00
|
|
|
"VMware %s version %s, or greater, is required. Found version: %s", product, versionWanted, versionFound)
|
2014-04-06 18:31:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|