forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcloudair.go
624 lines (522 loc) · 13.6 KB
/
vcloudair.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* Copyright 2014 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package vmwarevcloudair
import (
"fmt"
"io/ioutil"
"net"
"strconv"
"strings"
"github.com/vmware/govcloudair"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
)
type Driver struct {
*drivers.BaseDriver
UserName string
UserPassword string
ComputeID string
VDCID string
OrgVDCNet string
EdgeGateway string
PublicIP string
Catalog string
CatalogItem string
DockerPort int
CPUCount int
MemorySize int
VAppID string
}
const (
defaultCatalog = "Public Catalog"
defaultCatalogItem = "Ubuntu Server 12.04 LTS (amd64 20150127)"
defaultCpus = 1
defaultMemory = 2048
defaultSSHPort = 22
defaultDockerPort = 2376
)
// GetCreateFlags registers the flags this driver adds to
// "docker hosts create"
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_USERNAME",
Name: "vmwarevcloudair-username",
Usage: "vCloud Air username",
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_PASSWORD",
Name: "vmwarevcloudair-password",
Usage: "vCloud Air password",
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_COMPUTEID",
Name: "vmwarevcloudair-computeid",
Usage: "vCloud Air Compute ID (if using Dedicated Cloud)",
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_VDCID",
Name: "vmwarevcloudair-vdcid",
Usage: "vCloud Air VDC ID",
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_ORGVDCNETWORK",
Name: "vmwarevcloudair-orgvdcnetwork",
Usage: "vCloud Air Org VDC Network (Default is <vdcid>-default-routed)",
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_EDGEGATEWAY",
Name: "vmwarevcloudair-edgegateway",
Usage: "vCloud Air Org Edge Gateway (Default is <vdcid>)",
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_PUBLICIP",
Name: "vmwarevcloudair-publicip",
Usage: "vCloud Air Org Public IP to use",
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_CATALOG",
Name: "vmwarevcloudair-catalog",
Usage: "vCloud Air Catalog (default is Public Catalog)",
Value: defaultCatalog,
},
mcnflag.StringFlag{
EnvVar: "VCLOUDAIR_CATALOGITEM",
Name: "vmwarevcloudair-catalogitem",
Usage: "vCloud Air Catalog Item (default is Ubuntu Precise)",
Value: defaultCatalogItem,
},
mcnflag.IntFlag{
EnvVar: "VCLOUDAIR_CPU_COUNT",
Name: "vmwarevcloudair-cpu-count",
Usage: "vCloud Air VM Cpu Count (default 1)",
Value: defaultCpus,
},
mcnflag.IntFlag{
EnvVar: "VCLOUDAIR_MEMORY_SIZE",
Name: "vmwarevcloudair-memory-size",
Usage: "vCloud Air VM Memory Size in MB (default 2048)",
Value: defaultMemory,
},
mcnflag.IntFlag{
EnvVar: "VCLOUDAIR_SSH_PORT",
Name: "vmwarevcloudair-ssh-port",
Usage: "vCloud Air SSH port",
Value: defaultSSHPort,
},
mcnflag.IntFlag{
EnvVar: "VCLOUDAIR_DOCKER_PORT",
Name: "vmwarevcloudair-docker-port",
Usage: "vCloud Air Docker port",
Value: defaultDockerPort,
},
}
}
func NewDriver(hostName, storePath string) drivers.Driver {
return &Driver{
Catalog: defaultCatalog,
CatalogItem: defaultCatalogItem,
CPUCount: defaultCpus,
MemorySize: defaultMemory,
DockerPort: defaultDockerPort,
BaseDriver: &drivers.BaseDriver{
SSHPort: defaultSSHPort,
MachineName: hostName,
StorePath: storePath,
},
}
}
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "vmwarevcloudair"
}
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.UserName = flags.String("vmwarevcloudair-username")
d.UserPassword = flags.String("vmwarevcloudair-password")
d.VDCID = flags.String("vmwarevcloudair-vdcid")
d.PublicIP = flags.String("vmwarevcloudair-publicip")
d.SetSwarmConfigFromFlags(flags)
// Check for required Params
if d.UserName == "" || d.UserPassword == "" || d.VDCID == "" || d.PublicIP == "" {
return fmt.Errorf("Please specify vcloudair mandatory params using options: -vmwarevcloudair-username -vmwarevcloudair-password -vmwarevcloudair-vdcid and -vmwarevcloudair-publicip")
}
// If ComputeID is not set we're using a VPC, hence setting ComputeID = VDCID
if flags.String("vmwarevcloudair-computeid") == "" {
d.ComputeID = flags.String("vmwarevcloudair-vdcid")
} else {
d.ComputeID = flags.String("vmwarevcloudair-computeid")
}
// If the Org VDC Network is empty, set it to the default routed network.
if flags.String("vmwarevcloudair-orgvdcnetwork") == "" {
d.OrgVDCNet = flags.String("vmwarevcloudair-vdcid") + "-default-routed"
} else {
d.OrgVDCNet = flags.String("vmwarevcloudair-orgvdcnetwork")
}
// If the Edge Gateway is empty, just set it to the default edge gateway.
if flags.String("vmwarevcloudair-edgegateway") == "" {
d.EdgeGateway = flags.String("vmwarevcloudair-vdcid")
} else {
d.EdgeGateway = flags.String("vmwarevcloudair-edgegateway")
}
d.Catalog = flags.String("vmwarevcloudair-catalog")
d.CatalogItem = flags.String("vmwarevcloudair-catalogitem")
d.DockerPort = flags.Int("vmwarevcloudair-docker-port")
d.SSHUser = "root"
d.SSHPort = flags.Int("vmwarevcloudair-ssh-port")
d.CPUCount = flags.Int("vmwarevcloudair-cpu-count")
d.MemorySize = flags.Int("vmwarevcloudair-memory-size")
return nil
}
func (d *Driver) GetURL() (string, error) {
if err := drivers.MustBeRunning(d); err != nil {
return "", err
}
return fmt.Sprintf("tcp://%s", net.JoinHostPort(d.PublicIP, strconv.Itoa(d.DockerPort))), nil
}
func (d *Driver) GetIP() (string, error) {
return d.PublicIP, nil
}
func (d *Driver) GetState() (state.State, error) {
p, err := govcloudair.NewClient()
if err != nil {
return state.Error, err
}
log.Debug("Connecting to vCloud Air to fetch vApp Status...")
// Authenticate to vCloud Air
v, err := p.Authenticate(d.UserName, d.UserPassword, d.ComputeID, d.VDCID)
if err != nil {
return state.Error, err
}
vapp, err := v.FindVAppByID(d.VAppID)
if err != nil {
return state.Error, err
}
status, err := vapp.GetStatus()
if err != nil {
return state.Error, err
}
if err = p.Disconnect(); err != nil {
return state.Error, err
}
switch status {
case "POWERED_ON":
return state.Running, nil
case "POWERED_OFF":
return state.Stopped, nil
}
return state.None, nil
}
func (d *Driver) Create() error {
key, err := d.createSSHKey()
if err != nil {
return err
}
p, err := govcloudair.NewClient()
if err != nil {
return err
}
log.Infof("Connecting to vCloud Air...")
// Authenticate to vCloud Air
v, err := p.Authenticate(d.UserName, d.UserPassword, d.ComputeID, d.VDCID)
if err != nil {
return err
}
// Find VDC Network
net, err := v.FindVDCNetwork(d.OrgVDCNet)
if err != nil {
return err
}
// Find our Edge Gateway
edge, err := v.FindEdgeGateway(d.EdgeGateway)
if err != nil {
return err
}
// Get the Org our VDC belongs to
org, err := v.GetVDCOrg()
if err != nil {
return err
}
// Find our Catalog
cat, err := org.FindCatalog(d.Catalog)
if err != nil {
return err
}
// Find our Catalog Item
cati, err := cat.FindCatalogItem(d.CatalogItem)
if err != nil {
return err
}
// Fetch the vApp Template in the Catalog Item
vapptemplate, err := cati.GetVAppTemplate()
if err != nil {
return err
}
// Create a new empty vApp
vapp := govcloudair.NewVApp(p)
log.Infof("Creating a new vApp: %s...", d.MachineName)
// Compose the vApp with ComposeVApp
task, err := vapp.ComposeVApp(net, vapptemplate, d.MachineName, "Container Host created with Docker Host")
if err != nil {
return err
}
// Wait for the creation to be completed
if err = task.WaitTaskCompletion(); err != nil {
return err
}
task, err = vapp.ChangeCPUcount(d.CPUCount)
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
task, err = vapp.ChangeMemorySize(d.MemorySize)
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
sshCustomScript := "echo \"" + strings.TrimSpace(key) + "\" > /root/.ssh/authorized_keys"
task, err = vapp.RunCustomizationScript(d.MachineName, sshCustomScript)
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
task, err = vapp.PowerOn()
if err != nil {
return err
}
log.Infof("Waiting for the VM to power on and run the customization script...")
if err = task.WaitTaskCompletion(); err != nil {
return err
}
log.Infof("Creating NAT and Firewall Rules on %s...", d.EdgeGateway)
task, err = edge.Create1to1Mapping(vapp.VApp.Children.VM[0].NetworkConnectionSection.NetworkConnection.IPAddress, d.PublicIP, d.MachineName)
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
log.Debugf("Disconnecting from vCloud Air...")
if err = p.Disconnect(); err != nil {
return err
}
// Set VAppID with ID of the created VApp
d.VAppID = vapp.VApp.ID
d.IPAddress, err = d.GetIP()
return err
}
func (d *Driver) Remove() error {
p, err := govcloudair.NewClient()
if err != nil {
return err
}
log.Infof("Connecting to vCloud Air...")
// Authenticate to vCloud Air
v, err := p.Authenticate(d.UserName, d.UserPassword, d.ComputeID, d.VDCID)
if err != nil {
return err
}
// Find our Edge Gateway
edge, err := v.FindEdgeGateway(d.EdgeGateway)
if err != nil {
return err
}
vapp, err := v.FindVAppByID(d.VAppID)
if err != nil {
log.Infof("Can't find the vApp, assuming it was deleted already...")
return nil
}
status, err := vapp.GetStatus()
if err != nil {
return err
}
log.Infof("Removing NAT and Firewall Rules on %s...", d.EdgeGateway)
task, err := edge.Remove1to1Mapping(vapp.VApp.Children.VM[0].NetworkConnectionSection.NetworkConnection.IPAddress, d.PublicIP)
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
if status == "POWERED_ON" {
// If it's powered on, power it off before deleting
log.Infof("Powering Off %s...", d.MachineName)
task, err = vapp.PowerOff()
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
}
log.Debugf("Undeploying %s...", d.MachineName)
task, err = vapp.Undeploy()
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
log.Infof("Deleting %s...", d.MachineName)
task, err = vapp.Delete()
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
if err = p.Disconnect(); err != nil {
return err
}
return nil
}
func (d *Driver) Start() error {
p, err := govcloudair.NewClient()
if err != nil {
return err
}
log.Infof("Connecting to vCloud Air...")
// Authenticate to vCloud Air
v, err := p.Authenticate(d.UserName, d.UserPassword, d.ComputeID, d.VDCID)
if err != nil {
return err
}
vapp, err := v.FindVAppByID(d.VAppID)
if err != nil {
return err
}
status, err := vapp.GetStatus()
if err != nil {
return err
}
if status == "POWERED_OFF" {
log.Infof("Starting %s...", d.MachineName)
task, err := vapp.PowerOn()
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
}
if err = p.Disconnect(); err != nil {
return err
}
d.IPAddress, err = d.GetIP()
return err
}
func (d *Driver) Stop() error {
p, err := govcloudair.NewClient()
if err != nil {
return err
}
log.Infof("Connecting to vCloud Air...")
// Authenticate to vCloud Air
v, err := p.Authenticate(d.UserName, d.UserPassword, d.ComputeID, d.VDCID)
if err != nil {
return err
}
vapp, err := v.FindVAppByID(d.VAppID)
if err != nil {
return err
}
task, err := vapp.Shutdown()
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
if err = p.Disconnect(); err != nil {
return err
}
d.IPAddress = ""
return nil
}
func (d *Driver) Restart() error {
p, err := govcloudair.NewClient()
if err != nil {
return err
}
log.Infof("Connecting to vCloud Air...")
// Authenticate to vCloud Air
v, err := p.Authenticate(d.UserName, d.UserPassword, d.ComputeID, d.VDCID)
if err != nil {
return err
}
vapp, err := v.FindVAppByID(d.VAppID)
if err != nil {
return err
}
task, err := vapp.Reset()
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
if err = p.Disconnect(); err != nil {
return err
}
d.IPAddress, err = d.GetIP()
return err
}
func (d *Driver) Kill() error {
p, err := govcloudair.NewClient()
if err != nil {
return err
}
log.Infof("Connecting to vCloud Air...")
// Authenticate to vCloud Air
v, err := p.Authenticate(d.UserName, d.UserPassword, d.ComputeID, d.VDCID)
if err != nil {
return err
}
vapp, err := v.FindVAppByID(d.VAppID)
if err != nil {
return err
}
task, err := vapp.PowerOff()
if err != nil {
return err
}
if err = task.WaitTaskCompletion(); err != nil {
return err
}
if err = p.Disconnect(); err != nil {
return err
}
d.IPAddress = ""
return nil
}
// Helpers
func generateVMName() string {
randomID := mcnutils.TruncateID(mcnutils.GenerateRandomID())
return fmt.Sprintf("docker-host-%s", randomID)
}
func (d *Driver) createSSHKey() (string, error) {
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return "", err
}
publicKey, err := ioutil.ReadFile(d.publicSSHKeyPath())
if err != nil {
return "", err
}
return string(publicKey), nil
}
func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}