forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2_client.go
1632 lines (1468 loc) · 51.4 KB
/
ec2_client.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
package cloud
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/pricing"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/utility"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
const MockIPV6 = "abcd:1234:459c:2d00:cfe4:843b:1d60:8e47"
const MockIPV4 = "12.34.56.78"
var noReservationError = errors.New("no reservation returned for instance")
// AWSClient is a wrapper for aws-sdk-go so we can use a mock in testing.
type AWSClient interface {
// Create a new aws-sdk-client or mock if one does not exist, otherwise no-op.
Create(*credentials.Credentials, string) error
// Close an aws-sdk-client or mock.
Close()
// RunInstances is a wrapper for ec2.RunInstances.
RunInstances(context.Context, *ec2.RunInstancesInput) (*ec2.Reservation, error)
// DescribeInstances is a wrapper for ec2.DescribeInstances.
DescribeInstances(context.Context, *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)
// ModifyInstanceAttribute is a wrapper for ec2.ModifyInstanceAttribute
ModifyInstanceAttribute(context.Context, *ec2.ModifyInstanceAttributeInput) (*ec2.ModifyInstanceAttributeOutput, error)
// DescribeInstanceTypeOfferings is a wrapper for ec2.DescribeInstanceTypeOfferings.
DescribeInstanceTypeOfferings(context.Context, *ec2.DescribeInstanceTypeOfferingsInput) (*ec2.DescribeInstanceTypeOfferingsOutput, error)
// CreateTags is a wrapper for ec2.CreateTags.
CreateTags(context.Context, *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error)
// DeleteTags is a wrapper for ec2.DeleteTags.
DeleteTags(context.Context, *ec2.DeleteTagsInput) (*ec2.DeleteTagsOutput, error)
// TerminateInstances is a wrapper for ec2.TerminateInstances.
TerminateInstances(context.Context, *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error)
// StopInstances is a wrapper for ec2.StopInstances.
StopInstances(context.Context, *ec2.StopInstancesInput) (*ec2.StopInstancesOutput, error)
// StartInstances is a wrapper for ec2.StartInstances.
StartInstances(context.Context, *ec2.StartInstancesInput) (*ec2.StartInstancesOutput, error)
// RequestSpotInstances is a wrapper for ec2.RequestSpotInstances.
RequestSpotInstances(context.Context, *ec2.RequestSpotInstancesInput) (*ec2.RequestSpotInstancesOutput, error)
// DescribeSpotInstanceRequests is a wrapper for ec2.DescribeSpotInstanceRequests.
DescribeSpotInstanceRequests(ctx context.Context, input *ec2.DescribeSpotInstanceRequestsInput) (*ec2.DescribeSpotInstanceRequestsOutput, error)
// DescribeSpotRequestsAndSave is a wrapper for DescribeSpotInstanceRequests that also saves instance IDs to the db.
DescribeSpotRequestsAndSave(context.Context, []*host.Host) (*ec2.DescribeSpotInstanceRequestsOutput, error)
// GetSpotInstanceId returns the instance ID if already saved, otherwise looks it up.
GetSpotInstanceId(context.Context, *host.Host) (string, error)
// CancelSpotInstanceRequests is a wrapper for ec2.CancelSpotInstanceRequests.
CancelSpotInstanceRequests(context.Context, *ec2.CancelSpotInstanceRequestsInput) (*ec2.CancelSpotInstanceRequestsOutput, error)
// CreateVolume is a wrapper for ec2.CreateVolume.
CreateVolume(context.Context, *ec2.CreateVolumeInput) (*ec2.Volume, error)
// DeleteVolume is a wrapper for ec2.DeleteWrapper.
DeleteVolume(context.Context, *ec2.DeleteVolumeInput) (*ec2.DeleteVolumeOutput, error)
// AttachVolume is a wrapper for ec2.AttachVolume. Generates device name on error if applicable.
AttachVolume(context.Context, *ec2.AttachVolumeInput, generateDeviceNameOptions) (*ec2.VolumeAttachment, error)
// DetachVolume is a wrapper for ec2.DetachVolume.
DetachVolume(context.Context, *ec2.DetachVolumeInput) (*ec2.VolumeAttachment, error)
// ModifyVolume is a wrapper for ec2.ModifyVolume.
ModifyVolume(context.Context, *ec2.ModifyVolumeInput) (*ec2.ModifyVolumeOutput, error)
// DescribeVolumes is a wrapper for ec2.DescribeVolumes.
DescribeVolumes(context.Context, *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error)
// DescribeSpotPriceHistory is a wrapper for ec2.DescribeSpotPriceHistory.
DescribeSpotPriceHistory(context.Context, *ec2.DescribeSpotPriceHistoryInput) (*ec2.DescribeSpotPriceHistoryOutput, error)
// DescribeSubnets is a wrapper for ec2.DescribeSubnets.
DescribeSubnets(context.Context, *ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error)
// DescribeVpcs is a wrapper for ec2.DescribeVpcs.
DescribeVpcs(context.Context, *ec2.DescribeVpcsInput) (*ec2.DescribeVpcsOutput, error)
// GetInstanceInfo returns info about an ec2 instance.
GetInstanceInfo(context.Context, string) (*ec2.Instance, error)
// CreateKeyPair is a wrapper for ec2.CreateKeyPairWithContext.
CreateKeyPair(context.Context, *ec2.CreateKeyPairInput) (*ec2.CreateKeyPairOutput, error)
// ImportKeyPair is a wrapper for ec2.ImportKeyPairWithContext.
ImportKeyPair(context.Context, *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error)
// DeleteKeyPair is a wrapper for ec2.DeleteKeyPairWithContext.
DeleteKeyPair(context.Context, *ec2.DeleteKeyPairInput) (*ec2.DeleteKeyPairOutput, error)
// GetProducts is a wrapper for pricing.GetProducts.
GetProducts(context.Context, *pricing.GetProductsInput) (*pricing.GetProductsOutput, error)
// CreateLaunchTemplate is a wrapper for ec2.CreateLaunchTemplateWithContext
CreateLaunchTemplate(context.Context, *ec2.CreateLaunchTemplateInput) (*ec2.CreateLaunchTemplateOutput, error)
// DeleteLaunchTemplate is a wrapper for ec2.DeleteLaunchTemplateWithContext
DeleteLaunchTemplate(context.Context, *ec2.DeleteLaunchTemplateInput) (*ec2.DeleteLaunchTemplateOutput, error)
// CreateFleet is a wrapper for ec2.CreateFleetWithContext
CreateFleet(context.Context, *ec2.CreateFleetInput) (*ec2.CreateFleetOutput, error)
GetKey(context.Context, *host.Host) (string, error)
SetTags(context.Context, []string, *host.Host) error
GetInstanceBlockDevices(context.Context, *host.Host) ([]*ec2.InstanceBlockDeviceMapping, error)
GetVolumeIDs(context.Context, *host.Host) ([]string, error)
GetPublicDNSName(ctx context.Context, h *host.Host) (string, error)
}
// awsClientImpl wraps ec2.EC2.
type awsClientImpl struct { //nolint
session *session.Session
httpClient *http.Client
pricing *pricing.Pricing
*ec2.EC2
}
type generateDeviceNameOptions struct {
isWindows bool
existingDeviceNames []string
}
const (
awsClientImplAttempts = 9
awsClientImplStartPeriod = time.Second
)
func awsClientDefaultRetryOptions() utility.RetryOptions {
return utility.RetryOptions{
MaxAttempts: awsClientImplAttempts,
MinDelay: awsClientImplStartPeriod,
}
}
// Create a new aws-sdk-client if one does not exist, otherwise no-op.
func (c *awsClientImpl) Create(creds *credentials.Credentials, region string) error {
if creds == nil {
return errors.New("creds must not be nil")
}
if region == "" {
return errors.New("region must not be empty")
}
if c.session == nil {
c.httpClient = utility.GetHTTPClient()
s, err := session.NewSession(&aws.Config{
HTTPClient: c.httpClient,
Region: aws.String(region),
Credentials: creds,
})
if err != nil {
return errors.Wrap(err, "error creating session")
}
c.session = s
}
c.EC2 = ec2.New(c.session)
c.pricing = pricing.New(c.session)
return nil
}
func (c *awsClientImpl) Close() {
if c.httpClient != nil {
utility.PutHTTPClient(c.httpClient)
c.httpClient = nil
}
}
// RunInstances is a wrapper for ec2.RunInstances.
func (c *awsClientImpl) RunInstances(ctx context.Context, input *ec2.RunInstancesInput) (*ec2.Reservation, error) {
var output *ec2.Reservation
var err error
msg := makeAWSLogMessage("RunInstances", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.RunInstancesWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
if strings.Contains(ec2err.Code(), EC2InsufficientCapacity) {
return false, EC2InsufficientCapacityError
}
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DescribeInstances is a wrapper for ec2.DescribeInstances
func (c *awsClientImpl) DescribeInstances(ctx context.Context, input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
var output *ec2.DescribeInstancesOutput
var err error
msg := makeAWSLogMessage("DescribeInstances", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DescribeInstancesWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
func (c *awsClientImpl) ModifyInstanceAttribute(ctx context.Context, input *ec2.ModifyInstanceAttributeInput) (*ec2.ModifyInstanceAttributeOutput, error) {
var output *ec2.ModifyInstanceAttributeOutput
var err error
msg := makeAWSLogMessage("ModifyInstanceAttribute", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.ModifyInstanceAttributeWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
func (c *awsClientImpl) DescribeInstanceTypeOfferings(ctx context.Context, input *ec2.DescribeInstanceTypeOfferingsInput) (*ec2.DescribeInstanceTypeOfferingsOutput, error) {
var output *ec2.DescribeInstanceTypeOfferingsOutput
var err error
msg := makeAWSLogMessage("DescribeInstanceTypeOfferings", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DescribeInstanceTypeOfferingsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// CreateTags is a wrapper for ec2.CreateTags.
func (c *awsClientImpl) CreateTags(ctx context.Context, input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
var output *ec2.CreateTagsOutput
var err error
msg := makeAWSLogMessage("CreateTags", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.CreateTagsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DeleteTags is a wrapper for ec2.DeleteTags.
func (c *awsClientImpl) DeleteTags(ctx context.Context, input *ec2.DeleteTagsInput) (*ec2.DeleteTagsOutput, error) {
var output *ec2.DeleteTagsOutput
var err error
msg := makeAWSLogMessage("DeleteTags", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DeleteTagsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// TerminateInstances is a wrapper for ec2.TerminateInstances.
func (c *awsClientImpl) TerminateInstances(ctx context.Context, input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
var output *ec2.TerminateInstancesOutput
var err error
msg := makeAWSLogMessage("TerminateInstances", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.TerminateInstancesWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
if strings.Contains(ec2err.Code(), EC2ErrorNotFound) {
grip.Debug(message.WrapError(ec2err, message.Fields{
"client": fmt.Sprintf("%T", c),
"message": "instance ID not found in AWS",
"args": input,
"ec2_err_message": ec2err.Message(),
"ec2_err_code": ec2err.Code(),
}))
return false, nil
}
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// StopInstances is a wrapper for ec2.StopInstances.
func (c *awsClientImpl) StopInstances(ctx context.Context, input *ec2.StopInstancesInput) (*ec2.StopInstancesOutput, error) {
var output *ec2.StopInstancesOutput
var err error
msg := makeAWSLogMessage("StopInstances", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.StopInstancesWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// StartInstances is a wrapper for ec2.StartInstances.
func (c *awsClientImpl) StartInstances(ctx context.Context, input *ec2.StartInstancesInput) (*ec2.StartInstancesOutput, error) {
var output *ec2.StartInstancesOutput
var err error
msg := makeAWSLogMessage("StartInstances", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.StartInstancesWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// RequestSpotInstances is a wrapper for ec2.RequestSpotInstances.
func (c *awsClientImpl) RequestSpotInstances(ctx context.Context, input *ec2.RequestSpotInstancesInput) (*ec2.RequestSpotInstancesOutput, error) {
var output *ec2.RequestSpotInstancesOutput
var err error
msg := makeAWSLogMessage("RequestSpotInstances", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.RequestSpotInstancesWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DescribeSpotInstanceRequests is a wrapper for ec2.DescribeSpotInstanceRequests.
func (c *awsClientImpl) DescribeSpotInstanceRequests(ctx context.Context, input *ec2.DescribeSpotInstanceRequestsInput) (*ec2.DescribeSpotInstanceRequestsOutput, error) {
var output *ec2.DescribeSpotInstanceRequestsOutput
var err error
msg := makeAWSLogMessage("DescribeSpotInstanceRequests", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DescribeSpotInstanceRequestsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
func (c *awsClientImpl) DescribeSpotRequestsAndSave(ctx context.Context, hosts []*host.Host) (*ec2.DescribeSpotInstanceRequestsOutput, error) {
spotRequestIds := []*string{}
for idx := range hosts {
h := hosts[idx]
if h == nil {
return nil, errors.New("unable to describe spot request for nil host")
}
spotRequestIds = append(spotRequestIds, aws.String(h.Id))
}
apiInput := &ec2.DescribeSpotInstanceRequestsInput{
SpotInstanceRequestIds: spotRequestIds,
}
instances, err := c.DescribeSpotInstanceRequests(ctx, apiInput)
if err != nil {
return nil, errors.Wrap(err, "error describing spot requests")
}
catcher := grip.NewSimpleCatcher()
for idx := range hosts {
h := hosts[idx]
for idx := range instances.SpotInstanceRequests {
instance := instances.SpotInstanceRequests[idx]
if instance.SpotInstanceRequestId != nil && *instance.SpotInstanceRequestId == h.Id {
if instance.InstanceId != nil {
h.ExternalIdentifier = *instance.InstanceId
}
}
}
if h.ExternalIdentifier != "" {
catcher.Add(h.SetExtId())
}
}
return instances, catcher.Resolve()
}
func (c *awsClientImpl) GetSpotInstanceId(ctx context.Context, h *host.Host) (string, error) {
if h == nil {
return "", errors.New("unable to get spot instance for nil host")
}
if h.ExternalIdentifier != "" {
return h.ExternalIdentifier, nil
}
_, err := c.DescribeSpotRequestsAndSave(ctx, []*host.Host{h})
if err != nil {
return "", err
}
return h.ExternalIdentifier, nil
}
// CancelSpotInstanceRequests is a wrapper for ec2.CancelSpotInstanceRequests.
func (c *awsClientImpl) CancelSpotInstanceRequests(ctx context.Context, input *ec2.CancelSpotInstanceRequestsInput) (*ec2.CancelSpotInstanceRequestsOutput, error) {
var output *ec2.CancelSpotInstanceRequestsOutput
var err error
msg := makeAWSLogMessage("CancelSpotInstanceRequests", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.CancelSpotInstanceRequestsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Error(message.WrapError(ec2err, msg))
if ec2err.Code() == EC2ErrorSpotRequestNotFound {
return false, err
}
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// CreateVolume is a wrapper for ec2.CreateVolume.
func (c *awsClientImpl) CreateVolume(ctx context.Context, input *ec2.CreateVolumeInput) (*ec2.Volume, error) {
var output *ec2.Volume
var err error
msg := makeAWSLogMessage("CreateVolume", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.CreateVolumeWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
if strings.Contains(ec2err.Error(), EC2InvalidParam) {
return false, err
}
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DeleteVolume is a wrapper for ec2.DeleteWrapper.
func (c *awsClientImpl) DeleteVolume(ctx context.Context, input *ec2.DeleteVolumeInput) (*ec2.DeleteVolumeOutput, error) {
var output *ec2.DeleteVolumeOutput
var err error
msg := makeAWSLogMessage("DeleteVolume", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DeleteVolumeWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
if strings.Contains(ec2err.Error(), EC2VolumeNotFound) {
return false, nil
}
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// ModifyVolume is a wrapper for ec2.ModifyWrapper.
func (c *awsClientImpl) ModifyVolume(ctx context.Context, input *ec2.ModifyVolumeInput) (*ec2.ModifyVolumeOutput, error) {
var output *ec2.ModifyVolumeOutput
var err error
msg := makeAWSLogMessage("ModifyVolume", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.ModifyVolumeWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
if ModifyVolumeBadRequest(ec2err) {
return false, err
}
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// AttachVolume is a wrapper for ec2.AttachVolume.
func (c *awsClientImpl) AttachVolume(ctx context.Context, input *ec2.AttachVolumeInput, opts generateDeviceNameOptions) (*ec2.VolumeAttachment, error) {
var output *ec2.VolumeAttachment
var err error
msg := makeAWSLogMessage("AttachVolume", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.AttachVolumeWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
if AttachVolumeBadRequest(ec2err) {
return false, err
}
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DetachVolume is a wrapper for ec2.DetachVolume.
func (c *awsClientImpl) DetachVolume(ctx context.Context, input *ec2.DetachVolumeInput) (*ec2.VolumeAttachment, error) {
var output *ec2.VolumeAttachment
var err error
msg := makeAWSLogMessage("DetachVolume", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DetachVolumeWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DescribeVolumes is a wrapper for ec2.DescribeVolumes.
func (c *awsClientImpl) DescribeVolumes(ctx context.Context, input *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {
var output *ec2.DescribeVolumesOutput
var err error
msg := makeAWSLogMessage("DescribeVolumes", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DescribeVolumesWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DescribeSpotPriceHistory is a wrapper for ec2.DescribeSpotPriceHistory.
func (c *awsClientImpl) DescribeSpotPriceHistory(ctx context.Context, input *ec2.DescribeSpotPriceHistoryInput) (*ec2.DescribeSpotPriceHistoryOutput, error) {
var output *ec2.DescribeSpotPriceHistoryOutput
var err error
msg := makeAWSLogMessage("DescribeSpotPriceHistory", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DescribeSpotPriceHistoryWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DescribeSubnets is a wrapper for ec2.DescribeSubnets.
func (c *awsClientImpl) DescribeSubnets(ctx context.Context, input *ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) {
var output *ec2.DescribeSubnetsOutput
var err error
msg := makeAWSLogMessage("DescribeSubnets", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DescribeSubnetsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DescribeVpcs is a wrapper for ec2.DescribeVpcs.
func (c *awsClientImpl) DescribeVpcs(ctx context.Context, input *ec2.DescribeVpcsInput) (*ec2.DescribeVpcsOutput, error) {
var output *ec2.DescribeVpcsOutput
var err error
msg := makeAWSLogMessage("DescribeVpcs", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DescribeVpcsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
func (c *awsClientImpl) GetInstanceInfo(ctx context.Context, id string) (*ec2.Instance, error) {
if strings.HasPrefix(id, "sir") {
return nil, errors.Errorf("id appears to be a spot instance request ID, not a host ID (%s)", id)
}
if host.IsIntentHostId(id) {
return nil, errors.Errorf("host ID '%s' is for an intent host", id)
}
resp, err := c.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(id)},
})
if err != nil {
return nil, errors.Wrap(err, "EC2 API returned error for DescribeInstances")
}
reservation := resp.Reservations
if len(reservation) == 0 {
return nil, noReservationError
}
instances := reservation[0].Instances
if len(instances) == 0 {
err = errors.Errorf("'%s' was not found in reservation '%s'",
id, *resp.Reservations[0].ReservationId)
return nil, err
}
return instances[0], nil
}
// CreateKeyPair is a wrapper for ec2.CreateKeyPair.
func (c *awsClientImpl) CreateKeyPair(ctx context.Context, input *ec2.CreateKeyPairInput) (*ec2.CreateKeyPairOutput, error) {
var output *ec2.CreateKeyPairOutput
var err error
msg := makeAWSLogMessage("CreateKeyPair", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.CreateKeyPairWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
func (c *awsClientImpl) ImportKeyPair(ctx context.Context, input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
var output *ec2.ImportKeyPairOutput
var err error
msg := makeAWSLogMessage("ImportKeyPair", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx, func() (bool, error) {
output, err = c.EC2.ImportKeyPairWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
// Don't retry if the key already exists
if ec2err.Code() == EC2DuplicateKeyPair {
grip.Info(msg)
return false, ec2err
}
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DeleteKeyPair is a wrapper for ec2.DeleteKeyPair.
func (c *awsClientImpl) DeleteKeyPair(ctx context.Context, input *ec2.DeleteKeyPairInput) (*ec2.DeleteKeyPairOutput, error) {
var output *ec2.DeleteKeyPairOutput
var err error
msg := makeAWSLogMessage("DeleteKeyPair", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DeleteKeyPairWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// GetProducts is a wrapper for pricing.GetProducts.
func (c *awsClientImpl) GetProducts(ctx context.Context, input *pricing.GetProductsInput) (*pricing.GetProductsOutput, error) {
var output *pricing.GetProductsOutput
var err error
msg := makeAWSLogMessage("GetProducts", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.pricing.GetProductsWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// CreateLaunchTemplate is a wrapper for ec2.CreateLaunchTemplateWithContext
func (c *awsClientImpl) CreateLaunchTemplate(ctx context.Context, input *ec2.CreateLaunchTemplateInput) (*ec2.CreateLaunchTemplateOutput, error) {
var output *ec2.CreateLaunchTemplateOutput
var err error
msg := makeAWSLogMessage("CreateLaunchTemplate", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.CreateLaunchTemplateWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// DeleteLaunchTemplate is a wrapper for ec2.DeleteLaunchTemplateWithContext
func (c *awsClientImpl) DeleteLaunchTemplate(ctx context.Context, input *ec2.DeleteLaunchTemplateInput) (*ec2.DeleteLaunchTemplateOutput, error) {
var output *ec2.DeleteLaunchTemplateOutput
var err error
msg := makeAWSLogMessage("DeleteLaunchTemplate", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.DeleteLaunchTemplateWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())
if err != nil {
return nil, err
}
return output, nil
}
// CreateFleet is a wrapper for ec2.CreateFleetWithContext
func (c *awsClientImpl) CreateFleet(ctx context.Context, input *ec2.CreateFleetInput) (*ec2.CreateFleetOutput, error) {
var output *ec2.CreateFleetOutput
var err error
msg := makeAWSLogMessage("CreateFleet", fmt.Sprintf("%T", c), input)
err = utility.Retry(
ctx,
func() (bool, error) {
output, err = c.EC2.CreateFleetWithContext(ctx, input)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
if strings.Contains(ec2err.Code(), EC2InsufficientCapacity) {
return false, err
}
grip.Debug(message.WrapError(ec2err, msg))
}
return true, err
}
// CreateFleetWithContext has oddball behavior. If there is a
// RequestLimitExceeded error while using CreateFleet in instant mode,
// usually (but not always!) EC2.CreateFleetWithContext will not return an
// error. Instead it will populate the output.Error array with a single
// item describing the error, using an error type that does _not_
// implement awserr.Error. We therefore have to check this case in addition
// to the standard `err != nil` case above.
if !ec2CreateFleetResponseContainsInstance(output) {
if len(output.Errors) > 0 {
grip.Debug(message.WrapError(errors.New(output.Errors[0].String()), msg))
return true, errors.Errorf("Got error in CreateFleet response: %s", output.Errors[0].String())
}
grip.Error(message.WrapError(errors.New("No instance ID and no error in CreateFleet response"), msg))
// This condition is unexpected, so do not retry.
return false, errors.New("No instance ID no error in create fleet response")
}
grip.Info(msg)
return false, nil
}, awsClientDefaultRetryOptions())