-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebs.go
942 lines (853 loc) · 27.4 KB
/
ebs.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package ec2
import (
"regexp"
"sync"
"time"
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/utils"
"github.com/juju/utils/set"
"gopkg.in/amz.v3/ec2"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs/tags"
"github.com/juju/juju/instance"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/storage"
)
const (
EBS_ProviderType = storage.ProviderType("ebs")
// Config attributes
// The volume type (default standard):
// "gp2" for General Purpose (SSD) volumes
// "io1" for Provisioned IOPS (SSD) volumes,
// "standard" for Magnetic volumes.
EBS_VolumeType = "volume-type"
// The number of I/O operations per second (IOPS) per GiB
// to provision for the volume. Only valid for Provisioned
// IOPS (SSD) volumes.
EBS_IOPS = "iops"
// Specifies whether the volume should be encrypted.
EBS_Encrypted = "encrypted"
volumeTypeMagnetic = "magnetic" // standard
volumeTypeSSD = "ssd" // gp2
volumeTypeProvisionedIops = "provisioned-iops" // io1
volumeTypeStandard = "standard"
volumeTypeGP2 = "gp2"
volumeTypeIO1 = "io1"
rootDiskDeviceName = "/dev/sda1"
// defaultControllerDiskSizeMiB is the default size for the
// root disk of controller machines, if no root-disk constraint
// is specified.
defaultControllerDiskSizeMiB = 32 * 1024
)
// AWS error codes
const (
deviceInUse = "InvalidDevice.InUse"
attachmentNotFound = "InvalidAttachment.NotFound"
volumeNotFound = "InvalidVolume.NotFound"
)
const (
volumeStatusAvailable = "available"
volumeStatusInUse = "in-use"
volumeStatusCreating = "creating"
attachmentStatusAttaching = "attaching"
attachmentStatusAttached = "attached"
instanceStateShuttingDown = "shutting-down"
instanceStateTerminated = "terminated"
)
// Limits for volume parameters. See:
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
const (
// minMagneticVolumeSizeGiB is the minimum size for magnetic volumes in GiB.
minMagneticVolumeSizeGiB = 1
// maxMagneticVolumeSizeGiB is the maximum size for magnetic volumes in GiB.
maxMagneticVolumeSizeGiB = 1024
// minSSDVolumeSizeGiB is the minimum size for SSD volumes in GiB.
minSSDVolumeSizeGiB = 1
// maxSSDVolumeSizeGiB is the maximum size for SSD volumes in GiB.
maxSSDVolumeSizeGiB = 16 * 1024
// minProvisionedIopsVolumeSizeGiB is the minimum size of provisioned IOPS
// volumes in GiB.
minProvisionedIopsVolumeSizeGiB = 4
// maxProvisionedIopsVolumeSizeGiB is the maximum size of provisioned IOPS
// volumes in GiB.
maxProvisionedIopsVolumeSizeGiB = 16 * 1024
// maxProvisionedIopsSizeRatio is the maximum allowed ratio of IOPS to
// size (in GiB), for provisioend IOPS volumes.
maxProvisionedIopsSizeRatio = 30
// maxProvisionedIops is the maximum allowed IOPS in total for provisioned IOPS
// volumes. We take the minimum of volumeSize*maxProvisionedIopsSizeRatio and
// maxProvisionedIops.
maxProvisionedIops = 20000
)
const (
// devicePrefix is the prefix for device names specified when creating volumes.
devicePrefix = "/dev/sd"
// renamedDevicePrefix is the prefix for device names after they have
// been renamed. This should replace "devicePrefix" in the device name
// when recording the block device info in state.
renamedDevicePrefix = "xvd"
)
var deviceInUseRegexp = regexp.MustCompile(".*Attachment point .* is already in use")
// StorageProviderTypes implements storage.ProviderRegistry.
func (env *environ) StorageProviderTypes() ([]storage.ProviderType, error) {
return []storage.ProviderType{EBS_ProviderType}, nil
}
// StorageProvider implements storage.ProviderRegistry.
func (env *environ) StorageProvider(t storage.ProviderType) (storage.Provider, error) {
if t == EBS_ProviderType {
return &ebsProvider{env}, nil
}
return nil, errors.NotFoundf("storage provider %q", t)
}
// ebsProvider creates volume sources which use AWS EBS volumes.
type ebsProvider struct {
env *environ
}
var _ storage.Provider = (*ebsProvider)(nil)
var ebsConfigFields = schema.Fields{
EBS_VolumeType: schema.OneOf(
schema.Const(volumeTypeMagnetic),
schema.Const(volumeTypeSSD),
schema.Const(volumeTypeProvisionedIops),
schema.Const(volumeTypeStandard),
schema.Const(volumeTypeGP2),
schema.Const(volumeTypeIO1),
),
EBS_IOPS: schema.ForceInt(),
EBS_Encrypted: schema.Bool(),
}
var ebsConfigChecker = schema.FieldMap(
ebsConfigFields,
schema.Defaults{
EBS_VolumeType: volumeTypeMagnetic,
EBS_IOPS: schema.Omit,
EBS_Encrypted: false,
},
)
type ebsConfig struct {
volumeType string
iops int
encrypted bool
}
func newEbsConfig(attrs map[string]interface{}) (*ebsConfig, error) {
out, err := ebsConfigChecker.Coerce(attrs, nil)
if err != nil {
return nil, errors.Annotate(err, "validating EBS storage config")
}
coerced := out.(map[string]interface{})
iops, _ := coerced[EBS_IOPS].(int)
volumeType := coerced[EBS_VolumeType].(string)
ebsConfig := &ebsConfig{
volumeType: volumeType,
iops: iops,
encrypted: coerced[EBS_Encrypted].(bool),
}
switch ebsConfig.volumeType {
case volumeTypeMagnetic:
ebsConfig.volumeType = volumeTypeStandard
case volumeTypeSSD:
ebsConfig.volumeType = volumeTypeGP2
case volumeTypeProvisionedIops:
ebsConfig.volumeType = volumeTypeIO1
}
if ebsConfig.iops > 0 && ebsConfig.volumeType != volumeTypeIO1 {
return nil, errors.Errorf("IOPS specified, but volume type is %q", volumeType)
} else if ebsConfig.iops == 0 && ebsConfig.volumeType == volumeTypeIO1 {
return nil, errors.Errorf("volume type is %q, IOPS unspecified or zero", volumeTypeIO1)
}
return ebsConfig, nil
}
// ValidateConfig is defined on the Provider interface.
func (e *ebsProvider) ValidateConfig(cfg *storage.Config) error {
_, err := newEbsConfig(cfg.Attrs())
return errors.Trace(err)
}
// Supports is defined on the Provider interface.
func (e *ebsProvider) Supports(k storage.StorageKind) bool {
return k == storage.StorageKindBlock
}
// Scope is defined on the Provider interface.
func (e *ebsProvider) Scope() storage.Scope {
return storage.ScopeEnviron
}
// Dynamic is defined on the Provider interface.
func (e *ebsProvider) Dynamic() bool {
return true
}
// DefaultPools is defined on the Provider interface.
func (e *ebsProvider) DefaultPools() []*storage.Config {
ssdPool, _ := storage.NewConfig("ebs-ssd", EBS_ProviderType, map[string]interface{}{
EBS_VolumeType: volumeTypeSSD,
})
return []*storage.Config{ssdPool}
}
// VolumeSource is defined on the Provider interface.
func (e *ebsProvider) VolumeSource(cfg *storage.Config) (storage.VolumeSource, error) {
environConfig := e.env.Config()
source := &ebsVolumeSource{
env: e.env,
envName: environConfig.Name(),
modelUUID: environConfig.UUID(),
}
return source, nil
}
// FilesystemSource is defined on the Provider interface.
func (e *ebsProvider) FilesystemSource(providerConfig *storage.Config) (storage.FilesystemSource, error) {
return nil, errors.NotSupportedf("filesystems")
}
type ebsVolumeSource struct {
env *environ
envName string // non-unique, informational only
modelUUID string
}
var _ storage.VolumeSource = (*ebsVolumeSource)(nil)
// parseVolumeOptions uses storage volume parameters to make a struct used to create volumes.
func parseVolumeOptions(size uint64, attrs map[string]interface{}) (_ ec2.CreateVolume, _ error) {
ebsConfig, err := newEbsConfig(attrs)
if err != nil {
return ec2.CreateVolume{}, errors.Trace(err)
}
if ebsConfig.iops > maxProvisionedIopsSizeRatio {
return ec2.CreateVolume{}, errors.Errorf(
"specified IOPS ratio is %d/GiB, maximum is %d/GiB",
ebsConfig.iops, maxProvisionedIopsSizeRatio,
)
}
sizeInGib := mibToGib(size)
iops := uint64(ebsConfig.iops) * sizeInGib
if iops > maxProvisionedIops {
iops = maxProvisionedIops
}
vol := ec2.CreateVolume{
// Juju size is MiB, AWS size is GiB.
VolumeSize: int(sizeInGib),
VolumeType: ebsConfig.volumeType,
Encrypted: ebsConfig.encrypted,
IOPS: int64(iops),
}
return vol, nil
}
// CreateVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) CreateVolumes(params []storage.VolumeParams) (_ []storage.CreateVolumesResult, err error) {
// First, validate the params before we use them.
results := make([]storage.CreateVolumesResult, len(params))
instanceIds := set.NewStrings()
for i, p := range params {
if err := v.ValidateVolumeParams(p); err != nil {
results[i].Error = err
continue
}
instanceIds.Add(string(p.Attachment.InstanceId))
}
instances := make(instanceCache)
if instanceIds.Size() > 1 {
if err := instances.update(v.env.ec2, instanceIds.Values()...); err != nil {
logger.Debugf("querying running instances: %v", err)
// We ignore the error, because we don't want an invalid
// InstanceId reference from one VolumeParams to prevent
// the creation of another volume.
}
}
for i, p := range params {
if results[i].Error != nil {
continue
}
volume, attachment, err := v.createVolume(p, instances)
if err != nil {
results[i].Error = err
continue
}
results[i].Volume = volume
results[i].VolumeAttachment = attachment
}
return results, nil
}
func (v *ebsVolumeSource) createVolume(p storage.VolumeParams, instances instanceCache) (_ *storage.Volume, _ *storage.VolumeAttachment, err error) {
var volumeId string
defer func() {
if err == nil || volumeId == "" {
return
}
if _, err := v.env.ec2.DeleteVolume(volumeId); err != nil {
logger.Errorf("error cleaning up volume %v: %v", volumeId, err)
}
}()
// TODO(axw) if preference is to use ephemeral, use ephemeral
// until the instance stores run out. We'll need to know how
// many there are and how big each one is. We also need to
// unmap ephemeral0 in cloud-init.
// Create.
instId := string(p.Attachment.InstanceId)
if err := instances.update(v.env.ec2, instId); err != nil {
return nil, nil, errors.Trace(err)
}
inst, err := instances.get(instId)
if err != nil {
// Can't create the volume without the instance,
// because we need to know what its AZ is.
return nil, nil, errors.Trace(err)
}
vol, _ := parseVolumeOptions(p.Size, p.Attributes)
vol.AvailZone = inst.AvailZone
resp, err := v.env.ec2.CreateVolume(vol)
if err != nil {
return nil, nil, errors.Trace(err)
}
volumeId = resp.Id
// Tag.
resourceTags := make(map[string]string)
for k, v := range p.ResourceTags {
resourceTags[k] = v
}
resourceTags[tagName] = resourceName(p.Tag, v.envName)
if err := tagResources(v.env.ec2, resourceTags, volumeId); err != nil {
return nil, nil, errors.Annotate(err, "tagging volume")
}
volume := storage.Volume{
p.Tag,
storage.VolumeInfo{
VolumeId: volumeId,
Size: gibToMib(uint64(resp.Size)),
Persistent: true,
},
}
return &volume, nil, nil
}
// ListVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) ListVolumes() ([]string, error) {
filter := ec2.NewFilter()
filter.Add("tag:"+tags.JujuModel, v.modelUUID)
return listVolumes(v.env.ec2, filter)
}
func listVolumes(client *ec2.EC2, filter *ec2.Filter) ([]string, error) {
resp, err := client.Volumes(nil, filter)
if err != nil {
return nil, err
}
volumeIds := make([]string, 0, len(resp.Volumes))
for _, vol := range resp.Volumes {
var isRootDisk bool
for _, att := range vol.Attachments {
if att.Device == rootDiskDeviceName {
isRootDisk = true
break
}
}
if isRootDisk {
// We don't want to list root disks in the output.
// These are managed by the instance provisioning
// code; they will be created and destroyed with
// instances.
continue
}
volumeIds = append(volumeIds, vol.Id)
}
return volumeIds, nil
}
// DescribeVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) DescribeVolumes(volIds []string) ([]storage.DescribeVolumesResult, error) {
// TODO(axw) invalid volIds here should not cause the whole
// operation to fail. If we get an invalid volume ID response,
// fall back to querying each volume individually. That should
// be rare.
resp, err := v.env.ec2.Volumes(volIds, nil)
if err != nil {
return nil, err
}
byId := make(map[string]ec2.Volume)
for _, vol := range resp.Volumes {
byId[vol.Id] = vol
}
results := make([]storage.DescribeVolumesResult, len(volIds))
for i, volId := range volIds {
vol, ok := byId[volId]
if !ok {
results[i].Error = errors.NotFoundf("%s", volId)
continue
}
results[i].VolumeInfo = &storage.VolumeInfo{
Size: gibToMib(uint64(vol.Size)),
VolumeId: vol.Id,
Persistent: true,
}
for _, attachment := range vol.Attachments {
if attachment.DeleteOnTermination {
results[i].VolumeInfo.Persistent = false
break
}
}
}
return results, nil
}
// DestroyVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) DestroyVolumes(volIds []string) ([]error, error) {
return destroyVolumes(v.env.ec2, volIds), nil
}
func destroyVolumes(client *ec2.EC2, volIds []string) []error {
var wg sync.WaitGroup
wg.Add(len(volIds))
results := make([]error, len(volIds))
for i, volumeId := range volIds {
go func(i int, volumeId string) {
defer wg.Done()
results[i] = destroyVolume(client, volumeId)
}(i, volumeId)
}
wg.Wait()
return results
}
var destroyVolumeAttempt = utils.AttemptStrategy{
Total: 5 * time.Minute,
Delay: 5 * time.Second,
}
func destroyVolume(client *ec2.EC2, volumeId string) (err error) {
defer func() {
if err != nil {
if ec2ErrCode(err) == volumeNotFound || errors.IsNotFound(err) {
// Either the volume isn't found, or we queried the
// instance corresponding to a DeleteOnTermination
// attachment; in either case, the volume is or will
// be destroyed.
logger.Tracef("Ignoring error destroying volume %q: %v", volumeId, err)
err = nil
}
}
}()
logger.Debugf("destroying %q", volumeId)
// Volumes must not be in-use when destroying. A volume may
// still be in-use when the instance it is attached to is
// in the process of being terminated.
volume, err := waitVolume(client, volumeId, destroyVolumeAttempt, func(volume *ec2.Volume) (bool, error) {
if volume.Status != volumeStatusInUse {
// Volume is not in use, it should be OK to destroy now.
return true, nil
}
if len(volume.Attachments) == 0 {
// There are no attachments remaining now; keep querying
// until volume transitions out of in-use.
return false, nil
}
var deleteOnTermination []string
var args []storage.VolumeAttachmentParams
for _, a := range volume.Attachments {
switch a.Status {
case attachmentStatusAttaching, attachmentStatusAttached:
// The volume is attaching or attached to an
// instance, we need for it to be detached
// before we can destroy it.
args = append(args, storage.VolumeAttachmentParams{
AttachmentParams: storage.AttachmentParams{
InstanceId: instance.Id(a.InstanceId),
},
VolumeId: volumeId,
})
if a.DeleteOnTermination {
// The volume is still attached, and the
// attachment is "delete on termination";
// check if the related instance is being
// terminated, in which case we can stop
// waiting and skip destroying the volume.
//
// Note: we still accrue in "args" above
// in case the instance is not terminating;
// in that case we detach and destroy as
// usual.
deleteOnTermination = append(
deleteOnTermination, a.InstanceId,
)
}
}
}
if len(deleteOnTermination) > 0 {
result, err := client.Instances(deleteOnTermination, nil)
if err != nil {
return false, errors.Trace(err)
}
for _, reservation := range result.Reservations {
for _, instance := range reservation.Instances {
switch instance.State.Name {
case instanceStateShuttingDown, instanceStateTerminated:
// The instance is or will be terminated,
// and so the volume will be deleted by
// virtue of delete-on-termination.
return true, nil
}
}
}
}
if len(args) == 0 {
return false, nil
}
results, err := detachVolumes(client, args)
if err != nil {
return false, errors.Trace(err)
}
for _, err := range results {
if err != nil {
return false, errors.Trace(err)
}
}
return false, nil
})
if err != nil {
if err == errWaitVolumeTimeout {
return errors.Errorf("timed out waiting for volume %v to not be in-use", volumeId)
}
return errors.Trace(err)
}
if volume.Status == volumeStatusInUse {
// If the volume is in-use, that means it will be
// handled by delete-on-termination and we have
// nothing more to do.
return nil
}
if _, err := client.DeleteVolume(volumeId); err != nil {
return errors.Annotatef(err, "destroying %q", volumeId)
}
return nil
}
// ValidateVolumeParams is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) ValidateVolumeParams(params storage.VolumeParams) error {
vol, err := parseVolumeOptions(params.Size, params.Attributes)
if err != nil {
return err
}
var minVolumeSize, maxVolumeSize int
switch vol.VolumeType {
case volumeTypeStandard:
minVolumeSize = minMagneticVolumeSizeGiB
maxVolumeSize = maxMagneticVolumeSizeGiB
case volumeTypeGP2:
minVolumeSize = minSSDVolumeSizeGiB
maxVolumeSize = maxSSDVolumeSizeGiB
case volumeTypeIO1:
minVolumeSize = minProvisionedIopsVolumeSizeGiB
maxVolumeSize = maxProvisionedIopsVolumeSizeGiB
}
if vol.VolumeSize < minVolumeSize {
return errors.Errorf(
"volume size is %d GiB, must be at least %d GiB",
vol.VolumeSize, minVolumeSize,
)
}
if vol.VolumeSize > maxVolumeSize {
return errors.Errorf(
"volume size %d GiB exceeds the maximum of %d GiB",
vol.VolumeSize, maxVolumeSize,
)
}
return nil
}
// AttachVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) AttachVolumes(attachParams []storage.VolumeAttachmentParams) ([]storage.AttachVolumesResult, error) {
// We need the virtualisation types for each instance we are
// attaching to so we can determine the device name.
instIds := set.NewStrings()
for _, p := range attachParams {
instIds.Add(string(p.InstanceId))
}
instances := make(instanceCache)
if instIds.Size() > 1 {
if err := instances.update(v.env.ec2, instIds.Values()...); err != nil {
logger.Debugf("querying running instances: %v", err)
// We ignore the error, because we don't want an invalid
// InstanceId reference from one VolumeParams to prevent
// the creation of another volume.
}
}
results := make([]storage.AttachVolumesResult, len(attachParams))
for i, params := range attachParams {
instId := string(params.InstanceId)
// By default we should allocate device names without the
// trailing number. Block devices with a trailing number are
// not liked by some applications, e.g. Ceph, which want full
// disks.
//
// TODO(axw) introduce a configuration option if and when
// someone asks for it to enable use of numbers. This option
// must error if used with an "hvm" instance type.
const numbers = false
nextDeviceName := blockDeviceNamer(numbers)
_, deviceName, err := v.attachOneVolume(nextDeviceName, params.VolumeId, instId)
if err != nil {
results[i].Error = err
continue
}
results[i].VolumeAttachment = &storage.VolumeAttachment{
params.Volume,
params.Machine,
storage.VolumeAttachmentInfo{
DeviceName: deviceName,
},
}
}
return results, nil
}
func (v *ebsVolumeSource) attachOneVolume(
nextDeviceName func() (string, string, error),
volumeId, instId string,
) (string, string, error) {
// Wait for the volume to move out of "creating".
volume, err := v.waitVolumeCreated(volumeId)
if err != nil {
return "", "", errors.Trace(err)
}
// Possible statuses:
// creating | available | in-use | deleting | deleted | error
switch volume.Status {
default:
return "", "", errors.Errorf("cannot attach to volume with status %q", volume.Status)
case volumeStatusInUse:
// Volume is already attached; see if it's attached to the
// instance requested.
attachments := volume.Attachments
if len(attachments) != 1 {
return "", "", errors.Annotatef(err, "volume %v has unexpected attachment count: %v", volumeId, len(attachments))
}
if attachments[0].InstanceId != instId {
return "", "", errors.Annotatef(err, "volume %v is attached to %v", volumeId, attachments[0].InstanceId)
}
requestDeviceName := attachments[0].Device
actualDeviceName := renamedDevicePrefix + requestDeviceName[len(devicePrefix):]
return requestDeviceName, actualDeviceName, nil
case volumeStatusAvailable:
// Attempt to attach below.
break
}
for {
requestDeviceName, actualDeviceName, err := nextDeviceName()
if err != nil {
// Can't attach any more volumes.
return "", "", err
}
_, err = v.env.ec2.AttachVolume(volumeId, instId, requestDeviceName)
if ec2Err, ok := err.(*ec2.Error); ok {
switch ec2Err.Code {
case invalidParameterValue:
// InvalidParameterValue is returned by AttachVolume
// rather than InvalidDevice.InUse as the docs would
// suggest.
if !deviceInUseRegexp.MatchString(ec2Err.Message) {
break
}
fallthrough
case deviceInUse:
// deviceInUse means that the requested device name
// is in use already. Try again with the next name.
continue
}
}
if err != nil {
return "", "", errors.Annotate(err, "attaching volume")
}
return requestDeviceName, actualDeviceName, nil
}
}
func (v *ebsVolumeSource) waitVolumeCreated(volumeId string) (*ec2.Volume, error) {
var attempt = utils.AttemptStrategy{
Total: 5 * time.Second,
Delay: 200 * time.Millisecond,
}
var lastStatus string
volume, err := waitVolume(v.env.ec2, volumeId, attempt, func(volume *ec2.Volume) (bool, error) {
lastStatus = volume.Status
return volume.Status != volumeStatusCreating, nil
})
if err == errWaitVolumeTimeout {
return nil, errors.Errorf(
"timed out waiting for volume %v to become available (%v)",
volumeId, lastStatus,
)
} else if err != nil {
return nil, errors.Trace(err)
}
return volume, nil
}
var errWaitVolumeTimeout = errors.New("timed out")
func waitVolume(
client *ec2.EC2,
volumeId string,
attempt utils.AttemptStrategy,
pred func(v *ec2.Volume) (bool, error),
) (*ec2.Volume, error) {
for a := attempt.Start(); a.Next(); {
volume, err := describeVolume(client, volumeId)
if err != nil {
return nil, errors.Trace(err)
}
ok, err := pred(volume)
if err != nil {
return nil, errors.Trace(err)
}
if ok {
return volume, nil
}
}
return nil, errWaitVolumeTimeout
}
func describeVolume(client *ec2.EC2, volumeId string) (*ec2.Volume, error) {
resp, err := client.Volumes([]string{volumeId}, nil)
if err != nil {
return nil, errors.Annotate(err, "querying volume")
}
if len(resp.Volumes) == 0 {
return nil, errors.NotFoundf("%v", volumeId)
} else if len(resp.Volumes) != 1 {
return nil, errors.Errorf("expected one volume, got %d", len(resp.Volumes))
}
return &resp.Volumes[0], nil
}
type instanceCache map[string]ec2.Instance
func (c instanceCache) update(ec2client *ec2.EC2, ids ...string) error {
if len(ids) == 1 {
if _, ok := c[ids[0]]; ok {
return nil
}
}
filter := ec2.NewFilter()
filter.Add("instance-state-name", "running")
resp, err := ec2client.Instances(ids, filter)
if err != nil {
return errors.Annotate(err, "querying instance details")
}
for j := range resp.Reservations {
r := &resp.Reservations[j]
for _, inst := range r.Instances {
c[inst.InstanceId] = inst
}
}
return nil
}
func (c instanceCache) get(id string) (ec2.Instance, error) {
inst, ok := c[id]
if !ok {
return ec2.Instance{}, errors.Errorf("cannot attach to non-running instance %v", id)
}
return inst, nil
}
// DetachVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) DetachVolumes(attachParams []storage.VolumeAttachmentParams) ([]error, error) {
return detachVolumes(v.env.ec2, attachParams)
}
func detachVolumes(client *ec2.EC2, attachParams []storage.VolumeAttachmentParams) ([]error, error) {
results := make([]error, len(attachParams))
for i, params := range attachParams {
_, err := client.DetachVolume(params.VolumeId, string(params.InstanceId), "", false)
// Process aws specific error information.
if err != nil {
if ec2Err, ok := err.(*ec2.Error); ok {
switch ec2Err.Code {
// attachment not found means this volume is already detached.
case attachmentNotFound:
err = nil
}
}
}
if err != nil {
results[i] = errors.Annotatef(
err, "detaching %v from %v", params.Volume, params.Machine,
)
}
}
return results, nil
}
var errTooManyVolumes = errors.New("too many EBS volumes to attach")
// blockDeviceNamer returns a function that cycles through block device names.
//
// The returned function returns the device name that should be used in
// requests to the EC2 API, and and also the (kernel) device name as it
// will appear on the machine.
//
// See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html
func blockDeviceNamer(numbers bool) func() (requestName, actualName string, err error) {
const (
// deviceLetterMin is the first letter to use for EBS block device names.
deviceLetterMin = 'f'
// deviceLetterMax is the last letter to use for EBS block device names.
deviceLetterMax = 'p'
// deviceNumMax is the maximum value for trailing numbers on block device name.
deviceNumMax = 6
)
var n int
letterRepeats := 1
if numbers {
letterRepeats = deviceNumMax
}
return func() (string, string, error) {
letter := deviceLetterMin + (n / letterRepeats)
if letter > deviceLetterMax {
return "", "", errTooManyVolumes
}
deviceName := devicePrefix + string(letter)
if numbers {
deviceName += string('1' + (n % deviceNumMax))
}
n++
realDeviceName := renamedDevicePrefix + deviceName[len(devicePrefix):]
return deviceName, realDeviceName, nil
}
}
func minRootDiskSizeMiB(series string) uint64 {
return gibToMib(common.MinRootDiskSizeGiB(series))
}
// getBlockDeviceMappings translates constraints into BlockDeviceMappings.
//
// The first entry is always the root disk mapping, followed by instance
// stores (ephemeral disks).
func getBlockDeviceMappings(
cons constraints.Value,
series string,
controller bool,
) []ec2.BlockDeviceMapping {
minRootDiskSizeMiB := minRootDiskSizeMiB(series)
rootDiskSizeMiB := minRootDiskSizeMiB
if controller {
rootDiskSizeMiB = defaultControllerDiskSizeMiB
}
if cons.RootDisk != nil {
if *cons.RootDisk >= minRootDiskSizeMiB {
rootDiskSizeMiB = *cons.RootDisk
} else {
logger.Infof(
"Ignoring root-disk constraint of %dM because it is smaller than the minimum size %dM",
*cons.RootDisk,
minRootDiskSizeMiB,
)
}
}
// The first block device is for the root disk.
blockDeviceMappings := []ec2.BlockDeviceMapping{{
DeviceName: rootDiskDeviceName,
VolumeSize: int64(mibToGib(rootDiskSizeMiB)),
}}
// Not all machines have this many instance stores.
// Instances will be started with as many of the
// instance stores as they can support.
blockDeviceMappings = append(blockDeviceMappings, []ec2.BlockDeviceMapping{{
VirtualName: "ephemeral0",
DeviceName: "/dev/sdb",
}, {
VirtualName: "ephemeral1",
DeviceName: "/dev/sdc",
}, {
VirtualName: "ephemeral2",
DeviceName: "/dev/sdd",
}, {
VirtualName: "ephemeral3",
DeviceName: "/dev/sde",
}}...)
return blockDeviceMappings
}
// mibToGib converts mebibytes to gibibytes.
// AWS expects GiB, we work in MiB; round up
// to nearest GiB.
func mibToGib(m uint64) uint64 {
return (m + 1023) / 1024
}
// gibToMib converts gibibytes to mebibytes.
func gibToMib(g uint64) uint64 {
return g * 1024
}