2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.base.fs.volume import Volume
|
|
|
|
from bootstrapvz.base.fs.exceptions import VolumeError
|
2013-09-15 13:19:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EBSVolume(Volume):
|
|
|
|
|
2018-03-15 17:46:33 +02:00
|
|
|
def create(self, conn, zone, tags=[], encrypted=False, kms_key_id=None):
|
|
|
|
self.fsm.create(connection=conn, zone=zone, tags=tags, encrypted=encrypted, kms_key_id=kms_key_id)
|
2016-06-04 11:35:59 +02:00
|
|
|
|
|
|
|
def _before_create(self, e):
|
2016-11-12 19:58:51 +00:00
|
|
|
self.conn = e.connection
|
2016-06-04 11:35:59 +02:00
|
|
|
zone = e.zone
|
2018-01-14 23:49:36 +02:00
|
|
|
tags = e.tags
|
2016-06-04 11:35:59 +02:00
|
|
|
size = self.size.bytes.get_qty_in('GiB')
|
2018-03-15 17:46:33 +02:00
|
|
|
|
|
|
|
params = dict(Size=size,
|
|
|
|
AvailabilityZone=zone,
|
|
|
|
VolumeType='gp2',
|
|
|
|
TagSpecifications=[{'ResourceType': 'volume', 'Tags': tags}],
|
|
|
|
Encrypted=e.encrypted)
|
|
|
|
|
|
|
|
if e.encrypted and e.kms_key_id:
|
|
|
|
params['KmsKeyId'] = e.kms_key_id
|
|
|
|
|
|
|
|
self.volume = self.conn.create_volume(**params)
|
|
|
|
|
2016-11-12 19:58:51 +00:00
|
|
|
self.vol_id = self.volume['VolumeId']
|
|
|
|
waiter = self.conn.get_waiter('volume_available')
|
|
|
|
waiter.wait(VolumeIds=[self.vol_id],
|
|
|
|
Filters=[{'Name': 'status', 'Values': ['available']}])
|
2016-06-04 11:35:59 +02:00
|
|
|
|
|
|
|
def attach(self, instance_id):
|
|
|
|
self.fsm.attach(instance_id=instance_id)
|
|
|
|
|
|
|
|
def _before_attach(self, e):
|
|
|
|
import os.path
|
|
|
|
import string
|
2016-11-12 19:58:51 +00:00
|
|
|
|
|
|
|
self.instance_id = e.instance_id
|
2016-06-04 11:35:59 +02:00
|
|
|
for letter in string.ascii_lowercase[5:]:
|
|
|
|
dev_path = os.path.join('/dev', 'xvd' + letter)
|
|
|
|
if not os.path.exists(dev_path):
|
|
|
|
self.device_path = dev_path
|
|
|
|
self.ec2_device_path = os.path.join('/dev', 'sd' + letter)
|
|
|
|
break
|
|
|
|
|
|
|
|
if self.device_path is None:
|
|
|
|
raise VolumeError('Unable to find a free block device path for mounting the bootstrap volume')
|
|
|
|
|
2016-11-12 19:58:51 +00:00
|
|
|
self.conn.attach_volume(VolumeId=self.vol_id,
|
|
|
|
InstanceId=self.instance_id,
|
|
|
|
Device=self.ec2_device_path)
|
|
|
|
waiter = self.conn.get_waiter('volume_in_use')
|
|
|
|
waiter.wait(VolumeIds=[self.vol_id],
|
|
|
|
Filters=[{'Name': 'attachment.status', 'Values': ['attached']}])
|
2016-06-04 11:35:59 +02:00
|
|
|
|
|
|
|
def _before_detach(self, e):
|
2016-11-12 19:58:51 +00:00
|
|
|
self.conn.detach_volume(VolumeId=self.vol_id,
|
|
|
|
InstanceId=self.instance_id,
|
|
|
|
Device=self.ec2_device_path)
|
|
|
|
waiter = self.conn.get_waiter('volume_available')
|
|
|
|
waiter.wait(VolumeIds=[self.vol_id],
|
|
|
|
Filters=[{'Name': 'status', 'Values': ['available']}])
|
2016-06-04 11:35:59 +02:00
|
|
|
del self.ec2_device_path
|
|
|
|
self.device_path = None
|
|
|
|
|
|
|
|
def _before_delete(self, e):
|
2016-11-12 19:58:51 +00:00
|
|
|
self.conn.delete_volume(VolumeId=self.vol_id)
|
2016-06-04 11:35:59 +02:00
|
|
|
|
|
|
|
def snapshot(self):
|
2016-11-12 19:58:51 +00:00
|
|
|
snapshot = self.conn.create_snapshot(VolumeId=self.vol_id)
|
|
|
|
self.snap_id = snapshot['SnapshotId']
|
|
|
|
waiter = self.conn.get_waiter('snapshot_completed')
|
|
|
|
waiter.wait(SnapshotIds=[self.snap_id],
|
2018-06-14 10:16:22 -05:00
|
|
|
Filters=[{'Name': 'status', 'Values': ['completed']}],
|
|
|
|
WaiterConfig={'Delay': 15, 'MaxAttempts': 120})
|
2016-11-12 19:58:51 +00:00
|
|
|
return self.snap_id
|