-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcextreme.go
986 lines (833 loc) · 23.1 KB
/
pcextreme.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
package pcextreme
import (
"encoding/base64"
"fmt"
"io/ioutil"
"strings"
"crypto/md5"
"net/http"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
"github.com/xanzy/go-cloudstack/cloudstack"
"gopkg.in/yaml.v2"
)
const (
driverName = "pcextreme"
defaultAPIEndpoint = "api.auroracompute.eu"
defaultAPIRegion = "ams"
defaultZone = "zone01.ams02"
defaultServiceOffering = "Agile 2G"
defaultTemplate = "CoreOS Stable"
defaultSSHUser = "core"
defaultAsyncJobTimeout = 300
diskDataType = "DATADISK"
)
var (
dockerPort = 2376
swarmPort = 3376
keyExists bool
)
type configError struct {
option string
}
func (e *configError) Error() string {
return fmt.Sprintf("PCextreme driver requires the --pcextreme-%s option", e.option)
}
// Driver implements the libmachine/drivers.Driver interface
type Driver struct {
*drivers.BaseDriver
ID string
APIEndpoint string
APIURL string
APIRegion string
APIKey string
SecretKey string
HTTPGETOnly bool
JobTimeOut int64
SSHKeyPair string
CIDRList []string
Template string
TemplateID string
ServiceOffering string
ServiceOfferingID string
DeleteVolumes bool
DiskOffering string
DiskOfferingID string
DiskSize int
Zone string
ZoneID string
UserDataFile string
UserData string
Tags []string
DisplayName string
}
type userDataYAML struct {
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys,omitempty"`
SSHKeys struct {
RSAPrivate string `yaml:"rsa_private,omitempty"`
RSAPublic string `yaml:"rsa_public,omitempty"`
} `yaml:"ssh_keys,omitempty"`
}
// GetCreateFlags registers the flags this driver adds to
// "docker hosts create"
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: "pcextreme-api-endpoint",
Usage: "pcextreme API endpoint",
Value: defaultAPIEndpoint,
EnvVar: "pcextreme_API_ENDPOINT",
},
mcnflag.StringFlag{
Name: "pcextreme-api-region",
Usage: "pcextreme API region",
Value: defaultAPIRegion,
EnvVar: "pcextreme_API_REGION",
},
mcnflag.StringFlag{
Name: "pcextreme-api-key",
Usage: "pcextreme API key",
EnvVar: "pcextreme_API_KEY",
},
mcnflag.StringFlag{
Name: "pcextreme-secret-key",
Usage: "pcextreme API secret key",
EnvVar: "pcextreme_SECRET_KEY",
},
mcnflag.BoolFlag{
Name: "pcextreme-http-get-only",
Usage: "Only use HTTP GET to execute pcextreme API",
EnvVar: "pcextreme_HTTP_GET_ONLY",
},
mcnflag.IntFlag{
Name: "pcextreme-timeout",
Usage: "time(seconds) allowed to complete async job",
Value: defaultAsyncJobTimeout,
EnvVar: "pcextreme_TIMEOUT",
},
mcnflag.StringFlag{
Name: "pcextreme-ssh-user",
Usage: "pcextreme SSH user",
Value: defaultSSHUser,
EnvVar: "pcextreme_SSH_USER",
},
mcnflag.StringSliceFlag{
Name: "pcextreme-cidr",
Usage: "Source CIDR to give access to the machine. default 0.0.0.0/0",
},
mcnflag.StringFlag{
Name: "pcextreme-template",
Usage: "pcextreme template",
Value: defaultTemplate,
EnvVar: "pcextreme_TEMPLATE",
},
mcnflag.StringFlag{
Name: "pcextreme-template-id",
Usage: "pcextreme template id",
},
mcnflag.StringFlag{
Name: "pcextreme-service-offering",
Usage: "pcextreme service offering",
Value: defaultServiceOffering,
EnvVar: "pcextreme_SERVICE_OFFERING",
},
mcnflag.StringFlag{
Name: "pcextreme-service-offering-id",
Usage: "pcextreme service offering id",
},
mcnflag.StringFlag{
Name: "pcextreme-zone",
Usage: "pcextreme zone",
Value: defaultZone,
EnvVar: "pcextreme_ZONE",
},
mcnflag.StringFlag{
Name: "pcextreme-zone-id",
Usage: "pcextreme zone id",
},
mcnflag.StringFlag{
Name: "pcextreme-userdata-file",
Usage: "pcextreme Userdata file",
EnvVar: "pcextreme_USERDATA_FILE",
},
mcnflag.StringSliceFlag{
Name: "pcextreme-resource-tag",
Usage: "key:value resource tags to be created",
},
mcnflag.StringFlag{
Name: "pcextreme-disk-offering",
Usage: "pcextreme disk offering",
EnvVar: "pcextreme_DISK_OFFERING",
},
mcnflag.StringFlag{
Name: "pcextreme-disk-offering-id",
Usage: "pcextreme disk offering id",
},
mcnflag.IntFlag{
Name: "pcextreme-disk-size",
Usage: "Disk offering custom size",
EnvVar: "pcextreme_CUSTOM_DISK_SIZE",
},
mcnflag.BoolFlag{
Name: "pcextreme-delete-volumes",
Usage: "Whether or not to delete data volumes associated with the machine upon removal",
},
mcnflag.StringFlag{
Name: "pcextreme-displayname",
Usage: "pcextreme virtual machine displayname",
},
}
}
// NewDriver creates the driver with it's specified config
func NewDriver(hostName, storePath string) drivers.Driver {
driver := &Driver{
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
StorePath: storePath,
},
}
return driver
}
// DriverName returns the name of the driver as it is registered
func (d *Driver) DriverName() string {
return driverName
}
// GetSSHHostname returns a hostname used for connecting via SSH
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
// GetSSHUsername returns an username used for connecting via SSH
func (d *Driver) GetSSHUsername() string {
return d.SSHUser
}
func (d *Driver) setAPIURL(apiEndpoint string, apiRegion string) error {
d.APIEndpoint = apiEndpoint
d.APIRegion = apiRegion
if d.APIEndpoint != "" && d.APIRegion != "" {
d.APIURL = fmt.Sprintf("https://%s/%s", d.APIEndpoint, d.APIRegion)
}
log.Debugf("API: %q", d.APIURL)
return nil
}
// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.APIEndpoint = flags.String("pcextreme-api-endpoint")
d.APIRegion = flags.String("pcextreme-api-region")
d.APIKey = flags.String("pcextreme-api-key")
d.SecretKey = flags.String("pcextreme-secret-key")
d.HTTPGETOnly = flags.Bool("pcextreme-http-get-only")
d.JobTimeOut = int64(flags.Int("pcextreme-timeout"))
d.SSHUser = flags.String("pcextreme-ssh-user")
d.CIDRList = flags.StringSlice("pcextreme-cidr")
d.Tags = flags.StringSlice("pcextreme-resource-tag")
d.DeleteVolumes = flags.Bool("pcextreme-delete-volumes")
d.DiskSize = flags.Int("pcextreme-disk-size")
d.DisplayName = flags.String("pcextreme-displayname")
d.SwarmMaster = flags.Bool("swarm-master")
d.SwarmDiscovery = flags.String("swarm-discovery")
if err := d.setAPIURL(flags.String("pcextreme-api-endpoint"), flags.String("pcextreme-api-region")); err != nil {
return err
}
if err := d.setZone(flags.String("pcextreme-zone"), flags.String("pcextreme-zone-id")); err != nil {
return err
}
if err := d.setTemplate(flags.String("pcextreme-template"), flags.String("pcextreme-template-id")); err != nil {
return err
}
if err := d.setServiceOffering(flags.String("pcextreme-service-offering"), flags.String("pcextreme-service-offering-id")); err != nil {
return err
}
if err := d.setUserData(flags.String("pcextreme-userdata-file")); err != nil {
return err
}
if err := d.setDiskOffering(flags.String("pcextreme-disk-offering"), flags.String("pcextreme-disk-offering-id")); err != nil {
return err
}
if d.DisplayName == "" {
d.DisplayName = d.MachineName
}
d.SSHKeyPair = d.MachineName
if d.APIURL == "" {
return &configError{option: "api-url"}
}
if d.APIKey == "" {
return &configError{option: "api-key"}
}
if d.SecretKey == "" {
return &configError{option: "secret-key"}
}
if d.Template == "" {
return &configError{option: "template"}
}
if d.ServiceOffering == "" {
return &configError{option: "service-offering"}
}
if d.Zone == "" {
return &configError{option: "zone"}
}
if len(d.CIDRList) == 0 {
d.CIDRList = []string{"0.0.0.0/0"}
}
return nil
}
// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
func (d *Driver) GetURL() (string, error) {
ip, err := d.GetIP()
if err != nil {
return "", err
}
return fmt.Sprintf("tcp://%s:%d", ip, dockerPort), nil
}
// GetIP returns the IP that this host is available at
func (d *Driver) GetIP() (string, error) {
return d.IPAddress, nil
}
// GetState returns the state that the host is in (running, stopped, etc)
func (d *Driver) GetState() (state.State, error) {
cs := d.getClient()
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(d.ID, d.setParams)
if err != nil {
return state.Error, err
}
if count == 0 {
return state.None, fmt.Errorf("Machine does not exist, use create command to create it")
}
switch vm.State {
case "Starting":
return state.Starting, nil
case "Running":
return state.Running, nil
case "Stopping":
return state.Running, nil
case "Stopped":
return state.Stopped, nil
case "Destroyed":
return state.Stopped, nil
case "Expunging":
return state.Stopped, nil
case "Migrating":
return state.Paused, nil
case "Error":
return state.Error, nil
case "Unknown":
return state.Error, nil
case "Shutdowned":
return state.Stopped, nil
}
return state.None, nil
}
// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
func (d *Driver) PreCreateCheck() error {
if err := d.checkKeyPairByName(); err != nil {
return err
}
if err := d.checkInstance(); err != nil {
return err
}
return nil
}
// Create a host using the driver's config
func (d *Driver) Create() error {
cs := d.getClient()
if err := d.createKeyPair(); err != nil {
return err
}
p := cs.VirtualMachine.NewDeployVirtualMachineParams(
d.ServiceOfferingID, d.TemplateID, d.ZoneID)
p.SetName(d.MachineName)
p.SetDisplayname(d.DisplayName)
log.Infof("Setting Keypair for VM: %s", d.SSHKeyPair)
p.SetKeypair(d.SSHKeyPair)
if d.UserData != "" {
p.SetUserdata(d.UserData)
}
if d.DiskOfferingID != "" {
p.SetDiskofferingid(d.DiskOfferingID)
if d.DiskSize != 0 {
p.SetSize(int64(d.DiskSize))
}
}
if err := d.createSecurityGroup(); err != nil {
return err
}
p.SetSecuritygroupnames([]string{d.MachineName})
log.Info("Creating CloudStack instance...")
vm, err := cs.VirtualMachine.DeployVirtualMachine(p)
if err != nil {
return err
}
d.ID = vm.Id
if len(d.Tags) > 0 {
if err := d.createTags(); err != nil {
return err
}
}
d.IPAddress = vm.Nic[0].Ipaddress
return nil
}
// Remove a host
func (d *Driver) Remove() error {
cs := d.getClient()
p := cs.VirtualMachine.NewDestroyVirtualMachineParams(d.ID)
if err := d.deleteKeyPair(); err != nil {
return err
}
log.Info("Removing PCextreme instance...")
if _, err := cs.VirtualMachine.DestroyVirtualMachine(p); err != nil {
return err
}
if err := d.deleteSecurityGroup(); err != nil {
return err
}
if d.DeleteVolumes {
if err := d.deleteVolumes(); err != nil {
return err
}
}
return nil
}
// Start a host
func (d *Driver) Start() error {
vmstate, err := d.GetState()
if err != nil {
return err
}
if vmstate == state.Running {
log.Info("Machine is already running")
return nil
}
if vmstate == state.Starting {
log.Info("Machine is already starting")
return nil
}
cs := d.getClient()
p := cs.VirtualMachine.NewStartVirtualMachineParams(d.ID)
if _, err = cs.VirtualMachine.StartVirtualMachine(p); err != nil {
return err
}
return nil
}
// Stop a host gracefully
func (d *Driver) Stop() error {
vmstate, err := d.GetState()
if err != nil {
return err
}
if vmstate == state.Stopped {
log.Info("Machine is already stopped")
return nil
}
cs := d.getClient()
p := cs.VirtualMachine.NewStopVirtualMachineParams(d.ID)
if _, err = cs.VirtualMachine.StopVirtualMachine(p); err != nil {
return err
}
return nil
}
// Restart a host.
func (d *Driver) Restart() error {
vmstate, err := d.GetState()
if err != nil {
return err
}
if vmstate == state.Stopped {
return fmt.Errorf("Machine is stopped, use start command to start it")
}
cs := d.getClient()
p := cs.VirtualMachine.NewRebootVirtualMachineParams(d.ID)
if _, err = cs.VirtualMachine.RebootVirtualMachine(p); err != nil {
return err
}
return nil
}
// Kill stops a host forcefully
func (d *Driver) Kill() error {
return d.Stop()
}
func (d *Driver) getClient() *cloudstack.CloudStackClient {
cs := cloudstack.NewAsyncClient(d.APIURL, d.APIKey, d.SecretKey, false)
cs.HTTPGETOnly = d.HTTPGETOnly
cs.AsyncTimeout(d.JobTimeOut)
return cs
}
func (d *Driver) setZone(zone string, zoneID string) error {
d.Zone = zone
d.ZoneID = zoneID
if d.Zone == "" && d.ZoneID == "" {
return nil
}
cs := d.getClient()
var z *cloudstack.Zone
var err error
if d.ZoneID != "" {
z, _, err = cs.Zone.GetZoneByID(d.ZoneID, d.setParams)
} else {
z, _, err = cs.Zone.GetZoneByName(d.Zone, d.setParams)
}
if err != nil {
return fmt.Errorf("Unable to get zone: %v", err)
}
d.Zone = z.Name
d.ZoneID = z.Id
log.Debugf("zone: %q", d.Zone)
log.Debugf("zone id: %q", d.ZoneID)
return nil
}
func (d *Driver) setTemplate(templateName string, templateID string) error {
d.Template = templateName
d.TemplateID = templateID
if d.Template == "" && d.TemplateID == "" {
return nil
}
if d.ZoneID == "" {
return fmt.Errorf("Unable to get template: zone is not set")
}
cs := d.getClient()
var template *cloudstack.Template
var err error
if d.TemplateID != "" {
template, _, err = cs.Template.GetTemplateByID(d.TemplateID, "executable", d.setParams)
} else {
template, _, err = cs.Template.GetTemplateByName(d.Template, "executable", d.ZoneID, d.setParams)
}
if err != nil {
return fmt.Errorf("Unable to get template: %v", err)
}
d.TemplateID = template.Id
d.Template = template.Name
log.Debugf("template id: %q", d.TemplateID)
log.Debugf("template name: %q", d.Template)
return nil
}
func (d *Driver) setServiceOffering(serviceoffering string, serviceofferingID string) error {
d.ServiceOffering = serviceoffering
d.ServiceOfferingID = serviceofferingID
if d.ServiceOffering == "" && d.ServiceOfferingID == "" {
return nil
}
cs := d.getClient()
var service *cloudstack.ServiceOffering
var err error
if d.ServiceOfferingID != "" {
service, _, err = cs.ServiceOffering.GetServiceOfferingByID(d.ServiceOfferingID, d.setParams)
} else {
service, _, err = cs.ServiceOffering.GetServiceOfferingByName(d.ServiceOffering, d.setParams)
}
if err != nil {
return fmt.Errorf("Unable to get service offering: %v", err)
}
d.ServiceOfferingID = service.Id
d.ServiceOffering = service.Name
log.Debugf("service offering id: %q", d.ServiceOfferingID)
log.Debugf("service offering name: %q", d.ServiceOffering)
return nil
}
func (d *Driver) setDiskOffering(diskOffering string, diskOfferingID string) error {
d.DiskOffering = diskOffering
d.DiskOfferingID = diskOfferingID
if d.DiskOffering == "" && d.DiskOfferingID == "" {
return nil
}
cs := d.getClient()
var disk *cloudstack.DiskOffering
var err error
if d.DiskOfferingID != "" {
disk, _, err = cs.DiskOffering.GetDiskOfferingByID(d.DiskOfferingID, d.setParams)
} else {
disk, _, err = cs.DiskOffering.GetDiskOfferingByName(d.DiskOffering, d.setParams)
}
if err != nil {
return fmt.Errorf("Unable to get disk offering: %v", err)
}
d.DiskOfferingID = disk.Id
d.DiskOffering = disk.Name
log.Debugf("disk offering id: %q", d.DiskOfferingID)
log.Debugf("disk offering name: %q", d.DiskOffering)
return nil
}
func (d *Driver) setUserData(userDataFile string) error {
var data []byte
var err error
if userDataFile == "" {
return nil
}
if strings.HasPrefix(userDataFile, "http") {
data, err = d.readUserDataFromURL(userDataFile)
if err != nil {
return fmt.Errorf("Failed to read userdata from url %s: %s", d.UserDataFile, err)
}
} else {
data, err = ioutil.ReadFile(userDataFile)
if err != nil {
return fmt.Errorf("Failed to read user data file from path %s: %s", d.UserDataFile, err)
}
}
d.UserData = base64.StdEncoding.EncodeToString(data)
return nil
}
func (d *Driver) readUserDataFromURL(userDataURL string) ([]byte, error) {
c := &http.Client{}
resp, err := c.Get(userDataURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func (d *Driver) checkKeyPairByName() error {
cs := d.getClient()
log.Infof("Checking if SSH key pair (%v) already exists...", d.SSHKeyPair)
p := cs.SSH.NewListSSHKeyPairsParams()
p.SetName(d.SSHKeyPair)
res, err := cs.SSH.ListSSHKeyPairs(p)
if err != nil {
return err
}
if res.Count > 0 {
if res.SSHKeyPairs[0].Name == d.SSHKeyPair {
keyExists = true
return nil
}
}
keyExists = false
return nil
}
func (d *Driver) getPubKeyFingerprint(pub string) (string, error) {
parts := strings.Fields(string(pub))
if len(parts) < 2 {
return "", errors.New("Bad pub key")
}
k, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return "", err
}
fp := md5.Sum([]byte(k))
var fSum []string
fmt.Print("MD5:")
for i, b := range fp {
fSum = append(fSum, fmt.Sprintf("%02x", b))
if i < len(fp)-1 {
fSum = append(fSum, ":")
}
}
return strings.Join(fSum, ""), nil
}
func (d *Driver) checkKeyPairByFingerprint(pubKey string) (bool, error) {
cs := d.getClient()
fp, err := d.getPubKeyFingerprint(pubKey)
if err != nil {
return false, err
}
log.Infof("Checking for matching public key fingerprints...", d.SSHKeyPair)
p := cs.SSH.NewListSSHKeyPairsParams()
p.SetFingerprint(fp)
res, err := cs.SSH.ListSSHKeyPairs(p)
if err != nil {
return false, err
}
if res.Count > 0 {
for _, c := range res.SSHKeyPairs {
if c.Fingerprint == fp {
log.Infof("Found matching fingerprint, using key pair: %s", c.Name)
d.SSHKeyPair = c.Name
return true, nil
}
}
}
return false, nil
}
func (d *Driver) checkInstance() error {
cs := d.getClient()
log.Infof("Checking if instance (%v) already exists...", d.MachineName)
p := cs.VirtualMachine.NewListVirtualMachinesParams()
p.SetName(d.MachineName)
p.SetZoneid(d.ZoneID)
res, err := cs.VirtualMachine.ListVirtualMachines(p)
if err != nil {
return err
}
if res.Count > 0 {
return fmt.Errorf("Instance (%v) already exists", d.SSHKeyPair)
}
return nil
}
func (d *Driver) createKeyPair() error {
cs := d.getClient()
var publicKey []byte
var err error
if keyExists {
log.Infof("Using %s keypair", d.SSHKeyPair)
return nil
}
if d.UserDataFile != "" {
userDataContents, err := ioutil.ReadFile(d.UserDataFile)
if err != nil {
return err
}
sshKeyYaml := &userDataYAML{}
if err != nil {
return err
}
if err := yaml.Unmarshal(userDataContents, sshKeyYaml); err != nil {
return err
}
log.Infof("Setting publicKey to %s", sshKeyYaml.SSHAuthorizedKeys[0])
publicKey = []byte(sshKeyYaml.SSHAuthorizedKeys[0])
if err := d.writeSSHKeys(sshKeyYaml.SSHKeys.RSAPrivate, sshKeyYaml.SSHAuthorizedKeys[0]); err != nil {
return err
}
} else {
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
publicKey, err = ioutil.ReadFile(d.GetSSHKeyPath() + ".pub")
if err != nil {
return err
}
}
exists, err := d.checkKeyPairByFingerprint(string(publicKey))
if err != nil {
return err
}
if !exists {
log.Infof("Registering SSH key pair %s", d.SSHKeyPair)
p := cs.SSH.NewRegisterSSHKeyPairParams(d.SSHKeyPair, string(publicKey))
if _, err := cs.SSH.RegisterSSHKeyPair(p); err != nil {
return err
}
}
return nil
}
func (d *Driver) deleteKeyPair() error {
cs := d.getClient()
log.Infof("Deleting SSH key pair...")
fp, err := ioutil.ReadFile(d.SSHKeyPath + ".pub")
if err != nil {
return err
}
inUse, err := d.checkKeyPairByFingerprint(string(fp))
if err != nil {
return err
}
if inUse {
log.Infof("Key %s is still in use for another VM, skipping key deletion", string(fp))
return nil
}
p := cs.SSH.NewDeleteSSHKeyPairParams(d.SSHKeyPair)
if _, err := cs.SSH.DeleteSSHKeyPair(p); err != nil {
// Throw away the error because it most likely means that a key doesn't exist
// It is ok because we can use the same key for multiple machines.
log.Warnf("Key may not exist: %s", err)
return nil
}
return nil
}
func (d *Driver) deleteVolumes() error {
cs := d.getClient()
log.Info("Deleting volumes...")
p := cs.Volume.NewListVolumesParams()
p.SetVirtualmachineid(d.ID)
volResponse, err := cs.Volume.ListVolumes(p)
if err != nil {
return err
}
for _, v := range volResponse.Volumes {
if v.Type != diskDataType {
continue
}
p := cs.Volume.NewDetachVolumeParams()
p.SetId(v.Id)
_, err := cs.Volume.DetachVolume(p)
if err != nil {
return err
}
_, err = cs.Volume.DeleteVolume(cs.Volume.NewDeleteVolumeParams(v.Id))
if err != nil {
return err
}
}
return nil
}
func (d *Driver) createSecurityGroup() error {
log.Debugf("Creating security group ...")
cs := d.getClient()
p1 := cs.SecurityGroup.NewCreateSecurityGroupParams(d.MachineName)
if _, err := cs.SecurityGroup.CreateSecurityGroup(p1); err != nil {
return err
}
p2 := cs.SecurityGroup.NewAuthorizeSecurityGroupIngressParams()
p2.SetSecuritygroupname(d.MachineName)
p2.SetProtocol("tcp")
p2.SetCidrlist(d.CIDRList)
p2.SetStartport(22)
p2.SetEndport(22)
if _, err := cs.SecurityGroup.AuthorizeSecurityGroupIngress(p2); err != nil {
return err
}
p2.SetStartport(dockerPort)
p2.SetEndport(dockerPort)
if _, err := cs.SecurityGroup.AuthorizeSecurityGroupIngress(p2); err != nil {
return err
}
if d.SwarmMaster {
p2.SetStartport(swarmPort)
p2.SetEndport(swarmPort)
if _, err := cs.SecurityGroup.AuthorizeSecurityGroupIngress(p2); err != nil {
return err
}
}
return nil
}
func (d *Driver) deleteSecurityGroup() error {
log.Debugf("Deleting security group ...")
cs := d.getClient()
p := cs.SecurityGroup.NewDeleteSecurityGroupParams()
p.SetName(d.MachineName)
if _, err := cs.SecurityGroup.DeleteSecurityGroup(p); err != nil {
return err
}
return nil
}
func (d *Driver) createTags() error {
log.Info("Creating resource tags ...")
cs := d.getClient()
tags := make(map[string]string)
for _, t := range d.Tags {
parts := strings.SplitN(t, ":", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid resource tags format, each tag must be on the format KEY:VALUE")
}
tags[parts[0]] = parts[1]
}
params := cs.Resourcetags.NewCreateTagsParams([]string{d.ID}, "UserVm", tags)
_, err := cs.Resourcetags.CreateTags(params)
return err
}
func (d *Driver) setParams(c *cloudstack.CloudStackClient, p interface{}) error {
if o, ok := p.(interface {
SetZoneid(string)
}); ok && d.ZoneID != "" {
o.SetZoneid(d.ZoneID)
}
return nil
}
func (d *Driver) writeSSHKeys(priv, pub string) error {
if priv == "" {
return errors.New("A private key must be passed in the userdata file under the ssh_keys")
}
log.Infof("Writing first private key found in userdata file to %s", d.StorePath)
if err := ioutil.WriteFile(d.GetSSHKeyPath(), []byte(priv), 0600); err != nil {
return err
}
log.Infof("Writing public key to id_rsa to %s.pub", d.GetSSHKeyPath())
if err := ioutil.WriteFile(fmt.Sprintf("%s.pub", d.GetSSHKeyPath()), []byte(pub), 0600); err != nil {
return err
}
return nil
}