def disconnect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    fip_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    fip = get_floating_ip_by_id(ctx, cloud_driver, fip_id)

    firewall_rules = [rule for rule in cloud_driver.ex_list_firewall_rules()
                      if fip.id == rule.address.id]

    for rule in firewall_rules:

        ctx.logger.info('Deleting fw rule: {3}:{0}:{1}-{2}'.format(
            rule.cidr_list, rule.start_port, rule.end_port, rule.protocol))

        cloud_driver.ex_delete_firewall_rule(rule)

    ctx.logger.info('Deleting floating ip: {0}'.format(fip))

    try:
        cloud_driver.ex_release_public_ip(address=fip)
    except Exception as e:
        ctx.logger.warn('Floating IP: {0} may not have been deleted: {1}'
                        .format(fip, str(e)))
        pass
Ejemplo n.º 2
0
def delete_vpn_customer_gateway(**kwargs):
    """
    Deletes a VPN Customer Gateway.
    """
    cloud_driver = get_cloud_driver(ctx)

    id_ = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    vpn_customer_gateway = get_vpn_customer_gateway(cloud_driver, id_)

    if not vpn_customer_gateway:
        raise NonRecoverableError(
            'Could not find VPN Customer Gateway with id={0}'.format(id_))

    use_external = ctx.source.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if not use_external:
        for obj in get_vpn_connections():
            if obj.vpn_customer_gateway_id == id_:
                raise NonRecoverableError(
                    'Could not delete VPN Customer Gateway id={0} because '
                    'of VPN Connection id={1}'.format(id_, obj.id))

        vpn_customer_gateway.delete()

        ctx.logger.info('Deleted VPN Customer Gateway id={0}'.format(id_))
    else:
        ctx.logger.info(
            'Did not delete VPN Customer Gateway id={0}'.format(id_))
def delete(ctx, **kwargs):

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

    server_config = ctx.node.properties['server']
    expunge = server_config.get(['expunge'][0], False)

    cloud_driver = get_cloud_driver(ctx)

    instance_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    if instance_id is None:
        raise NameError('Could not find node ID in runtime context: {0} '
                        .format(instance_id))

    node = get_vm_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        raise NameError('Could not find node with ID: {0} '
                        .format(instance_id))

    ctx.logger.info('destroying vm: {0} expunge {1}'.format(node.name,
                                                            expunge))
    cloud_driver.destroy_node(node)

    delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
def delete(ctx, **kwargs):

    network_name = ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY]
    cloud_driver = get_cloud_driver(ctx)
    network = get_network(cloud_driver, network_name)

    if not ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY] is True:

        # firewall_rules = [rule for rule in cloud_driver.
        #                   ex_list_egress_firewall_rules() if
        #                   network.id == rule.network_id]
        #
        # for rule in firewall_rules:
        #
        #     ctx.logger.info('Deleting egress fw rule: {3}:{0}:{1}-{2} '
        #                     'from network: {4}'.format(
        #                     rule.cidr_list, rule.start_port, rule.end_port,
        #                     rule.protocol, network_name))
        #
        #     cloud_driver.ex_delete_egress_firewall_rule(rule)

        try:

            cloud_driver.ex_delete_network(network)
        except Exception as e:
            ctx.logger.warn('Network {0} may not have been deleted: {1}'
                            .format(network_name, str(e)))
            return False
            pass
    else:

        ctx.logger.info('This is an external resource only removing '
                        'runtime props')
    delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
    return True
Ejemplo n.º 5
0
def connect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    network_id = ctx.related.runtime_properties['network_id']
    network = get_network_by_id(ctx, cloud_driver, network_id)

    if network.extra['vpc_id'] is not None:

        ctx.logger.info('Acquiring IP for VPC with id: {0}'
                        .format(network.extra['vpc_id']))

        fip = cloud_driver.ex_allocate_public_ip(vpc_id=
                                                 network.extra['vpc_id'])

    elif network.id is not None:

        ctx.logger.info('Acquiring IP for network with id: {0}'.
                        format(network.id))

        fip = cloud_driver.ex_allocate_public_ip(network_id=network.id)

    else:
        raise NonRecoverableError('Cannot resolve network or vpc id')

    ctx.runtime_properties['external_id'] = fip.id
    ctx.runtime_properties['external_type'] = 'publicip'
    ctx.runtime_properties['floating_ip_address'] = fip.address
def get_state(ctx, **kwargs):

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

    instance_id = ctx.runtime_properties['instance_id']
    networking_type = ctx.runtime_properties['networking_type']

    ctx.logger.info('getting node with ID {0}'.format(instance_id))
    node = get_node_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        return False

    if networking_type == 'network':
        ctx.runtime_properties['ip'] = node.private_ips[0]
        #ctx.runtime_properties['ip_address'] = node.private_ips[0]
        ctx.logger.info('instance started successfully with IP {0}'
                        .format(ctx.runtime_properties['ip']))
        return True

    elif networking_type == 'security_group':
        ctx.runtime.properties['ip'] = node.public_ips[0]
        ctx.logger.info('instance started successfully with IP {0}'
                        .format(ctx.runtime_properties['ip']))
        return True

    else:
        ctx.runtime_properties['ip'] = node.private_ips[0]
        ctx.logger.info('Cannot determine networking type,'
                        ' using private_ip as {0} ip'
                        .format(ctx.runtime_properties['ip']))
        return True
def disconnect_floating_ip(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)
    node_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    node = get_vm_by_id(ctx, cloud_driver, node_id)
    portmaps = get_portmaps_by_vm_id(ctx, cloud_driver, node_id)

    for portmap in portmaps:

        try:

            ctx.logger.info('Deleting portmap for node: {0}:{1}-{2} on'
                            ' {3}:{4}-{5}'.
                            format(node.name, portmap.private_port,
                                   portmap.private_end_port,
                                   portmap.address, portmap.public_port,
                                   portmap.public_end_port))

            cloud_driver.ex_delete_port_forwarding_rule(node=node,
                                                        rule=portmap)
        except Exception as e:
            ctx.logger.warn('Port forward may not have been removed: '
                            '{0}'.format(str(e)))
        return False

    return True
