-
Notifications
You must be signed in to change notification settings - Fork 18
/
ecs.go
839 lines (701 loc) · 24.7 KB
/
ecs.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
package otc
import (
"crypto/md5"
"crypto/rand"
_ "encoding/base64"
"fmt"
"io"
"io/ioutil"
_ "os"
"strings"
"time"
_ "github.com/huawei/DockerMachineDriver4OTC/otcgo/ecs"
_ "github.com/huawei/DockerMachineDriver4OTC/otcgo/ims"
_ "github.com/huawei/DockerMachineDriver4OTC/otcgo/vpc"
"github.com/huawei/DockerMachineDriver4OTC/com/client"
"github.com/huawei/DockerMachineDriver4OTC/com/modules"
"github.com/huawei/DockerMachineDriver4OTC/com/modules/ecsModules"
"github.com/huawei/DockerMachineDriver4OTC/com/modules/imsModules"
"github.com/huawei/DockerMachineDriver4OTC/com/modules/novaModules"
"github.com/huawei/DockerMachineDriver4OTC/com/modules/vpcModules"
"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"
)
const (
driverName = "otc"
defaultSSHUser = "root"
dockerPort = 2376
serviceName = "ecs"
secGrpListNumPerPage = 200
imgListNumPerPage = 200
subnetListNumPerPage = 200
errorMandatoryEnvOrOption string = "%s must be specified either using the environment variable %s or the CLI option %s"
errorBothOptions string = "Both %s and %s must be specified"
)
var user_data_temp string = `#cloud-config
write_files:
- path: /home/linux/.ssh/authorized_keys
permissions: '0777'
content: `
type Driver struct {
*drivers.BaseDriver
ServiceEndpoint string
InstanceId string
AccessKey string
SecretKey string
VpcId string
PublicKey []byte
TenantId string
ImageId string
Region string
AvailableZone string
FlavorId string
RootVolType string
RootVolSize int
ElasticIpType string
BandwidthSize int
BandwidthType string
AdminPass string
KeyName string
JobId string
//network
ElasticIpBool int
ElasticIPId string
ElasticIP string
SecurityGroupName string
SecurityGroupId string
//Nic ecs.NicDesc
SubnetId string
PortId string
//individual business service client
/*ecsclient *ecs.Client
imsclient *ims.Client
vpcclient *vpc.Client*/
PrivateIPAddress string
}
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: "otc-elasticip-type",
Usage: "Elastic Ip Type",
Value: "5_bgp",
EnvVar: "ELASTICIP_TYPE",
},
mcnflag.StringFlag{
Name: "otc-bandwidth-type",
Usage: "Bandwidth Type",
Value: "PER",
EnvVar: "BANDWIDTH_TYPE",
},
mcnflag.IntFlag{
Name: "otc-bandwidth-size",
Usage: "Bandwidth Size",
Value: 10,
EnvVar: "BANDWIDTH_SIZE",
},
mcnflag.StringFlag{
Name: "otc-service-endpoint",
Usage: "Service Endpoint",
Value: "",
EnvVar: "SERVICE_ENDPOINT",
},
mcnflag.StringFlag{
Name: "otc-access-key-id",
Usage: "Access Key ID",
Value: "",
EnvVar: "ACCESS_KEY_ID",
},
mcnflag.StringFlag{
Name: "otc-access-key-secret",
Usage: "Access key Secret",
Value: "",
EnvVar: "ACCESS_KEY_SECRET",
},
mcnflag.StringFlag{
Name: "otc-security-group",
Usage: "VPC Security Group",
Value: "",
EnvVar: "SECURITY_GROUP",
},
mcnflag.StringFlag{
Name: "otc-image-id",
Usage: "Machine Image",
Value: "",
EnvVar: "IMAGE_ID",
},
mcnflag.StringFlag{
Name: "otc-available-zone",
Usage: "Availabe Zone",
Value: "",
EnvVar: "AVAILABLE_ZONE",
},
mcnflag.StringFlag{
Name: "otc-region",
Usage: "Region",
Value: "",
EnvVar: "REGION",
},
mcnflag.StringFlag{
Name: "otc-flavor-id",
Usage: "Flavor ID",
Value: "",
EnvVar: "FLAVOR_ID",
},
mcnflag.StringFlag{
Name: "otc-vpc-id",
Usage: "Vpc ID",
Value: "",
EnvVar: "VPC_ID",
},
mcnflag.StringFlag{
Name: "otc-subnet-id",
Usage: "Subnet ID",
Value: "",
EnvVar: "SUBNET_ID",
},
mcnflag.StringFlag{
Name: "otc-root-volume-type",
Usage: "Root Volume Type",
Value: "SATA",
EnvVar: "ROOT_VOLUME_TYPE",
},
mcnflag.IntFlag{
Name: "otc-root-volume-size",
Usage: "Root Volume Size",
Value: 40,
EnvVar: "ROOT_VOLUME_SIZE",
},
mcnflag.StringFlag{
Name: "otc-tenant-id",
Usage: "Tenant ID",
Value: "",
EnvVar: "TENANT_ID",
},
mcnflag.StringFlag{
Name: "otc-admin-password",
Usage: "Instance's Root Login Password",
Value: "",
EnvVar: "ADMIN_PWD",
},
mcnflag.StringFlag{
Name: "otc-ssh-user",
Usage: "Instance's optional ssh user",
Value: "",
EnvVar: "SSH_USER",
},
mcnflag.IntFlag{
Name: "otc-elastic-ip",
Usage: "Set it to 1 to allocate ElasticIP",
Value: 0,
EnvVar: "ELASTIC_IP",
},
}
}
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.ServiceEndpoint = flags.String("otc-service-endpoint")
d.SecurityGroupName = flags.String("otc-security-group")
d.AccessKey = flags.String("otc-access-key-id")
d.SecretKey = flags.String("otc-access-key-secret")
d.ImageId = flags.String("otc-image-id")
d.AvailableZone = flags.String("otc-available-zone")
d.FlavorId = flags.String("otc-flavor-id")
d.SubnetId = flags.String("otc-subnet-id")
d.RootVolType = flags.String("otc-root-volume-type")
d.RootVolSize = flags.Int("otc-root-volume-size")
d.TenantId = flags.String("otc-tenant-id")
d.Region = flags.String("otc-region")
d.VpcId = flags.String("otc-vpc-id")
d.AdminPass = flags.String("otc-admin-password")
d.BandwidthSize = flags.Int("otc-bandwidth-size")
d.BandwidthType = flags.String("otc-bandwidth-type")
d.ElasticIpType = flags.String("otc-elasticip-type")
d.SSHUser = flags.String("otc-ssh-user")
d.ElasticIpBool = flags.Int("otc-elastic-ip")
//fmt.Printf("test for region: %s\n", d.Region)
return d.checkConfig()
}
func (d *Driver) checkConfig() error {
if d.ServiceEndpoint == "" {
return fmt.Errorf(errorMandatoryEnvOrOption, "Service Endpoint", "SERVICE_ENDPOINT", "--otc-service-endpoint")
}
if d.Region == "" {
return fmt.Errorf(errorMandatoryEnvOrOption, "Region", "REGION", "otc-region")
}
if d.TenantId == "" {
return fmt.Errorf(errorMandatoryEnvOrOption, "Tenant ID", "TENANT_ID", "--otc-tenant-id")
}
if (d.AccessKey == "" && d.SecretKey == "") || (d.AccessKey != "" && d.SecretKey == "") || (d.AccessKey == "" && d.SecretKey != "") {
return fmt.Errorf(errorBothOptions, d.AccessKey, d.SecretKey)
}
return nil
}
func NewDriver(hostName, storePath string) drivers.Driver {
id := generateId()
return &Driver{
InstanceId: id,
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
StorePath: storePath,
SSHUser: defaultSSHUser,
}}
}
func generateId() string {
rb := make([]byte, 10)
_, err := rand.Read(rb)
if err != nil {
log.Errorf("Unable to generate id: %s", err)
}
h := md5.New()
io.WriteString(h, string(rb))
return fmt.Sprintf("%x", h.Sum(nil))
}
func (d *Driver) initClient() *client.Client {
var clientConf modules.ClientConfiguration
clientConf.Endpoint = strings.Split(d.ServiceEndpoint, "https://ecs.")[1]
clientConf.ServiceName = serviceName
clientConf.Region = d.Region
gClient := client.InitV4Client(d.AccessKey, d.SecretKey, d.TenantId, clientConf)
return gClient
}
func (d *Driver) Create() error {
var err error
if err := d.checkPrereqs(); err != nil {
return err
}
log.Infof("%s | Creating key pair for instance ...", d.MachineName)
if err := d.createKeyPair(); err != nil {
return fmt.Errorf("%s | Failed to create key pair: %v", d.MachineName, err)
}
//var base64string = "I2Nsb3VkLWNvbmZpZwp3cml0ZV9maWxlczoKICAtIHBhdGg6IC9ob21lL2xpbnV4L3Rlc3QKICAgIHBlcm1pc3Npb25zOiAnMDc3NycKICAgIGNvbnRlbnQ6IFlvdXIgVmFsdWU="
//templatebytes, err := base64.StdEncoding.DecodeString(base64string)
//if err != nil {
// log.Infof("%s | failed to decode base64 string", d.MachineName)
//}
//log.Infof("%s | templatebytes: %v", d.MachineName, templatebytes)
/*log.Infof("%s | const user data temp: %v", d.MachineName, []byte(user_data_temp))
testfile, err := os.Create("test")
if err != nil {
log.Infof("%s | failed to create test file", d.MachineName)
}
user_data_input := user_data_temp + string(d.PublicKey)
//if _, err := testfile.Write(templatebytes); err != nil {
if _, err := testfile.Write([]byte(user_data_input)); err != nil {
log.Infof("%s | failed to write file test", d.MachineName)
}
for {
}*/
log.Infof("%s | Configuring security groups for instance ...", d.MachineName)
if err := d.configureSecurityGroup(d.SecurityGroupName); err != nil {
return fmt.Errorf("%s | Failed to configure instance security groups: %v", d.MachineName, err)
}
log.Infof("%s | Configuring instance flavor ...", d.MachineName)
if err := d.configureFlavor(d.FlavorId); err != nil {
return fmt.Errorf("%s | Failed to configure instance flavor: %v", d.MachineName, err)
}
log.Infof("%s | Configure instance image ...", d.MachineName)
if err := d.configureImage(d.ImageId); err != nil {
return fmt.Errorf("%s | Failed to configure instance image: %v", d.MachineName, err)
}
log.Infof("%s | Configuring subnet for instance ...", d.MachineName)
if err := d.configureSubnet(d.SubnetId); err != nil {
return fmt.Errorf("%s | Failed to configure instance subnet: %v ", d.MachineName, err)
}
log.Infof("%s | Create SSH key pair ...", d.MachineName)
if err := d.instanceKeyPairCreate(); err != nil {
return fmt.Errorf("%s | Failed to crate ssh key pair: %v", d.MachineName, err)
}
log.Infof("%s | Issuing request to create instance ...", d.MachineName)
createResp, err := d.createInstance()
if err != nil || modules.HttpOK != createResp.ResponseCode {
pClient := d.initClient()
deleteKeyPairResp := pClient.DeleteKeyPair(d.KeyName)
if deleteKeyPairResp.ResponseCode != modules.HttpOK {
return fmt.Errorf("%s | Failed to delete key pair %s: %v", d.MachineName, d.InstanceId, deleteKeyPairResp.ErrorInfo.Description)
}
return fmt.Errorf("%s | Failed to create instance: %v, response code is %d", d.MachineName, err, createResp.ResponseCode)
}
d.JobId = createResp.Job_id
log.Infof("%s | job for creating instance: %s and reponse code: %d and err: %v", d.MachineName, d.JobId, createResp.ResponseCode, createResp.ErrorInfo)
//check status of creating instance
log.Infof("%s | Checking instance status...", d.MachineName)
if err := d.checkJobStatus(d.JobId); err != nil {
return fmt.Errorf("%s | Failed to check instance", d.MachineName)
}
//configure network for elastic cloud server
log.Infof("%s | Configure network for elastic colud server ...", d.MachineName)
if err := d.configureNetwork(); err != nil {
return fmt.Errorf("%s | Failed to configure instance network: %v", d.MachineName, err)
}
d.getIPAddress()
return nil
}
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
func (d *Driver) GetIP() (string, error) {
return d.IPAddress, nil
}
func (d *Driver) Start() error {
log.Infof("%s | Start instance...", d.MachineName)
pClient := d.initClient()
startResp := pClient.StartServer(d.InstanceId)
if startResp.ResponseCode != modules.HttpOK {
return fmt.Errorf("%s | Failed to start instance: %v", d.MachineName, startResp.ErrorInfo.Description)
}
return nil
}
func (d *Driver) Stop() error {
log.Infof("%s | Stop instance...", d.MachineName)
pClient := d.initClient()
stopResp := pClient.StopServer(d.InstanceId)
if stopResp.ResponseCode != modules.HttpOK {
return fmt.Errorf("%s | Failed to stop instance: %v", d.MachineName, stopResp.ErrorInfo.Description)
}
return nil
}
func (d *Driver) DriverName() string {
return driverName
}
func (d *Driver) GetURL() (string, error) {
return fmt.Sprintf("tcp://%s:%d", d.IPAddress, dockerPort), nil
}
func (d *Driver) getIPAddress() {
log.Infof("%s | Getting IP addresses...", d.MachineName)
pClient := d.initClient()
stateResp := pClient.ShowServer(d.InstanceId)
if stateResp.ResponseCode != modules.HttpOK {
log.Errorf("%s | Failed to get instance %s: state %v", d.MachineName, d.InstanceId, stateResp.ErrorInfo.Description)
return
}
// FIXME: PrivateIPAddress is not stored to config.json for now
// because docker-machine did not get any update from drivers
for _, paddrs := range stateResp.Server.Addresses {
for _, paddr := range paddrs {
if paddr.Addr != "" {
split := strings.Split(paddr.Addr, " ")
d.PrivateIPAddress = split[0]
}
}
}
// Populate standard BaseDriver.IPAddress property
// ref: https://github.com/docker/machine/blob/master/libmachine/drivers/base.go#L14-L17
if d.ElasticIP != "" {
d.IPAddress = d.ElasticIP
} else {
d.IPAddress = d.PrivateIPAddress
}
log.Infof("%s | Driver IP address set to %s", d.MachineName, d.IPAddress)
return
}
func (d *Driver) GetState() (state.State, error) {
log.Infof("%s | Checking instance status...", d.MachineName)
pClient := d.initClient()
stateResp := pClient.ShowServer(d.InstanceId)
if stateResp.ResponseCode != modules.HttpOK {
err := fmt.Errorf("%s | Failed to get instance %s: state %v", d.MachineName, d.InstanceId, stateResp.ErrorInfo.Description)
log.Error(err)
return state.None, err
}
switch stateResp.Server.Status {
case "ACTIVE":
return state.Running, nil
case "SHUTOFF":
return state.Stopped, nil
case "ERROR":
return state.Error, nil
}
return state.None, nil
}
func (d *Driver) Kill() error {
return d.Stop()
}
func (d *Driver) Remove() error {
log.Infof("%s | Removing instance...", d.MachineName)
/*pClient := d.initClient()
bandwidthListResp := pClient.ListBandwidths(20, "")
log.Infof("bandwidth list: %v", bandwidthListResp.Bandwidths)*/
var server ecsModules.ServerId
server.Init(d.InstanceId)
var serverList []ecsModules.ServerId
serverList = append(serverList, server)
var deleteServerReq ecsModules.DeleteCloudServerReq
deleteServerReq.Init(serverList, false, false)
pClient := d.initClient()
deleteServerResp := pClient.DeleteCloudServer(deleteServerReq)
if deleteServerResp.ResponseCode != modules.HttpOK {
return fmt.Errorf("%s | Failed to delete instance %s: %v", d.MachineName, d.InstanceId, deleteServerResp.ErrorInfo.Description)
}
//check status of creating instance
log.Infof("%s | Checking instance status...", d.MachineName)
if err := d.checkJobStatus(deleteServerResp.Job_id); err != nil {
return fmt.Errorf("%s | Failed to delete instance %s: %v", d.MachineName, d.InstanceId, err)
}
if d.ElasticIPId != "" {
log.Infof("%s | Removing elastic ip...", d.MachineName)
pClient = d.initClient()
deleteElasticIpResp := pClient.DeletePublicIp(d.ElasticIPId)
if deleteElasticIpResp.ResponseCode != modules.HttpOK {
return fmt.Errorf("%s | Failed to remove elastic ip: %v", d.MachineName, deleteElasticIpResp.ErrorInfo.Description)
}
}
if d.KeyName != "" {
log.Infof("%s | Removing instance key pair(%s)...", d.MachineName, d.KeyName)
pClient = d.initClient()
deleteKeyPairResp := pClient.DeleteKeyPair(d.KeyName)
if deleteKeyPairResp.ResponseCode != modules.HttpOK {
return fmt.Errorf("%s | Failed to delete key pair %s: %v", d.MachineName, d.InstanceId, deleteKeyPairResp.ErrorInfo.Description)
}
}
return nil
}
func (d *Driver) Restart() error {
log.Infof("%s | Restarting instance...", d.MachineName)
pClient := d.initClient()
rebootServerResp := pClient.RebootServer(d.InstanceId, "SOFT")
if rebootServerResp.ResponseCode != modules.HttpOK {
return fmt.Errorf("%s | Failed to restart instance %s: %v", d.MachineName, d.InstanceId, rebootServerResp.ErrorInfo.Description)
}
return nil
}
func (d *Driver) instanceKeyPairCreate() error {
var keypairCreate novaModules.KeypairCreate
var createKeypairReq novaModules.CreateKeypairReq
log.Debugf("%s | Create ssh key pair...", d.MachineName)
pClient := d.initClient()
keypairCreate.Init(d.MachineName)
keypairCreate.SetPublic_key(string(d.PublicKey))
createKeypairReq.Init(keypairCreate)
kp := pClient.CreateKeypair(createKeypairReq)
if kp.Keypair.Name != "" {
log.Debugf("%s | Success to create key pair: %s", d.MachineName, kp.Keypair.Name)
d.KeyName = kp.Keypair.Name
return nil
} else if kp.ResponseCode == 409 {
return fmt.Errorf("Keypair(%s) existed, please remove it from the console, err %v", d.MachineName, kp.ErrorInfo)
} else {
return fmt.Errorf("unknown error, status code: %d, err: %v", kp.ResponseCode, kp.ErrorInfo)
}
}
func (d *Driver) createInstance() (ecsModules.CreateCloudServerResp, error) {
var nics ecsModules.Nics
nics.Init(d.SubnetId)
var nicsList []ecsModules.Nics
nicsList = append(nicsList, nics)
/*var secGrp ecsModules.SecGrp
secGrp.Init(d.SecurityGroupId)
var sgList []ecsModules.SecGrp
sgList = append(sgList, secGrp)*/
var secGrp ecsModules.SecurityGroup
secGrp.Init()
secGrp.SetId(d.SecurityGroupId)
var sgList []ecsModules.SecurityGroup
sgList = append(sgList, secGrp)
var rootVol ecsModules.RootVolume
rootVol.Init(d.RootVolType)
rootVol.SetSize(d.RootVolSize)
var instanceDesc ecsModules.CreateCloudServer
//instanceDesc.Init(d.ImageId, d.FlavorId, d.GetMachineName(), d.VpcId, nicsList, rootVol, d.AdminPass, d.KeyName, d.AvailableZone, sgList)
instanceDesc.Init(d.ImageId, d.FlavorId, d.GetMachineName(), d.VpcId, nicsList, rootVol, d.AvailableZone)
instanceDesc.SetKey_name(d.KeyName)
instanceDesc.SetAdminPass(d.AdminPass)
instanceDesc.SetSecurity_groups(sgList)
/*log.Debugf("%s | SSH User: %s", d.MachineName, d.SSHUser)
if d.SSHUser != "" {
var perItem ecsModules.Personality
path := "/home/" + d.SSHUser + "/.ssh/authorized_keys"
perItem.Init(path, base64.StdEncoding.EncodeToString([]byte(d.PublicKey)))
//encoding := base64.StdEncoding.EncodeToString([]byte(d.PublicKey))
//perItem.Init(path, encoding)
//decoding, _ := base64.StdEncoding.DecodeString(encoding)
//log.Infof("decoding: %s", string(decoding))
var perList []ecsModules.Personality
perList = append(perList, perItem)
instanceDesc.SetPersonality(perList)
}
user_data_input := user_data_temp + string(d.PublicKey)
instanceDesc.SetUser_data(base64.StdEncoding.EncodeToString([]byte(user_data_input)))*/
var instanceCreateReq ecsModules.CreateCloudServerReq
instanceCreateReq.Init(instanceDesc)
pClient := d.initClient()
instanceCreateResp := pClient.CreateCloudServer(instanceCreateReq)
return instanceCreateResp, nil
}
func (d *Driver) checkJobStatus(jobid string) error {
pClient := d.initClient()
for {
ecsJobStatusResp := pClient.ShowEcsJob(jobid)
if len(ecsJobStatusResp.Entities.SubJobs) > 0 {
d.InstanceId = ecsJobStatusResp.Entities.SubJobs[0].Entities.Server_id
}
if ecsJobStatusResp.Status == "SUCCESS" {
log.Debugf("%s | job return value are: %v and returned instance id is: %s", d.MachineName, ecsJobStatusResp, d.InstanceId)
break
}
if ecsJobStatusResp.Status == "FAIL" {
log.Debugf("%s | Failed to check instance status: %v", d.MachineName, ecsJobStatusResp.ErrorInfo.Description)
return fmt.Errorf("%s | Failed to check instance status: %v", d.MachineName, ecsJobStatusResp.ErrorInfo.Description)
}
log.Debugf("job status: %s", ecsJobStatusResp.Status)
time.Sleep(10 * time.Second)
}
return nil
}
func (d *Driver) configureNetwork() error {
// Exit if ElasticIp is set to false
if d.ElasticIpBool == 0 {
log.Infof("%s | An ElasticIp won't be allocated", d.MachineName)
return nil
}
log.Debugf("%s | Allocate elastic ip ...", d.MachineName)
elasticIpCreate := &vpcModules.PublicipCreate{}
elasticIpCreate.SetType(d.ElasticIpType)
bandwidthCreate := &vpcModules.BandwidthCreate{}
bandwidthCreate.Init(d.MachineName, d.BandwidthSize, d.BandwidthType)
createElasticIp := &vpcModules.CreatePublicIpReq{}
createElasticIp.Init(elasticIpCreate, bandwidthCreate)
pClient := d.initClient()
createElasticIpResp := pClient.CreatePublicIp(createElasticIp)
if createElasticIpResp.Id != "" {
log.Debugf("%s | Success to allocate elastic ip %s and status is %s", d.MachineName, createElasticIpResp.Public_ip_address, createElasticIpResp.Status)
d.ElasticIPId = createElasticIpResp.Id
d.ElasticIP = createElasticIpResp.Public_ip_address
}
log.Debugf("%s | Query nic port id ...", d.MachineName)
//check elastic server instance's interface
pClient = d.initClient()
interfaceList := pClient.ListInterfaces(d.InstanceId)
for _, iface := range interfaceList.InterfaceAttachments {
if iface.Port_id != "" {
log.Debugf("%s | Found instance network interface port %s", d.MachineName, iface.Port_id)
d.PortId = iface.Port_id
break
}
}
log.Debugf("%s | Waiting for elastic ip to be ready ...", d.MachineName)
// waiting for elastic ip to be ready
// TODO: Wait for READY status instead of relying on sleep
time.Sleep(20 * time.Second)
log.Debugf("%s | Associate elastic ip to instance nic port ...", d.MachineName)
elasticIpUpdate := &vpcModules.PublicipUpdate{}
elasticIpUpdate.SetPortId(d.PortId)
updateElasticIpReq := &vpcModules.UpdatePublicIpReq{}
updateElasticIpReq.SetPublicip(elasticIpUpdate)
pClient = d.initClient()
updateElasticIpResp := pClient.UpdatePublicIp(d.ElasticIPId, updateElasticIpReq)
if updateElasticIpResp.ErrorInfo.Description != "" {
return fmt.Errorf("%s", updateElasticIpResp.ErrorInfo.Description)
}
log.Debugf("%s | Success to associate elastic ip to instance nic port ...", d.MachineName)
return nil
}
func (d *Driver) configureSubnet(subnetId string) error {
log.Debugf("%s | Checking instance subnet list on cloud ...", d.MachineName)
pClient := d.initClient()
subnetList := pClient.ListSubnets(subnetListNumPerPage, "", d.VpcId)
log.Debugf("subnets:%v response code:%d error info:%v", subnetList.Subnets, subnetList.ResponseCode, subnetList.ErrorInfo)
for _, subnet := range subnetList.Subnets {
if subnetId == subnet.Id {
log.Debugf("%s | Found existing subnet %s", d.MachineName, subnet.Id)
d.SubnetId = subnet.Id
//there is no azs in response in new apis
//d.AvailableZone = subnet.Availability_zone
log.Debugf("%s | Subnet id is: %s : %s and available zone is: %s", d.MachineName, subnet.Id, d.SubnetId, d.AvailableZone)
break
}
}
return nil
}
func (d *Driver) configureImage(imageId string) error {
log.Debugf("%s | Checking instance image list on cloud ...", d.MachineName)
pClient := d.initClient()
listImgsReq := &imsModules.ListCloudImagesReqEx{}
listImgsReq.Init()
//listImgsReq.SetLimit(imgListNumPerPage)
imagesList := pClient.ListCloudImages(listImgsReq)
// buggy log.Debugf("%v", imagesList.Images)
for _, image := range imagesList.Images {
if imageId == image.Id {
log.Debugf("%s | Found existing image %s", d.MachineName, image.Id)
d.ImageId = image.Id
log.Debugf("%s | Image id is: %s and name is: %s", d.MachineName, d.ImageId, image.Name)
break
}
}
return nil
}
func (d *Driver) configureFlavor(flavorId string) error {
log.Debugf("%s | Checking instance flavor list on cloud ...", d.MachineName)
pClient := d.initClient()
flavorList := pClient.ListCloudServerFlavorsExt()
log.Debugf("%v", flavorList.Flavors)
if flavorList.ErrorInfo.Code != "" {
log.Infof("%s | Failed to get flavor", d.MachineName)
log.Debugf("Error code: %s and description: %s", flavorList.ErrorInfo.Code, flavorList.ErrorInfo.Description)
return fmt.Errorf("Error code: %s and description: %s", flavorList.ErrorInfo.Code, flavorList.ErrorInfo.Description)
}
for _, flavor := range flavorList.Flavors {
if flavorId == flavor.Id {
log.Debugf("%s | Found existing flavor spec %s", d.MachineName, flavor.Id)
d.FlavorId = flavor.Id
log.Debugf("%s | Flavor id is: %s", d.MachineName, d.FlavorId)
break
}
}
return nil
}
func (d *Driver) configureSecurityGroup(groupName string) error {
log.Debugf("%s | Configure security groups", d.MachineName)
pClient := d.initClient()
listSecGrpRsp := pClient.ListSecurityGroups(secGrpListNumPerPage, "", "")
for _, grp := range listSecGrpRsp.SecurityGroups {
if grp.Name == groupName {
log.Debugf("%s | Found existing security group (%s) in %s", d.MachineName, groupName, d.VpcId)
d.SecurityGroupId = grp.Id
log.Debugf("%s | Security group id is: %s", d.MachineName, d.SecurityGroupId)
break
}
}
return nil
}
func (d *Driver) PreCreateCheck() error {
return d.checkPrereqs()
}
func (d *Driver) checkPrereqs() error {
return nil
}
/*func (d *Driver) getECSClient() *ecs.Client {
if d.ecsclient == nil {
client := ecs.NewClient(d.AccessKey, d.SecretKey)
d.ecsclient = client
}
return d.ecsclient
}
func (d *Driver) getVPCClient() *vpc.Client {
if d.vpcclient == nil {
client := vpc.NewClient(d.AccessKey, d.SecretKey)
d.vpcclient = client
}
return d.vpcclient
}
func (d *Driver) getIMSClient() *ims.Client {
if d.imsclient == nil {
client := ims.NewClient(d.AccessKey, d.SecretKey)
d.imsclient = client
}
return d.imsclient
}*/
func (d *Driver) createKeyPair() error {
log.Debugf("%s | SSH key path:%s", d.MachineName, d.GetSSHKeyPath())
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
publicKey, err := ioutil.ReadFile(d.GetSSHKeyPath() + ".pub")
if err != nil {
return err
}
d.PublicKey = publicKey
return nil
}