def attach(self):
ec2 = boto.connect_ec2()
if self.logical_volume_name:
# if a logical volume was specified, override the specified volume_id
# (if there was one) with the current AWS volume for the logical volume:
logical_volume = next(Volume.find(name=self.logical_volume_name))
self.volume_id = logical_volume._volume_id
volume = ec2.get_all_volumes([self.volume_id])[0]
# wait for the volume to be available. The volume may still be being created
# from a snapshot.
while volume.update() != 'available':
boto.log.info('Volume %s not yet available. Current status = %s.' % (volume.id, volume.status))
time.sleep(5)
instance = ec2.get_only_instances([self.instance_id])[0]
attempt_attach = True
while attempt_attach:
try:
ec2.attach_volume(self.volume_id, self.instance_id, self.device)
attempt_attach = False
except EC2ResponseError as e:
if e.error_code != 'IncorrectState':
# if there's an EC2ResonseError with the code set to IncorrectState, delay a bit for ec2
# to realize the instance is running, then try again. Otherwise, raise the error:
boto.log.info('Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.' % (self.volume_id, self.instance_id, e.errors))
time.sleep(2)
else:
raise e
boto.log.info('Attached volume %s to instance %s as device %s' % (self.volume_id, self.instance_id, self.device))
# now wait for the volume device to appear
while not os.path.exists(self.device):
boto.log.info('%s still does not exist, waiting 2 seconds' % self.device)
time.sleep(2)
评论列表
文章目录