Esempio n. 1
0
def ec2_client(region, zone, access_key_id, secret_access_key):
    """
    Establish connection to EC2 client.

    :param str region: The name of the EC2 region to connect to.
    :param str zone: The zone for the EC2 region to connect to.
    :param str access_key_id: "aws_access_key_id" credential for EC2.
    :param str secret_access_key: "aws_secret_access_key" EC2 credential.

    :return: An ``_EC2`` giving information about EC2 client connection
        and EC2 instance zone.
    """
    # Set 2 retry knobs in Boto to BOTO_NUM_RETRIES:
    # 1. ``num_retries``:
    # Request automatic exponential backoff and retry
    # attempts by Boto if an EC2 API call fails with
    # ``RequestLimitExceeded`` due to system load.
    # 2. ``metadata_service_num_attempts``:
    # Request for retry attempts by Boto to
    # retrieve data from Metadata Service used to retrieve
    # credentials for IAM roles on EC2 instances.
    if not config.has_section('Boto'):
        config.add_section('Boto')
    config.set('Boto', 'num_retries', BOTO_NUM_RETRIES)
    config.set('Boto', 'metadata_service_num_attempts', BOTO_NUM_RETRIES)

    # Get Boto EC2 connection with ``EC2ResponseError`` logged by Eliot.
    connection = ec2.connect_to_region(region,
                                       aws_access_key_id=access_key_id,
                                       aws_secret_access_key=secret_access_key)
    return _EC2(zone=zone,
                connection=_LoggedBotoConnection(connection=connection))
Esempio n. 2
0
 def conn(self):
     if self.__conn is None:
         log.debug('creating ec2 connection to %s' % self.config['region_name'])
         validate_certs = self.config['validate_certs']
         if validate_certs:
             if not HAVE_HTTPS_CONNECTION:
                 raise CloudNotAvailableException(
                     "Failed to validate AWS SSL certificates. "
                     "SSL certificate validation is only supported "
                     "on Python>=2.6.\n\nSet AWS_VALIDATE_CERTS=False in "
                     "your config to skip SSL certificate verification and"
                     " suppress this error AT YOUR OWN RISK.")
         if not boto_config.has_section('Boto'):
             boto_config.add_section('Boto')
         # Hack to get around the fact that boto ignores validate_certs
         # if https_validate_certificates is declared in the boto config
         boto_config.setbool('Boto', 'https_validate_certificates',
                             validate_certs)
         #boto_config.setint('Boto', 'http_socket_timeout',
         #                    10)
         kwargs=dict(aws_access_key_id=self.config['access_key_id'], aws_secret_access_key=self.config['secret_access_key'],
             validate_certs=self.config['validate_certs'])
         if 'proxy' in self.config:
             kwargs['proxy']=self.config['proxy']
             kwargs['proxy_port']=self.config['proxy_port']
         self.__conn = ec2.connect_to_region(self.config['region_name'], **kwargs)
         self.__conn.https_validate_certificates = validate_certs
     return self.__conn
Esempio n. 3
0
        def inject_default( name, default ):
            section = 'Boto'
            value = config.get( section, name )

            if value != default:
                if not config.has_section( section ):
                    config.add_section( section )
                config.set( section, name, default )
Esempio n. 4
0
def setupLogging(level, logfile=None, queue=False):
    # this overrides any user debug setting in the boto configuration
    if not boto_config.has_section('Boto'):
        boto_config.add_section('Boto')

    import httplib
    httplib.HTTPConnection.debuglevel = level

    if level == 0:
        level = 'WARN'
        boto_config.set('Boto', 'debug', '0')
    elif level == 1:
        level = 'INFO'
        boto_config.set('Boto', 'debug', '0')
    elif level == 2:
        level = 'DEBUG'
        boto_config.set('Boto', 'debug', '1')
    else:
        level = 'DEBUG'
        boto_config.set('Boto', 'debug', '2')

    if logfile is None:
        logging.config.dictConfig({
            'version': 1,
            'formatters': {
                'simple': {
                    'class': 'logging.Formatter',
                    'format': simplefmt,
                }
            },
            'handlers': {
                'console': {
                    'class': 'logging.StreamHandler',
                    'level': level,
                    'formatter': 'simple',
                },
            },
            'loggers': {
                'boto': {
                    'handlers': ['console'],
                },
                'lacli': {
                    'handlers': ['console']
                },
                'multiprocessing': {
                    'handlers': ['console']
                },
                'requests.packages.urllib3': {
                    'handlers': ['console'],
                    'propagate': True
                },
            },
            'root': {
                'level': level,
            },
        })
    else:
        raise NotImplementedError("Log to file is not implemented yet")
