forked from hashicorp/packer
/
driver_workstation_unix.go
150 lines (123 loc) · 4.12 KB
/
driver_workstation_unix.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// +build !windows
// These functions are compatible with WS 9 and 10 on *NIX
package common
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
)
func workstationCheckLicense() error {
matches, err := filepath.Glob("/etc/vmware/license-ws-*")
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.")
}
return nil
}
func workstationFindVdiskManager() (string, error) {
return exec.LookPath("vmware-vdiskmanager")
}
func workstationFindVMware() (string, error) {
return exec.LookPath("vmware")
}
func workstationFindVmrun() (string, error) {
return exec.LookPath("vmrun")
}
// return the base path to vmware's config on the host
func workstationVMwareRoot() (s string, err error) {
return "/etc/vmware", nil
}
func workstationDhcpLeasesPath(device string) string {
base, err := workstationVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
return ""
}
// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
devicebase := filepath.Join(base, device)
// Walk through a list of paths searching for the correct permutation...
// ...as it appears that in >= WS14 and < WS14, the leases file may be labelled differently.
// Docs say we should expect: dhcpd/dhcpd.leases
paths := []string{"dhcpd/dhcpd.leases", "dhcpd/dhcp.leases", "dhcp/dhcpd.leases", "dhcp/dhcp.leases"}
for _, p := range paths {
fp := filepath.Join(devicebase, p)
if _, err := os.Stat(fp); !os.IsNotExist(err) {
return fp
}
}
log.Printf("Error finding VMWare DHCP Server Leases (dhcpd.leases) under device path: %s", devicebase)
return ""
}
func workstationDhcpConfPath(device string) string {
base, err := workstationVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
return ""
}
// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
devicebase := filepath.Join(base, device)
// Walk through a list of paths searching for the correct permutation...
// ...as it appears that in >= WS14 and < WS14, the dhcp config may be labelled differently.
// Docs say we should expect: dhcp/dhcp.conf
paths := []string{"dhcp/dhcp.conf", "dhcp/dhcpd.conf", "dhcpd/dhcp.conf", "dhcpd/dhcpd.conf"}
for _, p := range paths {
fp := filepath.Join(devicebase, p)
if _, err := os.Stat(fp); !os.IsNotExist(err) {
return fp
}
}
log.Printf("Error finding VMWare DHCP Server Configuration (dhcp.conf) under device path: %s", devicebase)
return ""
}
func workstationVmnetnatConfPath(device string) string {
base, err := workstationVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
return ""
}
return filepath.Join(base, device, "nat/nat.conf")
}
func workstationNetmapConfPath() string {
base, err := workstationVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
return ""
}
return filepath.Join(base, "netmap.conf")
}
func workstationToolsIsoPath(flavor string) string {
return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
}
func workstationVerifyVersion(version string) error {
if runtime.GOOS != "linux" {
return fmt.Errorf("The VMware WS version %s driver is only supported on Linux, and Windows, at the moment. Your OS: %s", version, runtime.GOOS)
}
//TODO(pmyjavec) there is a better way to find this, how?
//the default will suffice for now.
vmxpath := "/usr/lib/vmware/bin/vmware-vmx"
var stderr bytes.Buffer
cmd := exec.Command(vmxpath, "-v")
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return err
}
return workstationTestVersion(version, stderr.String())
}
func workstationTestVersion(wanted, versionOutput string) error {
versionRe := regexp.MustCompile(`(?i)VMware Workstation (\d+)\.`)
matches := versionRe.FindStringSubmatch(versionOutput)
if matches == nil {
return fmt.Errorf(
"Could not find VMware WS version in output: %s", wanted)
}
log.Printf("Detected VMware WS version: %s", matches[1])
return compareVersions(matches[1], wanted, "Workstation")
}