def delete(ctx, **kwargs):

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

    server_config = ctx.node.properties['server']
    expunge = server_config.get(['expunge'][0], False)

    cloud_driver = get_cloud_driver(ctx)

    instance_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    if instance_id is None:
        raise NameError('Could not find node ID in runtime context: {0} '
                        .format(instance_id))

    node = get_vm_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        raise NameError('Could not find node with ID: {0} '
                        .format(instance_id))

    ctx.logger.info('destroying vm: {0} expunge {1}'.format(node.name,
                                                            expunge))
    cloud_driver.destroy_node(node)

    delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
def disconnect_floating_ip(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)
    node_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    node = get_vm_by_id(ctx, cloud_driver, node_id)
    portmaps = get_portmaps_by_vm_id(ctx, cloud_driver, node_id)

    for portmap in portmaps:

        try:

            ctx.logger.info('Deleting portmap for node: {0}:{1}-{2} on'
                            ' {3}:{4}-{5}'.
                            format(node.name, portmap.private_port,
                                   portmap.private_end_port,
                                   portmap.address, portmap.public_port,
                                   portmap.public_end_port))

            cloud_driver.ex_delete_port_forwarding_rule(node=node,
                                                        rule=portmap)
        except Exception as e:
            ctx.logger.warn('Port forward may not have been removed: '
                            '{0}'.format(str(e)))
        return False

    return True
Ejemplo n.º 10
0
def delete_vpn_customer_gateway(**kwargs):
    """
    Deletes a VPN Customer Gateway.
    """
    cloud_driver = get_cloud_driver(ctx)

    id_ = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    vpn_customer_gateway = get_vpn_customer_gateway(cloud_driver, id_)

    if not vpn_customer_gateway:
        raise NonRecoverableError("Could not find VPN Customer Gateway with id={0}".format(id_))

    use_external = ctx.source.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if not use_external:
        for obj in get_vpn_connections():
            if obj.vpn_customer_gateway_id == id_:
                raise NonRecoverableError(
                    "Could not delete VPN Customer Gateway id={0} because "
                    "of VPN Connection id={1}".format(id_, obj.id)
                )

        vpn_customer_gateway.delete()

        ctx.logger.info("Deleted VPN Customer Gateway id={0}".format(id_))
    else:
        ctx.logger.info("Did not delete VPN Customer Gateway id={0}".format(id_))
Ejemplo n.º 11
0
def create_vpn_customer_gateway(**kwargs):
    """
    Creates a VPN Customer Gateway.
    """
    cloud_driver = get_cloud_driver(ctx)

    vpn_gateway = get_vpn_gateway(
        cloud_driver,
        ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY],
    )

    if not vpn_gateway:
        raise NonRecoverableError('Could not find VPN Gateway id={0}'.format(
            ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]))
    try:
        id_ = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    except KeyError:
        id_ = None

    exists = vpn_customer_gateway_exists(cloud_driver, id_)
    use_external = ctx.source.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if (not exists) and (not use_external):
        data = {
            'cidr_list': ctx.source.node.properties['cidr_list'],
            'dpd': ctx.source.node.properties['dpd'],
            'esp_lifetime': ctx.source.node.properties['esp_lifetime'],
            'esp_policy': ctx.source.node.properties['esp_policy'],
            'gateway': vpn_gateway.public_ip,
            'ike_lifetime': ctx.source.node.properties['ike_lifetime'],
            'ike_policy': ctx.source.node.properties['ike_policy'],
            'ipsec_psk': ctx.source.node.properties['ipsec_psk'],
        }

        vpn_customer_gateway = cloud_driver.ex_create_vpn_customer_gateway(
            **data)

        ctx.logger.info(
            'Created VPN Customer Gateway id={0} vpn_gateway_id={1}'.format(
                vpn_customer_gateway.id, vpn_gateway.id))

    elif exists and use_external:
        vpn_customer_gateway = get_vpn_customer_gateway(cloud_driver, id_)

        ctx.logger.info('Using VPN Customer Gateway id={0}'.format(
            vpn_customer_gateway.id))

    elif exists and (not use_external):
        raise NonRecoverableError(
            'VPN Customer Gateway already exists id={0}'.format(id_))

    elif (not exists) and use_external:
        raise NonRecoverableError(
            'Could not find VPN Customer Gateway id={0}'.format(id_))

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = \
        vpn_customer_gateway.id
    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        VPN_CUSTOMER_GATEWAY_CLOUDSTACK_TYPE
def get_state(ctx, **kwargs):

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

    instance_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    networking_type = ctx.instance.runtime_properties[
        NETWORKINGTYPE_CLOUDSTACK_TYPE]

    node = get_vm_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        return False

    if networking_type == 'network':

        if ctx.node.properties['management_network_name']:

            ctx.logger.info('Management network defined: {0}'
                            .format(ctx.node.properties[
                                    'management_network_name']))

            mgt_net = get_network(cloud_driver, ctx.node.properties[
                'management_network_name'])

            nic = get_nic_by_node_and_network_id(ctx, cloud_driver, node,
                                                 mgt_net.id)

            ctx.logger.info('CFY will use {0} for management,'
                            ' overwriting previously set value'
                            .format(nic))

            ctx.instance.runtime_properties[IP_PROPERTY] = nic.ip_address

            return True

        else:

            ctx.instance.runtime_properties[IP_PROPERTY] = node.private_ips[0]

            ctx.logger.info('VM {1} started successfully with IP {0}'
                            .format(ctx.instance.runtime_properties[
                                    IP_PROPERTY],
                                    ctx.instance.runtime_properties[
                                        CLOUDSTACK_NAME_PROPERTY]))
            return True

    elif networking_type == 'security_group':
        ctx.runtime.properties[IP_PROPERTY] = node.public_ips[0]
        ctx.logger.info('instance started successfully with IP {0}'
                        .format(ctx.instance.runtime_properties[IP_PROPERTY]))
        return True

    else:
        ctx.instance.runtime_properties[IP_PROPERTY] = node.private_ips[0]
        ctx.logger.info('Cannot determine networking type,'
                        ' using private_ip as {0} ip'
                        .format(ctx.instance.runtime_properties[IP_PROPERTY]))
        return True
