def test_create_floating_ip(self):
        self.patch_object(openstack_utils, "get_net_uuid")
        self.get_net_uuid.return_value = self.net_uuid

        # Already exists
        floatingip = openstack_utils.create_floating_ip(
            self.neutronclient, self.ext_net, port=self.port["port"])
        self.assertEqual(floatingip, self.floatingip["floatingip"])
        self.neutronclient.create_floatingip.assert_not_called()

        # Does not yet exist
        self.neutronclient.list_floatingips.return_value = {"floatingips": []}
        self.floatingip["floatingip"].pop("id")
        floatingip = openstack_utils.create_floating_ip(
            self.neutronclient, self.private_net, port=self.port["port"])
        self.assertEqual(floatingip, self.floatingip["floatingip"])
        self.neutronclient.create_floatingip.assert_called_once_with(
            self.floatingip)
def assign_ip(neutron_client, instance_id):
    port = openstack_utils.get_ports_from_device_id(neutron_client,
                                                    instance_id)[0]
    logger.debug("Found port for {}".format(instance_id))
    ip = openstack_utils.create_floating_ip(neutron_client,
                                            "ext_net",
                                            port=port)['floating_ip_address']
    logger.debug("Assigned {} to {}".format(ip, instance_id))
    return ip
Beispiel #3
0
    def launch_instance(self, instance_key):
        """Launch an instance.

        :param instance_key: Key to collect associated config data with.
        :type instance_key: str
        """
        # Collect resource information.
        vm_name = time.strftime("%Y%m%d%H%M%S")
        image = self.nova_client.glance.find_image(
            self.boot_tests[instance_key]['image_name'])
        flavor = self.nova_client.flavors.find(
            name=self.boot_tests[instance_key]['flavor_name'])
        net = self.neutron_client.find_resource("network", "private")
        nics = [{'net-id': net.get('id')}]

        # Launch instance.
        logging.info('Launching instance {}'.format(vm_name))
        instance = self.nova_client.servers.create(
            name=vm_name,
            image=image,
            flavor=flavor,
            key_name=nova_utils.KEYPAIR_NAME,
            nics=nics)

        # Test Instance is ready.
        logging.info('Checking instance is active')
        openstack_utils.resource_reaches_status(
            self.nova_client.servers,
            instance.id,
            expected_status='ACTIVE')

        logging.info('Checking cloud init is complete')
        openstack_utils.cloud_init_complete(
            self.nova_client,
            instance.id,
            self.boot_tests[instance_key]['bootstring'])
        port = openstack_utils.get_ports_from_device_id(
            self.neutron_client,
            instance.id)[0]
        logging.info('Assigning floating ip.')
        ip = openstack_utils.create_floating_ip(
            self.neutron_client,
            "ext_net",
            port=port)['floating_ip_address']
        logging.info('Assigned floating IP {} to {}'.format(ip, vm_name))
        openstack_utils.ping_response(ip)

        # Check ssh'ing to instance.
        logging.info('Testing ssh access.')
        openstack_utils.ssh_test(
            username=self.boot_tests[instance_key]['username'],
            ip=ip,
            vm_name=vm_name,
            password=self.boot_tests[instance_key].get('password'),
            privkey=openstack_utils.get_private_key(nova_utils.KEYPAIR_NAME))
Beispiel #4
0
def setup_bgp_speaker(peer_application_name, keystone_session=None):
    """Perform BGP Speaker setup.

    :param peer_application_name: String name of BGP peer application
    :type peer_application_name: string
    :param keystone_session: Keystone session object for overcloud
    :type keystone_session: keystoneauth1.session.Session object
    :returns: None
    :rtype: None
    """
    # Get ASNs from deployment
    dr_relation = juju_utils.get_relation_from_unit(
        'neutron-dynamic-routing',
        peer_application_name,
        'bgpclient')
    peer_asn = dr_relation.get('asn')
    logging.debug('peer ASn: "{}"'.format(peer_asn))
    peer_relation = juju_utils.get_relation_from_unit(
        peer_application_name,
        'neutron-dynamic-routing',
        'bgp-speaker')
    dr_asn = peer_relation.get('asn')
    logging.debug('our ASn: "{}"'.format(dr_asn))

    # If a session has not been provided, acquire one
    if not keystone_session:
        keystone_session = openstack_utils.get_overcloud_keystone_session()

    # Get authenticated clients
    neutron_client = openstack_utils.get_neutron_session_client(
        keystone_session)

    # Create BGP speaker
    logging.info("Setting up BGP speaker")
    bgp_speaker = openstack_utils.create_bgp_speaker(
        neutron_client, local_as=dr_asn)

    # Add networks to bgp speaker
    logging.info("Advertising BGP routes")
    openstack_utils.add_network_to_bgp_speaker(
        neutron_client, bgp_speaker, EXT_NET)
    openstack_utils.add_network_to_bgp_speaker(
        neutron_client, bgp_speaker, PRIVATE_NET)
    logging.debug("Advertised routes: {}"
                  .format(
                      neutron_client.list_route_advertised_from_bgp_speaker(
                          bgp_speaker["id"])))

    # Create peer
    logging.info("Setting up BGP peer")
    bgp_peer = openstack_utils.create_bgp_peer(neutron_client,
                                               peer_application_name,
                                               remote_as=peer_asn)
    # Add peer to bgp speaker
    logging.info("Adding BGP peer to BGP speaker")
    openstack_utils.add_peer_to_bgp_speaker(
        neutron_client, bgp_speaker, bgp_peer)

    # Create Floating IP to advertise
    logging.info("Creating floating IP to advertise")
    port = openstack_utils.create_port(neutron_client, FIP_TEST, PRIVATE_NET)
    floating_ip = openstack_utils.create_floating_ip(neutron_client, EXT_NET,
                                                     port=port)
    logging.info(
        "Advertised floating IP: {}".format(
            floating_ip["floating_ip_address"]))

    # NOTE(fnordahl): As a workaround for LP: #1784083 remove BGP speaker from
    # dragent and add it back.
    logging.info(
        "Waiting for Neutron agent 'neutron-bgp-dragent' to appear...")
    keystone_session = openstack_utils.get_overcloud_keystone_session()
    neutron_client = openstack_utils.get_neutron_session_client(
        keystone_session)
    agents = openstack_utils.neutron_agent_appears(neutron_client,
                                                   'neutron-bgp-dragent')
    agent_id = None
    for agent in agents.get('agents', []):
        agent_id = agent.get('id', None)
        if agent_id is not None:
            break
    logging.info(
        'Waiting for BGP speaker to appear on agent "{}"...'.format(agent_id))
    bgp_speakers = openstack_utils.neutron_bgp_speaker_appears_on_agent(
        neutron_client, agent_id)
    logging.info(
        "Removing and adding back bgp-speakers to agent (LP: #1784083)...")
    while True:
        try:
            for bgp_speaker in bgp_speakers.get('bgp_speakers', []):
                bgp_speaker_id = bgp_speaker.get('id', None)
                logging.info('removing "{}" from "{}"'
                             ''.format(bgp_speaker_id, agent_id))
                neutron_client.remove_bgp_speaker_from_dragent(
                    agent_id, bgp_speaker_id)
        except neutronclient.common.exceptions.NotFound as e:
            logging.info('Exception: "{}"'.format(e))
            break
    neutron_client.add_bgp_speaker_to_dragent(
        agent_id, {'bgp_speaker_id': bgp_speaker_id})
