Python boto.exception.BotoServerError() Examples

The following are 30 code examples of boto.exception.BotoServerError(). 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 stacks with MIT License 7 votes vote down vote up
def throttling_retry(func):
    """Retry when AWS is throttling API calls"""

    def retry_call(*args, **kwargs):
        retries = 0
        while True:
            try:
                retval = func(*args)
                return retval
            except BotoServerError as err:
                if (err.code == 'Throttling' or err.code == 'RequestLimitExceeded') and retries <= 3:
                    sleep = 3 * (2 ** retries)
                    print('Being throttled. Retrying after {} seconds..'.format(sleep))
                    time.sleep(sleep)
                    retries += 1
                else:
                    raise err

    return retry_call 
Example #2
Source File: product_sync_new.py    From amdeb-amazon with GNU General Public License v3.0 6 votes vote down vote up
def _mws_send(self, mws_send, syncs, sync_values):
        log_template = "about to call MWS send() for product syncs."
        _logger.debug(log_template.format(len(sync_values)))

        try:
            results = mws_send(sync_values)
            sync_result = self._convert_results(results)
            ProductSyncAccess.update_sync_status(syncs, sync_result)
        except BotoServerError as boto_ex:
            _logger.debug("MWS Request error: {}".format(
                boto_ex.error_code))
            if boto_ex.error_code in ["RequestThrottled",
                                      "ServiceUnavailable"]:
                _logger.debug("Continue with throttled or unavailable error.")
            else:
                ProductSyncAccess.update_sync_new_exception(syncs, boto_ex)
        except Exception as ex:
            # we may want to re-try for recoverable exceptions
            # for now, just report error
            _logger.exception("mws send() threw exception.")
            ProductSyncAccess.update_sync_new_exception(syncs, ex) 
Example #3
Source File: aws.py    From eggo with Apache License 2.0 6 votes vote down vote up
def create_cf_stack(cf_conn, stack_name, cf_template_path, availability_zone):
    try:
        if len(cf_conn.describe_stacks(stack_name)) > 0:
            print "Stack '{n}' already exists. Reusing.".format(n=stack_name)
            return
    except BotoServerError:
        # stack does not exist
        pass

    print "Creating stack with name '{n}'.".format(n=stack_name)
    with open(cf_template_path, 'r') as template_file:
        template_body = template_file.read()
    cf_conn.create_stack(stack_name, template_body=template_body,
                         parameters=[('KeyPairName', get_ec2_key_pair()),
                                     ('AZ', availability_zone)],
                         tags={'owner': getuser(),
                               'ec2_key_pair': get_ec2_key_pair()})
    wait_for_stack_status(cf_conn, stack_name, 'CREATE_COMPLETE') 
Example #4
Source File: context.py    From toil with Apache License 2.0 6 votes vote down vote up
def setup_iam_ec2_role(self, role_name, policies):
        aws_role_name = self.to_aws_name(role_name)
        try:
            self.iam.create_role(aws_role_name, assume_role_policy_document=json.dumps({
                "Version": "2012-10-17",
                "Statement": [{
                    "Effect": "Allow",
                    "Principal": {"Service": ["ec2.amazonaws.com"]},
                    "Action": ["sts:AssumeRole"]}
                ]}))
        except BotoServerError as e:
            if e.status == 409 and e.error_code == 'EntityAlreadyExists':
                pass
            else:
                raise

        self.__setup_entity_policies(aws_role_name, policies,
                                     list_policies=self.iam.list_role_policies,
                                     delete_policy=self.iam.delete_role_policy,
                                     get_policy=self.iam.get_role_policy,
                                     put_policy=self.iam.put_role_policy)

        return aws_role_name 