def get_state(ctx, **kwargs):

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

    instance_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    networking_type = ctx.instance.runtime_properties[
        NETWORKINGTYPE_CLOUDSTACK_TYPE]

    node = get_vm_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        return False

    if networking_type == 'network':

        if ctx.node.properties['management_network_name']:

            ctx.logger.info('Management network defined: {0}'
                            .format(ctx.node.properties[
                            'management_network_name']))

            mgt_net = get_network(cloud_driver, ctx.node.properties[
                'management_network_name'])

            nic = get_nic_by_node_and_network_id(ctx, cloud_driver, node,
                                                 mgt_net.id)

            ctx.logger.info('CFY will use {0} for management,'
                            ' overwriting previously set value'
                            .format(nic))

            ctx.instance.runtime_properties[IP_PROPERTY] = nic.ip_address

            return True

        else:

            ctx.instance.runtime_properties[IP_PROPERTY] = node.private_ips[0]

            ctx.logger.info('VM {1} started successfully with IP {0}'
                            .format(ctx.instance.runtime_properties[IP_PROPERTY],
                                    ctx.instance.runtime_properties[
                                        CLOUDSTACK_NAME_PROPERTY]))
            return True

    elif networking_type == 'security_group':
        ctx.runtime.properties[IP_PROPERTY] = node.public_ips[0]
        ctx.logger.info('instance started successfully with IP {0}'
                        .format(ctx.instance.runtime_properties[IP_PROPERTY]))
        return True

    else:
        ctx.instance.runtime_properties[IP_PROPERTY] = node.private_ips[0]
        ctx.logger.info('Cannot determine networking type,'
                        ' using private_ip as {0} ip'
                        .format(ctx.instance.runtime_properties[IP_PROPERTY]))
        return True
Ejemplo n.º 14
0
def connect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    network_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    network = get_network_by_id(ctx, cloud_driver, network_id)
    #firewall_rules = ctx.node.target.properties.get(['firewall'][0], None)

    if network.extra['vpc_id'] is not None:

        ctx.logger.info('Acquiring IP for VPC with id: {0}'
                        .format(network.extra['vpc_id']))

        fip = cloud_driver.ex_allocate_public_ip(vpc_id=
                                                 network.extra['vpc_id'])

    elif network.id is not None:

        ctx.logger.info('Acquiring IP for network with id: {0}'.
                        format(network.id))

        fip = cloud_driver.ex_allocate_public_ip(network_id=network.id)

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

                ingress_rules = [rule for rule in firewall_config if
                                 rule['type'] == 'ingress']

                for rule in ingress_rules:
                    rule_cidr = rule.get('cidr')
                    rule_protocol = rule.get('protocol')
                    rule_ports = rule.get('ports')

                    for port in rule_ports:
                        ctx.logger.info('Creating ingress fw rule:'
                                        ' {3}:{0}:{1}-{2}'
                                        .format(rule_cidr,
                                                port,
                                                port,
                                                rule_protocol))

                        cloud_driver.ex_create_firewall_rule(
                            address=fip,
                            cidr_list=rule_cidr,
                            protocol=rule_protocol,
                            start_port=port,
                            end_port=port
                        )

    else:
        raise NonRecoverableError('Cannot resolve network or vpc id')

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = fip.id
    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        FLOATINGIP_CLOUDSTACK_TYPE
    ctx.source.instance.runtime_properties[IP_ADDRESS_PROPERTY] = fip.address
def delete(ctx, **kwargs):
    try:
        cloud_driver = get_cloud_driver(ctx)
        cloud_driver.ex_delete_security_group(
            ctx.runtime_properties['external_id'])
    except:
        ctx.logger.warn('security-group {0} may not have been deleted'.format(
            ctx.runtime_properties['external_id']))
        pass
def create(**kwargs):
    """ Create a volume
    """

    cloud_driver = get_cloud_driver(ctx)
    volume = {}

    if ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY] is False:

        ctx.logger.debug('reading volume attributes.')
        volume.update(copy.deepcopy(ctx.node.properties['volume']))

        if 'name' in volume:
            volume_name = volume['name']
        else:
            raise NonRecoverableError("To create a volume, the name of the "
                                      "volume is needed")

        if 'size' in volume:
            volume_size = volume['size']
        else:
            raise NonRecoverableError("To create a volume, the size of the "
                                      "volume is needed")

        volume = cloud_driver.create_volume(name=volume_name,
                                            size=volume_size)

        if volume_exists(cloud_driver, volume.id):

            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = volume.id
            ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                VOLUME_CLOUDSTACK_TYPE
        else:
            raise NonRecoverableError("Volume not created")

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

        if ctx.node.properties['resource_id']:
            resource_id = ctx.node.properties['resource_id']

            volume = get_volume_by_id(cloud_driver, resource_id)

            if volume is not None:
                ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = \
                    volume.id
                ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
                    volume.name
                ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                    VOLUME_CLOUDSTACK_TYPE
            else:
                raise NonRecoverableError("Could not find volume with id {0}".
                                          format(resource_id))
        else:
            raise NonRecoverableError("Resource_id for volume is not supplied")

        return
