2013-06-06 15:19:38 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
2013-07-16 18:02:27 -04:00
|
|
|
"bytes"
|
2013-06-08 00:46:59 -04:00
|
|
|
"fmt"
|
2013-07-16 18:02:27 -04:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
2013-07-16 17:23:01 -04:00
|
|
|
"runtime"
|
2013-07-16 18:02:27 -04:00
|
|
|
"strings"
|
2013-06-06 15:19:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// A driver is able to talk to VMware, control virtual machines, etc.
|
|
|
|
type Driver interface {
|
2013-07-01 22:25:33 -04:00
|
|
|
// CompactDisk compacts a virtual disk.
|
|
|
|
CompactDisk(string) error
|
|
|
|
|
2013-06-06 15:19:38 -04:00
|
|
|
// CreateDisk creates a virtual disk with the given size.
|
|
|
|
CreateDisk(string, string) error
|
|
|
|
|
2013-06-06 19:14:07 -04:00
|
|
|
// Checks if the VMX file at the given path is running.
|
|
|
|
IsRunning(string) (bool, error)
|
|
|
|
|
2013-06-06 15:19:38 -04:00
|
|
|
// Start starts a VM specified by the path to the VMX given.
|
2013-07-02 00:13:13 -04:00
|
|
|
Start(string, bool) error
|
2013-06-06 15:19:38 -04:00
|
|
|
|
|
|
|
// Stop stops a VM specified by the path to the VMX given.
|
|
|
|
Stop(string) error
|
2013-06-08 00:46:59 -04:00
|
|
|
|
2013-07-01 13:40:38 -04:00
|
|
|
// Get the path to the VMware ISO for the given flavor.
|
|
|
|
ToolsIsoPath(string) string
|
2013-06-29 23:20:38 -04:00
|
|
|
|
2013-07-01 15:17:47 -04:00
|
|
|
// Get the path to the DHCP leases file for the given device.
|
|
|
|
DhcpLeasesPath(string) string
|
|
|
|
|
2013-06-08 00:46:59 -04:00
|
|
|
// 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-06-06 15:19:38 -04:00
|
|
|
}
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
// NewDriver returns a new driver implementation for this operating
|
|
|
|
// system, or an error if the driver couldn't be initialized.
|
|
|
|
func NewDriver() (Driver, error) {
|
2013-07-12 19:03:32 -04:00
|
|
|
drivers := []Driver{}
|
2013-06-08 00:46:59 -04:00
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
2013-07-12 19:03:32 -04:00
|
|
|
drivers = []Driver{
|
|
|
|
&Fusion5Driver{
|
|
|
|
AppPath: "/Applications/VMware Fusion.app",
|
|
|
|
},
|
2013-06-08 00:46:59 -04:00
|
|
|
}
|
2013-07-16 17:23:01 -04:00
|
|
|
case "linux":
|
2013-07-12 19:03:32 -04:00
|
|
|
drivers = []Driver{
|
|
|
|
new(Workstation9Driver),
|
|
|
|
new(Player5LinuxDriver),
|
|
|
|
}
|
2013-07-27 16:00:21 -04:00
|
|
|
case "windows":
|
2013-07-12 19:03:32 -04:00
|
|
|
drivers = []Driver{
|
|
|
|
new(Workstation9Driver),
|
|
|
|
}
|
2013-07-16 17:23:01 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
|
2013-06-08 00:46:59 -04:00
|
|
|
}
|
|
|
|
|
2013-07-12 19:03:32 -04:00
|
|
|
errs := ""
|
|
|
|
for _, driver := range drivers {
|
|
|
|
err := driver.Verify()
|
|
|
|
if err == nil {
|
|
|
|
return driver, nil
|
|
|
|
}
|
2013-08-01 01:52:36 -04:00
|
|
|
errs += "* " + err.Error() + "\n"
|
2013-06-08 00:46:59 -04:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:52:36 -04:00
|
|
|
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)
|
2013-07-01 15:17:47 -04:00
|
|
|
}
|
2013-07-16 18:02:27 -04:00
|
|
|
|
|
|
|
func runAndLog(cmd *exec.Cmd) (string, string, error) {
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
|
|
|
log.Printf("Executing: %s %v", cmd.Path, cmd.Args[1:])
|
|
|
|
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 {
|
|
|
|
err = fmt.Errorf("VMware error: %s", stderrString)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|