Example #5
Source File: cf.py    From stacks with MIT License 6 votes vote down vote up
def delete_stack(conn, stack_name, region, profile, confirm):
    """Deletes stack given its name"""
    msg = ('You are about to delete the following stack:\n'
           'Name: {}\n'
           'Region: {}\n'
           'Profile: {}\n').format(stack_name, region, profile)
    if not confirm:
        print(msg)
        response = input('Are you sure? [y/N] ')
    else:
        response = 'yes'

    if response in YES:
        try:
            conn.delete_stack(stack_name)
        except BotoServerError as err:
            if 'does not exist' in err.message:
                print(err.message)
                sys.exit(0)
            else:
                print(err.message)
                sys.exit(1)
    else:
        sys.exit(0) 
Example #6
Source File: cf.py    From stacks with MIT License 6 votes vote down vote up
def stack_outputs(conn, stack_name, output_name):
    """List stacks outputs"""
    try:
        result = conn.describe_stacks(stack_name)
    except BotoServerError as err:
        print(err.message)
        sys.exit(1)

    outputs = []
    outs = [s.outputs for s in result][0]
    for o in outs:
        if not output_name:
            columns = [o.key, o.value]
            outputs.append(columns)
        elif output_name and o.key == output_name:
            outputs.append([o.value])

    if len(result) >= 1:
        return tabulate(outputs, tablefmt='plain')
    return None 
Example #7
Source File: cf.py    From stacks with MIT License 6 votes vote down vote up
def stack_resources(conn, stack_name, logical_resource_id=None):
    """List stack resources"""
    try:
        result = conn.describe_stack_resources(stack_name_or_id=stack_name,
                                               logical_resource_id=logical_resource_id)
    except BotoServerError as err:
        print(err.message)
        sys.exit(1)
    resources = []
    if logical_resource_id:
        resources.append([r.physical_resource_id for r in result])
    else:
        for r in result:
            columns = [
                r.logical_resource_id,
                r.physical_resource_id,
                r.resource_type,
                r.resource_status,
            ]
            resources.append(columns)

    if len(result) >= 1:
        return tabulate(resources, tablefmt='plain')
    return None 
Example #8
Source File: routes.py    From aws-mock-metadata with MIT License 6 votes vote down vote up
def create_session():
    metadata = request.app.config.meta_get('metadata', 'obj')
    token = _get_value(request, 'token')
    profile = _get_value(request, 'profile')

    if not token and not profile:
        response.status = 400
        return {
            'error': {
                'message': 'token and/or profile is required'
            }
        }

    if profile:
        metadata.profile_name = profile

    if token:
        try:
            request.app.config.meta_get('metadata', 'obj').get_session(token)
        except BotoServerError as e:
            response.status = e.status
            return {'error': {'message': e.message}}

    return get_session() 
Example #9
Source File: sqs_wrapper.py    From mycroft with MIT License 6 votes vote down vote up
def get_messages_from_queue(self):
        """ Fetches messages from the sqs queue of name passed in during this
        object's construction.
        Does not handle exceptions from Boto.

        :rtype: a list of SQS message or None
        """
        try:
            msgs = self._queue.get_messages(
                num_messages=self._num_messages_to_fetch,
                wait_time_seconds=self._wait_time_secs)
            return msgs
        except (BotoClientError, BotoServerError):
            log_exception(
                "Boto exception in fetching messages from SQS, for queue name:"
                + self._queue.id)
            raise 
Example #10
Source File: test_models.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_ses_blacklist_silently_fails(self):
        def send_fail(messages):
            raise BotoServerError(400, "Bad Request",
            """<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
                 <Error>
                   <Type>Sender</Type>
                   <Code>MessageRejected</Code>
                   <Message>Address blacklisted.</Message>
                 </Error>
                 <RequestId>a693e02d-00f2-11e1-9a52-ed3836840b28</RequestId>
               </ErrorResponse>""")

        with mocks.override_send_messages(send_fail):
            send_email('to@example.com', 'from@example.com', 'subjek', 'test', {}) 