def connect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    network_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    network = get_network_by_id(ctx, cloud_driver, network_id)

    if network.extra['vpc_id'] is not None:

        ctx.logger.info('Acquiring IP for VPC with id: {0}'
                        .format(network.extra['vpc_id']))

        fip = cloud_driver.ex_allocate_public_ip(
            vpc_id=network.extra['vpc_id'])

    elif network.id is not None:

        ctx.logger.info('Acquiring IP for network with id: {0}'.
                        format(network.id))

        fip = cloud_driver.ex_allocate_public_ip(network_id=network.id)

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

                ingress_rules = [rule for rule in firewall_config
                                 if rule['type'] == 'ingress']

                for rule in ingress_rules:
                    rule_cidr = rule.get('cidr')
                    rule_protocol = rule.get('protocol')
                    rule_ports = rule.get('ports')

                    for port in rule_ports:
                        ctx.logger.info('Creating ingress fw rule:'
                                        ' {3}:{0}:{1}-{2}'
                                        .format(rule_cidr,
                                                port,
                                                port,
                                                rule_protocol))

                        cloud_driver.ex_create_firewall_rule(
                            address=fip,
                            cidr_list=rule_cidr,
                            protocol=rule_protocol,
                            start_port=port,
                            end_port=port
                        )

    else:
        raise NonRecoverableError('Cannot resolve network or vpc id')

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = fip.id
    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        FLOATINGIP_CLOUDSTACK_TYPE
    ctx.source.instance.runtime_properties[IP_ADDRESS_PROPERTY] = fip.address
def delete(ctx, **kwargs):
    try:
        cloud_driver = get_cloud_driver(ctx)
        cloud_driver.ex_delete_security_group(
            ctx.instance.runtime_properties['external_id'])
    except:
        ctx.logger.warn(
            'security-group {0} may not have been deleted'.format(
                ctx.instance.runtime_properties['external_id']))
        pass
Ejemplo n.º 19
0
def disconnect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    fip_id = ctx.runtime_properties['external_id']
    fip = get_floating_ip_by_id(ctx, cloud_driver, fip_id)

    ctx.logger.info('Deleting floating ip: {0}'.format(fip))

    cloud_driver.ex_release_public_ip(address=fip)
Ejemplo n.º 20
0
def create(**kwargs):
    """ Create a volume
    """

    cloud_driver = get_cloud_driver(ctx)
    volume = {}

    if ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY] is False:

        ctx.logger.debug('reading volume attributes.')
        volume.update(copy.deepcopy(ctx.node.properties['volume']))

        if 'name' in volume:
            volume_name = volume['name']
        else:
            raise NonRecoverableError("To create a volume, the name of the "
                                      "volume is needed")

        if 'size' in volume:
            volume_size = volume['size']
        else:
            raise NonRecoverableError("To create a volume, the size of the "
                                      "volume is needed")

        volume = cloud_driver.create_volume(name=volume_name, size=volume_size)

        if volume_exists(cloud_driver, volume.id):

            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = volume.id
            ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                VOLUME_CLOUDSTACK_TYPE
        else:
            raise NonRecoverableError("Volume not created")

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

        if ctx.node.properties['resource_id']:
            resource_id = ctx.node.properties['resource_id']

            volume = get_volume_by_id(cloud_driver, resource_id)

            if volume is not None:
                ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = \
                    volume.id
                ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY] = \
                    volume.name
                ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
                    VOLUME_CLOUDSTACK_TYPE
            else:
                raise NonRecoverableError(
                    "Could not find volume with id {0}".format(resource_id))
        else:
            raise NonRecoverableError("Resource_id for volume is not supplied")

        return
Ejemplo n.º 21
0
def create_vpn_connection(**kwargs):
    """
    Creates a VPN Connection.
    """
    cloud_driver = get_cloud_driver(ctx)

    vpn_gateway_id = kwargs.get("vpn_gateway_id")
    vpn_customer_gateway_id = kwargs.get("vpn_customer_gateway_id")

    vpn_gateway = get_vpn_gateway(cloud_driver, vpn_gateway_id)

    if not vpn_gateway:
        raise NonRecoverableError("Could not find VPN Gateway id={0}".format(vpn_gateway_id))

    vpn_customer_gateway = get_vpn_customer_gateway(cloud_driver, vpn_customer_gateway_id)

    if not vpn_customer_gateway:
        raise NonRecoverableError("Could not find VPN Customer Gateway id={0}".format(vpn_customer_gateway_id))

    try:
        id_ = ctx.node.properties["resource_id"]
    except KeyError:
        id_ = None

    exists = get_vpn_connection(cloud_driver, id_)
    use_external = ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if (not exists) and (not use_external):
        if not are_vpc_routers_running(cloud_driver, vpn_gateway.vpc_id):
            return ctx.operation.retry(message="VPC Routers not running, waiting...", retry_after=5)

        vpn_connection = cloud_driver.ex_create_vpn_connection(
            vpn_customer_gateway=vpn_customer_gateway, vpn_gateway=vpn_gateway, passive=ctx.node.properties["passive"]
        )

        ctx.logger.info(
            "Created VPN Connection id={0} for VPN Gateway "
            "id={1} and VPN Customer Gateway id={2}".format(vpn_connection.id, vpn_gateway.id, vpn_customer_gateway.id)
        )

    elif exists and use_external:
        vpn_connection = get_vpn_connection(cloud_driver, id_)

        if not vpn_connection:
            raise NonRecoverableError("Could not find VPN Connection id={0}".format(id_))

    elif exists and (not use_external):
        raise NonRecoverableError("VPN Connection id={0} already exists".format(exists.id))

    elif (not exists) and use_external:
        raise NonRecoverableError("Could not find VPN Connection id={0}".format(id_))

    ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = vpn_connection.id
    ctx.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = VPN_CONNECTION_CLOUDSTACK_TYPE
Ejemplo n.º 22
0
def delete(ctx, **kwargs):

    vpc_name = ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY]
    cloud_driver = get_cloud_driver(ctx)
    vpc = get_vpc(cloud_driver, vpc_name)

    try:
        cloud_driver.ex_delete_vpc(vpc)
    except Exception as e:
        ctx.logger.warn('vpc {0} may not have been deleted: {1}'.format(
            vpc.name, str(e)))
        pass
Ejemplo n.º 23
0
def delete(ctx, **kwargs):

    vpc_name = ctx.runtime_properties['vpc_name']
    cloud_driver = get_cloud_driver(ctx)
    vpc = get_vpc(cloud_driver, vpc_name)

    try:
        cloud_driver.ex_delete_vpc(vpc)
    except:
        ctx.logger.warn('vpc {0} may not have been deleted'.format(
            ctx.runtime_properties['vpc_name']))
        pass
