本文整理汇总了Python中boto.exception.EC2ResponseError方法的典型用法代码示例。如果您正苦于以下问题:Python exception.EC2ResponseError方法的具体用法?Python exception.EC2ResponseError怎么用?Python exception.EC2ResponseError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto.exception
的用法示例。
在下文中一共展示了exception.EC2ResponseError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _node_is_booting
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def _node_is_booting(instance):
"""
Check if an instance is still booting, where booting is defined
as either a pending or rebooting instance that is expected to
become running.
:param boto.ec2.instance.Instance instance: The instance to check.
"""
try:
instance.update()
except EC2ResponseError as e:
_check_response_error(
e,
u"flocker:provision:aws:node_is_booting:retry"
)
Message.new(
message_type=u"flocker:provision:aws:node_is_booting:update",
instance_state=instance.state,
ip_address=instance.ip_address,
).write()
# Sometimes an instance can be reported as running but without a public
# address being set, we consider that instance to be still pending.
return (instance.state == u'pending' or instance.state == u'rebooting' or
(instance.state == u'running' and instance.ip_address is None))
示例2: _set_metadata
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def _set_metadata(self, instance, metadata):
"""
Set metadata for an instance.
:param boto.ec2.instance.Instance instance: The instance to configure.
:param dict metadata: The tag-value metadata.
"""
try:
self._connection.create_tags([instance.id], metadata)
return True
except EC2ResponseError as e:
_check_response_error(
e,
u"flocker:provision:aws:set_metadata:retry"
)
return False
示例3: delete
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def delete(self, resource):
if resource.wrapped.state in Instance.VALID_TARGET_STATES:
raise Warning("state '{0}' is a valid target state, skipping".format(
resource.wrapped.state))
connection = ec2.connect_to_region(resource.region)
if self.dry_run:
try:
connection.terminate_instances([resource.wrapped.id], dry_run=True)
except EC2ResponseError as exc:
if exc.status == 412: # Precondition Failed
raise Warning("Termination {message}".format(**vars(exc)))
raise
else:
instances = connection.terminate_instances([resource.wrapped.id], dry_run=False)
self.logger.info("Initiating shutdown sequence for {0}".format(instances))
return instances
示例4: _wait_for_image_state
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def _wait_for_image_state(self, ami_id, desired_state):
"""Timer to wait for the image/snapshot to reach a desired state
:params:ami_id: correspoding image id in Amazon
:params:desired_state: the desired new state of the image to be in.
"""
def _wait_for_state():
"""Called at an interval until the AMI image is available."""
try:
images = self.ec2_conn.get_all_images(image_ids=[ami_id], owners=None,
executable_by=None, filters=None, dry_run=None)
state = images[0].state
if state == desired_state:
LOG.info("Image has changed state to %s." % desired_state)
raise loopingcall.LoopingCallDone()
except boto_exc.EC2ResponseError:
pass
timer = loopingcall.FixedIntervalLoopingCall(_wait_for_state)
timer.start(interval=0.5).wait()
示例5: unwatch
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def unwatch(connection, volume_id):
""" Remove watching of a volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:returns: bool - True if the watch was successful
"""
try:
volume = connection.get_all_volumes(volume_ids=[volume_id])[0]
volume.remove_tag('AutomatedEBSSnapshots')
except EC2ResponseError:
pass
logger.info('Removed {} from the watchlist'.format(volume_id))
return True
示例6: _setup_ec2
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def _setup_ec2(self):
if self.ec2 and self._instance and self._reservation:
return
if self.id:
if self.region_name:
for region in boto.ec2.regions():
if region.name == self.region_name:
self.ec2 = region.connect()
if self.instance_id and not self._instance:
try:
rs = self.ec2.get_all_reservations([self.instance_id])
if len(rs) >= 1:
for instance in rs[0].instances:
if instance.id == self.instance_id:
self._reservation = rs[0]
self._instance = instance
except EC2ResponseError:
pass
示例7: _setup_ec2
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def _setup_ec2(self):
if self.ec2 and self._instance and self._reservation:
return
if self.id:
if self.region_name:
for region in boto.ec2.regions():
if region.name == self.region_name:
self.ec2 = region.connect()
if self.instance_id and not self._instance:
try:
rs = self.ec2.get_all_instances([self.instance_id])
if len(rs) >= 1:
for instance in rs[0].instances:
if instance.id == self.instance_id:
self._reservation = rs[0]
self._instance = instance
except EC2ResponseError:
pass
示例8: delete_security_group
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def delete_security_group(conn):
try:
sgs = conn.get_all_security_groups(groupnames=[RESOURCE_NAME])
lgr.info('Found security groups: {0}'.format(sgs))
for sg in sgs:
lgr.info('Deleting security group: {0}'.format(sg))
sg.delete()
except boto_exception.EC2ResponseError as e:
lgr.warning('Cannot find Security group {0} [e.status={1}]'.format(
RESOURCE_NAME,
e.status))
if e.status != 400:
raise
lgr.warning('Security group {0} not found, ignoring...'.format(
RESOURCE_NAME))
示例9: _remove_old_snapshots
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def _remove_old_snapshots(connection, volume):
""" Remove old snapshots
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: boto.ec2.volume.Volume
:param volume: Volume to check
:returns: None
"""
if 'AutomatedEBSSnapshotsRetention' not in volume.tags:
logger.warning(
'Missing tag AutomatedEBSSnapshotsRetention for volume {}'.format(
volume.id))
return
retention = int(volume.tags['AutomatedEBSSnapshotsRetention'])
snapshots = connection.get_all_snapshots(filters={'volume-id': volume.id})
# Sort the list based on the start time
snapshots.sort(key=lambda x: x.start_time)
# Remove snapshots we want to keep
snapshots = snapshots[:-int(retention)]
if not snapshots:
logger.info('No old snapshots to remove')
return
for snapshot in snapshots:
logger.info('Deleting snapshot {}'.format(snapshot.id))
try:
snapshot.delete()
except EC2ResponseError as error:
logger.warning('Could not remove snapshot: {}'.format(
error.message))
logger.info('Done deleting snapshots')
示例10: watch
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def watch(connection, volume_id, interval='daily', retention=0):
""" Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:type interval: str
:param interval: Backup interval [hourly|daily|weekly|monthly|yearly]
:type retention: int
:param retention: Number of snapshots to keep. 0 == keep all
:returns: bool - True if the watch was successful
"""
try:
volume = connection.get_all_volumes(volume_ids=[volume_id])[0]
except EC2ResponseError:
logger.warning('Volume {} not found'.format(volume_id))
return False
if interval not in VALID_INTERVALS:
logger.warning(
'{} is not a valid interval. Valid intervals are {}'.format(
interval, ', '.join(VALID_INTERVALS)))
# Remove the tag first
volume.remove_tag('AutomatedEBSSnapshots')
# Re-add the tag
volume.add_tag('AutomatedEBSSnapshots', value=interval)
# Remove the tag first
volume.remove_tag('AutomatedEBSSnapshotsRetention')
# Re-add the tag
volume.add_tag('AutomatedEBSSnapshotsRetention', value=int(retention))
logger.info('Updated the rotation interval to {} for {}'.format(
interval, volume_id))
return True
示例11: get_volume_id
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def get_volume_id(connection, volume):
"""
Get Volume ID from the given volume. Input can be volume id
or its Name tag.
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: str
:param volume: Volume ID or Volume Name
:returns: Volume ID or None if the given volume does not exist
"""
# Regular expression to check whether input is a volume id
volume_id_pattern = re.compile('vol-\w{8}')
if volume_id_pattern.match(volume):
# input is volume id
try:
# Check whether it exists
connection.get_all_volumes(volume_ids=[volume])
volume_id = volume
except EC2ResponseError:
logger.warning('Volume {} not found'.format(volume))
return None
else:
# input is volume name
name_filter = {'tag-key': 'Name', 'tag-value': volume}
volumes = connection.get_all_volumes(filters=name_filter)
if not volumes:
logger.warning('Volume {} not found'.format(volume))
return None
if len(volumes) > 1:
logger.warning('Volume {} not unique'.format(volume))
volume_id = volumes[0].id
return volume_id
示例12: get_ami_id
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def get_ami_id(self, params):
valid = False
while not valid:
ami = params.get('ami', None)
if not ami:
prop = StringProperty(name='ami', verbose_name='AMI')
ami = propget.get(prop)
try:
rs = self.ec2.get_all_images([ami])
if len(rs) == 1:
valid = True
params['ami'] = rs[0]
except EC2ResponseError:
pass
示例13: attach
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
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)
示例14: _createSecurityGroup
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
def _createSecurityGroup(self):
assert self._ctx
def groupNotFound(e):
retry = (e.status == 400 and 'does not exist in default VPC' in e.body)
return retry
vpcId = None
if self._vpcSubnet:
conn = boto.connect_vpc(region=self._ctx.ec2.region)
subnets = conn.get_all_subnets(subnet_ids=[self._vpcSubnet])
if len(subnets) > 0:
vpcId = subnets[0].vpc_id
# security group create/get. ssh + all ports open within the group
try:
web = self._ctx.ec2.create_security_group(self.clusterName,
'Toil appliance security group', vpc_id=vpcId)
except EC2ResponseError as e:
if e.status == 400 and 'already exists' in e.body:
pass # group exists- nothing to do
else:
raise
else:
for attempt in retry(predicate=groupNotFound, timeout=300):
with attempt:
# open port 22 for ssh-ing
web.authorize(ip_protocol='tcp', from_port=22, to_port=22, cidr_ip='0.0.0.0/0')
for attempt in retry(predicate=groupNotFound, timeout=300):
with attempt:
# the following authorizes all TCP access within the web security group
web.authorize(ip_protocol='tcp', from_port=0, to_port=65535, src_group=web)
for attempt in retry(predicate=groupNotFound, timeout=300):
with attempt:
# We also want to open up UDP, both for user code and for the RealtimeLogger
web.authorize(ip_protocol='udp', from_port=0, to_port=65535, src_group=web)
out = []
for sg in self._ctx.ec2.get_all_security_groups():
if sg.name == self.clusterName and (vpcId is None or sg.vpc_id == vpcId):
out.append(sg)
return out
示例15: attach
# 需要导入模块: from boto import exception [as 别名]
# 或者: from boto.exception import EC2ResponseError [as 别名]
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 = Volume.find(name = self.logical_volume_name).next()
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_all_instances([self.instance_id])[0].instances[0]
attempt_attach = True
while attempt_attach:
try:
ec2.attach_volume(self.volume_id, self.instance_id, self.device)
attempt_attach = False
except EC2ResponseError, 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