2013-06-06 15:19:38 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
2013-06-06 19:14:07 -04:00
|
|
|
"bytes"
|
2013-06-08 00:46:59 -04:00
|
|
|
"fmt"
|
2013-06-20 15:32:22 -04:00
|
|
|
"log"
|
2013-06-08 00:46:59 -04:00
|
|
|
"os"
|
2013-06-06 15:19:38 -04:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2013-06-06 19:14:07 -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
|
|
|
}
|
|
|
|
|
|
|
|
// Fusion5Driver is a driver that can run VMWare Fusion 5.
|
|
|
|
type Fusion5Driver struct {
|
|
|
|
// This is the path to the "VMware Fusion.app"
|
|
|
|
AppPath string
|
|
|
|
}
|
|
|
|
|
2013-07-01 22:25:33 -04:00
|
|
|
func (d *Fusion5Driver) CompactDisk(diskPath string) error {
|
2013-07-01 22:45:46 -04:00
|
|
|
defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath)
|
|
|
|
if _, _, err := d.runAndLog(defragCmd); err != nil {
|
2013-07-01 22:25:33 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-01 22:45:46 -04:00
|
|
|
shrinkCmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath)
|
|
|
|
if _, _, err := d.runAndLog(shrinkCmd); err != nil {
|
2013-07-01 22:25:33 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-06 15:19:38 -04:00
|
|
|
func (d *Fusion5Driver) CreateDisk(output string, size string) error {
|
2013-06-08 00:46:59 -04:00
|
|
|
cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-t", "1", output)
|
2013-06-20 15:32:22 -04:00
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
2013-06-06 15:19:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-06 19:14:07 -04:00
|
|
|
func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) {
|
2013-06-08 22:01:49 -04:00
|
|
|
vmxPath, err := filepath.Abs(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2013-06-06 19:14:07 -04:00
|
|
|
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "list")
|
2013-06-20 15:32:22 -04:00
|
|
|
stdout, _, err := d.runAndLog(cmd)
|
|
|
|
if err != nil {
|
2013-06-06 19:14:07 -04:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2013-06-20 15:32:22 -04:00
|
|
|
for _, line := range strings.Split(stdout, "\n") {
|
2013-06-06 19:14:07 -04:00
|
|
|
if line == vmxPath {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2013-07-02 00:13:13 -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-06-20 15:32:22 -04:00
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
2013-06-06 15:19:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion5Driver) Stop(vmxPath string) error {
|
|
|
|
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "stop", vmxPath, "hard")
|
2013-06-20 15:32:22 -04:00
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
2013-06-06 15:19:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-08 00:46:59 -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) {
|
|
|
|
return fmt.Errorf("Critical application 'vmrun' not found at path: %s", d.vmrunPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(d.vdiskManagerPath()); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("Critical application vdisk manager not found at path: %s", d.vdiskManagerPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Fusion5Driver) vdiskManagerPath() string {
|
|
|
|
return filepath.Join(d.AppPath, "Contents", "Library", "vmware-vdiskmanager")
|
|
|
|
}
|
|
|
|
|
2013-06-06 15:19:38 -04:00
|
|
|
func (d *Fusion5Driver) vmrunPath() string {
|
|
|
|
return filepath.Join(d.AppPath, "Contents", "Library", "vmrun")
|
|
|
|
}
|
2013-06-20 15:32:22 -04:00
|
|
|
|
2013-07-01 13:40:38 -04:00
|
|
|
func (d *Fusion5Driver) ToolsIsoPath(k string) string {
|
|
|
|
return filepath.Join(d.AppPath, "Contents", "Library", "isoimages", k+".iso")
|
2013-06-29 23:08:27 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 15:17:47 -04:00
|
|
|
func (d *Fusion5Driver) DhcpLeasesPath(device string) string {
|
|
|
|
return "/etc/vmware/vmnet-dhcpd-" + device + ".leases"
|
|
|
|
}
|
|
|
|
|
2013-06-20 15:32:22 -04:00
|
|
|
func (d *Fusion5Driver) 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()
|
|
|
|
|
|
|
|
log.Printf("stdout: %s", strings.TrimSpace(stdout.String()))
|
|
|
|
log.Printf("stderr: %s", strings.TrimSpace(stderr.String()))
|
|
|
|
|
|
|
|
return stdout.String(), stderr.String(), err
|
|
|
|
}
|
2013-07-01 15:17:47 -04:00
|
|
|
|
|
|
|
// WS9LnxDriver is a driver that can run VMware Workstation 9 on Linux.
|
|
|
|
type WS9LnxDriver struct {
|
|
|
|
// These are paths to useful VMware Workstation binaries
|
|
|
|
AppPath string
|
|
|
|
VdiskManagerPath string
|
|
|
|
VmrunPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) CompactDisk(diskPath string) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) CreateDisk(output string, size string) error {
|
|
|
|
cmd := exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", "1", output)
|
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) IsRunning(vmxPath string) (bool, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) Start(vmxPath string, headless bool) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) Stop(vmxPath string) error {
|
|
|
|
cmd := exec.Command(d.VmrunPath, "-T", "ws", "stop", vmxPath, "hard")
|
|
|
|
if _, _, err := d.runAndLog(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) Verify() error {
|
|
|
|
if err := d.findApp(); err != nil {
|
|
|
|
return fmt.Errorf("VMware Workstation application ('vmware') not found in path.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.findVmrun(); err != nil {
|
|
|
|
return fmt.Errorf("Critical application 'vmrun' not found in path.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.findVdiskManager(); err != nil {
|
|
|
|
return fmt.Errorf("Critical application 'vmware-vdiskmanager' not found in path.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) findApp() error {
|
|
|
|
path, err := exec.LookPath("vmware")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.AppPath = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) findVdiskManager() error {
|
|
|
|
path, err := exec.LookPath("vmware-vdiskmanager")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.VdiskManagerPath = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) findVmrun() error {
|
|
|
|
path, err := exec.LookPath("vmrun")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.VmrunPath = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) ToolsIsoPath(flavor string) string {
|
|
|
|
return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) DhcpLeasesPath(device string) string {
|
|
|
|
return "/etc/vmware/" + device + "/dhcpd/dhcpd.leases"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *WS9LnxDriver) 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()
|
|
|
|
|
|
|
|
log.Printf("stdout: %s", strings.TrimSpace(stdout.String()))
|
|
|
|
log.Printf("stderr: %s", strings.TrimSpace(stderr.String()))
|
|
|
|
|
|
|
|
return stdout.String(), stderr.String(), err
|
|
|
|
}
|