Commit df180d61 authored by Julian Phillips's avatar Julian Phillips Committed by Mitchell Hashimoto

builder/vmware: We only need one runAndLog function

We shouldn't need a different one for each driver ...

Conflicts:
	builder/vmware/driver_workstation9.go
parent 7b65daad
package vmware package vmware
import ( import (
"bytes"
"fmt" "fmt"
"log"
"os/exec"
"runtime" "runtime"
"strings"
) )
// A driver is able to talk to VMware, control virtual machines, etc. // A driver is able to talk to VMware, control virtual machines, etc.
...@@ -71,3 +75,29 @@ func NewDriver() (Driver, error) { ...@@ -71,3 +75,29 @@ func NewDriver() (Driver, error) {
return nil, fmt.Errorf("Unable to initialize any driver:\n%s", errs) return nil, fmt.Errorf("Unable to initialize any driver:\n%s", errs)
} }
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
}
package vmware package vmware
import ( import (
"bytes"
"fmt" "fmt"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
...@@ -18,12 +16,12 @@ type Fusion5Driver struct { ...@@ -18,12 +16,12 @@ type Fusion5Driver struct {
func (d *Fusion5Driver) CompactDisk(diskPath string) error { func (d *Fusion5Driver) CompactDisk(diskPath string) error {
defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath) defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath)
if _, _, err := d.runAndLog(defragCmd); err != nil { if _, _, err := runAndLog(defragCmd); err != nil {
return err return err
} }
shrinkCmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath) shrinkCmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath)
if _, _, err := d.runAndLog(shrinkCmd); err != nil { if _, _, err := runAndLog(shrinkCmd); err != nil {
return err return err
} }
...@@ -32,7 +30,7 @@ func (d *Fusion5Driver) CompactDisk(diskPath string) error { ...@@ -32,7 +30,7 @@ func (d *Fusion5Driver) CompactDisk(diskPath string) error {
func (d *Fusion5Driver) CreateDisk(output string, size string) error { func (d *Fusion5Driver) CreateDisk(output string, size string) error {
cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-t", "1", output) cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-t", "1", output)
if _, _, err := d.runAndLog(cmd); err != nil { if _, _, err := runAndLog(cmd); err != nil {
return err return err
} }
...@@ -46,7 +44,7 @@ func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) { ...@@ -46,7 +44,7 @@ func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) {
} }
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "list") cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "list")
stdout, _, err := d.runAndLog(cmd) stdout, _, err := runAndLog(cmd)
if err != nil { if err != nil {
return false, err return false, err
} }
...@@ -67,7 +65,7 @@ func (d *Fusion5Driver) Start(vmxPath string, headless bool) error { ...@@ -67,7 +65,7 @@ func (d *Fusion5Driver) Start(vmxPath string, headless bool) error {
} }
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "start", vmxPath, guiArgument) cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "start", vmxPath, guiArgument)
if _, _, err := d.runAndLog(cmd); err != nil { if _, _, err := runAndLog(cmd); err != nil {
return err return err
} }
...@@ -76,7 +74,7 @@ func (d *Fusion5Driver) Start(vmxPath string, headless bool) error { ...@@ -76,7 +74,7 @@ func (d *Fusion5Driver) Start(vmxPath string, headless bool) error {
func (d *Fusion5Driver) Stop(vmxPath string) error { func (d *Fusion5Driver) Stop(vmxPath string) error {
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "stop", vmxPath, "hard") cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "stop", vmxPath, "hard")
if _, _, err := d.runAndLog(cmd); err != nil { if _, _, err := runAndLog(cmd); err != nil {
return err return err
} }
...@@ -126,17 +124,3 @@ func (d *Fusion5Driver) ToolsIsoPath(k string) string { ...@@ -126,17 +124,3 @@ func (d *Fusion5Driver) ToolsIsoPath(k string) string {
func (d *Fusion5Driver) DhcpLeasesPath(device string) string { func (d *Fusion5Driver) DhcpLeasesPath(device string) string {
return "/var/db/vmware/vmnet-dhcpd-" + device + ".leases" return "/var/db/vmware/vmnet-dhcpd-" + device + ".leases"
} }
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
}
...@@ -35,7 +35,7 @@ func (d *Player5LinuxDriver) CompactDisk(diskPath string) error { ...@@ -35,7 +35,7 @@ func (d *Player5LinuxDriver) CompactDisk(diskPath string) error {
} }
func (d *Player5LinuxDriver) qemuCompactDisk(diskPath string) error { func (d *Player5LinuxDriver) qemuCompactDisk(diskPath string) error {
cmd := exec.Command(d.QemuImgPath, "convert", "-f", "vmdk", "-O", "vmdk", "-o", "compat6", diskPath, diskPath + ".new") cmd := exec.Command(d.QemuImgPath, "convert", "-f", "vmdk", "-O", "vmdk", "-o", "compat6", diskPath, diskPath+".new")
if _, _, err := runAndLog(cmd); err != nil { if _, _, err := runAndLog(cmd); err != nil {
return err return err
} }
...@@ -44,7 +44,7 @@ func (d *Player5LinuxDriver) qemuCompactDisk(diskPath string) error { ...@@ -44,7 +44,7 @@ func (d *Player5LinuxDriver) qemuCompactDisk(diskPath string) error {
return err return err
} }
if err := os.Rename(diskPath + ".new", diskPath); err != nil { if err := os.Rename(diskPath+".new", diskPath); err != nil {
return err return err
} }
......
package vmware package vmware
import ( import (
"bytes"
"fmt" "fmt"
"log" "log"
"os" "os"
...@@ -20,12 +19,12 @@ type Workstation9Driver struct { ...@@ -20,12 +19,12 @@ type Workstation9Driver struct {
func (d *Workstation9Driver) CompactDisk(diskPath string) error { func (d *Workstation9Driver) CompactDisk(diskPath string) error {
defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath) defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath)
if _, _, err := d.runAndLog(defragCmd); err != nil { if _, _, err := runAndLog(defragCmd); err != nil {
return err return err
} }
shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath) shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath)
if _, _, err := d.runAndLog(shrinkCmd); err != nil { if _, _, err := runAndLog(shrinkCmd); err != nil {
return err return err
} }
...@@ -34,7 +33,7 @@ func (d *Workstation9Driver) CompactDisk(diskPath string) error { ...@@ -34,7 +33,7 @@ func (d *Workstation9Driver) CompactDisk(diskPath string) error {
func (d *Workstation9Driver) CreateDisk(output string, size string) error { func (d *Workstation9Driver) CreateDisk(output string, size string) error {
cmd := exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", "1", output) cmd := exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", "1", output)
if _, _, err := d.runAndLog(cmd); err != nil { if _, _, err := runAndLog(cmd); err != nil {
return err return err
} }
...@@ -48,7 +47,7 @@ func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) { ...@@ -48,7 +47,7 @@ func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) {
} }
cmd := exec.Command(d.VmrunPath, "-T", "ws", "list") cmd := exec.Command(d.VmrunPath, "-T", "ws", "list")
stdout, _, err := d.runAndLog(cmd) stdout, _, err := runAndLog(cmd)
if err != nil { if err != nil {
return false, err return false, err
} }
...@@ -69,7 +68,7 @@ func (d *Workstation9Driver) Start(vmxPath string, headless bool) error { ...@@ -69,7 +68,7 @@ func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
} }
cmd := exec.Command(d.VmrunPath, "-T", "ws", "start", vmxPath, guiArgument) cmd := exec.Command(d.VmrunPath, "-T", "ws", "start", vmxPath, guiArgument)
if _, _, err := d.runAndLog(cmd); err != nil { if _, _, err := runAndLog(cmd); err != nil {
return err return err
} }
...@@ -78,7 +77,7 @@ func (d *Workstation9Driver) Start(vmxPath string, headless bool) error { ...@@ -78,7 +77,7 @@ func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
func (d *Workstation9Driver) Stop(vmxPath string) error { func (d *Workstation9Driver) Stop(vmxPath string) error {
cmd := exec.Command(d.VmrunPath, "-T", "ws", "stop", vmxPath, "hard") cmd := exec.Command(d.VmrunPath, "-T", "ws", "stop", vmxPath, "hard")
if _, _, err := d.runAndLog(cmd); err != nil { if _, _, err := runAndLog(cmd); err != nil {
return err return err
} }
...@@ -136,29 +135,3 @@ func (d *Workstation9Driver) ToolsIsoPath(flavor string) string { ...@@ -136,29 +135,3 @@ func (d *Workstation9Driver) ToolsIsoPath(flavor string) string {
func (d *Workstation9Driver) DhcpLeasesPath(device string) string { func (d *Workstation9Driver) DhcpLeasesPath(device string) string {
return workstationDhcpLeasesPath(device) return workstationDhcpLeasesPath(device)
} }
func (d *Workstation9Driver) 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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment