Ejemplo n.º 1
0
def start_new_server(ctx, server_client, **kwargs):
    provider_context = provider(ctx)

    server = {
        'name': ctx.instance.id
    }
    server.update(copy.deepcopy(ctx.node.properties['server']))
    transform_resource_name(server, ctx)

    ctx.logger.info("Creating VM")

    server = server_client.create(ctx.instance.id,
                                  ctx,
                                  server,
                                  provider_context)
    server_client.wait_for_server_to_be_running(server, TIMEOUT, SLEEP_TIME)

    ctx.instance.runtime_properties[LIBCLOUD_SERVER_ID_PROPERTY] = server.id
Ejemplo n.º 2
0
 def rename(name):
     return transform_resource_name(name, ctx)
Ejemplo n.º 3
0
def create(ctx, security_group_client, **kwargs):
    security_group = {
        'description': None,
        'name': ctx.instance.id,
    }

    security_group.update(ctx.node.properties['security_group'])
    transform_resource_name(security_group, ctx)

    existing_sg = _find_existing_sg(ctx, security_group_client,
                                    security_group['name'])
    if existing_sg:
        existing_description = security_group_client\
            .get_description(existing_sg)
        if existing_description and existing_description.lower() == 'none':
            existing_description = None
        description_to_create = security_group['description']
        if existing_description != description_to_create:
            raise NonRecoverableError("Descriptions of existing security group"
                                      " and the security group to be created "
                                      "do not match while the names do match."
                                      " Security group name: {0}".format(
                                          security_group['name']))

    rules_to_apply = ctx.node.properties['rules']

    security_group_rules = []
    for rule in rules_to_apply:
        ctx.logger.debug(
            "security_group.create() rule before transformations: {0}".format(
                rule))
        sgr = {
            'direction': 'ingress',
            'port_range_max': str(rule.get('port', 65535)),
            'port_range_min': str(rule.get('port', 1)),
            'protocol': 'tcp',
            'remote_group_id': None,
            'remote_ip_prefix': str('0.0.0.0/0'),
        }
        sgr.update(rule)

        if 'port' in sgr:
            del sgr['port']

        if ('remote_group_node' in sgr) and sgr['remote_group_node']:
            _, remote_group_node = _capabilities_of_node_named(
                sgr['remote_group_node'], ctx)
            sgr['remote_ip_prefix'] = remote_group_node.ip
            del sgr['remote_group_node']
            del sgr['remote_ip_prefix']

        if ('remote_group_name' in sgr) and sgr['remote_group_name']:
            sgroups = security_group_client\
                .get_list_by_name(sgr['remote_group_name'])
            sg_count = len(sgroups)
            if sg_count > 1:
                raise NonRecoverableError('More than one security group found'
                                          ' for remote_group_name: {0}'.format(
                                              sgr['remote_group_name']))
            elif sg_count < 1:
                raise NonRecoverableError('None security group found'
                                          ' for remote_group_name: {0}'.format(
                                              sgr['remote_group_name']))
            sgr['remote_group_id'] = security_group_client.get_id(sgroups[0])
            del sgr['remote_group_name']
            del sgr['remote_ip_prefix']

        ctx.logger.debug(
            "security_group.create() rule after transformations: {0}".format(
                sgr))
        security_group_rules.append(sgr)

    if existing_sg:
        r1 = security_group_client.get_rules(existing_sg)
        r2 = security_group_rules
        if _sg_rules_are_equal(r1, r2):
            existing_sg_id = security_group_client.get_id(existing_sg)
            ctx.logger.info("Using existing security group named '{0}' with "
                            "id {1}".format(security_group['name'],
                                            existing_sg_id))
            ctx.instance.runtime_properties['external_id'] = existing_sg_id
            return
        else:
            raise RulesMismatchError("Rules of existing security group"
                                     " and the security group to be created "
                                     "or used do not match while the names "
                                     "do match. Security group name: '{0}'. "
                                     "Existing rules: {1}. "
                                     "Requested/expected rules: {2} "
                                     "".format(security_group['name'], r1, r2))

    sg = security_group_client.create(security_group)
    sg_id = security_group_client.get_id(sg)

    for sgr in security_group_rules:
        sgr['security_group_id'] = sg_id
        security_group_client.create_security_group_rule(sgr)
    ctx.instance.runtime_properties['external_id'] = sg_id
