Example #1
0
def create(ctx, **kwargs):

    private_key_path = _get_private_key_path()
    pk_exists = _check_private_key_exists(private_key_path)

    if ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY] is True:
        if not pk_exists:
            delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
            raise NonRecoverableError(
                'Failed to use external keypair (node {0}): the public key {1}'
                ' is available on Openstack, but the private key could not be '
                'found at {2}'.format(ctx.node.id,
                                      ctx.node.properties['resource_id'],
                                      private_key_path))
        return

    if pk_exists:
        raise NonRecoverableError(
            "Can't create keypair - private key path already exists: {0}".
            format(private_key_path))

    ctx.logger.info("Initializing {0} cloud driver".format(
        Provider.CLOUDSTACK))
    cloud_driver = get_cloud_driver(ctx)

    keypair = {
        'name': get_resource_id(ctx, KEYPAIR_CLOUDSTACK_TYPE),
    }
    keypair.update(ctx.node.properties['keypair'])
    #transform_resource_name(ctx, keypair)

    keypair = cloud_driver.create_key_pair(keypair['name'])

    # Cloudstack does not have an ID on keypair, so using name instead,
    # which is unique
    ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = keypair.name
    ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        KEYPAIR_CLOUDSTACK_TYPE
    ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = keypair.name

    try:
        # write private key file
        _mkdir_p(os.path.dirname(private_key_path))
        with open(private_key_path, 'w') as f:
            f.write(keypair.private_key)
            os.fchmod(f.fileno(), stat.S_IRUSR | stat.S_IWUSR)
    except Exception:
        _delete_private_key_file()
        delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
        raise
def create(ctx, **kwargs):

    private_key_path = _get_private_key_path()
    pk_exists = _check_private_key_exists(private_key_path)

    if ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY] is True:
        if not pk_exists:
            delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
            raise NonRecoverableError(
                'Failed to use external keypair (node {0}): the public key {1}'
                ' is available on Openstack, but the private key could not be '
                'found at {2}'.format(ctx.node.id,
                                      ctx.node.properties['resource_id'],
                                      private_key_path))
        return

    if pk_exists:
        raise NonRecoverableError(
            "Can't create keypair - private key path already exists: {0}"
            .format(private_key_path))

    ctx.logger.info("Initializing {0} cloud driver"
                    .format(Provider.CLOUDSTACK))
    cloud_driver = get_cloud_driver(ctx)

    keypair = {
        'name': get_resource_id(ctx, KEYPAIR_CLOUDSTACK_TYPE),
    }
    keypair.update(ctx.node.properties['keypair'])

    keypair = cloud_driver.create_key_pair(keypair['name'])

    # Cloudstack does not have an ID on keypair, so using name instead,
    # which is unique
    ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = keypair.name
    ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        KEYPAIR_CLOUDSTACK_TYPE
    ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = keypair.name

    try:
        # write private key file
        _mkdir_p(os.path.dirname(private_key_path))
        with open(private_key_path, 'w') as f:
            f.write(keypair.private_key)
            os.fchmod(f.fileno(), stat.S_IRUSR | stat.S_IWUSR)
    except Exception:
        _delete_private_key_file()
        delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
        raise
