/
vm.go
462 lines (378 loc) · 10.7 KB
/
vm.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package vix
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"runtime/debug"
"strings"
"time"
"github.com/c4milo/unpackit"
"github.com/dustin/go-humanize"
govix "github.com/hooklift/govix"
)
// Virtual machine configuration
type VM struct {
// Which VMware VIX service provider to use. ie: fusion, workstation, server, etc
Provider string
// Whether to verify SSL or not for remote connections in ESXi
VerifySSL bool
// Name of the virtual machine
Name string
// Description for the virtual machine, it is created as an annotation in
// VMware.
Description string
// Image to use during the creation of this virtual machine
Image Image
// Number of virtual cpus
CPUs uint
// Memory size in megabytes.
Memory string
// Switches to where this machine is going to be attach to
VSwitches []string
// Whether to upgrade the VM virtual hardware
UpgradeVHardware bool
// The timeout to wait for VMware Tools to be initialized inside the VM
ToolsInitTimeout time.Duration
// Whether to launch the VM with graphical environment
LaunchGUI bool
// Whether to enable or disable shared folders for this VM
SharedFolders bool
// Network adapters
VNetworkAdapters []*govix.NetworkAdapter
// CD/DVD drives
CDDVDDrives []*govix.CDDVDDrive
// VM IP address as reported by VIX
IPAddress string
}
// Creates VIX instance with VMware
func (v *VM) client() (*govix.Host, error) {
var p govix.Provider
switch strings.ToLower(v.Provider) {
case "fusion", "workstation":
p = govix.VMWARE_WORKSTATION
case "serverv1":
p = govix.VMWARE_SERVER
case "serverv2":
p = govix.VMWARE_VI_SERVER
case "player":
p = govix.VMWARE_PLAYER
case "workstation_shared":
p = govix.VMWARE_WORKSTATION_SHARED
default:
p = govix.VMWARE_WORKSTATION
}
var options govix.HostOption
if v.VerifySSL {
options = govix.VERIFY_SSL_CERT
}
host, err := govix.Connect(govix.ConnectConfig{
Provider: p,
Options: options,
})
if err != nil {
return nil, err
}
log.Printf("[INFO] VIX client configured for product: VMware %s. SSL: %t", v.Provider, v.VerifySSL)
return host, nil
}
// Sets default values for VM attributes
func (v *VM) SetDefaults() {
if v.CPUs <= 0 {
v.CPUs = 2
}
if v.Memory == "" {
v.Memory = "512mib"
}
if v.Description == "" {
v.Description = "Machine was created using Terraform VIX provider"
}
if v.ToolsInitTimeout.Seconds() <= 0 {
v.ToolsInitTimeout = time.Duration(30) * time.Second
}
}
// Downloads, extracts and opens Gold virtual machine, then it creates a clone
// out of it.
func (v *VM) Create() (string, error) {
log.Printf("[DEBUG] Creating VM resource...")
usr, err := user.Current()
if err != nil {
return "", err
}
image := v.Image
goldPath := filepath.Join(usr.HomeDir, filepath.Join(".terraform/vix/gold", image.Checksum))
_, err = os.Stat(goldPath)
finfo, _ := ioutil.ReadDir(goldPath)
goldPathEmpty := len(finfo) == 0
if os.IsNotExist(err) || goldPathEmpty {
log.Println("[DEBUG] Gold virtual machine does not exist or is empty")
imgPath := filepath.Join(usr.HomeDir, ".terraform/vix/images", image.Checksum)
if err = image.Download(imgPath); err != nil {
return "", err
}
defer image.file.Close()
// Makes sure file cursor is in the right position.
_, err := image.file.Seek(0, 0)
if err != nil {
return "", err
}
log.Printf("[DEBUG] Unpacking Gold virtual machine into %s\n", goldPath)
_, err = unpackit.Unpack(image.file, goldPath)
if err != nil {
debug.PrintStack()
log.Printf("[ERROR] Unpacking Gold image %s\n", image.file.Name())
return "", err
}
}
pattern := filepath.Join(goldPath, "**.vmx")
log.Printf("[DEBUG] Finding Gold virtual machine vmx file in %s", pattern)
files, _ := filepath.Glob(pattern)
if len(files) == 0 {
return "", fmt.Errorf("[ERROR] vmx file was not found: %s", pattern)
}
vmxFile := files[0]
log.Printf("[DEBUG] Gold virtual machine vmx file found %v", vmxFile)
// Gets VIX instance
client, err := v.client()
if err != nil {
return "", err
}
defer client.Disconnect()
log.Printf("[INFO] Opening Gold virtual machine from %s", vmxFile)
vm, err := client.OpenVM(vmxFile, v.Image.Password)
if err != nil {
return "", err
}
baseVMDir := filepath.Join(usr.HomeDir, ".terraform", "vix", "vms",
image.Checksum, v.Name)
newvmx := filepath.Join(baseVMDir, v.Name+".vmx")
if _, err = os.Stat(newvmx); os.IsNotExist(err) {
log.Printf("[INFO] Virtual machine clone not found: %s, err: %+v", newvmx, err)
// If there is not a VMX file, make sure nothing else is in there either.
// We were seeing VIX 13004 errors when only a nvram file existed.
os.RemoveAll(baseVMDir)
log.Printf("[INFO] Cloning Gold virtual machine into %s...", newvmx)
_, err := vm.Clone(govix.CLONETYPE_FULL, newvmx)
// If there is an error and the error is other than "The snapshot already exists"
// then return the error
if err != nil && err.(*govix.Error).Code != 13004 {
return "", err
}
} else {
log.Printf("[INFO] Virtual Machine clone %s already exist, moving on.", newvmx)
}
if err = v.Update(newvmx); err != nil {
return "", err
}
return newvmx, nil
}
// Opens and updates virtual machine resource
func (v *VM) Update(vmxFile string) error {
// Sets default values if some attributes were not set or have
// invalid values
v.SetDefaults()
// Gets VIX instance
client, err := v.client()
if err != nil {
return err
}
defer client.Disconnect()
if client.Provider == govix.VMWARE_VI_SERVER ||
client.Provider == govix.VMWARE_SERVER {
log.Printf("[INFO] Registering VM in host's inventory...")
err = client.RegisterVM(vmxFile)
if err != nil {
return err
}
}
log.Printf("[INFO] Opening virtual machine from %s", vmxFile)
vm, err := client.OpenVM(vmxFile, v.Image.Password)
if err != nil {
return err
}
running, err := vm.IsRunning()
if err != nil {
return err
}
if running {
log.Printf("[INFO] Virtual machine seems to be running, we need to " +
"power it off in order to make changes.")
err = v.powerOff(vm)
if err != nil {
return err
}
}
memoryInMb, err := humanize.ParseBytes(v.Memory)
if err != nil {
log.Printf("[WARN] Unable to set memory size, defaulting to 512mib: %s", err)
memoryInMb = 512
} else {
memoryInMb = (memoryInMb / 1024) / 1024
}
log.Printf("[DEBUG] Setting memory size to %d megabytes", memoryInMb)
vm.SetMemorySize(uint(memoryInMb))
log.Printf("[DEBUG] Setting vcpus to %d", v.CPUs)
vm.SetNumberVcpus(v.CPUs)
log.Printf("[DEBUG] Setting name to %s", v.Name)
vm.SetDisplayName(v.Name)
log.Printf("[DEBUG] Setting description to %s", v.Description)
vm.SetAnnotation(v.Description)
if v.UpgradeVHardware &&
client.Provider != govix.VMWARE_PLAYER {
log.Println("[INFO] Upgrading virtual hardware...")
err = vm.UpgradeVHardware()
if err != nil {
return err
}
}
log.Printf("[DEBUG] Removing all network adapters from vmx file...")
err = vm.RemoveAllNetworkAdapters()
if err != nil {
return err
}
log.Println("[INFO] Attaching virtual network adapters...")
for _, adapter := range v.VNetworkAdapters {
adapter.StartConnected = true
if adapter.ConnType == govix.NETWORK_BRIDGED {
adapter.LinkStatePropagation = true
}
log.Printf("[DEBUG] Adapter: %+v", adapter)
err := vm.AddNetworkAdapter(adapter)
if err != nil {
return err
}
}
log.Printf("[DEBUG] Removing all CD/DVD drives from vmx file...")
err = vm.RemoveAllCDDVDDrives()
if err != nil {
return err
}
log.Println("[INFO] Attaching CD/DVD drives... ")
for _, cdrom := range v.CDDVDDrives {
err := vm.AttachCDDVD(cdrom)
if err != nil {
return err
}
}
log.Println("[INFO] Powering virtual machine on...")
var options govix.VMPowerOption
if v.LaunchGUI {
log.Println("[INFO] Preparing to launch GUI...")
options |= govix.VMPOWEROP_LAUNCH_GUI
}
options |= govix.VMPOWEROP_NORMAL
err = vm.PowerOn(options)
if err != nil {
return err
}
log.Println("[INFO] Waiting for VMware Tools to initialize...")
err = vm.WaitForToolsInGuest(v.ToolsInitTimeout)
if err != nil {
log.Println("[WARN] VMware Tools took too long to initialize or is not " +
"installed.")
if v.SharedFolders {
log.Println("[WARN] Enabling shared folders is not possible.")
}
return nil
}
if v.SharedFolders {
log.Println("[DEBUG] Enabling shared folders...")
err = vm.EnableSharedFolders(v.SharedFolders)
if err != nil {
return err
}
}
return nil
}
// Powers off a virtual machine attempting a graceful shutdown.
func (v *VM) powerOff(vm *govix.VM) error {
tstate, err := vm.ToolsState()
if err != nil {
return err
}
var powerOpts govix.VMPowerOption
log.Printf("Tools state %d", tstate)
if (tstate & govix.TOOLSSTATE_RUNNING) != 0 {
log.Printf("[INFO] VMware Tools is running, attempting a graceful shutdown...")
// if VMware Tools is running, attempt a graceful shutdown.
powerOpts |= govix.VMPOWEROP_FROM_GUEST
} else {
log.Printf("[INFO] VMware Tools is NOT running, shutting down the " +
"machine abruptly...")
powerOpts |= govix.VMPOWEROP_NORMAL
}
err = vm.PowerOff(powerOpts)
if err != nil {
return err
}
log.Printf("[DEBUG] Virtual machine is off.")
return nil
}
// Destroys a virtual machine resource
func (v *VM) Destroy(vmxFile string) error {
log.Printf("[DEBUG] Destroying VM resource %s...", vmxFile)
client, err := v.client()
if err != nil {
return err
}
defer client.Disconnect()
vm, err := client.OpenVM(vmxFile, v.Image.Password)
if err != nil {
return err
}
running, err := vm.IsRunning()
if err != nil {
return err
}
if running {
if err = v.powerOff(vm); err != nil {
return err
}
}
if client.Provider == govix.VMWARE_VI_SERVER ||
client.Provider == govix.VMWARE_SERVER {
log.Printf("[INFO] Unregistering VM from host's inventory...")
err := client.UnregisterVM(vmxFile)
if err != nil {
return err
}
}
return vm.Delete(govix.VMDELETE_KEEP_FILES | govix.VMDELETE_FORCE)
}
// Refreshes state with VMware
func (v *VM) Refresh(vmxFile string) (bool, error) {
log.Printf("[DEBUG] Syncing VM resource %s...", vmxFile)
client, err := v.client()
if err != nil {
return false, err
}
defer client.Disconnect()
vm, err := client.OpenVM(vmxFile, v.Image.Password)
if err != nil {
return false, err
}
running, err := vm.IsRunning()
if !running {
return running, err
}
vcpus, err := vm.Vcpus()
if err != nil {
return running, err
}
memory, err := vm.MemorySize()
if err != nil {
return running, err
}
// We need to convert memory value to megabytes so humanize can interpret it
// properly.
memory = (memory * 1024) * 1024
v.Memory = strings.ToLower(humanize.IBytes(uint64(memory)))
v.CPUs = uint(vcpus)
v.Name, err = vm.DisplayName()
v.Description, err = vm.Annotation()
v.VNetworkAdapters, err = vm.NetworkAdapters()
v.IPAddress, err = vm.IPAddress()
return running, err
}