Ejemplo n.º 4
0
 def rename(name):
     return transform_resource_name(name, ctx)
def create(ctx, security_group_client, **kwargs):
    security_group = {
        'description': None,
        'name': ctx.instance.id,
    }

    security_group.update(ctx.node.properties['security_group'])
    transform_resource_name(security_group, ctx)

    existing_sg = _find_existing_sg(ctx,
                                    security_group_client,
                                    security_group['name'])
    if existing_sg:
        existing_description = security_group_client\
            .get_description(existing_sg)
        if existing_description and existing_description.lower() == 'none':
            existing_description = None
        description_to_create = security_group['description']
        if existing_description != description_to_create:
            raise NonRecoverableError("Descriptions of existing security group"
                                      " and the security group to be created "
                                      "do not match while the names do match."
                                      " Security group name: {0}".format(
                                          security_group['name']))

    rules_to_apply = ctx.node.properties['rules']

    security_group_rules = []
    for rule in rules_to_apply:
        ctx.logger.debug(
            "security_group.create() rule before transformations: {0}".format(
                rule))
        sgr = {
            'direction': 'ingress',
            'port_range_max': str(rule.get('port', 65535)),
            'port_range_min': str(rule.get('port', 1)),
            'protocol': 'tcp',
            'remote_group_id': None,
            'remote_ip_prefix': str('0.0.0.0/0'),
        }
        sgr.update(rule)

        if 'port' in sgr:
            del sgr['port']

        if ('remote_group_node' in sgr) and sgr['remote_group_node']:
            _, remote_group_node = _capabilities_of_node_named(
                sgr['remote_group_node'], ctx)
            sgr['remote_ip_prefix'] = remote_group_node.ip
            del sgr['remote_group_node']
            del sgr['remote_ip_prefix']

        if ('remote_group_name' in sgr) and sgr['remote_group_name']:
            sgroups = security_group_client\
                .get_list_by_name(sgr['remote_group_name'])
            sg_count = len(sgroups)
            if sg_count > 1:
                raise NonRecoverableError('More than one security group found'
                                          ' for remote_group_name: {0}'
                                          .format(sgr['remote_group_name']))
            elif sg_count < 1:
                raise NonRecoverableError('None security group found'
                                          ' for remote_group_name: {0}'
                                          .format(sgr['remote_group_name']))
            sgr['remote_group_id'] = security_group_client.get_id(sgroups[0])
            del sgr['remote_group_name']
            del sgr['remote_ip_prefix']

        ctx.logger.debug(
            "security_group.create() rule after transformations: {0}".format(
                sgr))
        security_group_rules.append(sgr)

    if existing_sg:
        r1 = security_group_client.get_rules(existing_sg)
        r2 = security_group_rules
        if _sg_rules_are_equal(r1, r2):
            existing_sg_id = security_group_client.get_id(existing_sg)
            ctx.logger.info("Using existing security group named '{0}' with "
                            "id {1}".format(
                                security_group['name'],
                                existing_sg_id))
            ctx.instance.runtime_properties['external_id'] = existing_sg_id
            return
        else:
            raise RulesMismatchError("Rules of existing security group"
                                     " and the security group to be created "
                                     "or used do not match while the names "
                                     "do match. Security group name: '{0}'. "
                                     "Existing rules: {1}. "
                                     "Requested/expected rules: {2} "
                                     "".format(
                                         security_group['name'],
                                         r1,
                                         r2))

    sg = security_group_client.create(security_group)
    sg_id = security_group_client.get_id(sg)

    for sgr in security_group_rules:
        sgr['security_group_id'] = sg_id
        security_group_client.create_security_group_rule(sgr)
    ctx.instance.runtime_properties['external_id'] = sg_id