def create(ctx, **kwargs):
    """ Create vpc with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    vpc = {
        'description': None,
        'name': get_resource_id(ctx, VPC_CLOUDSTACK_TYPE),
    }

    ctx.logger.debug('reading vpc configuration.')
    vpc.update(ctx.node.properties['network'])

    vpc_name = vpc['name']
    cidr = vpc['cidr']
    zone = vpc['zone']
    location = get_location(cloud_driver, zone)
    vpcoffer = vpc['service_offering']
    vpc_offering = get_vpc_offering(cloud_driver, vpcoffer)

    ctx.logger.info('Creating VPC {0}'.format(vpc_name))

    if not vpc_exists(cloud_driver, vpc_name):
        ctx.logger.info('creating vpc: {0}'.format(vpc_name))

        vpc = cloud_driver.ex_create_vpc(
            cidr=cidr,
            name=vpc_name,
            display_text=vpc_name,
            vpc_offering=vpc_offering,
            zone_id=location.id)
    else:
        ctx.logger.info('Using existing vpc network {0}'.
                        format(vpc_name))
        vpc = get_vpc(cloud_driver, vpc_name)

    ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = vpc.id
    ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
        vpc.name
    ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        VPC_CLOUDSTACK_TYPE
def create(ctx, **kwargs):
    """ Create network with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    network = {
        'description': None,
        'name': get_resource_id(ctx, NETWORK_CLOUDSTACK_TYPE),
    }

    ctx.logger.debug('reading network configuration.')
    network.update(copy.deepcopy(ctx.node.properties['network']))

    network_name = network['name']
    zone = network['zone']
    location = get_location(cloud_driver, zone)
    netoffer = network['service_offering']
    network_offering = get_network_offering(cloud_driver, netoffer)
    existing_net = network_exists(cloud_driver, network_name)

    # TODO, bit messy below, should be reviewed.

    if 'vpc' in network:
        if network['vpc']:
            vpc = get_vpc(cloud_driver, network['vpc'])
            ctx.logger.info('DEBUG: VPC id: {0} '.format(vpc.id))
    else:
        vpc = None

    if not existing_net and ctx.node.properties[
            USE_EXTERNAL_RESOURCE_PROPERTY] is False:

        if vpc:
            ctx.logger.info('Creating network: {0} in VPC with ID: {1}'.format(
                network_name, vpc.id))

            net = cloud_driver.ex_create_network(
                display_text=network_name,
                name=network_name,
                network_offering=network_offering,
                location=location,
                gateway=network.get(['gateway'][0], None),
                netmask=network.get(['netmask'][0], None),
                vpc_id=vpc.id)

            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
            ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
                net.name
            ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                NETWORK_CLOUDSTACK_TYPE

            # Create ACL for the network if it's is part of a VPC
            acl_list = create_acl_list(cloud_driver, network_name, vpc.id,
                                       net.id)

            if 'firewall' in ctx.node.properties:
                firewall_config = ctx.node.properties['firewall']

                for acl in firewall_config:
                    acl_cidr = acl.get('cidr')
                    acl_protocol = acl.get('protocol')
                    acl_ports = acl.get('ports')
                    acl_type = acl.get('type')

                    for port in acl_ports:
                        create_acl(cloud_driver, acl_protocol, acl_list.id,
                                   acl_cidr, port, port, acl_type)

        else:
            ctx.logger.info('Creating network: {0}'.format(network_name))

            net = cloud_driver.ex_create_network(
                display_text=network_name,
                name=network_name,
                gateway=network.get(['gateway'][0], None),
                netmask=network.get(['netmask'][0], None),
                network_offering=network_offering,
                location=location,
            )

            ctx.logger.info('Created Network: {0}'.format(net.name))

            # Create egress rules only as they are part of a network,
            # ingress rules are bound to a floating/public_ip so,
            # this will get arranged on the floating ip relationship

            _create_egress_rules(ctx, cloud_driver, net.id)

            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
            ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
                net.name
            ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                NETWORK_CLOUDSTACK_TYPE

    elif existing_net and ctx.node.properties[
            USE_EXTERNAL_RESOURCE_PROPERTY] is False:

        net = get_network(cloud_driver, network_name)

        ctx.logger.info('Using existing network: {0}'.format(network_name))

        _create_egress_rules(ctx, cloud_driver, net.id)

        ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
        ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
            net.name
        ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
            NETWORK_CLOUDSTACK_TYPE

    elif existing_net and ctx.node.properties[
            USE_EXTERNAL_RESOURCE_PROPERTY] is True:

        net = get_network(cloud_driver, network_name)

        ctx.logger.warn('Using existing network: {0} while use'
                        '_external_resource'
                        ' is set to False, no egress rules will '
                        'be configured'.format(network_name))

        ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
        ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
            net.name
        ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
            NETWORK_CLOUDSTACK_TYPE