Ejemplo n.º 24
0
def delete(ctx, **kwargs):

    vpc_name = ctx.runtime_properties['vpc_name']
    cloud_driver = get_cloud_driver(ctx)
    vpc = get_vpc(cloud_driver, vpc_name)

    try:
        cloud_driver.ex_delete_vpc(vpc)
    except:
        ctx.logger.warn(
            'vpc {0} may not have been deleted'
                .format(ctx.runtime_properties['vpc_name']))
        pass
def detach_volume(ctx, **kwargs):
    '''Detaches a volume and deletes if expunge is requested.'''
    cloud_driver = get_cloud_driver(ctx)

    volume_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume = get_volume_by_id(cloud_driver, volume_id)

    if not volume:
        raise NonRecoverableError('Volume with id {0} not found'.format(
            volume_id))

    ctx.logger.info('Detaching volume {0}'.format(volume))
    cloud_driver.detach_volume(volume=volume)
def delete(ctx, **kwargs):

    vpc_name = ctx.instance.runtime_properties[CLOUDSTACK_NAME_PROPERTY]
    cloud_driver = get_cloud_driver(ctx)
    vpc = get_vpc(cloud_driver, vpc_name)

    try:
        cloud_driver.ex_delete_vpc(vpc)
    except Exception as e:
        ctx.logger.warn(
            'vpc {0} may not have been deleted: {1}'
            .format(vpc.name, str(e)))
        pass
def detach_volume(ctx, **kwargs):
    '''Detaches a volume and deletes if expunge is requested.'''
    cloud_driver = get_cloud_driver(ctx)

    volume_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume = get_volume_by_id(cloud_driver, volume_id)

    if not volume:
        raise NonRecoverableError('Volume with id {0} not found'.format(
            volume_id))

    ctx.logger.info('Detaching volume {0}'.format(volume))
    cloud_driver.detach_volume(volume=volume)
Ejemplo n.º 28
0
def create_vpn_customer_gateway(**kwargs):
    """
    Creates a VPN Customer Gateway.
    """
    cloud_driver = get_cloud_driver(ctx)

    vpn_gateway = get_vpn_gateway(cloud_driver, ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY])

    if not vpn_gateway:
        raise NonRecoverableError(
            "Could not find VPN Gateway id={0}".format(ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY])
        )
    try:
        id_ = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    except KeyError:
        id_ = None

    exists = vpn_customer_gateway_exists(cloud_driver, id_)
    use_external = ctx.source.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if (not exists) and (not use_external):
        data = {
            "cidr_list": ctx.source.node.properties["cidr_list"],
            "dpd": ctx.source.node.properties["dpd"],
            "esp_lifetime": ctx.source.node.properties["esp_lifetime"],
            "esp_policy": ctx.source.node.properties["esp_policy"],
            "gateway": vpn_gateway.public_ip,
            "ike_lifetime": ctx.source.node.properties["ike_lifetime"],
            "ike_policy": ctx.source.node.properties["ike_policy"],
            "ipsec_psk": ctx.source.node.properties["ipsec_psk"],
        }

        vpn_customer_gateway = cloud_driver.ex_create_vpn_customer_gateway(**data)

        ctx.logger.info(
            "Created VPN Customer Gateway id={0} vpn_gateway_id={1}".format(vpn_customer_gateway.id, vpn_gateway.id)
        )

    elif exists and use_external:
        vpn_customer_gateway = get_vpn_customer_gateway(cloud_driver, id_)

        ctx.logger.info("Using VPN Customer Gateway id={0}".format(vpn_customer_gateway.id))

    elif exists and (not use_external):
        raise NonRecoverableError("VPN Customer Gateway already exists id={0}".format(id_))

    elif (not exists) and use_external:
        raise NonRecoverableError("Could not find VPN Customer Gateway id={0}".format(id_))

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = vpn_customer_gateway.id
    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = VPN_CUSTOMER_GATEWAY_CLOUDSTACK_TYPE
Ejemplo n.º 29
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
Ejemplo n.º 31
0
def delete(ctx, **kwargs):

    network_name = ctx.runtime_properties['network_name']
    cloud_driver = get_cloud_driver(ctx)
    network = get_network(cloud_driver, network_name)

    try:

        cloud_driver.ex_delete_network(network)
    except Exception as e:
        ctx.logger.warn('network {0} may not have been deleted: {1}'
                        .format(ctx.runtime_properties['network_name'], str(e)))
        return False
        pass

    return True
def disconnect_floating_ip(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)
    node_id = ctx.runtime_properties['instance_id']
    node = get_node_by_id(ctx, cloud_driver, node_id)
    portmaps = get_portmaps_by_node_id(ctx, cloud_driver, node_id)

    for portmap in portmaps:

        try:
            cloud_driver.ex_delete_port_forwarding_rule(node=node, rule=portmap)
        except Exception as e:
            ctx.logger.warn('Port forward may not have been removed: '
                            '{0}'.format(str(e)))
        return False

    return True
def start(ctx, **kwargs):
    ctx.logger.info("initializing {0} cloud driver"
                    .format(Provider.CLOUDSTACK))
    cloud_driver = get_cloud_driver(ctx)

    instance_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    if instance_id is None:
        raise RuntimeError(
            'Could not find node ID in runtime context: {0} '
            .format(instance_id))

    node = get_vm_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        raise RuntimeError('Could not find node with ID {0}'
                           .format(instance_id))

    ctx.logger.info('Starting node: {0}'.format(node.name))
    cloud_driver.ex_start(node)
def start(ctx, **kwargs):
    ctx.logger.info("initializing {0} cloud driver"
                    .format(Provider.CLOUDSTACK))
    cloud_driver = get_cloud_driver(ctx)

    instance_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    if instance_id is None:
        raise RuntimeError(
            'Could not find node ID in runtime context: {0} '
            .format(instance_id))

    node = get_vm_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        raise RuntimeError('Could not find node with ID {0}'
                           .format(instance_id))

    ctx.logger.info('Starting node: {0}'.format(node.name))
    cloud_driver.ex_start(node)