Beispiel #5
0
def launch_instance(instance_key, use_boot_volume=False, vm_name=None,
                    private_network_name=None, image_name=None,
                    flavor_name=None, external_network_name=None, meta=None):
    """Launch an instance.

    :param instance_key: Key to collect associated config data with.
    :type instance_key: str
    :param use_boot_volume: Whether to boot guest from a shared volume.
    :type use_boot_volume: boolean
    :param vm_name: Name to give guest.
    :type vm_name: str
    :param private_network_name: Name of private network to attach guest to.
    :type private_network_name: str
    :param image_name: Image name to use with guest.
    :type image_name: str
    :param flavor_name: Flavor name to use with guest.
    :type flavor_name: str
    :param external_network_name: External network to create floating ip from
                                  for guest.
    :type external_network_name: str
    :param meta: A dict of arbitrary key/value metadata to store for this
                 server. Both keys and values must be <=255 characters.
    :type meta: dict
    """
    keystone_session = openstack_utils.get_overcloud_keystone_session()
    nova_client = openstack_utils.get_nova_session_client(keystone_session)
    neutron_client = openstack_utils.get_neutron_session_client(
        keystone_session)

    # Collect resource information.
    vm_name = vm_name or time.strftime("%Y%m%d%H%M%S")

    image_name = image_name or boot_tests[instance_key]['image_name']
    image = nova_client.glance.find_image(image_name)

    flavor_name = flavor_name or boot_tests[instance_key]['flavor_name']
    flavor = nova_client.flavors.find(name=flavor_name)

    private_network_name = private_network_name or "private"
    net = neutron_client.find_resource("network", private_network_name)
    nics = [{'net-id': net.get('id')}]

    meta = meta or {}
    external_network_name = external_network_name or "ext_net"

    if use_boot_volume:
        bdmv2 = [{
            'boot_index': '0',
            'uuid': image.id,
            'source_type': 'image',
            'volume_size': flavor.disk,
            'destination_type': 'volume',
            'delete_on_termination': True}]
        image = None
    else:
        bdmv2 = None

    # Launch instance.
    logging.info('Launching instance {}'.format(vm_name))
    instance = nova_client.servers.create(
        name=vm_name,
        image=image,
        block_device_mapping_v2=bdmv2,
        flavor=flavor,
        key_name=nova_utils.KEYPAIR_NAME,
        meta=meta,
        nics=nics)

    # Test Instance is ready.
    logging.info('Checking instance is active')
    openstack_utils.resource_reaches_status(
        nova_client.servers,
        instance.id,
        expected_status='ACTIVE',
        stop_after_attempt=16)

    logging.info('Checking cloud init is complete')
    openstack_utils.cloud_init_complete(
        nova_client,
        instance.id,
        boot_tests[instance_key]['bootstring'])
    port = openstack_utils.get_ports_from_device_id(
        neutron_client,
        instance.id)[0]
    logging.info('Assigning floating ip.')
    ip = openstack_utils.create_floating_ip(
        neutron_client,
        external_network_name,
        port=port)['floating_ip_address']
    logging.info('Assigned floating IP {} to {}'.format(ip, vm_name))
    openstack_utils.ping_response(ip)

    # Check ssh'ing to instance.
    logging.info('Testing ssh access.')
    openstack_utils.ssh_test(
        username=boot_tests[instance_key]['username'],
        ip=ip,
        vm_name=vm_name,
        password=boot_tests[instance_key].get('password'),
        privkey=openstack_utils.get_private_key(nova_utils.KEYPAIR_NAME))