Esempio n. 5
0
    def __init__(self, access_key, secret_key, s3_host='s3.amazonaws.com', secure=True, num_retries=5, socket_timeout=15):
        self.access_key     = access_key
        self.secret_key     = secret_key
        self.s3_host        = s3_host
        self.secure         = secure
        self.num_retries    = num_retries
        self.socket_timeout = socket_timeout

        for section in config.sections():
            config.remove_section(section)
        config.add_section('Boto')
        config.setbool('Boto', 'is_secure', self.secure)
        config.set('Boto', 'http_socket_timeout', str(self.socket_timeout))
        config.set('Boto', 'num_retries', str(self.num_retries))

        self._conn = None
        self.connect()
Esempio n. 6
0
    def setUp(self):
        super(AmazonSWFBackendThreadTestCase, self).setUp()

        # Make long-polling connections time out really quickly
        # Note: this will break in the normal case, since SWF will continue to
        # assign tasks to identities that have already closed their connection
        try:
            config.add_section('Boto')
        except:
            pass
        config.set('Boto', 'http_socket_timeout', '5')

        Defaults.DECISION_TIMEOUT = 1

        self.backends = []
        backend = self.construct_backend()
        for p in backend.processes():
            backend.cancel_process(p)
Esempio n. 7
0
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Setup a database connection to be used in the rest of the code
DB_HOST = os.environ.get('DB_HOST', 'localhost:27017')
client = MongoClient(DB_HOST)
DB_NAME = os.environ.get('DB_NAME', 'dealsdb')
db1 = client[DB_NAME]
if 'DB_USER' in os.environ:
    db1.authenticate(os.environ.get('DB_USER'), os.environ.get('DB_PASSWORD'))
on_aws = False #"ON_AWS" in os.environ

if on_aws:
    if not botoconfig.has_section('Credentials'):
        botoconfig.add_section('Credentials')
    if not botoconfig.has_option('Credentials', 'aws_access_key_id'):
        botoconfig.set('Credentials', 'aws_access_key_id',
                       os.environ.get('AWS_KEY'))
    if not botoconfig.has_option('Credentials', 'aws_secret_access_key'):
        botoconfig.set('Credentials', 'aws_secret_access_key',
                        os.environ.get('AWS_SECRET'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'je+95#mqpv5%c2(sdp69q)5(o0_-5%*j-a(s0w%[email protected]'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Esempio n. 8
0
 def enable_debug(self):
     if self.debug:
         if not config.has_section('Boto'):
             config.add_section('Boto')
         config.set('Boto', 'debug', '2')
Esempio n. 9
0
def enable_debug(args):
    if "debug" in args.__dict__ and args.debug == True:
        if not config.has_section('Boto'):
            config.add_section('Boto')
        config.set('Boto', 'debug', '2')
Esempio n. 10
0
# -*- coding: utf-8 -*-
import json
import os

from boto import config
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
from boto.sqs.queue import Queue as AwsQueue

if not config.has_section('Boto'):
    config.add_section('Boto')
config.set('Boto', 'debug', '0')

class Queue(object):
    """docstring for Queue"""
    aws_key = os.environ.get('PAYPARROT_AWS_SQS_KEY')
    aws_secret = os.environ.get('PAYPARROT_AWS_SQS_SECRET')

    
    import logging
    logging.basicConfig(level=logging.ERROR)

    @classmethod
    def get_queue(cls, queue_name):
        queue_url = os.environ.get('PAYPARROT_AWS_SQS_QUEUE_%s' % queue_name.upper())
        if queue_url:
            conn = SQSConnection(cls.aws_key, cls.aws_secret)
            return AwsQueue(connection = conn, url = queue_url)
        else:
            return None
Esempio n. 11
0
import os
import sys
import datetime
import json

from bottle import get, post, run, request
from bottle import jinja2_template as template

import boto
import boto.s3.connection
from boto import config as botoconfig
from boto.s3.key import Key

botoconfig.add_section('s3')
botoconfig.set('s3', 'use-sigv4', 'True')

# make sure we have AWS credentials and a S3 Bucket
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
if not AWS_ACCESS_KEY_ID:
    print >> sys.stderr, 'Missing environment variable AWS_ACCESS_KEY_ID'
    exit(1)


AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
if not AWS_SECRET_ACCESS_KEY:
    print >> sys.stderr, 'Missing environment variable AWS_SECRET_ACCESS_KEY'
    exit(1)


# make sure we have AWS credentials and a S3 Bucket
S3_REGION = os.environ.get('S3_REGION')