Example #11
Source File: routes.py    From aws-mock-metadata with MIT License 5 votes vote down vote up
def get_credentials():
    try:
        session = request.app.config.meta_get('metadata', 'obj').get_session()

        return {
            'Code':           'Success',
            'AccessKeyId':     session.access_key,
            'SecretAccessKey': session.secret_key,
            'Token':           session.session_token,
            'Expiration':      session.expiration
        }
    except BotoServerError as e:
        response.status = e.status
        return {'error': {'message': e.message}} 
Example #12
Source File: cloudWatchMetrics.py    From engine-python with Apache License 2.0 5 votes vote down vote up
def runHistorical(job_id, start_date, end_date, cloudwatch_conn, engine_client):
    '''
    Query and analyze the CloudWatch metrics from start_date to end_date.
    If end_date == None then run until the time now.
    '''

    end = start_date
    delta = calculateIntervalBetweenQueries(REPORTING_INTERVAL)

    while True:

        start = end
        end = start + delta
        if (end > end_date):
            end = end_date

        if start == end:
            break

        print "Querying metrics starting at time " + str(start.isoformat())

        try:
            metrics = cloudwatch_conn.list_metrics(namespace='AWS/EC2')
            metric_records = queryMetricRecords(metrics, start, end, reporting_interval = REPORTING_INTERVAL)

            tranposed_metrics = transposeMetrics(metric_records)

            data = ''
            for met in tranposed_metrics:
                json_str = json.dumps(met)
                data += json_str + '\n'

            (http_status, response) = engine_client.upload(job_id, data)
            if http_status != 202:
                print "Error uploading metric data to the Engine"
                print http_status, json.dumps(response)

        except BotoServerError as error:
            print "Error querying CloudWatch"
            print error 
