builder/vmware: We only need one runAndLog function

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

Conflicts:
	builder/vmware/driver_workstation9.go
This commit is contained in:
Julian Phillips 2013-07-16 23:02:27 +01:00 committed by Mitchell Hashimoto
parent 7b65daad37
commit df180d6146
4 changed files with 44 additions and 57 deletions

View File

@ -1,8 +1,12 @@
package vmware
import (
"bytes"
"fmt"
"log"
"os/exec"
"runtime"
"strings"
)
// A driver is able to talk to VMware, control virtual machines, etc.
@ -71,3 +75,29 @@ func NewDriver() (Driver, error) {
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
}

View File

@ -1,9 +1,7 @@
package vmware
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
@ -18,12 +16,12 @@ type Fusion5Driver struct {
func (d *Fusion5Driver) CompactDisk(diskPath string) error {
defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath)
if _, _, err := d.runAndLog(defragCmd); err != nil {
if _, _, err := runAndLog(defragCmd); err != nil {
return err
}
shrinkCmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath)
if _, _, err := d.runAndLog(shrinkCmd); err != nil {
if _, _, err := runAndLog(shrinkCmd); err != nil {
return err
}
@ -32,7 +30,7 @@ func (d *Fusion5Driver) CompactDisk(diskPath string) error {
func (d *Fusion5Driver) 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 {
if _, _, err := runAndLog(cmd); err != nil {
return err
}
@ -46,7 +44,7 @@ func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) {
}
cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "list")
stdout, _, err := d.runAndLog(cmd)
stdout, _, err := runAndLog(cmd)
if err != nil {
return false, err
}
@ -67,7 +65,7 @@ func (d *Fusion5Driver) Start(vmxPath string, headless bool) error {
}
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
}
@ -76,7 +74,7 @@ func (d *Fusion5Driver) Start(vmxPath string, headless bool) error {
func (d *Fusion5Driver) Stop(vmxPath string) error {
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
}
@ -126,17 +124,3 @@ func (d *Fusion5Driver) ToolsIsoPath(k string) string {
func (d *Fusion5Driver) DhcpLeasesPath(device string) string {
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
}

View File

@ -35,7 +35,7 @@ func (d *Player5LinuxDriver) CompactDisk(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 {
return err
}
@ -44,7 +44,7 @@ func (d *Player5LinuxDriver) qemuCompactDisk(diskPath string) error {
return err
}
if err := os.Rename(diskPath + ".new", diskPath); err != nil {
if err := os.Rename(diskPath+".new", diskPath); err != nil {
return err
}

View File

@ -1,7 +1,6 @@
package vmware
import (
"bytes"
"fmt"
"log"
"os"
@ -20,12 +19,12 @@ type Workstation9Driver struct {
func (d *Workstation9Driver) CompactDisk(diskPath string) error {
defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath)
if _, _, err := d.runAndLog(defragCmd); err != nil {
if _, _, err := runAndLog(defragCmd); err != nil {
return err
}
shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath)
if _, _, err := d.runAndLog(shrinkCmd); err != nil {
if _, _, err := runAndLog(shrinkCmd); err != nil {
return err
}
@ -34,7 +33,7 @@ func (d *Workstation9Driver) CompactDisk(diskPath string) error {
func (d *Workstation9Driver) 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 {
if _, _, err := runAndLog(cmd); err != nil {
return err
}
@ -48,7 +47,7 @@ func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) {
}
cmd := exec.Command(d.VmrunPath, "-T", "ws", "list")
stdout, _, err := d.runAndLog(cmd)
stdout, _, err := runAndLog(cmd)
if err != nil {
return false, err
}
@ -69,7 +68,7 @@ func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
}
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
}
@ -78,7 +77,7 @@ func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
func (d *Workstation9Driver) Stop(vmxPath string) error {
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
}
@ -136,29 +135,3 @@ func (d *Workstation9Driver) ToolsIsoPath(flavor string) string {
func (d *Workstation9Driver) DhcpLeasesPath(device string) string {
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
}