2013-07-27 16:00:21 -04:00
|
|
|
// +build darwin freebsd linux netbsd openbsd
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-07-16 17:58:16 -04:00
|
|
|
"errors"
|
2013-07-16 17:23:01 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
// Workstation9Driver is a driver that can run VMware Workstation 9
|
|
|
|
// on non-Windows platforms.
|
|
|
|
type Workstation9Driver struct {
|
2013-07-16 17:23:01 -04:00
|
|
|
AppPath string
|
|
|
|
VdiskManagerPath string
|
|
|
|
VmrunPath string
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if _, _, err := d.runAndLog(defragCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath)
|
|
|
|
if _, _, err := d.runAndLog(shrinkCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) CreateDisk(output string, size string) error {
|
2013-07-16 17:23:01 -04:00
|
|
|
cmd := exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", "1", output)
|
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
|
|
|
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")
|
|
|
|
stdout, _, err := d.runAndLog(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range strings.Split(stdout, "\n") {
|
|
|
|
if line == vmxPath {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
|
|
|
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")
|
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) Verify() error {
|
2013-07-16 17:23:01 -04:00
|
|
|
if err := d.findApp(); err != nil {
|
|
|
|
return fmt.Errorf("VMware Workstation application ('vmware') not found in path.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.findVmrun(); err != nil {
|
2013-07-16 17:23:52 -04:00
|
|
|
return fmt.Errorf("Required application 'vmrun' not found in path.")
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.findVdiskManager(); err != nil {
|
2013-07-16 17:23:52 -04:00
|
|
|
return fmt.Errorf("Required application 'vmware-vdiskmanager' not found in path.")
|
2013-07-16 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
2013-07-16 17:58:16 -04:00
|
|
|
// Check to see if it APPEARS to be licensed.
|
|
|
|
matches, err := filepath.Glob("/etc/vmware/license-*")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error looking for VMware license: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return errors.New("Workstation does not appear to be licensed. Please license it.")
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:23:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) findApp() error {
|
2013-07-16 17:23:01 -04:00
|
|
|
path, err := exec.LookPath("vmware")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.AppPath = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) findVdiskManager() error {
|
2013-07-16 17:23:01 -04:00
|
|
|
path, err := exec.LookPath("vmware-vdiskmanager")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.VdiskManagerPath = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) findVmrun() error {
|
2013-07-16 17:23:01 -04:00
|
|
|
path, err := exec.LookPath("vmrun")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.VmrunPath = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) ToolsIsoPath(flavor string) string {
|
2013-07-16 17:23:01 -04:00
|
|
|
return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) DhcpLeasesPath(device string) string {
|
2013-07-16 17:23:01 -04:00
|
|
|
return "/etc/vmware/" + device + "/dhcpd/dhcpd.leases"
|
|
|
|
}
|
|
|
|
|
2013-07-27 16:00:21 -04:00
|
|
|
func (d *Workstation9Driver) runAndLog(cmd *exec.Cmd) (string, string, error) {
|
2013-07-16 17:23:01 -04:00
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
|
|
|
log.Printf("Executing: %s %v", cmd.Path, cmd.Args[1:])
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
|
2013-07-16 17:46:34 -04:00
|
|
|
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)
|
2013-07-16 17:23:01 -04:00
|
|
|
|
|
|
|
return stdout.String(), stderr.String(), err
|
|
|
|
}
|