Example #1
0
def test_config_specified_file_source(minimum_config_fixture):
    """ Test that a user data file can be specified from cloud configuration
    and then rendered with config based parameters """

    desired_subs = {
        'key_1': 'config_value_1',
        'key_2': 'config_value_2',
    }

    hostname = 'staging-test-host-001'

    with NamedTemporaryFile() as tmp_file:
        for key in desired_subs.keys():
            tmp_file.write("{{%s}} " % key)
        tmp_file.flush()
        config.get_cloud().update({
            'DEFAULT_USER_DATA': tmp_file.name,
            'USER_DATA_PARAMS': desired_subs
        })

        uri = config.get_cloud_config_value('DEFAULT_USER_DATA', override=None)
        user_data = get_parsed_document(entity_name=hostname,
                                        uri=uri,
                                        config_params_key='USER_DATA_PARAMS',
                                        additional_params=None)

    for value in desired_subs.values():
        assert value in user_data
Example #2
0
def test_config_specified_file_source(minimum_config_fixture):
    """ Test that a user data file can be specified from cloud configuration
    and then rendered with config based parameters """

    desired_subs = {
        'key_1': 'config_value_1',
        'key_2': 'config_value_2',
    }

    hostname = 'staging-test-host-001'

    with NamedTemporaryFile() as tmp_file:
        for key in desired_subs.keys():
            tmp_file.write("{{%s}} " % key)
        tmp_file.flush()
        config.get_cloud().update({
            'DEFAULT_USER_DATA': tmp_file.name,
            'USER_DATA_PARAMS': desired_subs
        })

        uri = config.get_cloud_config_value('DEFAULT_USER_DATA', override=None)
        user_data = get_parsed_document(entity_name=hostname, uri=uri,
                                        config_params_key='USER_DATA_PARAMS',
                                        additional_params=None)

    for value in desired_subs.values():
        assert value in user_data
Example #3
0
def launch_instance(env_type, size=None,
                    user_data_uri=None, user_data_params=None,
                    security_groups=None, extra_tags=None,
                    image_name=None, owner=None):
    """ Launch instances

        Arguments:
            env_type (string): environment-server_type
            size (string): size of the instance to launch.
            user_data_uri (string): File path or URL for user data script.
                If None, Config value will be used.
            user_data_params (dict): Dictionary or parameters to supplement
                the defaults when generating user-data.
            security_groups (list): List of security groups to create (if
                necessary) and supplement the defaults.
            owner (string): username to set as owner
    """

    cloud = get_current_cloud()
    environment, server_type = env_type.split("-", 1)

    name = get_next_hostname(env_type)

    image_name = config.get_cloud_config_value('IMAGE_NAME',
                                               override=image_name)

    if size is None:
        sizes = config.SIZES
        default_size = sizes['default']
        size = sizes.get(server_type, default_size)

    zone = cloud.next_az(server_type)

    key_name = config.CLOUD['PUBLIC_KEY_NAME']

    tags = extra_tags or {}
    tags.update({
        'environment': environment,
        'server_type': server_type,
    })

    if owner:
        tags['owner'] = owner

    security_groups = add_default_security_groups(server_type, security_groups)
    for security_group in security_groups:
        create_if_not_exist_security_group(security_group)

    user_data = None
    user_data_uri = config.get_cloud_config_value('DEFAULT_USER_DATA',
                                                  override=user_data_uri)
    if user_data_uri is not None:
        user_data = get_parsed_document(name, user_data_uri,
                                        'USER_DATA_PARAMS', user_data_params)

    return cloud.launch_instance(
        name, image_name, size, zone, security_groups, key_name,
        user_data=user_data, tags=tags)
Example #4
0
def generate_stack_template(stack_type, stack_name,
                            template_uri, template_params,
                            owner=None):
    template_uri = config.get_namespaced_cloud_config_value(
        'ORCHESTRATION_TEMPLATE_URIS', stack_type, override=template_uri)
    if template_uri is None:
        raise ValueError('A template must be specified by argument or '
                         'in config')

    template = get_parsed_document(stack_name, template_uri,
                               'ORCHESTRATION_TEMPLATE_PARAMS',
                               template_params)

    # Parse as json for validation and for injecting gonzo defaults
    template_dict = json.loads(template)

    if owner:
        template_dict = insert_stack_owner_output(template_dict, owner)

    return json.dumps(template_dict)
Example #5
0
def test_arg_specified_url_source(req, minimum_config_fixture):
    """ Test that a user data file can be specified from cloud configuration
    and then rendered with config and argument based parameters"""

    hostname = 'staging-test-host-001'

    desired_subs = {
        'key_1': 'config_value_1',
        'key_2': 'argument_value_2',
        'hostname': hostname,
    }

    config_params = {
        'key_1': 'config_value_1',
        'key_2': 'config_value_2',
    }
    config_ud_url = 'http://this.should.not.be.requested.com/user-data.txt'
    config.get_cloud().update({
        'DEFAULT_USER_DATA': config_ud_url,
        'USER_DATA_PARAMS': config_params,
    })

    cli_params = csv_dict('key_2=argument_value_2')
    cli_uri = 'http://this.should.be.requested.com/user-data.txt'
    uri = config.get_cloud_config_value('DEFAULT_USER_DATA', override=cli_uri)

    ud_contents = ""
    for key in desired_subs.keys():
        ud_contents += "{{%s}} " % key
    req.return_value = Mock(text=ud_contents, status_code=200)

    user_data = get_parsed_document(entity_name=hostname,
                                    uri=uri,
                                    config_params_key='USER_DATA_PARAMS',
                                    additional_params=cli_params)

    assert_called_with(req, uri)
    for value in desired_subs.values():
        assert value in user_data
Example #6
0
def test_arg_specified_url_source(req, minimum_config_fixture):
    """ Test that a user data file can be specified from cloud configuration
    and then rendered with config and argument based parameters"""

    hostname = 'staging-test-host-001'

    desired_subs = {
        'key_1': 'config_value_1',
        'key_2': 'argument_value_2',
        'hostname': hostname,
    }

    config_params = {
        'key_1': 'config_value_1',
        'key_2': 'config_value_2',
    }
    config_ud_url = 'http://this.should.not.be.requested.com/user-data.txt'
    config.get_cloud().update({
        'DEFAULT_USER_DATA': config_ud_url,
        'USER_DATA_PARAMS': config_params,
    })

    cli_params = csv_dict('key_2=argument_value_2')
    cli_uri = 'http://this.should.be.requested.com/user-data.txt'
    uri = config.get_cloud_config_value('DEFAULT_USER_DATA', override=cli_uri)

    ud_contents = ""
    for key in desired_subs.keys():
        ud_contents += "{{%s}} " % key
    req.return_value = Mock(text=ud_contents, status_code=200)

    user_data = get_parsed_document(entity_name=hostname, uri=uri,
                                    config_params_key='USER_DATA_PARAMS',
                                    additional_params=cli_params)

    assert_called_with(req, uri)
    for value in desired_subs.values():
        assert value in user_data
Example #7
0
def launch(args):
    """ Launch instances """
    cloud_config = config_proxy.get_cloud(args.cloud)
    cloud = get_current_cloud(args.cloud)

    # Instantiate DNS
    dns = DNS(cloud_config['AWS_ACCESS_KEY_ID'],
              cloud_config['AWS_SECRET_ACCESS_KEY'])

    # Server Type
    server_type = ("-").join(args.env_type.split("-")[-2:])

    # Instance Full Name
    zone_name = cloud_config['DNS_ZONE']

    full_instance_name = dns.get_next_host(
        args.env_type,
        zone_name
    )

    # Owner
    username = os.environ.get('USER')

    # Instance Size
    if args.size is None:
        sizes = config_proxy.SIZES
        default_size = sizes['default']
        size = sizes.get(server_type, default_size)
    else:
        size = args.size

    # Security Group List
    if args.security_groups is None:
        security_groups = [server_type, 'gonzo']
    else:
        security_groups = args.security_groups

    # Instance Base Image
    if args.image_id is None:
        image_id = cloud_config['IMAGE_ID']
    else:
        image_id = args.image_id

    # User Data
    if args.user_data_params is None:
        user_data_params = cloud_config.get('USER_DATA_PARAMS')
    else:
        user_data_params = args.user_data_params

    if not args.user_data_uri:
        user_data_uri = cloud_config.get('DEFAULT_USER_DATA')
        if user_data_uri is None:
            user_data = {}
        else:
            user_data = get_parsed_document(
                full_instance_name, user_data_uri,
                'USER_DATA_PARAMS', user_data_params
            )

    # Launch Instance
    instance = cloud.create_instance(
        name=full_instance_name,
        size=size,
        user_data=user_data,
        image_name=image_id,
        security_groups=security_groups,
        owner=username,
        key_name=cloud_config.get('PUBLIC_KEY_NAME'),
        volume_size=args.volume_size
    )

    print "Instance created: {}.{}".format(
        instance.name,
        cloud_config['DNS_ZONE']
    )

    dns.create_dns_record(instance.name,
                          instance.extra['gonzo_network_address'],
                          cloud_config['DNS_TYPE'],
                          cloud_config['DNS_ZONE'])
Example #8
0
def launch(args):
    """ Launch instances """
    cloud_config = config_proxy.get_cloud(args.cloud)
    cloud = get_current_cloud(args.cloud)

    # Instantiate DNS
    dns = DNS(cloud_config['AWS_ACCESS_KEY_ID'],
              cloud_config['AWS_SECRET_ACCESS_KEY'])

    # Server Type
    server_type = ("-").join(args.env_type.split("-")[-2:])

    # Instance Full Name
    zone_name = cloud_config['DNS_ZONE']

    full_instance_name = dns.get_next_host(args.env_type, zone_name)

    # Owner
    username = os.environ.get('USER')

    # Instance Size
    if args.size is None:
        sizes = config_proxy.SIZES
        default_size = sizes['default']
        size = sizes.get(server_type, default_size)
    else:
        size = args.size

    # Security Group List
    if args.security_groups is None:
        security_groups = [server_type, 'gonzo']
    else:
        security_groups = args.security_groups

    # Instance Base Image
    if args.image_id is None:
        image_id = cloud_config['IMAGE_ID']
    else:
        image_id = args.image_id

    # User Data
    if args.user_data_params is None:
        user_data_params = cloud_config.get('USER_DATA_PARAMS')
    else:
        user_data_params = args.user_data_params

    if not args.user_data_uri:
        user_data_uri = cloud_config.get('DEFAULT_USER_DATA')
        if user_data_uri is None:
            user_data = {}
        else:
            user_data = get_parsed_document(full_instance_name, user_data_uri,
                                            'USER_DATA_PARAMS',
                                            user_data_params)

    # Launch Instance
    instance = cloud.create_instance(
        name=full_instance_name,
        size=size,
        user_data=user_data,
        image_name=image_id,
        security_groups=security_groups,
        owner=username,
        key_name=cloud_config.get('PUBLIC_KEY_NAME'),
        volume_size=args.volume_size)

    print "Instance created: {}.{}".format(instance.name,
                                           cloud_config['DNS_ZONE'])

    dns.create_dns_record(instance.name,
                          instance.extra['gonzo_network_address'],
                          cloud_config['DNS_TYPE'], cloud_config['DNS_ZONE'])