/
configuration.go
481 lines (373 loc) · 12.4 KB
/
configuration.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package vsphere
import (
"encoding/json"
"fmt"
"net"
"net/url"
"time"
"github.com/Fred78290/kubernetes-vmware-autoscaler/context"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/vim25/soap"
)
// Configuration declares vsphere connection info
type Configuration struct {
URL string `json:"url"`
UserName string `json:"uid"`
Password string `json:"password"`
Insecure bool `json:"insecure"`
DataCenter string `json:"dc"`
DataStore string `json:"datastore"`
Resource string `json:"resource-pool"`
VMBasePath string `json:"vmFolder"`
Timeout time.Duration `json:"timeout"`
TemplateName string `json:"template-name"`
Template bool `json:"template"`
LinkedClone bool `json:"linked"`
AllowUpgrade bool `json:"allow-upgrade"`
Customization string `json:"customization"`
Network *Network `json:"network"`
TestMode bool `json:"-"`
}
// Status shortened vm status
type Status struct {
Interfaces []NetworkInterface
Powered bool
}
// Copy Make a deep copy from src into dst.
func Copy(dst interface{}, src interface{}) error {
if dst == nil {
return fmt.Errorf("dst cannot be nil")
}
if src == nil {
return fmt.Errorf("src cannot be nil")
}
bytes, err := json.Marshal(src)
if err != nil {
return fmt.Errorf("unable to marshal src: %s", err)
}
err = json.Unmarshal(bytes, dst)
if err != nil {
return fmt.Errorf("unable to unmarshal into dst: %s", err)
}
return nil
}
func (conf *Configuration) getURL() (string, error) {
u, err := url.Parse(conf.URL)
if err != nil {
return "", err
}
u.User = url.UserPassword(conf.UserName, conf.Password)
return u.String(), err
}
// Create a shadow copy
func (conf *Configuration) Copy() *Configuration {
var dup Configuration
_ = Copy(&dup, conf)
dup.TestMode = conf.TestMode
return &dup
}
// Clone duplicate the conf, change ip address in network config if needed
func (conf *Configuration) Clone(nodeIndex int) (*Configuration, error) {
dup := conf.Copy()
if dup.Network != nil {
for _, inf := range dup.Network.Interfaces {
if !inf.DHCP {
ip := net.ParseIP(inf.IPAddress)
address := ip.To4()
address[3] += byte(nodeIndex)
inf.IPAddress = ip.String()
}
}
}
return dup, nil
}
func (conf *Configuration) FindPreferredIPAddress(interfaces []NetworkInterface) string {
address := ""
for _, inf := range interfaces {
if declaredInf := conf.FindInterfaceByName(inf.NetworkName); declaredInf != nil {
if declaredInf.Primary {
return inf.IPAddress
}
}
}
return address
}
func (conf *Configuration) FindInterfaceByName(networkName string) *NetworkInterface {
if conf.Network != nil {
for _, inf := range conf.Network.Interfaces {
if inf.NetworkName == networkName {
return inf
}
}
}
return nil
}
// GetClient create a new govomi client
func (conf *Configuration) GetClient(ctx *context.Context) (*Client, error) {
var u *url.URL
var sURL string
var err error
var c *govmomi.Client
if sURL, err = conf.getURL(); err == nil {
if u, err = soap.ParseURL(sURL); err == nil {
// Connect and log in to ESX or vCenter
if c, err = govmomi.NewClient(ctx, u, conf.Insecure); err == nil {
return &Client{
Client: c,
Configuration: conf,
}, nil
}
}
}
return nil, err
}
// CreateWithContext will create a named VM not powered
// memory and disk are in megabytes
func (conf *Configuration) CreateWithContext(ctx *context.Context, name string, userName, authKey string, cloudInit interface{}, network *Network, annotation string, expandHardDrive bool, memory, cpus, disk, nodeIndex int) (*VirtualMachine, error) {
var err error
var client *Client
var dc *Datacenter
var ds *Datastore
var vm *VirtualMachine
if client, err = conf.GetClient(ctx); err == nil {
if dc, err = client.GetDatacenter(ctx, conf.DataCenter); err == nil {
if ds, err = dc.GetDatastore(ctx, conf.DataStore); err == nil {
if vm, err = ds.CreateVirtualMachine(ctx, name, conf.TemplateName, conf.VMBasePath, conf.Resource, conf.Template, conf.LinkedClone, network, conf.Customization, nodeIndex); err == nil {
err = vm.Configure(ctx, userName, authKey, cloudInit, network, annotation, expandHardDrive, memory, cpus, disk, nodeIndex, conf.AllowUpgrade)
}
}
}
}
// If an error occured delete VM
if err != nil && vm != nil {
_ = vm.Delete(ctx)
}
return vm, err
}
// Create will create a named VM not powered
// memory and disk are in megabytes
func (conf *Configuration) Create(name string, userName, authKey string, cloudInit interface{}, network *Network, annotation string, expandHardDrive bool, memory, cpus, disk, nodeIndex int) (*VirtualMachine, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.CreateWithContext(ctx, name, userName, authKey, cloudInit, network, annotation, expandHardDrive, memory, cpus, disk, nodeIndex)
}
// DeleteWithContext a VM by name
func (conf *Configuration) DeleteWithContext(ctx *context.Context, name string) error {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return err
}
return vm.Delete(ctx)
}
// Delete a VM by name
func (conf *Configuration) Delete(name string) error {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.DeleteWithContext(ctx, name)
}
// VirtualMachineWithContext Retrieve VM by name
func (conf *Configuration) VirtualMachineWithContext(ctx *context.Context, name string) (*VirtualMachine, error) {
var err error
var client *Client
var dc *Datacenter
var ds *Datastore
if client, err = conf.GetClient(ctx); err == nil {
if dc, err = client.GetDatacenter(ctx, conf.DataCenter); err == nil {
if ds, err = dc.GetDatastore(ctx, conf.DataStore); err == nil {
return ds.VirtualMachine(ctx, name)
}
}
}
return nil, err
}
// VirtualMachine Retrieve VM by name
func (conf *Configuration) VirtualMachine(name string) (*VirtualMachine, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.VirtualMachineWithContext(ctx, name)
}
// VirtualMachineListWithContext return all VM for the current datastore
func (conf *Configuration) VirtualMachineListWithContext(ctx *context.Context) ([]*VirtualMachine, error) {
var err error
var client *Client
var dc *Datacenter
var ds *Datastore
if client, err = conf.GetClient(ctx); err == nil {
if dc, err = client.GetDatacenter(ctx, conf.DataCenter); err == nil {
if ds, err = dc.GetDatastore(ctx, conf.DataStore); err == nil {
return ds.List(ctx)
}
}
}
return nil, err
}
// VirtualMachineList return all VM for the current datastore
func (conf *Configuration) VirtualMachineList() ([]*VirtualMachine, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.VirtualMachineListWithContext(ctx)
}
// UUID get VM UUID by name
func (conf *Configuration) UUIDWithContext(ctx *context.Context, name string) (string, error) {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return "", err
}
return vm.UUID(ctx), nil
}
// UUID get VM UUID by name
func (conf *Configuration) UUID(name string) (string, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.UUIDWithContext(ctx, name)
}
// WaitForIPWithContext wait ip a VM by name
func (conf *Configuration) WaitForIPWithContext(ctx *context.Context, name string) (string, error) {
if conf.TestMode {
return "127.0.0.1", nil
}
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return "", err
}
return vm.WaitForIP(ctx)
}
// WaitForIP wait ip a VM by name
func (conf *Configuration) WaitForIP(name string) (string, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.WaitForIPWithContext(ctx, name)
}
// SetAutoStartWithContext set autostart for the VM
func (conf *Configuration) SetAutoStartWithContext(ctx *context.Context, esxi, name string, startOrder int) error {
var err error = nil
if !conf.TestMode {
var client *Client
var dc *Datacenter
var host *HostAutoStartManager
if client, err = conf.GetClient(ctx); err == nil {
if dc, err = client.GetDatacenter(ctx, conf.DataCenter); err == nil {
if host, err = dc.GetHostAutoStartManager(ctx, esxi); err == nil {
return host.SetAutoStart(ctx, conf.DataStore, name, startOrder)
}
}
}
}
return err
}
// WaitForToolsRunningWithContext wait vmware tools is running a VM by name
func (conf *Configuration) WaitForToolsRunningWithContext(ctx *context.Context, name string) (bool, error) {
if conf.TestMode {
return true, nil
}
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return false, err
}
return vm.WaitForToolsRunning(ctx)
}
func (conf *Configuration) GetHostSystemWithContext(ctx *context.Context, name string) (string, error) {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return "*", err
}
return vm.HostSystem(ctx)
}
// GetHostSystem return the host where the virtual machine leave
func (conf *Configuration) GetHostSystem(name string) (string, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.GetHostSystemWithContext(ctx, name)
}
// SetAutoStart set autostart for the VM
func (conf *Configuration) SetAutoStart(esxi, name string, startOrder int) error {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.SetAutoStartWithContext(ctx, esxi, name, startOrder)
}
// WaitForToolsRunning wait vmware tools is running a VM by name
func (conf *Configuration) WaitForToolsRunning(name string) (bool, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.WaitForToolsRunningWithContext(ctx, name)
}
// PowerOnWithContext power on a VM by name
func (conf *Configuration) PowerOnWithContext(ctx *context.Context, name string) error {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return err
}
return vm.PowerOn(ctx)
}
// PowerOn power on a VM by name
func (conf *Configuration) PowerOn(name string) error {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.PowerOnWithContext(ctx, name)
}
// PowerOffWithContext power off a VM by name
func (conf *Configuration) PowerOffWithContext(ctx *context.Context, name string) error {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return err
}
return vm.PowerOff(ctx)
}
// ShutdownGuestWithContext power off a VM by name
func (conf *Configuration) ShutdownGuestWithContext(ctx *context.Context, name string) error {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return err
}
return vm.ShutdownGuest(ctx)
}
// PowerOff power off a VM by name
func (conf *Configuration) PowerOff(name string) error {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.PowerOffWithContext(ctx, name)
}
// ShutdownGuest power off a VM by name
func (conf *Configuration) ShutdownGuest(name string) error {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.ShutdownGuestWithContext(ctx, name)
}
// StatusWithContext return the current status of VM by name
func (conf *Configuration) StatusWithContext(ctx *context.Context, name string) (*Status, error) {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return nil, err
}
return vm.Status(ctx)
}
// Status return the current status of VM by name
func (conf *Configuration) Status(name string) (*Status, error) {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.StatusWithContext(ctx, name)
}
func (conf *Configuration) RetrieveNetworkInfosWithContext(ctx *context.Context, name string, nodeIndex int) error {
vm, err := conf.VirtualMachineWithContext(ctx, name)
if err != nil {
return err
}
return vm.collectNetworkInfos(ctx, conf.Network, nodeIndex)
}
func (conf *Configuration) RetrieveNetworkInfos(name string, nodeIndex int) error {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.RetrieveNetworkInfosWithContext(ctx, name, nodeIndex)
}
// ExistsWithContext return the current status of VM by name
func (conf *Configuration) ExistsWithContext(ctx *context.Context, name string) bool {
if _, err := conf.VirtualMachineWithContext(ctx, name); err == nil {
return true
}
return false
}
func (conf *Configuration) Exists(name string) bool {
ctx := context.NewContext(conf.Timeout)
defer ctx.Cancel()
return conf.ExistsWithContext(ctx, name)
}