/
compute.go
1063 lines (901 loc) · 28.3 KB
/
compute.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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018, CS Systemes d'Information, http://www.c-s.fr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gcp
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"strconv"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"github.com/CS-SI/SafeScale/lib/server/iaas/resources"
"github.com/CS-SI/SafeScale/lib/server/iaas/resources/enums/HostProperty"
"github.com/CS-SI/SafeScale/lib/server/iaas/resources/enums/HostState"
converters "github.com/CS-SI/SafeScale/lib/server/iaas/resources/properties"
propsv1 "github.com/CS-SI/SafeScale/lib/server/iaas/resources/properties/v1"
"github.com/CS-SI/SafeScale/lib/server/iaas/resources/userdata"
"github.com/CS-SI/SafeScale/lib/utils"
"github.com/CS-SI/SafeScale/lib/utils/concurrency"
"github.com/CS-SI/SafeScale/lib/utils/retry"
"github.com/CS-SI/SafeScale/lib/utils/scerr"
"github.com/CS-SI/SafeScale/lib/utils/temporal"
)
//-------------IMAGES---------------------------------------------------------------------------------------------------
// ListImages lists available OS images
func (s *Stack) ListImages() (images []resources.Image, err error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
compuService := s.ComputeService
images = []resources.Image{}
families := []string{"centos-cloud", "debian-cloud", "rhel-cloud", "ubuntu-os-cloud", "suse-cloud", "rhel-sap-cloud", "suse-sap-cloud"}
for _, family := range families {
token := ""
for paginate := true; paginate; {
resp, err := compuService.Images.List(family).Filter("deprecated.replacement ne .*images.*").PageToken(token).Do()
if err != nil {
logrus.Warnf("Can't list public images for project %q", family)
break
}
for _, image := range resp.Items {
images = append(images, resources.Image{Name: image.Name, URL: image.SelfLink, ID: strconv.FormatUint(image.Id, 10)})
}
token := resp.NextPageToken
paginate = token != ""
}
}
if len(images) == 0 {
return images, err
}
return images, nil
}
// GetImage returns the Image referenced by id
func (s *Stack) GetImage(id string) (*resources.Image, error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
if id == "" {
return nil, scerr.InvalidParameterError("id", "cannot be empty string")
}
images, err := s.ListImages()
if err != nil {
return nil, err
}
for _, image := range images {
if image.ID == id {
return &image, nil
}
}
return nil, fmt.Errorf("image with id [%s] not found", id)
}
//-------------TEMPLATES------------------------------------------------------------------------------------------------
// ListTemplates overload OpenStackGcp ListTemplate method to filter wind and flex instance and add GPU configuration
func (s *Stack) ListTemplates(all bool) (templates []resources.HostTemplate, err error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
compuService := s.ComputeService
templates = []resources.HostTemplate{}
token := ""
for paginate := true; paginate; {
resp, err := compuService.MachineTypes.List(s.GcpConfig.ProjectID, s.GcpConfig.Zone).PageToken(token).Do()
if err != nil {
logrus.Warnf("Can't list public types...: %s", err)
break
} else {
for _, matype := range resp.Items {
ht := resources.HostTemplate{
Cores: int(matype.GuestCpus),
RAMSize: float32(matype.MemoryMb / 1024),
//VPL: GCP Template disk sizing is ridiculous at best, so fill it to 0 and let us size the disk ourselves
//DiskSize: int(matype.ImageSpaceGb),
DiskSize: 0,
ID: strconv.FormatUint(matype.Id, 10),
Name: matype.Name,
}
templates = append(templates, ht)
}
}
token := resp.NextPageToken
paginate = token != ""
}
if len(templates) == 0 {
return templates, err
}
return templates, nil
}
//GetTemplate overload OpenStackGcp GetTemplate method to add GPU configuration
func (s *Stack) GetTemplate(id string) (*resources.HostTemplate, error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
if id == "" {
return nil, scerr.InvalidParameterError("id", "cannot be empty string")
}
templates, err := s.ListTemplates(true)
if err != nil {
return nil, err
}
for _, template := range templates {
if template.ID == id {
return &template, nil
}
}
return nil, fmt.Errorf("template with id [%s] not found", id)
}
//-------------SSH KEYS-------------------------------------------------------------------------------------------------
// CreateKeyPair creates and import a key pair
func (s *Stack) CreateKeyPair(name string) (*resources.KeyPair, error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
if name == "" {
return nil, scerr.InvalidParameterError("name", "cannot be empty string")
}
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
publicKey := privateKey.PublicKey
pub, _ := ssh.NewPublicKey(&publicKey)
pubBytes := ssh.MarshalAuthorizedKey(pub)
pubKey := string(pubBytes)
priBytes := x509.MarshalPKCS1PrivateKey(privateKey)
priKeyPem := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: priBytes,
},
)
priKey := string(priKeyPem)
return &resources.KeyPair{
ID: name,
Name: name,
PublicKey: pubKey,
PrivateKey: priKey,
}, nil
}
// GetKeyPair returns the key pair identified by id
func (s *Stack) GetKeyPair(id string) (*resources.KeyPair, error) {
return nil, scerr.NotImplementedError("GetKeyPair() not implemented yet")
}
// ListKeyPairs lists available key pairs
func (s *Stack) ListKeyPairs() ([]resources.KeyPair, error) {
return nil, scerr.NotImplementedError("ListKeyPairs() not implemented yet")
}
// DeleteKeyPair deletes the key pair identified by id
func (s *Stack) DeleteKeyPair(id string) error {
return scerr.NotImplementedError("DeleteKeyPair() not implemented yet")
}
// CreateHost creates an host satisfying request
func (s *Stack) CreateHost(request resources.HostRequest) (host *resources.Host, userData *userdata.Content, err error) {
if s == nil {
return nil, nil, scerr.InvalidInstanceError()
}
userData = userdata.NewContent()
resourceName := request.ResourceName
networks := request.Networks
hostMustHavePublicIP := request.PublicIP
if len(networks) == 0 {
return nil, userData, fmt.Errorf("the host %s must be on at least one network (even if public)", resourceName)
}
// If no key pair is supplied create one
if request.KeyPair == nil {
id, err := uuid.NewV4()
if err != nil {
msg := fmt.Sprintf("failed to create host UUID: %+v", err)
logrus.Debugf(utils.Capitalize(msg))
return nil, userData, fmt.Errorf(msg)
}
name := fmt.Sprintf("%s_%s", request.ResourceName, id)
request.KeyPair, err = s.CreateKeyPair(name)
if err != nil {
msg := fmt.Sprintf("failed to create host key pair: %+v", err)
logrus.Debugf(utils.Capitalize(msg))
return nil, userData, fmt.Errorf(msg)
}
}
if request.Password == "" {
password, err := utils.GeneratePassword(16)
if err != nil {
return nil, userData, fmt.Errorf("failed to generate password: %s", err.Error())
}
request.Password = password
}
// The Default Network is the first of the provided list, by convention
defaultNetwork := request.Networks[0]
defaultNetworkID := defaultNetwork.ID
defaultGateway := request.DefaultGateway
isGateway := defaultGateway == nil && defaultNetwork.Name != resources.SingleHostNetworkName
defaultGatewayID := ""
defaultGatewayPrivateIP := ""
if defaultGateway != nil {
err := defaultGateway.Properties.LockForRead(HostProperty.NetworkV1).ThenUse(func(v interface{}) error {
hostNetworkV1 := v.(*propsv1.HostNetwork)
defaultGatewayPrivateIP = hostNetworkV1.IPv4Addresses[defaultNetworkID]
defaultGatewayID = defaultGateway.ID
return nil
})
if err != nil {
return nil, userData, err
}
}
if defaultGateway == nil && !hostMustHavePublicIP {
return nil, userData, fmt.Errorf("the host %s must have a gateway or be public", resourceName)
}
var nets []servers.Network
// FIXME add provider network to host networks ?
// Add private networks
for _, n := range request.Networks {
nets = append(nets, servers.Network{
UUID: n.ID,
})
}
// --- prepares data structures for Provider usage ---
// Constructs userdata content
err = userData.Prepare(*s.Config, request, defaultNetwork.CIDR, "")
if err != nil {
msg := fmt.Sprintf("failed to prepare user data content: %+v", err)
logrus.Debugf(utils.Capitalize(msg))
return nil, userData, fmt.Errorf(msg)
}
// Determine system disk size based on vcpus count
template, err := s.GetTemplate(request.TemplateID)
if err != nil {
return nil, userData, fmt.Errorf("failed to get image: %s", err)
}
if request.DiskSize > template.DiskSize {
template.DiskSize = request.DiskSize
} else if template.DiskSize == 0 {
// Determines appropriate disk size
if template.Cores < 16 {
template.DiskSize = 100
} else if template.Cores < 32 {
template.DiskSize = 200
} else {
template.DiskSize = 400
}
}
rim, err := s.GetImage(request.ImageID)
if err != nil {
return nil, nil, err
}
logrus.Debugf("Selected template: '%s', '%s'", template.ID, template.Name)
// Select usable availability zone, the first one in the list
if s.GcpConfig.Zone == "" {
azList, err := s.ListAvailabilityZones()
if err != nil {
return nil, userData, err
}
var az string
for az = range azList {
break
}
s.GcpConfig.Zone = az
logrus.Debugf("Selected Availability Zone: '%s'", az)
}
// Sets provider parameters to create host
userDataPhase1, err := userData.Generate("phase1")
if err != nil {
return nil, userData, err
}
// --- Initializes resources.Host ---
host = resources.NewHost()
host.PrivateKey = request.KeyPair.PrivateKey // Add PrivateKey to host definition
host.Password = request.Password
err = host.Properties.LockForWrite(HostProperty.NetworkV1).ThenUse(func(v interface{}) error {
hostNetworkV1 := v.(*propsv1.HostNetwork)
hostNetworkV1.DefaultNetworkID = defaultNetworkID
hostNetworkV1.DefaultGatewayID = defaultGatewayID
hostNetworkV1.DefaultGatewayPrivateIP = defaultGatewayPrivateIP
hostNetworkV1.IsGateway = isGateway
return nil
})
if err != nil {
return nil, userData, err
}
// Adds Host property SizingV1
err = host.Properties.LockForWrite(HostProperty.SizingV1).ThenUse(func(v interface{}) error {
hostSizingV1 := v.(*propsv1.HostSizing)
// Note: from there, no idea what was the RequestedSize; caller will have to complement this information
hostSizingV1.Template = request.TemplateID
hostSizingV1.AllocatedSize = converters.ModelHostTemplateToPropertyHostSize(template)
return nil
})
if err != nil {
return nil, userData, err
}
// --- query provider for host creation ---
logrus.Debugf("requesting host resource creation...")
var desistError error
// Retry creation until success, for 10 minutes
retryErr := retry.WhileUnsuccessfulDelay5Seconds(
func() error {
server, err := buildGcpMachine(s.ComputeService, s.GcpConfig.ProjectID, request.ResourceName, rim.URL, s.GcpConfig.Zone, s.GcpConfig.NetworkName, defaultNetwork.Name, string(userDataPhase1), isGateway, template)
if err != nil {
if server != nil {
// try deleting server
killErr := s.DeleteHost(server.ID)
if killErr != nil {
switch killErr.(type) {
case scerr.ErrTimeout:
logrus.Error("Timeout cleaning up gcp instance")
default:
logrus.Errorf("Something else happened to gcp instance: %+v", killErr)
}
err = scerr.AddConsequence(err, killErr)
}
return err
}
if gerr, ok := err.(*googleapi.Error); ok {
logrus.Warnf("Received GCP errorcode: %d", gerr.Code)
if gerr.Code == 403 {
desistError = gerr
return nil
}
}
logrus.Warnf("error creating host: %+v", err)
return err
}
if server == nil {
return fmt.Errorf("failed to create server")
}
host.ID = server.ID
host.Name = server.Name
// Wait that Host is ready, not just that the build is started
_, err = s.WaitHostReady(host, temporal.GetLongOperationTimeout())
if err != nil {
killErr := s.DeleteHost(host.ID)
if killErr != nil {
switch killErr.(type) {
case scerr.ErrTimeout:
logrus.Error("Timeout cleaning up gcp instance")
default:
logrus.Errorf("Something else happened to gcp instance: %+v", killErr)
}
err = scerr.AddConsequence(err, killErr)
}
return err
}
return nil
},
temporal.GetLongOperationTimeout(),
)
if retryErr != nil {
return nil, userData, retryErr
}
if desistError != nil {
return nil, userData, resources.ResourceForbiddenError(request.ResourceName, fmt.Sprintf("error creating host: %s", desistError.Error()))
}
logrus.Debugf("host resource created.")
newHost := host
// Starting from here, delete host if exiting with error
defer func() {
if err != nil {
logrus.Infof("Cleanup, deleting host '%s'", newHost.Name)
derr := s.DeleteHost(newHost.ID)
if derr != nil {
switch derr.(type) {
case scerr.ErrNotFound:
logrus.Errorf("Cleaning up on failure, failed to delete host '%s', resource not found: '%v'", newHost.Name, derr)
case scerr.ErrTimeout:
logrus.Errorf("Cleaning up on failure, failed to delete host '%s', timeout: '%v'", newHost.Name, derr)
default:
logrus.Errorf("Cleaning up on failure, failed to delete host '%s': '%v'", newHost.Name, derr)
}
err = scerr.AddConsequence(err, derr)
}
}
}()
if host == nil {
return nil, nil, fmt.Errorf("unexpected nil host")
}
if !host.OK() {
logrus.Warnf("Missing data in host: %s", spew.Sdump(host))
}
return host, userData, nil
}
// WaitHostReady waits an host achieve ready state
// hostParam can be an ID of host, or an instance of *resources.Host; any other type will return an utils.ErrInvalidParameter.
func (s *Stack) WaitHostReady(hostParam interface{}, timeout time.Duration) (res *resources.Host, err error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
var host *resources.Host
switch hostParam := hostParam.(type) {
case string:
host = resources.NewHost()
host.ID = hostParam
case *resources.Host:
host = hostParam
}
if host == nil {
return nil, scerr.InvalidParameterError("hostParam", "must be a not-empty string or a *resources.Host")
}
tracer := concurrency.NewTracer(nil, fmt.Sprintf("(%s)", host.ID), true).GoingIn()
defer tracer.OnExitTrace()()
defer scerr.OnExitLogError(tracer.TraceMessage(""), &err)()
retryErr := retry.WhileUnsuccessful(
func() error {
hostTmp, err := s.InspectHost(host)
if err != nil {
return err
}
host = hostTmp
if host.LastState != HostState.STARTED {
return fmt.Errorf("not in ready state (current state: %s)", host.LastState.String())
}
return nil
},
temporal.GetDefaultDelay(),
timeout,
)
if retryErr != nil {
if _, ok := retryErr.(retry.ErrTimeout); ok {
return host, resources.TimeoutError(fmt.Sprintf("timeout waiting to get host '%s' information after %v", host.Name, timeout), timeout)
}
return host, retryErr
}
return host, nil
}
func publicAccess(isPublic bool) []*compute.AccessConfig {
if isPublic {
return []*compute.AccessConfig{
{
Type: "ONE_TO_ONE_NAT",
Name: "External NAT",
},
}
}
return []*compute.AccessConfig{}
}
// buildGcpMachine ...
func buildGcpMachine(service *compute.Service, projectID string, instanceName string, imageID string, zone string, network string, subnetwork string, userdata string, isPublic bool, template *resources.HostTemplate) (*resources.Host, error) {
prefix := "https://www.googleapis.com/compute/v1/projects/" + projectID
imageURL := imageID
tag := "nat"
if !isPublic {
tag = fmt.Sprintf("no-ip-%s", subnetwork)
}
instance := &compute.Instance{
Name: instanceName,
Description: "compute sample instance",
MachineType: prefix + "/zones/" + zone + "/machineTypes/" + template.Name,
CanIpForward: isPublic,
Tags: &compute.Tags{
Items: []string{tag},
},
Disks: []*compute.AttachedDisk{
{
AutoDelete: true,
Boot: true,
Type: "PERSISTENT",
InitializeParams: &compute.AttachedDiskInitializeParams{
DiskName: fmt.Sprintf("%s-disk", instanceName),
SourceImage: imageURL,
DiskSizeGb: int64(template.DiskSize),
},
},
},
NetworkInterfaces: []*compute.NetworkInterface{
{
AccessConfigs: publicAccess(isPublic),
Network: prefix + "/global/networks/" + network,
Subnetwork: prefix + "/regions/europe-west1/subnetworks/" + subnetwork,
},
},
ServiceAccounts: []*compute.ServiceAccount{
{
Email: "default",
Scopes: []string{
compute.DevstorageFullControlScope,
compute.ComputeScope,
},
},
},
Metadata: &compute.Metadata{
Items: []*compute.MetadataItems{
{
Key: "startup-script",
Value: &userdata,
},
},
},
}
op, err := service.Instances.Insert(projectID, zone, instance).Do()
if err != nil {
return nil, err
}
etag := op.Header.Get("Etag")
oco := OpContext{
Operation: op,
ProjectID: projectID,
Service: service,
DesiredState: "DONE",
}
err = waitUntilOperationIsSuccessfulOrTimeout(oco, temporal.GetMinDelay(), temporal.GetHostTimeout())
if err != nil {
return nil, err
}
inst, err := service.Instances.Get(projectID, zone, instanceName).IfNoneMatch(etag).Do()
if err != nil {
return nil, err
}
logrus.Tracef("Got compute.Instance, err: %#v, %v", inst, err)
if googleapi.IsNotModified(err) {
logrus.Warnf("Instance not modified since insert.")
}
host := resources.NewHost()
host.ID = strconv.FormatUint(inst.Id, 10)
host.Name = inst.Name
return host, nil
}
// InspectHost returns the host identified by ref (name or id) or by a *resources.Host containing an id
func (s *Stack) InspectHost(hostParam interface{}) (host *resources.Host, err error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
switch hostParam := hostParam.(type) {
case string:
host = resources.NewHost()
host.ID = hostParam
case *resources.Host:
host = hostParam
}
if host == nil {
return nil, scerr.InvalidParameterError("hostParam", "must be a string or a *resources.Host")
}
hostRef := host.Name
if hostRef == "" {
hostRef = host.ID
}
if utils.IsEmpty(host) {
return nil, resources.ResourceNotFoundError("host", hostRef)
}
gcpHost, err := s.ComputeService.Instances.Get(s.GcpConfig.ProjectID, s.GcpConfig.Zone, hostRef).Do()
if err != nil {
return nil, err
}
host.LastState, err = stateConvert(gcpHost.Status)
if err != nil {
return nil, err
}
var subnets []IPInSubnet
for _, nit := range gcpHost.NetworkInterfaces {
snet := genURL(nit.Subnetwork)
if !utils.IsEmpty(snet) {
pubIP := ""
for _, aco := range nit.AccessConfigs {
if aco != nil {
if aco.NatIP != "" {
pubIP = aco.NatIP
}
}
}
subnets = append(subnets, IPInSubnet{
Subnet: snet,
IP: nit.NetworkIP,
PublicIP: pubIP,
})
}
}
var resouceNetworks []IPInSubnet
for _, sn := range subnets {
region, err := getRegionFromSelfLink(sn.Subnet)
if err != nil {
continue
}
psg, err := s.ComputeService.Subnetworks.Get(s.GcpConfig.ProjectID, region, getResourceNameFromSelfLink(sn.Subnet)).Do()
if err != nil {
continue
}
resouceNetworks = append(resouceNetworks, IPInSubnet{
Subnet: sn.Subnet,
Name: psg.Name,
ID: strconv.FormatUint(psg.Id, 10),
IP: sn.IP,
PublicIP: sn.PublicIP,
})
}
ip4bynetid := make(map[string]string)
netnamebyid := make(map[string]string)
netidbyname := make(map[string]string)
ipv4 := ""
for _, rn := range resouceNetworks {
ip4bynetid[rn.ID] = rn.IP
netnamebyid[rn.ID] = rn.Name
netidbyname[rn.Name] = rn.ID
if rn.PublicIP != "" {
ipv4 = rn.PublicIP
}
}
err = host.Properties.LockForWrite(HostProperty.NetworkV1).ThenUse(func(v interface{}) error {
hostNetworkV1 := v.(*propsv1.HostNetwork)
hostNetworkV1.IPv4Addresses = ip4bynetid
hostNetworkV1.IPv6Addresses = make(map[string]string)
hostNetworkV1.NetworksByID = netnamebyid
hostNetworkV1.NetworksByName = netidbyname
if hostNetworkV1.PublicIPv4 == "" {
hostNetworkV1.PublicIPv4 = ipv4
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to update HostProperty.NetworkV1 : %s", err.Error())
}
allocated := fromMachineTypeToAllocatedSize(gcpHost.MachineType)
err = host.Properties.LockForWrite(HostProperty.SizingV1).ThenUse(func(v interface{}) error {
hostSizingV1 := v.(*propsv1.HostSizing)
hostSizingV1.AllocatedSize.Cores = allocated.Cores
hostSizingV1.AllocatedSize.RAMSize = allocated.RAMSize
hostSizingV1.AllocatedSize.DiskSize = allocated.DiskSize
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to update HostProperty.SizingV1 : %s", err.Error())
}
if !host.OK() {
logrus.Warnf("[TRACE] Unexpected host status: %s", spew.Sdump(host))
}
return host, nil
}
func fromMachineTypeToAllocatedSize(machineType string) propsv1.HostSize {
hz := propsv1.HostSize{}
// FIXME Implement mapping
return hz
}
func stateConvert(gcpHostStatus string) (HostState.Enum, error) {
switch gcpHostStatus {
case "PROVISIONING":
return HostState.STARTING, nil
case "REPAIRING":
return HostState.ERROR, nil
case "RUNNING":
return HostState.STARTED, nil
case "STAGING":
return HostState.STARTING, nil
case "STOPPED":
return HostState.STOPPED, nil
case "STOPPING":
return HostState.STOPPING, nil
case "SUSPENDED":
return HostState.STOPPED, nil
case "SUSPENDING":
return HostState.STOPPING, nil
case "TERMINATED":
return HostState.STOPPED, nil
default:
return -1, fmt.Errorf("Unexpected host status: [%s]", gcpHostStatus)
}
}
// GetHostByName returns the host identified by ref (name or id)
func (s *Stack) GetHostByName(name string) (*resources.Host, error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
if name == "" {
return nil, scerr.InvalidParameterError("name", "cannot be empty string")
}
hosts, err := s.ListHosts()
if err != nil {
return nil, err
}
for _, host := range hosts {
if host.Name == name {
return host, nil
}
}
return nil, resources.ResourceNotFoundError("host", name)
}
// DeleteHost deletes the host identified by id
func (s *Stack) DeleteHost(id string) (err error) {
if s == nil {
return scerr.InvalidInstanceError()
}
if id == "" {
return scerr.InvalidParameterError("id", "cannot be empty string")
}
service := s.ComputeService
projectID := s.GcpConfig.ProjectID
zone := s.GcpConfig.Zone
instanceName := id
_, err = service.Instances.Get(projectID, zone, instanceName).Do()
if err != nil {
return err
}
op, err := service.Instances.Delete(projectID, zone, instanceName).Do()
if err != nil {
return err
}
oco := OpContext{
Operation: op,
ProjectID: projectID,
Service: service,
DesiredState: "DONE",
}
err = waitUntilOperationIsSuccessfulOrTimeout(oco, temporal.GetMinDelay(), temporal.GetHostCleanupTimeout())
waitErr := retry.WhileUnsuccessfulDelay5Seconds(func() error {
_, recErr := service.Instances.Get(projectID, zone, instanceName).Do()
if gerr, ok := recErr.(*googleapi.Error); ok {
if gerr.Code == 404 {
return nil
}
}
return fmt.Errorf("error waiting for instance [%s] to disappear: [%v]", instanceName, recErr)
}, temporal.GetContextTimeout())
if waitErr != nil {
logrus.Error(scerr.Cause(waitErr))
}
return err
}
// ResizeHost change the template used by an host
func (s *Stack) ResizeHost(id string, request resources.SizingRequirements) (*resources.Host, error) {
return nil, scerr.NotImplementedError("ResizeHost() not implemented yet")
}
// ListHosts lists available hosts
func (s *Stack) ListHosts() ([]*resources.Host, error) {
if s == nil {
return nil, scerr.InvalidInstanceError()
}
compuService := s.ComputeService
var hostList []*resources.Host
token := ""
for paginate := true; paginate; {
resp, err := compuService.Instances.List(s.GcpConfig.ProjectID, s.GcpConfig.Zone).PageToken(token).Do()
if err != nil {
return hostList, fmt.Errorf("cannot list hosts: %v", err)
}
for _, instance := range resp.Items {
nhost := resources.NewHost()
nhost.ID = strconv.FormatUint(instance.Id, 10)
nhost.Name = instance.Name
nhost.LastState, _ = stateConvert(instance.Status)
// FIXME Populate host, what's missing ?
hostList = append(hostList, nhost)
}
token := resp.NextPageToken
paginate = token != ""
}
return hostList, nil
}
// StopHost stops the host identified by id
func (s *Stack) StopHost(id string) error {
if s == nil {
return scerr.InvalidInstanceError()
}
if id == "" {
return scerr.InvalidParameterError("id", "cannot be empty string")
}
service := s.ComputeService
op, err := service.Instances.Stop(s.GcpConfig.ProjectID, s.GcpConfig.Zone, id).Do()
if err != nil {
return err
}
oco := OpContext{
Operation: op,
ProjectID: s.GcpConfig.ProjectID,
Service: service,
DesiredState: "DONE",
}
err = waitUntilOperationIsSuccessfulOrTimeout(oco, temporal.GetMinDelay(), temporal.GetHostTimeout())
return err
}
// StartHost starts the host identified by id
func (s *Stack) StartHost(id string) error {
if s == nil {
return scerr.InvalidInstanceError()
}
if id == "" {
return scerr.InvalidParameterError("id", "cannot be empty string")
}
service := s.ComputeService
op, err := service.Instances.Start(s.GcpConfig.ProjectID, s.GcpConfig.Zone, id).Do()
if err != nil {
return err
}
oco := OpContext{
Operation: op,
ProjectID: s.GcpConfig.ProjectID,
Service: service,
DesiredState: "DONE",
}
err = waitUntilOperationIsSuccessfulOrTimeout(oco, temporal.GetMinDelay(), temporal.GetHostTimeout())
return err
}
// RebootHost reboot the host identified by id
func (s *Stack) RebootHost(id string) error {
if s == nil {
return scerr.InvalidInstanceError()
}
if id == "" {
return scerr.InvalidParameterError("id", "cannot be empty string")
}
service := s.ComputeService
op, err := service.Instances.Stop(s.GcpConfig.ProjectID, s.GcpConfig.Zone, id).Do()
if err != nil {
return err
}
oco := OpContext{
Operation: op,
ProjectID: s.GcpConfig.ProjectID,
Service: service,
DesiredState: "DONE",
}
err = waitUntilOperationIsSuccessfulOrTimeout(oco, temporal.GetMinDelay(), temporal.GetHostTimeout())
if err != nil {
return err
}
op, err = service.Instances.Start(s.GcpConfig.ProjectID, s.GcpConfig.Zone, id).Do()
if err != nil {
return err
}
oco = OpContext{
Operation: op,