Exemplo n.º 1
0
    def test_retry_url_using_bytes_and_string_response(self):
        test_value = 'normal response'
        fake_response = mock.Mock()

        # test using unicode
        fake_response.read.return_value = test_value
        self.opener.return_value.open.return_value = fake_response
        response = retry_url('http://10.10.10.10/foo', num_retries=1)
        self.assertEqual(response, test_value)

        # test using bytes
        fake_response.read.return_value = test_value.encode('utf-8')
        self.opener.return_value.open.return_value = fake_response
        response = retry_url('http://10.10.10.10/foo', num_retries=1)
        self.assertEqual(response, test_value)
Exemplo n.º 2
0
    def test_retry_url_using_bytes_and_string_response(self):
        test_value = 'normal response'
        fake_response = mock.Mock()

        # test using unicode
        fake_response.read.return_value = test_value
        self.opener.return_value.open.return_value = fake_response
        response = retry_url('http://10.10.10.10/foo', num_retries=1)
        self.assertEqual(response, test_value)

        # test using bytes
        fake_response.read.return_value = test_value.encode('utf-8')
        self.opener.return_value.open.return_value = fake_response
        response = retry_url('http://10.10.10.10/foo', num_retries=1)
        self.assertEqual(response, test_value)
Exemplo n.º 3
0
def get_userdata(version='latest',
                 url='http://169.254.169.254',
                 timeout=None, num_retries=5):
    """
    Returns the instance userdata as a string by default.

    If the timeout is specified, the connection to the specified url
    will time out after the specified number of seconds.

    :type version: str
    :param version: API version from AWS

    :type timeout: int
    :param timeout: socket timeout

    :type num_retries: int
    :param num_retries: how many times of retrying

    :rtype: string
    :return: a string containing user data

    """
    if timeout is not None:
        original = socket.getdefaulttimeout()
        socket.setdefaulttimeout(timeout)
    ud_url = '%s/%s/%s/' % (url, version, 'user-data')
    user_data = retry_url(
        ud_url, retry_on_404=False, num_retries=num_retries)
    if timeout is not None:
        socket.setdefaulttimeout(original)
    return user_data
Exemplo n.º 4
0
def get_userdata(version='latest',
                 url='http://169.254.169.254',
                 timeout=None,
                 num_retries=5):
    """
    Returns the instance userdata as a string by default.

    If the timeout is specified, the connection to the specified url
    will time out after the specified number of seconds.

    :type version: str
    :param version: API version from AWS

    :type timeout: int
    :param timeout: socket timeout

    :type num_retries: int
    :param num_retries: how many times of retrying

    :rtype: string
    :return: a string containing user data

    """
    if timeout is not None:
        original = socket.getdefaulttimeout()
        socket.setdefaulttimeout(timeout)
    ud_url = '%s/%s/%s/' % (url, version, 'user-data')
    user_data = retry_url(ud_url, retry_on_404=False, num_retries=num_retries)
    if timeout is not None:
        socket.setdefaulttimeout(original)
    return user_data
Exemplo n.º 5
0
    def test_retry_url_uses_proxy(self):
        self.set_normal_response('normal response')
        self.set_no_proxy_allowed_response('no proxy response')

        response = retry_url('http://10.10.10.10/foo', num_retries=1)
        self.assertEqual(response, 'no proxy response')
Exemplo n.º 6
0
    def test_retry_url_uses_proxy(self):
        self.set_normal_response('normal response')
        self.set_no_proxy_allowed_response('no proxy response')

        response = retry_url('http://10.10.10.10/foo', num_retries=1)
        self.assertEqual(response, 'no proxy response')
Exemplo n.º 7
0
    def __init__(self, **kwargs):
        super(Context, self).__init__()
        
        # Ensure that no none keywords are given
        for k in kwargs.keys():
            if kwargs[k] == None:
                del kwargs[k]
            
        if 'config' in kwargs:
            self.config = kwargs['config']
            del kwargs['config']
        elif 'config_file' in kwargs and kwargs['config_file']:
            self.config = boto.pyami.config.Config(path=kwargs['config_file'])
            del kwargs['config_file']
        else:
            self.config = boto.config

        if 'security_credentials' in kwargs:
            import json
            credentials_url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/%s/' % kwargs['security_credentials']
            credentials = json.loads(retry_url(credentials_url, retry_on_404=False))
            identity_url = 'http://169.254.169.254/latest/dynamic/instance-identity/document'
            identity = json.loads(retry_url(identity_url, retry_on_404=False))
            kwargs['aws_access_key_id'] = credentials['AccessKeyId']
            kwargs['aws_secret_access_key'] = credentials['SecretAccessKey']
            self.security_token = credentials['Token']
            kwargs['ec2_region_name'] = identity['region']
            kwargs['ec2_region_endpoint'] = "ec2.%s.amazonaws.com" % identity['region']
            del kwargs['security_credentials']
        else:
            self.security_token = None
        
        # Manually check the config
        # save debug, it's used in each section
        if 'debug' in kwargs:
            debug = kwargs['debug']
            del kwargs['debug']
        else:
            debug = None
        for section_info in (('Boto',
                                ('num_retries', 'ec2_region_name', 'ec2_region_endpoint',
                                 'is_secure', 'default_ami', 'ami_provider')),
                         ('Credentials',
                                ('aws_access_key_id','aws_secret_access_key', 'key_name', 'key_file', 'user'))
                             ):
            section = section_info[0]
            if not self.config.has_section(section):
                self.config.add_section(section)
            for key in section_info[1]:
                if key in kwargs:
                    self.config.set(section, key, kwargs[key].__str__())
                    del kwargs[key]
            if debug:
                self.config.set(section, 'debug', debug.__str__())

        # Create object config
        self.object_defaults = {}
        for obj in oslib.objects.keys():
            self.object_defaults[obj] = {}
            if self.config.has_section(obj):
                for entry in self.config.items(obj):
                    self.object_defaults[obj][entry[0]] = entry[1]
                
        boto.config = self.config

        if debug >= 2:
            import StringIO
            fp = StringIO.StringIO()
            self.config.dump_safe(fp)
            print fp.getvalue()
        
        self.name = self.config.get('Boto','ec2_region_name')
        self.default_ami = self.config.get('Boto','default_ami')
        self.key_name = self.config.get('Credentials','key_name')
        self.key_file = self.config.get('Credentials','key_file')
        self.user = self.config.get('Credentials','user', 'root')

        if 'zone_name' in kwargs:
            self.zone = self.conn.get_all_zones(zones=[kwargs['zone_name']])[0]
            del kwargs['zone_name']
        else:
            self.zone = self.cnx_ec2.get_all_zones()[0]