def create(ctx, **kwargs):

    network_ids = get_cloudstack_ids_of_connected_nodes_by_cloudstack_type(
        ctx, NETWORK_CLOUDSTACK_TYPE)

    provider_context = provider(ctx)

    ctx.logger.info('Network IDs: {0}'.format(network_ids))

    # Cloudstack does not support _underscore in vm-name

    server_config = {
        'name': get_resource_id(ctx, SERVER_CLOUDSTACK_TYPE).replace('_', '-')
    }
    server_config.update(copy.deepcopy(ctx.node.properties['server']))

    ctx.logger.info("Initializing {0} cloud driver"
                    .format(Provider.CLOUDSTACK))
    cloud_driver = get_cloud_driver(ctx)

    # TODO Currently a generated network name (resource_id) \
    # TODO is not support for the default network

    network_config = ctx.node.properties['network']
    name = server_config['name']
    image_id = server_config['image_id']
    size_name = server_config['size']
    zone = server_config.get(['zone'][0], None)

    if zone is not None:
        location = get_location(cloud_driver, zone)
    else:
        location = None

    # server keypair handling
    # Cloudstack does not have id's for keys, just unique names which we store
    # as id.
    keypair_id = get_cloudstack_ids_of_connected_nodes_by_cloudstack_type(
        ctx, KEYPAIR_CLOUDSTACK_TYPE)

    if 'key_name' in server_config:
        if keypair_id:
            raise NonRecoverableError("server can't both have the "
                                      '"key_name" nested property and be '
                                      'connected to a keypair via a '
                                      'relationship at the same time')
        #server_config['key_name'] = rename(server_config['key_name'])
    elif keypair_id:

        # TODO pointfix, this must be UTF8, otherwise cloudstack interface breaks

        keyname = keypair_id[0].encode('UTF8')
        server_config['key_name'] = keyname

    elif provider_context.agents_keypair:
        server_config['key_name'] = provider_context.agents_keypair['name']
        print ('provider ')
    else:
        raise NonRecoverableError(
            'server must have a keypair, yet no keypair was connected to the '
            'server node, the "key_name" nested property'
            "wasn't used, and there is no agent keypair in the provider "
            "context")

   #keypair_name = server_config['keypair_name']
    keypair_name = server_config['key_name']
    default_security_group = network_config.get(['default_security_group'][0],
                                                None)
    default_network = network_config.get(['default_network'][0], None)
    ip_address = network_config.get(['ip_address'][0], None)
    external_id = ctx.instance.runtime_properties.get(
        [CLOUDSTACK_ID_PROPERTY][0], None)

    if external_id is not None:
        if get_vm_by_id(ctx, cloud_driver, ctx.instance.runtime_properties[
                CLOUDSTACK_ID_PROPERTY]):

            ctx.logger.info('VM already created, skipping creation')

            return

    ctx.logger.info('Getting service_offering: {0}'.format(size_name))
    sizes = [size for size in cloud_driver.list_sizes() if size.name
             == size_name]
    if sizes is None:
        raise RuntimeError(
            'Could not find service_offering with name {0}'.format(size_name))
    size = sizes[0]

    ctx.logger.info('Getting required image with ID {0}'.format(image_id))
    images = [template for template in cloud_driver.list_images()
              if image_id == template.id]
    if images is None:
        raise RuntimeError('Could not find image with ID {0}'.format(image_id))
    image = images[0]

    # TODO add check if default network is really existing!

    if default_network is None:
        if default_security_group is None:
            raise RuntimeError("We need either a default_security_group "
                               "or default_network, "
                               "none specified")

    if default_network is not None:
        if default_security_group is not None:
            raise RuntimeError("We need either a default_security_group "
                               "or default_network, "
                               "both are specified")

    if default_network is not None:

        _create_in_network(ctx=ctx,
                           cloud_driver=cloud_driver,
                           name=name,
                           image=image,
                           size=size,
                           keypair_name=keypair_name,
                           network_ids=network_ids,
                           default_network=default_network,
                           ip_address=ip_address,
                           location=location)

    if default_security_group is not None:
        ctx.logger.info('Creating this VM in default_security_group.'.
                        format(default_security_group))
        ctx.logger.info("Creating VM with the following details: {0}".format(
            server_config))
        _create_in_security_group(ctx=ctx,
                                  cloud_driver=cloud_driver,
                                  name=name,
                                  image=image,
                                  size=size,
                                  keypair_name=keypair_name,
                                  default_security_group_name=
                                  default_security_group,
                                  ip_address=ip_address,
                                  location=location)