def delete(ctx, **kwargs):

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

    instance_id = ctx['instance_id']
    if instance_id is None:
        raise NameError('could not find node ID in runtime context: {0} '
                        .format(instance_id))

    ctx.logger.info('getting node with ID: {0} '.format(instance_id))
    node = get_node_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        raise NameError('could not find node with ID: {0} '
                        .format(instance_id))

    ctx.logger.info('destroying vm with details: {0}'.format(node.name))
    cloud_driver.destroy_node(node)
def delete(ctx, **kwargs):
    if not ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]:
        ctx.logger.info('deleting keypair')

        _delete_private_key_file()

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

        key = get_key_pair(ctx, cloud_driver,
                           ctx.instance.runtime_properties
                           [CLOUDSTACK_ID_PROPERTY])
        cloud_driver.delete_key_pair(key_pair=key)
    else:
        ctx.logger.info('not deleting keypair since an external keypair is '
                        'being used')

    delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
def delete(**kwargs):
    """ Delete a volume
    """

    cloud_driver = get_cloud_driver(ctx)

    volume_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume = get_volume_by_id(cloud_driver, volume_id)

    if volume is None:
        raise NonRecoverableError('Volume with id {0} not found'
                                  .format(volume_id))

    if not ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]:
        ctx.logger.info('Trying to destroy volume {0}'.format(volume))
        cloud_driver.destroy_volume(volume=volume)
    else:
        ctx.logger.info('Volume {0} does not need to be destroyed'.format(
            volume))
Ejemplo n.º 38
0
def create_vpn_gateway(**kwargs):
    """
    Creates a VPN Gateway for a VPC.
    """
    cloud_driver = get_cloud_driver(ctx)

    vpc_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    vpc = get_vpc_by_id(cloud_driver, vpc_id)

    if not vpc:
        raise NonRecoverableError('Could not find VPC id={0}'.format(vpc_id))

    if not are_vpc_routers_running(cloud_driver, vpc.id):
        return ctx.operation.retry(
            message='VPC Routers not running, waiting...', retry_after=5)

    exists = vpn_gateway_exists_for_vpc(cloud_driver, vpc_id)
    use_external = ctx.source.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if (not exists) and (not use_external):
        vpn_gateway = cloud_driver.ex_create_vpn_gateway(vpc)

        ctx.logger.info('Created VPN Gateway id={0} vpc_id={1}'.format(
            vpn_gateway.id, vpc_id))

    elif exists and use_external:
        vpn_gateway = get_vpn_gateway_by_vpc(cloud_driver, vpc_id)

        ctx.logger.info('Using VPN Gateway id={0}'.format(vpn_gateway.id))

    elif exists and (not use_external):
        raise NonRecoverableError(
            'VPN Gateway already exists vpc_id={0}'.format(vpc_id))

    elif (not exists) and use_external:
        raise NonRecoverableError(
            'Could not find VPN Gateway vpc_id={0}'.format(vpc_id))

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = \
        vpn_gateway.id

    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        VPN_GATEWAY_CLOUDSTACK_TYPE
Ejemplo n.º 39
0
def delete(**kwargs):
    """ Delete a volume
    """

    cloud_driver = get_cloud_driver(ctx)

    volume_id = ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume = get_volume_by_id(cloud_driver, volume_id)

    if volume is None:
        raise NonRecoverableError(
            'Volume with id {0} not found'.format(volume_id))

    if not ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]:
        ctx.logger.info('Trying to destroy volume {0}'.format(volume))
        cloud_driver.destroy_volume(volume=volume)
    else:
        ctx.logger.info(
            'Volume {0} does not need to be destroyed'.format(volume))
Ejemplo n.º 40
0
def delete(ctx, **kwargs):
    if not ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]:
        ctx.logger.info('deleting keypair')

        _delete_private_key_file()

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

        key = get_key_pair(
            ctx, cloud_driver,
            ctx.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY])
        cloud_driver.delete_key_pair(key_pair=key)
    else:
        ctx.logger.info('not deleting keypair since an external keypair is '
                        'being used')

    delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
def start(ctx, **kwargs):
    ctx.logger.info("initializing {0} cloud driver"
                    .format(Provider.CLOUDSTACK))
    cloud_driver = get_cloud_driver(ctx)

    instance_id = ctx.runtime_properties['instance_id']
    if instance_id is None:
        raise RuntimeError(
            'could not find node ID in runtime context: {0} '
            .format(instance_id))

    ctx.logger.info('getting node with ID: {0} '.format(instance_id))
    node = get_node_by_id(ctx, cloud_driver, instance_id)
    if node is None:
        raise RuntimeError('could not find node with ID {0}'
                           .format(instance_id))

    ctx.logger.info('starting node with details {0}'.format(node.name))
    cloud_driver.ex_start(node)
Ejemplo n.º 42
0
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
Ejemplo n.º 43
0
def delete_vpn_connection(**kwargs):
    """
    Deletes a VPN Connection.
    """
    cloud_driver = get_cloud_driver(ctx)

    id_ = ctx.node.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    vpn_connection = get_vpn_connection(cloud_driver, id_)

    if not vpn_connection:
        raise NonRecoverableError("Could not find VPN Connection id={0}".format(id_))

    use_external = ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if not use_external:
        vpn_connection.delete()

        ctx.logger.info("Deleted VPN Connection id={0}".format(id_))
    else:
        ctx.logger.info("Did not delete VPN Connection id={0}".format(id_))
Ejemplo n.º 44
0
def delete_vpn_gateway(**kwargs):
    """
    Deletes a VPN Gateway for a VPC.
    """
    cloud_driver = get_cloud_driver(ctx)

    id_ = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    vpn_gateway = get_vpn_gateway(cloud_driver, id_)

    if not vpn_gateway:
        raise NonRecoverableError("Could not find VPN Gateway id={0}".format(id_))

    use_external = ctx.source.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if not use_external:
        vpn_gateway.delete()

        ctx.logger.info("Deleted VPN Gateway id={0} vpc_id={1}".format(id_, vpn_gateway.vpc_id))
    else:
        ctx.logger.info("Did not delete VPN Gateway id={0} vpc_id={1}".format(id_, vpn_gateway.vpc_id))
Ejemplo n.º 45
0
def create(ctx, **kwargs):
    """ Create vpc with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    vpc = {
        'description': None,
        'name': ctx.node_id,
    }

    ctx.logger.debug('reading vpc configuration.')
    vpc.update(ctx.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('Current node {0}{1}'.format(ctx.node_id, ctx.properties))

    ctx['vpc_id'] = ctx.properties

    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['vpc_id'] = vpc.id
    ctx['vpc_name'] = vpc.name
Ejemplo n.º 46
0
def delete_vpn_connection(**kwargs):
    """
    Deletes a VPN Connection.
    """
    cloud_driver = get_cloud_driver(ctx)

    id_ = ctx.node.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    vpn_connection = get_vpn_connection(cloud_driver, id_)

    if not vpn_connection:
        raise NonRecoverableError(
            'Could not find VPN Connection id={0}'.format(id_))

    use_external = ctx.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if not use_external:
        vpn_connection.delete()

        ctx.logger.info('Deleted VPN Connection id={0}'.format(id_))
    else:
        ctx.logger.info('Did not delete VPN Connection id={0}'.format(id_))
def create(ctx, **kwargs):
    """ Create security group with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    security_group = {
        'description': None,
        'name': ctx.node_id,
    }

    ctx.logger.debug('reading security-group configuration.')
    rules_to_apply = ctx.properties['rules']
    security_group.update(ctx.properties['security_group'])

    security_group_name = security_group['name']
    if not _sg_exists(cloud_driver, security_group_name):
        ctx.logger.info(
            'creating security group: {0}'.format(security_group_name))
        cloud_driver.ex_create_security_group(
            security_group_name, description=security_group['description'])

        for rule in rules_to_apply:
            cidr = rule.get('cidr', None)
            protocol = rule.get('protocol', 'TCP')
            start_port = rule.get('start_port', None)
            if start_port is None:
                raise RuntimeError('You must specify start_port '
                                   'for a security group rule')
            end_port = rule.get('end_port', None)
            _add_ingress_rule(ctx,
                              cloud_driver,
                              security_group_name=security_group_name,
                              start_port=start_port,
                              end_port=end_port,
                              cidr_list=cidr,
                              protocol=protocol)
    else:
        ctx.logger.info('using existing management security group {0}'.format(
            security_group_name))
Ejemplo n.º 48
0
def create(ctx, **kwargs):
    """ Create vpc with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    vpc = {
        'description': None,
        'name': ctx.node_id,
    }

    ctx.logger.debug('reading vpc configuration.')
    vpc.update(ctx.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('Current node {0}{1}'.format(ctx.node_id, ctx.properties))

    ctx['vpc_id'] = ctx.properties

    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['vpc_id'] = vpc.id
    ctx['vpc_name'] = vpc.name
def attach_volume(ctx, **kwargs):
    """ Attach a volume to virtual machine.
    """

    cloud_driver = get_cloud_driver(ctx)

    server_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume_id = ctx.target.instance.runtime_properties.get(
        CLOUDSTACK_ID_PROPERTY, None)

    server = get_vm_by_id(ctx, cloud_driver, server_id)
    volume = get_volume_by_id(cloud_driver, volume_id)

    if server is None:
        raise NonRecoverableError(
            'Server with id {0} not found'.format(server_id))

    if volume is None:
        raise NonRecoverableError(
            'Volume with id {0} not found'.format(volume_id))

    cloud_driver.attach_volume(node=server, volume=volume)
def create(ctx, **kwargs):
    """ Create security group with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    security_group = {
        'description': None,
        'name': ctx.node_id,
    }

    ctx.logger.debug('reading security-group configuration.')
    rules_to_apply = ctx.node.properties['rules']
    security_group.update(ctx.node.properties['security_group'])

    security_group_name = security_group['name']
    if not _sg_exists(cloud_driver, security_group_name):
        ctx.logger.info('creating security group: {0}'.format(
            security_group_name))
        cloud_driver.ex_create_security_group(
            security_group_name, description=security_group['description'])

        for rule in rules_to_apply:
            cidr = rule.get('cidr', None)
            protocol = rule.get('protocol', 'TCP')
            start_port = rule.get('start_port', None)
            if start_port is None:
                raise RuntimeError('You must specify start_port '
                                   'for a security group rule')
            end_port = rule.get('end_port', None)
            _add_ingress_rule(ctx, cloud_driver,
                              security_group_name=security_group_name,
                              start_port=start_port,
                              end_port=end_port,
                              cidr_list=cidr,
                              protocol=protocol)
    else:
        ctx.logger.info('using existing management security group {0}'.format(
            security_group_name))
def connect_network(ctx, **kwargs):

    instance_id = ctx.runtime_properties['instance_id']
    network_id = ctx.related.runtime_properties['network_id']



    cloud_driver = get_cloud_driver(ctx)
    # nodes = cloud_driver.list_nodes(instance_id)

    # network = [net for net in cloud_driver.ex_list_networks() if
    #            net.id == network_id ][0]
    # instance = [node for node in cloud_driver.list_nodes() if
    #             node.id == instance_id][0]

    node = get_node_by_id(ctx, cloud_driver, instance_id)
    network = get_network_by_id(ctx, cloud_driver, network_id)

    ctx.logger.info('Checking if there is a nic for  '
                    'vm: {0} with id: {1} in network {2} with id: {3}'
                    .format(node.name, network.name, instance_id, network_id,))

    nic_exists = get_nic_by_node_and_network_id(ctx, cloud_driver, node,
                                                network_id)

    #ctx.logger.info('Adding a NIC to VM {0} in Network {1}'.format(
    # node.name, network.name))

    if nic_exists is not None:
        ctx.logger.info('No need to connect network {0}, '
                        'already connected to nic {1}'
                        .format(network.name, nic_exists.id))
        return False

    cloud_driver.ex_attach_nic_to_node(node=node, network=network)
    #ctx.runtime_properties['nic_id'] = result.id

    return True
def attach_volume(ctx, **kwargs):
    """ Attach a volume to virtual machine.
    """

    cloud_driver = get_cloud_driver(ctx)

    server_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    volume_id = ctx.target.instance.runtime_properties.get(
        CLOUDSTACK_ID_PROPERTY, None)

    server = get_vm_by_id(ctx, cloud_driver, server_id)
    volume = get_volume_by_id(cloud_driver, volume_id)

    if server is None:
        raise NonRecoverableError('Server with id {0} not found'
                                  .format(server_id))

    if volume is None:
        raise NonRecoverableError('Volume with id {0} not found'
                                  .format(volume_id))

    cloud_driver.attach_volume(node=server,
                               volume=volume)
Ejemplo n.º 53
0
def create_vpn_gateway(**kwargs):
    """
    Creates a VPN Gateway for a VPC.
    """
    cloud_driver = get_cloud_driver(ctx)

    vpc_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    vpc = get_vpc_by_id(cloud_driver, vpc_id)

    if not vpc:
        raise NonRecoverableError("Could not find VPC id={0}".format(vpc_id))

    if not are_vpc_routers_running(cloud_driver, vpc.id):
        return ctx.operation.retry(message="VPC Routers not running, waiting...", retry_after=5)

    exists = vpn_gateway_exists_for_vpc(cloud_driver, vpc_id)
    use_external = ctx.source.node.properties[USE_EXTERNAL_RESOURCE_PROPERTY]

    if (not exists) and (not use_external):
        vpn_gateway = cloud_driver.ex_create_vpn_gateway(vpc)

        ctx.logger.info("Created VPN Gateway id={0} vpc_id={1}".format(vpn_gateway.id, vpc_id))

    elif exists and use_external:
        vpn_gateway = get_vpn_gateway_by_vpc(cloud_driver, vpc_id)

        ctx.logger.info("Using VPN Gateway id={0}".format(vpn_gateway.id))

    elif exists and (not use_external):
        raise NonRecoverableError("VPN Gateway already exists vpc_id={0}".format(vpc_id))

    elif (not exists) and use_external:
        raise NonRecoverableError("Could not find VPN Gateway vpc_id={0}".format(vpc_id))

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = vpn_gateway.id

    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = VPN_GATEWAY_CLOUDSTACK_TYPE
def connect_floating_ip(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    network_config = ctx.source.node.properties['network']
    default_network_config = network_config['default_network']

    default_net = get_network(cloud_driver, default_network_config)

    ctx.logger.debug('reading portmap configuration.')
    portmaps = ctx.source.node.properties['portmaps']

    if not portmaps:
        raise NonRecoverableError('Relation defined but no portmaps set'
                                  ' either remove relation or'
                                  ' define the portmaps')

    server_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    floating_ip_id = ctx.target.instance.runtime_properties[
        CLOUDSTACK_ID_PROPERTY]

    # TODO Needs more elegant solution, problem only seen on VPC with CS4.3
    # TODO Should be fixed in Cloudstack
    ctx.logger.info('Sleeping for 15 secs so router can stabilize, '
                    'so we wont have to wait for ARP timeouts '
                    'when reusing public IPs')
    sleep(15)

    for portmap in portmaps:

        protocol = portmap.get(['protocol'][0], None)
        pub_port = portmap.get(['public_port'][0], None)
        pub_end_port = portmap.get(['public_end_port'][0], None)
        priv_port = portmap.get(['private_port'][0], None)
        priv_end_port = portmap.get(['private_end_port'][0], None)

        # If not specified assume closed
        open_fw = portmap.get(['open_firewall'][0], False)

        if pub_port is None:
            raise NonRecoverableError('Please specify the public_port')
        elif pub_end_port is None:
            pub_end_port = pub_port

        if priv_port is None:
            raise NonRecoverableError('Please specify the private_port')
        elif priv_end_port is None:
            priv_end_port = priv_port

        if protocol is None:
            raise NonRecoverableError('Please specify the protocol TCP or UDP')

        node = get_vm_by_id(ctx, cloud_driver, server_id)
        public_ip = get_public_ip_by_id(ctx, cloud_driver, floating_ip_id)

        try:
            ctx.logger.info('Creating portmap for node: {0}:{1}-{2} on'
                            ' {3}:{4}-{5}'.
                            format(node.name, priv_port, priv_end_port,
                                   public_ip.address, pub_port, pub_end_port))

            cloud_driver.ex_create_port_forwarding_rule(
                node=node,
                address=public_ip,
                protocol=protocol,
                public_port=pub_port,
                public_end_port=pub_end_port,
                private_port=priv_port,
                private_end_port=priv_end_port,
                openfirewall=open_fw,
                network_id=default_net.id)
        except Exception as e:
            ctx.logger.warn('Port forward creation failed: '
                            '{0}'.format(str(e)))
            return False

    return True
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)
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
Ejemplo n.º 57
0
def create(ctx, **kwargs):
    """ Create network with rules.
    """

    cloud_driver = get_cloud_driver(ctx)

    network = {
        'description': None,
        'name': ctx.node_id,
    }

    ctx.logger.debug('reading network configuration.')
    network.update(ctx.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)

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

    ctx.logger.info('Current node {0}{1}'.format(ctx.node_id, ctx.properties))

    ctx['network_id'] = ctx.node_id

    if not _network_exists(cloud_driver, network_name):

        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['gateway'],
                netmask=network['netmask'],
                vpc_id=vpc.id)

            # 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.properties:
                firewall_config = ctx.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,
                network_offering=network_offering,
                location=location)

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

                for rule in firewall_config:
                    rule_cidr = rule.get('cidr')
                    rule_protocol = rule.get('protocol')
                    rule_ports = rule.get('ports')

                    for port in rule_ports:
                        _create_egress_rules(cloud_driver, net.id,
                                             rule_cidr,
                                             rule_protocol,
                                             port, port)

    else:
        ctx.logger.info('using existing management network {0}'.
                        format(network_name))
        net = get_network(cloud_driver, network_name)

    ctx['network_id'] = net.id
    ctx['network_name'] = net.name