Example #13
Source File: compile_js.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upload_compiled_js_to_s3(local_path, s3_path):
    with file(local_path, 'rb') as handle:
        raw_filedata = handle.read()

    filedata = gzip_string(raw_filedata)

    headers = {
        'Cache-Control': 'max-age=315360000, public',
        'Expires': 'Thu, 31 Dec 2037 23:55:55 GMT',
        'Content-Encoding': 'gzip',
        'Content-Type': 'text/javascript',
    }

    conn = S3Connection(*aws)
    bucket = conn.get_bucket(Config['compress_bucket'])

    key = Key(bucket)
    key.key = s3_path
    try:
        if key.exists():
            print "Skipping", s3_path, " already exists."
        else:
            print "Uploading %s (%s kb)" % (s3_path, len(filedata) // 1024)
            key.set_contents_from_string(filedata, headers=headers)
    except BotoServerError, bse:
        print bse 
Example #14
Source File: ec2_remote_facts.py    From container-networking-ansible with Apache License 2.0 5 votes vote down vote up
def list_ec2_instances(connection, module):

    filters = module.params.get("filters")
    instance_dict_array = []

    try:
        all_instances = connection.get_only_instances(filters=filters)
    except BotoServerError as e:
        module.fail_json(msg=e.message)

    for instance in all_instances:
        instance_dict_array.append(get_instance_info(instance))

    module.exit_json(instances=instance_dict_array) 
Example #15
Source File: ec2_remote_facts.py    From container-networking-ansible with Apache License 2.0 5 votes vote down vote up
def list_ec2_instances(connection, module):

    filters = module.params.get("filters")
    instance_dict_array = []

    try:
        all_instances = connection.get_only_instances(filters=filters)
    except BotoServerError as e:
        module.fail_json(msg=e.message)

    for instance in all_instances:
        instance_dict_array.append(get_instance_info(instance))

    module.exit_json(instances=instance_dict_array) 
Example #16
Source File: ec2_remote_facts.py    From container-networking-ansible with Apache License 2.0 5 votes vote down vote up
def list_ec2_instances(connection, module):

    filters = module.params.get("filters")
    instance_dict_array = []

    try:
        all_instances = connection.get_only_instances(filters=filters)
    except BotoServerError as e:
        module.fail_json(msg=e.message)

    for instance in all_instances:
        instance_dict_array.append(get_instance_info(instance))

    module.exit_json(instances=instance_dict_array) 
Example #17
Source File: ec2_remote_facts.py    From container-networking-ansible with Apache License 2.0 5 votes vote down vote up
def list_ec2_instances(connection, module):

    filters = module.params.get("filters")
    instance_dict_array = []

    try:
        all_instances = connection.get_only_instances(filters=filters)
    except BotoServerError as e:
        module.fail_json(msg=e.message)

    for instance in all_instances:
        instance_dict_array.append(get_instance_info(instance))

    module.exit_json(instances=instance_dict_array) 
Example #18
Source File: ec2_remote_facts.py    From container-networking-ansible with Apache License 2.0 5 votes vote down vote up
def list_ec2_instances(connection, module):

    filters = module.params.get("filters")
    instance_dict_array = []

    try:
        all_instances = connection.get_only_instances(filters=filters)
    except BotoServerError as e:
        module.fail_json(msg=e.message)

    for instance in all_instances:
        instance_dict_array.append(get_instance_info(instance))

    module.exit_json(instances=instance_dict_array) 
Example #19
Source File: models.py    From django-scarface with MIT License 5 votes vote down vote up
def register_or_update(self, new_token=None, custom_user_data=u"",
                           connection=None):
        '''
        Registers the device to SNS. If the device was
        previously registered the registration is updated.
        :return: True if the registration/update was successful
        '''
        if self.is_registered:
            result = self.update(new_token, custom_user_data, connection)
        else:
            try:
                result = self.register(custom_user_data, connection)
            # Heavily inspired by http://stackoverflow.com/a/28316993/270265
            except BotoServerError as err:
                result_re = re.compile(r'Endpoint(.*)already', re.IGNORECASE)
                result = result_re.search(err.message)
                if result:
                    arn = result.group(0).replace('Endpoint ', '').replace(
                        ' already', '')
                    self.arn = arn
                    self.update(new_token, custom_user_data, connection)
                else:
                    sns_exc = SNSNotCreatedException(err)
                    sns_exc.message = err.message
                    raise sns_exc

        return result 
Example #20
Source File: tests.py    From django-scarface with MIT License 5 votes vote down vote up
def test_deregister_device(self):
        app = self.application
        platform = self.get_apns_platform(app)
        topic = self.get_topic(app)
        device = self.get_ios_device(platform)
        connection = Mock()
        connection.subscribe.return_value = {
            'SubscribeResponse': {
                'SubscribeResult': {
                    'SubscriptionArn': TEST_ARN_TOKEN_SUBSCRIPTION
                }
            }
        }
        connection.unsubscribe.return_value = True
        topic.register_device(device, connection)
        subscription_arn = Subscription.objects.get(
                device=device,
                topic=topic
        ).arn

        try:
            topic.deregister_device(device, connection)
        except BotoServerError as e:
            pass

        connection.unsubscribe.assert_called_once_with(
                subscription_arn
        )
        try:
            subscription = Subscription.objects.get(
                    device=device,
                    topic=topic
            )
            self.assertTrue(False)
        except Subscription.DoesNotExist:
            pass 
Example #21
Source File: test_aws_elasticsearch_connection.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_boto_service_unavailable(self, mock_make_request):
        connection = AwsHttpConnection(aws_access_key_id='access_key', aws_secret_access_key='secret')
        mock_make_request.side_effect = BotoServerError(503, 'Service Unavailable')
        try:
            connection.perform_request('get', 'http://example.com')
        except TransportError as transport_error:
            self.assertEqual(transport_error.status_code, 503)
        else:
            self.fail('Expected a transport error to be raised.') 
Example #22
Source File: rotate.py    From awsudo with MIT License 5 votes vote down vote up
def main():
    if len(sys.argv) > 2:
        printUsage()
        raise SystemExit(1)

    try:
        section = sys.argv[1]
    except IndexError:
        section = 'default'

    credentials = CredentialsFile(section)

    iam = IAMConnection(
        aws_access_key_id=credentials.keyId,
        aws_secret_access_key=credentials.secretKey,
    )

    userName = getUserName(iam)
    deleteOldKeys(iam, credentials.keyId, userName)
    newKey = makeNewKey(iam, userName)

    iam = IAMConnection(
        aws_access_key_id=newKey['access_key_id'],
        aws_secret_access_key=newKey['secret_access_key'],
    )

    oldKey = credentials.keyId
    try:
        deactivateKey(iam, oldKey, userName)
    except BotoServerError as e:
        print(e)
        raise SystemExit(dedent('''
        Failed to deactivate the old key (%s) after one minute of
        retrying. Manual remediation will be required.
        %s
        ''' % (oldKey, ACCESS_KEY_DOCS)).strip())

    credentials.updateCredentials(
        newKey['access_key_id'],
        newKey['secret_access_key'],
    ) 
Example #23
Source File: aws_elasticsearch_connection.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
        """
        Called when making requests to elasticsearch.  Requests are signed and
        http status, headers, and response are returned.

        Note: the "timeout" kwarg is ignored in this case.  Boto manages the timeout
        and the default is 70 seconds.
        See: https://github.com/boto/boto/blob/develop/boto/connection.py#L533
        """
        if not isinstance(body, basestring):
            body = json.dumps(body)

        response = None
        start = time.time()
        try:
            response = self.connection.make_request(method, url, params=params, data=body)
            status = response.status
        except BotoServerError as boto_server_error:
            status = boto_server_error.status
        duration = time.time() - start

        raw_data = ''
        headers = {}
        if response:
            raw_data = response.read()
            headers = dict(response.getheaders())

        # Raise errors based on http status codes and let the client handle them.
        if not (200 <= status < 300) and status not in ignore:
            self.log_request_fail(method, url, body, duration, status)
            self._raise_error(status, raw_data)

        self.log_request_success(method, url, url, body, status, raw_data, duration)

        return status, headers, raw_data 
Example #24
Source File: awsProvisioner.py    From toil with Apache License 2.0 5 votes vote down vote up
def _getProfileArn(self):
        assert self._ctx
        def addRoleErrors(e):
            return e.status == 404
        policy = dict(iam_full=iamFullPolicy, ec2_full=ec2FullPolicy,
                      s3_full=s3FullPolicy, sbd_full=sdbFullPolicy)
        iamRoleName = self._ctx.setup_iam_ec2_role(role_name=_INSTANCE_PROFILE_ROLE_NAME, policies=policy)

        try:
            profile = self._ctx.iam.get_instance_profile(iamRoleName)
        except BotoServerError as e:
            if e.status == 404:
                profile = self._ctx.iam.create_instance_profile(iamRoleName)
                profile = profile.create_instance_profile_response.create_instance_profile_result
            else:
                raise
        else:
            profile = profile.get_instance_profile_response.get_instance_profile_result
        profile = profile.instance_profile
        profile_arn = profile.arn

        if len(profile.roles) > 1:
                raise RuntimeError('Did not expect profile to contain more than one role')
        elif len(profile.roles) == 1:
            # this should be profile.roles[0].role_name
            if profile.roles.member.role_name == iamRoleName:
                return profile_arn
            else:
                self._ctx.iam.remove_role_from_instance_profile(iamRoleName,
                                                          profile.roles.member.role_name)
        for attempt in retry(predicate=addRoleErrors):
            with attempt:
                self._ctx.iam.add_role_to_instance_profile(iamRoleName, iamRoleName)
        return profile_arn 
Example #25
Source File: awsProvisioner.py    From toil with Apache License 2.0 5 votes vote down vote up
def awsRetryPredicate(e):
    if not isinstance(e, BotoServerError):
        return False
    # boto/AWS gives multiple messages for the same error...
    if e.status == 503 and 'Request limit exceeded' in e.body:
        return True
    elif e.status == 400 and 'Rate exceeded' in e.body:
        return True
    elif e.status == 400 and 'NotFound' in e.body:
        # EC2 can take a while to propagate instance IDs to all servers.
        return True
    elif e.status == 400 and e.error_code == 'Throttling':
        return True
    return False 
Example #26
Source File: __init__.py    From aws-extender with MIT License 5 votes vote down vote up
def get_response(self, action, params, page=0, itemSet=None):
        """
        Utility method to handle calls to ECS and parsing of responses.
        """
        params['Service'] = "AWSECommerceService"
        params['Operation'] = action
        if page:
            params['ItemPage'] = page
        response = self.make_request(None, params, "/onca/xml")
        body = response.read().decode('utf-8')
        boto.log.debug(body)

        if response.status != 200:
            boto.log.error('%s %s' % (response.status, response.reason))
            boto.log.error('%s' % body)
            raise BotoServerError(response.status, response.reason, body)

        if itemSet is None:
            rs = ItemSet(self, action, params, page)
        else:
            rs = itemSet
        h = handler.XmlHandler(rs, self)
        xml.sax.parseString(body.encode('utf-8'), h)
        if not rs.is_valid:
            raise BotoServerError(response.status, '{Code}: {Message}'.format(**rs.errors[0]))
        return rs

    #
    # Group methods
    # 
Example #27
Source File: wrapper.py    From aws-extender with MIT License 5 votes vote down vote up
def beanstalk_wrapper(func, name):
    def _wrapped_low_level_api(*args, **kwargs):
        try:
            response = func(*args, **kwargs)
        except BotoServerError as e:
            raise exception.simple(e)
        # Turn 'this_is_a_function_name' into 'ThisIsAFunctionNameResponse'.
        cls_name = ''.join([part.capitalize() for part in name.split('_')]) + 'Response'
        cls = getattr(boto.beanstalk.response, cls_name)
        return cls(response)
    return _wrapped_low_level_api 
Example #28
Source File: exception.py    From aws-extender with MIT License 5 votes vote down vote up
def __call__(self, status, reason, body=None):
        server = BotoServerError(status, reason, body=body)
        supplied = self.find_element(server.error_code, '', ResponseError)
        print(supplied.__name__)
        return supplied(status, reason, body=body) 
Example #29
Source File: connection.py    From aws-extender with MIT License 5 votes vote down vote up
def _post_request(self, request, params, parser, body='', headers=None):
        """Make a POST request, optionally with a content body,
           and return the response, optionally as raw text.
        """
        headers = headers or {}
        path = self._sandboxify(request['path'])
        request = self.build_base_http_request('POST', path, None, data=body,
                                               params=params, headers=headers,
                                               host=self.host)
        try:
            response = self._mexe(request, override_num_retries=None)
        except BotoServerError as bs:
            raise self._response_error_factory(bs.status, bs.reason, bs.body)
        body = response.read()
        boto.log.debug(body)
        if not body:
            boto.log.error('Null body %s' % body)
            raise self._response_error_factory(response.status,
                                               response.reason, body)
        if response.status != 200:
            boto.log.error('%s %s' % (response.status, response.reason))
            boto.log.error('%s' % body)
            raise self._response_error_factory(response.status,
                                               response.reason, body)
        digest = response.getheader('Content-MD5')
        if digest is not None:
            assert content_md5(body) == digest
        contenttype = response.getheader('Content-Type')
        return self._parse_response(parser, contenttype, body) 
Example #30
Source File: exception.py    From aws-extender with MIT License 5 votes vote down vote up
def __new__(cls, *args, **kw):
        error = BotoServerError(*args, **kw)
        newclass = globals().get(error.error_code, ResponseError)
        obj = newclass.__new__(newclass, *args, **kw)
        obj.__dict__.update(error.__dict__)
        return obj