def create(ctx, **kwargs):
    """ Create network with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    network = {
        'description': None,
        'name': get_resource_id(ctx, NETWORK_CLOUDSTACK_TYPE),
    }

    ctx.logger.debug('reading network configuration.')
    network.update(copy.deepcopy(ctx.node.properties['network']))

    network_name = network['name']
    zone = network['zone']
    location = get_location(cloud_driver, zone)
    netoffer = network['service_offering']
    network_offering = get_network_offering(cloud_driver, netoffer)
    existing_net = network_exists(cloud_driver, network_name)

    # TODO, bit messy below, should be reviewed.

    if 'vpc' in network:
        if network['vpc']:
            vpc = get_vpc(cloud_driver, network['vpc'])
            ctx.logger.info('DEBUG: VPC id: {0} '.format(vpc.id))
    else:
        vpc = None

    if not existing_net and ctx.node.properties[
            USE_EXTERNAL_RESOURCE_PROPERTY] is False:

        if vpc:
            ctx.logger.info('Creating network: {0} in VPC with ID: {1}'.
                            format(network_name, vpc.id))

            net = cloud_driver.ex_create_network(
                display_text=network_name,
                name=network_name,
                network_offering=network_offering,
                location=location,
                gateway=network.get(['gateway'][0], None),
                netmask=network.get(['netmask'][0], None),
                vpc_id=vpc.id)

            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
            ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
                net.name
            ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                NETWORK_CLOUDSTACK_TYPE

            # Create ACL for the network if it's is part of a VPC
            acl_list = create_acl_list(cloud_driver, network_name,
                                       vpc.id, net.id)

            if 'firewall' in ctx.node.properties:
                firewall_config = ctx.node.properties['firewall']

                for acl in firewall_config:
                    acl_cidr = acl.get('cidr')
                    acl_protocol = acl.get('protocol')
                    acl_ports = acl.get('ports')
                    acl_type = acl.get('type')

                    for port in acl_ports:
                        create_acl(cloud_driver, acl_protocol, acl_list.id,
                                   acl_cidr, port, port, acl_type)

        else:
            ctx.logger.info('Creating network: {0}'.format(network_name))

            net = cloud_driver.ex_create_network(
                display_text=network_name,
                name=network_name,
                gateway=network.get(['gateway'][0], None),
                netmask=network.get(['netmask'][0], None),
                network_offering=network_offering,
                location=location,)

            ctx.logger.info('Created Network: {0}'.format(net.name))

            # Create egress rules only as they are part of a network,
            # ingress rules are bound to a floating/public_ip so,
            # this will get arranged on the floating ip relationship

            _create_egress_rules(ctx, cloud_driver, net.id)

            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
            ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
                net.name
            ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                NETWORK_CLOUDSTACK_TYPE

    elif existing_net and ctx.node.properties[
            USE_EXTERNAL_RESOURCE_PROPERTY] is False:

            net = get_network(cloud_driver, network_name)

            ctx.logger.info('Using existing network: {0}'.
                            format(network_name))

            _create_egress_rules(ctx, cloud_driver, net.id)

            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
            ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
                net.name
            ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                NETWORK_CLOUDSTACK_TYPE

    elif existing_net and ctx.node.properties[
            USE_EXTERNAL_RESOURCE_PROPERTY] is True:

        net = get_network(cloud_driver, network_name)

        ctx.logger.warn('Using existing network: {0} while use'
                        '_external_resource'
                        ' is set to False, no egress rules will '
                        'be configured'.format(network_name))

        ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = net.id
        ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
            net.name
        ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
            NETWORK_CLOUDSTACK_TYPE
def create(ctx, **kwargs):

    network_ids = get_cloudstack_ids_of_connected_nodes_by_cloudstack_type(
        ctx, NETWORK_CLOUDSTACK_TYPE)

    provider_context = provider(ctx)

    ctx.logger.info('Network IDs: {0}'.format(network_ids))

    # Cloudstack does not support _underscore in vm-name

    server_config = {
        'name': get_resource_id(ctx, SERVER_CLOUDSTACK_TYPE).replace('_', '-')
    }
    server_config.update(copy.deepcopy(ctx.node.properties['server']))

    ctx.logger.info("Initializing {0} cloud driver"
                    .format(Provider.CLOUDSTACK))
    cloud_driver = get_cloud_driver(ctx)

    # TODO Currently a generated network name (resource_id) \
    # TODO is not support for the default network

    network_config = ctx.node.properties['network']
    name = server_config['name']
    image_id = server_config['image_id']
    size_name = server_config['size']
    zone = server_config.get(['zone'][0], None)

    if zone is not None:
        location = get_location(cloud_driver, zone)
    else:
        location = None

    # server keypair handling
    # Cloudstack does not have id's for keys, just unique names which we store
    # as id.
    keypair_id = get_cloudstack_ids_of_connected_nodes_by_cloudstack_type(
        ctx, KEYPAIR_CLOUDSTACK_TYPE)

    if 'key_name' in server_config:
        if keypair_id:
            raise NonRecoverableError("server can't both have the "
                                      '"key_name" nested property and be '
                                      'connected to a keypair via a '
                                      'relationship at the same time')
    elif keypair_id:

        # TODO pointfix, this must be UTF8,
        # otherwise cloudstack interface breaks

        keyname = keypair_id[0].encode('UTF8')
        server_config['key_name'] = keyname

    elif provider_context.agents_keypair:
        server_config['key_name'] = provider_context.agents_keypair['name']
        print ('provider ')
    else:
        raise NonRecoverableError(
            'server must have a keypair, yet no keypair was connected to the '
            'server node, the "key_name" nested property'
            "wasn't used, and there is no agent keypair in the provider "
            "context")

    keypair_name = server_config['key_name']
    default_security_group = network_config.get(['default_security_group'][0],
                                                None)
    default_network = network_config.get(['default_network'][0], None)
    ip_address = network_config.get(['ip_address'][0], None)
    external_id = ctx.instance.runtime_properties.get(
        [CLOUDSTACK_ID_PROPERTY][0], None)

    if external_id is not None:
        if get_vm_by_id(ctx, cloud_driver, ctx.instance.runtime_properties[
                CLOUDSTACK_ID_PROPERTY]):

            ctx.logger.info('VM already created, skipping creation')

            return

    ctx.logger.info('Getting service_offering: {0}'.format(size_name))
    sizes = [size for size in cloud_driver.list_sizes()
             if size.name == size_name]
    if sizes is None:
        raise RuntimeError(
            'Could not find service_offering with name {0}'.format(size_name))
    size = sizes[0]

    ctx.logger.info('Getting required image with ID {0}'.format(image_id))
    images = [template for template in cloud_driver.list_images()
              if image_id == template.id]
    if images is None:
        raise RuntimeError('Could not find image with ID {0}'.format(image_id))
    image = images[0]

    # TODO add check if default network is really existing!

    if default_network is None:
        if default_security_group is None:
            raise RuntimeError("We need either a default_security_group "
                               "or default_network, "
                               "none specified")

    if default_network is not None:
        if default_security_group is not None:
            raise RuntimeError("We need either a default_security_group "
                               "or default_network, "
                               "both are specified")

    if default_network is not None:

        _create_in_network(ctx=ctx,
                           cloud_driver=cloud_driver,
                           name=name,
                           image=image,
                           size=size,
                           keypair_name=keypair_name,
                           network_ids=network_ids,
                           default_network=default_network,
                           ip_address=ip_address,
                           location=location)

    if default_security_group is not None:
        ctx.logger.info('Creating this VM in default_security_group.'.
                        format(default_security_group))
        ctx.logger.info("Creating VM with the following details: {0}".format(
            server_config))
        _create_in_security_group(
            ctx=ctx,
            cloud_driver=cloud_driver,
            name=name,
            image=image,
            size=size,
            keypair_name=keypair_name,
            default_security_group_name=default_security_group,
            ip_address=ip_address,
            location=location)