| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This code is part of Ansible, but is an independent component. |
| 4 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 5 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 6 |
# still belong to the author of the module, and may assign their own license |
| 7 |
# to the complete work. |
| 8 |
# |
| 9 |
# Copyright (c), Michael DeHaan <[email protected]>, 2012-2013 |
| 10 |
# All rights reserved. |
| 11 |
# |
| 12 |
# Redistribution and use in source and binary forms, with or without modification, |
| 13 |
# are permitted provided that the following conditions are met: |
| 14 |
# |
| 15 |
# * Redistributions of source code must retain the above copyright |
| 16 |
# notice, this list of conditions and the following disclaimer. |
| 17 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 18 |
# this list of conditions and the following disclaimer in the documentation |
| 19 |
# and/or other materials provided with the distribution. |
| 20 |
# |
| 21 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 22 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 24 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 29 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
from functools import wraps |
| 32 |
|
| 33 |
try: |
| 34 |
from botocore.exceptions import ClientError |
| 35 |
|
| 36 |
HAS_BOTO3 = True |
| 37 |
except ImportError: |
| 38 |
HAS_BOTO3 = False |
| 39 |
|
| 40 |
from .cloud import CloudRetry |
| 41 |
|
| 42 |
|
| 43 |
def _botocore_exception_maybe(): |
| 44 |
""" |
| 45 |
Allow for boto3 not being installed when using these utils by wrapping |
| 46 |
botocore.exceptions instead of assigning from it directly. |
| 47 |
""" |
| 48 |
if HAS_BOTO3: |
| 49 |
return ClientError |
| 50 |
return type(None) |
| 51 |
|
| 52 |
|
| 53 |
class AWSRetry(CloudRetry): |
| 54 |
base_class = _botocore_exception_maybe() |
| 55 |
|
| 56 |
@staticmethod |
| 57 |
def status_code_from_exception(error): |
| 58 |
return error.response["Error"]["Code"] |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def found(response_code, catch_extra_error_codes=None): |
| 62 |
# This list of failures is based on this API Reference |
| 63 |
# http://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html |
| 64 |
# |
| 65 |
# TooManyRequestsException comes from inside botocore when it |
| 66 |
# does retrys, unfortunately however it does not try long |
| 67 |
# enough to allow some services such as API Gateway to |
| 68 |
# complete configuration. At the moment of writing there is a |
| 69 |
# botocore/boto3 bug open to fix this. |
| 70 |
# |
| 71 |
# https://github.com/boto/boto3/issues/876 (and linked PRs etc) |
| 72 |
retry_on = [ |
| 73 |
"RequestLimitExceeded", |
| 74 |
"Unavailable", |
| 75 |
"ServiceUnavailable", |
| 76 |
"InternalFailure", |
| 77 |
"InternalError", |
| 78 |
"TooManyRequestsException", |
| 79 |
"Throttling", |
| 80 |
] |
| 81 |
if catch_extra_error_codes: |
| 82 |
retry_on.extend(catch_extra_error_codes) |
| 83 |
|
| 84 |
return response_code in retry_on |
| 85 |
|
| 86 |
|
| 87 |
class RetryingBotoClientWrapper: |
| 88 |
__never_wait = ( |
| 89 |
"get_paginator", |
| 90 |
"can_paginate", |
| 91 |
"get_waiter", |
| 92 |
"generate_presigned_url", |
| 93 |
) |
| 94 |
|
| 95 |
def __init__(self, client, retry): |
| 96 |
self.client = client |
| 97 |
self.retry = retry |
| 98 |
|
| 99 |
def _create_optional_retry_wrapper_function(self, unwrapped): |
| 100 |
retrying_wrapper = self.retry(unwrapped) |
| 101 |
|
| 102 |
@wraps(unwrapped) |
| 103 |
def deciding_wrapper(*args, aws_retry=False, **kwargs): |
| 104 |
if aws_retry: |
| 105 |
return retrying_wrapper(*args, **kwargs) |
| 106 |
else: |
| 107 |
return unwrapped(*args, **kwargs) |
| 108 |
|
| 109 |
return deciding_wrapper |
| 110 |
|
| 111 |
def __getattr__(self, name): |
| 112 |
unwrapped = getattr(self.client, name) |
| 113 |
if name in self.__never_wait: |
| 114 |
return unwrapped |
| 115 |
elif callable(unwrapped): |
| 116 |
wrapped = self._create_optional_retry_wrapper_function(unwrapped) |
| 117 |
setattr(self, name, wrapped) |
| 118 |
return wrapped |
| 119 |
else: |
| 120 |
return unwrapped |