Python boto.exception.EC2ResponseError() Examples

The following are 19 code examples of boto.exception.EC2ResponseError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module boto.exception , or try the search function .
Example #1
Source File: _aws.py    From flocker with Apache License 2.0 6 votes vote down vote up
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)) 
Example #2
Source File: _aws.py    From flocker with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: ec2.py    From aws-monocyte with Apache License 2.0 6 votes vote down vote up
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 
Example #4
Source File: server.py    From canvas with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #5
Source File: ec2driver.py    From openstack-omni with Apache License 2.0 6 votes vote down vote up
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() 
Example #6
Source File: volume_manager.py    From automated-ebs-snapshots with Apache License 2.0 6 votes vote down vote up
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 
Example #7
Source File: server.py    From aws-extender with MIT License 6 votes vote down vote up
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 
Example #8
Source File: ebs.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #9
Source File: sanity.py    From cloudify-manager-blueprints with Apache License 2.0 5 votes vote down vote up
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)) 
Example #10
Source File: server.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #11
Source File: awsProvisioner.py    From toil with Apache License 2.0 5 votes vote down vote up
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 
Example #12
Source File: ebs.py    From aws-extender with MIT License 5 votes vote down vote up
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) 
Example #13
Source File: server.py    From aws-extender with MIT License 5 votes vote down vote up
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 
Example #14
Source File: volume_manager.py    From automated-ebs-snapshots with Apache License 2.0 5 votes vote down vote up
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 
Example #15
Source File: volume_manager.py    From automated-ebs-snapshots with Apache License 2.0 5 votes vote down vote up
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 
Example #16
Source File: snapshot_manager.py    From automated-ebs-snapshots with Apache License 2.0 5 votes vote down vote up
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') 
Example #17
Source File: ec2.py    From aws-monocyte with Apache License 2.0 5 votes vote down vote up
def delete(self, resource):
        connection = ec2.connect_to_region(resource.region)

        if self.dry_run:
            try:
                connection.delete_volume(resource.wrapped.id, dry_run=True)
            except EC2ResponseError as exc:
                if exc.status == 412:  # Precondition Failed
                    warnings.warn(Warning("Termination {message}".format(**vars(exc))))
                raise
        else:
            self.logger.info("Initiating deletion of EBS volume {0}".format(resource.wrapped.id))
            connection.delete_volume(resource.wrapped.id, dry_run=False) 
Example #18
Source File: awsProvisionerTest.py    From toil with Apache License 2.0 4 votes vote down vote up
def _test(self, preemptableJobs=False):
        """Does the work of the testing.  Many features' tests are thrown in here in no particular order."""
        self.launchCluster()
        # get the leader so we know the IP address - we don't need to wait since create cluster
        # already insures the leader is running
        self.cluster = clusterFactory(provisioner='aws', clusterName=self.clusterName)
        self.leader = self.cluster.getLeader()
        self.sshUtil(['mkdir', '-p', self.scriptDir])  # hot deploy doesn't seem permitted to work in normal /tmp or /home

        assert len(self.getMatchingRoles()) == 1
        # --never-download prevents silent upgrades to pip, wheel and setuptools
        venv_command = ['virtualenv', '--system-site-packages', '--python', exactPython, '--never-download', '/home/venv']
        self.sshUtil(venv_command)

        upgrade_command = ['/home/venv/bin/pip', 'install', 'setuptools==28.7.1', 'pyyaml==3.12']
        self.sshUtil(upgrade_command)

        self._getScript()

        toilOptions = [self.jobStore,
                       '--batchSystem=mesos',
                       '--workDir=/var/lib/toil',
                       '--clean=always',
                       '--retryCount=2',
                       '--clusterStats=/tmp/t/',
                       '--logDebug',
                       '--logFile=/tmp/t/sort.log',
                       '--provisioner=aws']

        toilOptions.extend(['--nodeTypes=' + ",".join(self.instanceTypes),
                            '--maxNodes=' + ",".join(self.numWorkers)])
        if preemptableJobs:
            toilOptions.extend(['--defaultPreemptable'])

        self._runScript(toilOptions)

        assert len(self.getMatchingRoles()) == 1

        # check stats
        self.sshUtil(['/home/venv/bin/python', '-c', 'import json; import os; '
                      'json.load(open("/home/" + [f for f in os.listdir("/tmp/t/") if f.endswith(".json")].pop()))'])

        from boto.exception import EC2ResponseError
        volumeID = self.getRootVolID()
        self.cluster.destroyCluster()
        for attempt in range(6):
            # https://github.com/BD2KGenomics/toil/issues/1567
            # retry this for up to 1 minute until the volume disappears
            try:
                self.cluster._ctx.ec2.get_all_volumes(volume_ids=[volumeID])
                time.sleep(10)
            except EC2ResponseError as e:
                if e.status == 400 and 'InvalidVolume.NotFound' in e.code:
                    break
                else:
                    raise
        else:
            self.fail('Volume with ID %s was not cleaned up properly' % volumeID)

        assert len(self.getMatchingRoles()) == 0 
Example #19
Source File: utils.py    From aws-deployments with MIT License 4 votes vote down vote up
def touchImage(region="", imageId="",
  keyName="", instanceType="", vpcId="",
  subnetId="", okayStatusCodes=[401]):
  """
  This method attempts to launch an AMI in AWS using the dry_run 
  flag.  This allows us to check whether all the conditions 
  required to use the image are satisfied.
  """

  try: 
    ec2_conn = boto.ec2.connect_to_region(region)
    ec2_conn.get_image(image_id=imageId).run(
      instance_type=instanceType,
      key_name=keyName,
      subnet_id=subnetId,
      dry_run=True
      )
  except EC2ResponseError, e:
    
    status=int(e.status)

    if status not in okayStatusCodes:
      # e.reason == 'Unauthorized' => EULA needs to be accepted
      if int(e.status) == 401:
        print "Error: Unauthorized to use this image {} in {}, \
  have the terms and conditions been accepted?".format(
          imageId, region)
        return False

      # e.reason == 'Bad Request' => bad image launch conditions
      # for example:
      # "The image id '[ami-4c7a3924]' does not exist"
      #   "Virtualization type 'hvm' is required for instances of type 't2.micro'."
      elif int(e.status) == 400:
        print "Error: Unable to launch image with params region={}, \
imageId={}, keyName={}, instanceType={}\r\n\
\tReason was: {}".format(
            region, imageId, keyName, instanceType, e.message)
        return False

      # e.reason = "Precondition Failed"
      # for example: 
      #   Request would have succeeded, but DryRun flag is set.
      elif int(e.status) == 412:
        return True
      else: 
        raise e
        return False