Exemplo n.º 1
0
def port_unbind(endpoint_id, neutron_port):
    """Unbinds the Neutron port from the network interface on the host.

    :param endpoint_id: the ID of the Docker container as string
    :param neutron_port: a port dictionary returned from python-neutronclient
    :returns: the tuple of stdout and stderr returned by processutils.execute
              invoked with the executable script for unbinding
    :raises: processutils.ProcessExecutionError, pyroute2.netlink.NetlinkError
    """

    vif_type = neutron_port.get(VIF_TYPE_KEY, FALLBACK_VIF_TYPE)
    vif_details = utils.string_mappings(neutron_port.get(VIF_DETAILS_KEY))
    unbinding_exec_path = os.path.join(config.CONF.bindir, vif_type)
    ifname = VETH_PREFIX + endpoint_id[:8]
    port_id = neutron_port['id']
    mac_address = neutron_port['mac_address']
    stdout, stderr = processutils.execute(
        unbinding_exec_path, UNBINDING_SUBCOMMAND, port_id, ifname,
        endpoint_id, mac_address, vif_details, run_as_root=True)
    try:
        cleanup_veth(ifname)
    except pyroute2.netlink.NetlinkError:
        raise exceptions.VethDeleteionFailure(
            'Deleting the veth pair failed.')
    return (stdout, stderr)
Exemplo n.º 2
0
def port_bind(endpoint_id, neutron_port, neutron_subnets):
    """Binds the Neutron port to the network interface on the host.

    :param endpoint_id:     the ID of the endpoint as string
    :param neutron_port:    a port dictionary returned from
                            python-neutronclient
    :param neutron_subnets: a list of all subnets under network to which this
                            endpoint is trying to join
    :returns: the tuple of the names of the veth pair and the tuple of stdout
              and stderr returned by processutils.execute invoked with the
              executable script for binding
    :raises: kuryr.common.exceptions.VethCreationFailure,
             processutils.ProcessExecutionError
    """
    ip = get_ipdb()

    ifname = VETH_PREFIX + endpoint_id[:8]
    peer_name = CONTAINER_VETH_PREFIX + ifname
    subnets_dict = {subnet['id']: subnet for subnet in neutron_subnets}

    try:
        with ip.create(ifname=ifname, kind=KIND_VETH,
                       reuse=True, peer=peer_name) as host_veth:
            if not _is_up(host_veth):
                host_veth.up()
        with ip.interfaces[peer_name] as peer_veth:
            fixed_ips = neutron_port.get(FIXED_IP_KEY, [])
            if not fixed_ips and (IP_ADDRESS_KEY in neutron_port):
                peer_veth.add_ip(neutron_port[IP_ADDRESS_KEY])
            for fixed_ip in fixed_ips:
                if IP_ADDRESS_KEY in fixed_ip and (SUBNET_ID_KEY in fixed_ip):
                    subnet_id = fixed_ip[SUBNET_ID_KEY]
                    subnet = subnets_dict[subnet_id]
                    cidr = netaddr.IPNetwork(subnet['cidr'])
                    peer_veth.add_ip(fixed_ip[IP_ADDRESS_KEY], cidr.prefixlen)
            peer_veth.address = neutron_port[MAC_ADDRESS_KEY].lower()
            if not _is_up(peer_veth):
                peer_veth.up()
    except pyroute2.ipdb.common.CreateException:
        raise exceptions.VethCreationFailure(
            'Creating the veth pair was failed.')
    except pyroute2.ipdb.common.CommitException:
        raise exceptions.VethCreationFailure(
            'Could not configure the veth endpoint for the container.')

    vif_type = neutron_port.get(VIF_TYPE_KEY, FALLBACK_VIF_TYPE)
    vif_details = utils.string_mappings(neutron_port.get(VIF_DETAILS_KEY))
    binding_exec_path = os.path.join(config.CONF.bindir, vif_type)
    port_id = neutron_port['id']
    network_id = neutron_port['network_id']
    tenant_id = neutron_port['tenant_id']
    mac_address = neutron_port['mac_address']
    try:
        stdout, stderr = processutils.execute(
            binding_exec_path, BINDING_SUBCOMMAND, port_id, ifname,
            endpoint_id, mac_address, network_id, tenant_id, vif_details,
            run_as_root=True)
    except processutils.ProcessExecutionError:
        with excutils.save_and_reraise_exception():
            cleanup_veth(ifname)

    return (ifname, peer_name, (stdout, stderr))