forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
106 lines (86 loc) · 2.69 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package vmware
import (
"bytes"
"fmt"
"log"
"os/exec"
"runtime"
"strings"
)
// A driver is able to talk to VMware, control virtual machines, etc.
type Driver interface {
// CompactDisk compacts a virtual disk.
CompactDisk(string) error
// CreateDisk creates a virtual disk with the given size.
CreateDisk(string, string, string) error
// Checks if the VMX file at the given path is running.
IsRunning(string) (bool, error)
// Start starts a VM specified by the path to the VMX given.
Start(string, bool) error
// Stop stops a VM specified by the path to the VMX given.
Stop(string) error
// Get the path to the VMware ISO for the given flavor.
ToolsIsoPath(string) string
// Get the path to the DHCP leases file for the given device.
DhcpLeasesPath(string) string
// 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
}
// NewDriver returns a new driver implementation for this operating
// system, or an error if the driver couldn't be initialized.
func NewDriver() (Driver, error) {
drivers := []Driver{}
switch runtime.GOOS {
case "darwin":
drivers = []Driver{
&Fusion5Driver{
AppPath: "/Applications/VMware Fusion.app",
},
}
case "linux":
drivers = []Driver{
new(Workstation9Driver),
new(Player5LinuxDriver),
}
case "windows":
drivers = []Driver{
new(Workstation9Driver),
}
default:
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
}
errs := ""
for _, driver := range drivers {
err := driver.Verify()
if err == nil {
return driver, nil
}
errs += "* " + err.Error() + "\n"
}
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)
}
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
}