def _get_nets(vif, subnet, version, net_num, link_id):
    """Get networks for the given VIF and subnet

    :param vif: Neutron VIF
    :param subnet: Neutron subnet
    :param version: IP version as an int, either '4' or '6'
    :param net_num: Network index for generating name of each network
    :param link_id: Arbitrary identifier for the link the networks are
        attached to
    """
    net_type = ''
    if subnet.get_meta('ipv6_address_mode') is not None:
        net_type = '_%s' % subnet.get_meta('ipv6_address_mode')
    elif subnet.get_meta('dhcp_server') is not None:
        net_info = {
            'id': 'network%d' % net_num,
            'type': 'ipv%d_dhcp' % version,
            'link': link_id,
            'network_id': vif['network']['id']
        }
        return net_info

    ip = subnet['ips'][0]
    address = ip['address']
    if version == 4:
        netmask = model.get_netmask(ip, subnet)
    elif version == 6:
        netmask = str(subnet.as_netaddr().netmask)

    net_info = {
        'id': 'network%d' % net_num,
        'type': 'ipv%d%s' % (version, net_type),
        'link': link_id,
        'ip_address': address,
        'netmask': netmask,
        'routes': _get_default_route(version, subnet),
        'network_id': vif['network']['id']
    }

    # Add any additional routes beyond the default route
    for route in subnet['routes']:
        route_addr = netaddr.IPNetwork(route['cidr'])
        new_route = {
            'network': str(route_addr.network),
            'netmask': str(route_addr.netmask),
            'gateway': route['gateway']['address']
        }
        net_info['routes'].append(new_route)

    net_info['services'] = _get_dns_services(subnet)

    return net_info
示例#2
0
文件: netutils.py 项目: Juniper/nova
def _get_nets(vif, subnet, version, net_num, link_id):
    """Get networks for the given VIF and subnet

    :param vif: Neutron VIF
    :param subnet: Neutron subnet
    :param version: IP version as an int, either '4' or '6'
    :param net_num: Network index for generating name of each network
    :param link_id: Arbitrary identifier for the link the networks are
        attached to
    """
    net_type = ''
    if subnet.get_meta('ipv6_address_mode') is not None:
        net_type = '_%s' % subnet.get_meta('ipv6_address_mode')
    elif subnet.get_meta('dhcp_server') is not None:
        net_info = {
            'id': 'network%d' % net_num,
            'type': 'ipv%d_dhcp' % version,
            'link': link_id,
            'network_id': vif['network']['id']
        }
        return net_info

    ip = subnet['ips'][0]
    address = ip['address']
    if version == 4:
        netmask = model.get_netmask(ip, subnet)
    elif version == 6:
        netmask = str(subnet.as_netaddr().netmask)

    net_info = {
        'id': 'network%d' % net_num,
        'type': 'ipv%d%s' % (version, net_type),
        'link': link_id,
        'ip_address': address,
        'netmask': netmask,
        'routes': _get_default_route(version, subnet),
        'network_id': vif['network']['id']
    }

    # Add any additional routes beyond the default route
    for route in subnet['routes']:
        route_addr = netaddr.IPNetwork(route['cidr'])
        new_route = {
            'network': str(route_addr.network),
            'netmask': str(route_addr.netmask),
            'gateway': route['gateway']['address']
        }
        net_info['routes'].append(new_route)

    return net_info
示例#3
0
def get_injected_network_template(network_info, use_ipv6=None, template=None,
                                  libvirt_virt_type=None):
    """Returns a rendered network template for the given network_info.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    :param template: Path to the interfaces template file.
    :param libvirt_virt_type: The Libvirt `virt_type`, will be `None` for
        other hypervisors..
    """
    if use_ipv6 is None:
        use_ipv6 = CONF.use_ipv6

    if not template:
        template = CONF.injected_network_template

    if not (network_info and template):
        return

    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif['network'] or not vif['network']['subnets']:
            continue

        network = vif['network']
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = _get_first_network(network, 4)
        subnet_v6 = _get_first_network(network, 6)

        ifc_num += 1

        if not network.get_meta('injected'):
            continue

        hwaddress = vif.get('address')
        address = None
        netmask = None
        gateway = ''
        broadcast = None
        dns = None
        routes = []
        if subnet_v4:
            if subnet_v4.get_meta('dhcp_server') is not None:
                continue

            if subnet_v4['ips']:
                ip = subnet_v4['ips'][0]
                address = ip['address']
                netmask = model.get_netmask(ip, subnet_v4)
                if subnet_v4['gateway']:
                    gateway = subnet_v4['gateway']['address']
                broadcast = str(subnet_v4.as_netaddr().broadcast)
                dns = ' '.join([i['address'] for i in subnet_v4['dns']])
                for route_ref in subnet_v4['routes']:
                    (net, mask) = get_net_and_mask(route_ref['cidr'])
                    route = {'gateway': str(route_ref['gateway']['address']),
                             'cidr': str(route_ref['cidr']),
                             'network': net,
                             'netmask': mask}
                    routes.append(route)

        address_v6 = None
        gateway_v6 = ''
        netmask_v6 = None
        dns_v6 = None
        have_ipv6 = (use_ipv6 and subnet_v6)
        if have_ipv6:
            if subnet_v6.get_meta('dhcp_server') is not None:
                continue

            if subnet_v6['ips']:
                ipv6_is_available = True
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']
                dns_v6 = ' '.join([i['address'] for i in subnet_v6['dns']])

        net_info = {'name': 'eth%d' % ifc_num,
                    'hwaddress': hwaddress,
                    'address': address,
                    'netmask': netmask,
                    'gateway': gateway,
                    'broadcast': broadcast,
                    'dns': dns,
                    'routes': routes,
                    'address_v6': address_v6,
                    'gateway_v6': gateway_v6,
                    'netmask_v6': netmask_v6,
                    'dns_v6': dns_v6,
                   }
        nets.append(net_info)

    if not nets:
        return

    tmpl_path, tmpl_file = os.path.split(template)
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path),
                             trim_blocks=True)
    template = env.get_template(tmpl_file)
    return template.render({'interfaces': nets,
                            'use_ipv6': ipv6_is_available,
                            'libvirt_virt_type': libvirt_virt_type})
示例#4
0
def get_injected_network_template(network_info,
                                  template=None,
                                  libvirt_virt_type=None):
    """Returns a rendered network template for the given network_info.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param template: Path to the interfaces template file.
    :param libvirt_virt_type: The Libvirt `virt_type`, will be `None` for
        other hypervisors..
    """
    if not template:
        template = CONF.injected_network_template

    if not (network_info and template):
        return

    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif['network'] or not vif['network']['subnets']:
            continue

        network = vif['network']
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = _get_first_network(network, 4)
        subnet_v6 = _get_first_network(network, 6)

        ifc_num += 1

        if not network.get_meta('injected'):
            continue

        hwaddress = vif.get('address')
        address = None
        netmask = None
        gateway = ''
        broadcast = None
        dns = None
        routes = []
        if subnet_v4:
            if subnet_v4.get_meta('dhcp_server') is not None:
                continue

            if subnet_v4['ips']:
                ip = subnet_v4['ips'][0]
                address = ip['address']
                netmask = model.get_netmask(ip, subnet_v4)
                if subnet_v4['gateway']:
                    gateway = subnet_v4['gateway']['address']
                broadcast = str(subnet_v4.as_netaddr().broadcast)
                dns = ' '.join([i['address'] for i in subnet_v4['dns']])
                for route_ref in subnet_v4['routes']:
                    (net, mask) = get_net_and_mask(route_ref['cidr'])
                    route = {
                        'gateway': str(route_ref['gateway']['address']),
                        'cidr': str(route_ref['cidr']),
                        'network': net,
                        'netmask': mask
                    }
                    routes.append(route)

        address_v6 = None
        gateway_v6 = ''
        netmask_v6 = None
        dns_v6 = None
        if subnet_v6:
            if subnet_v6.get_meta('dhcp_server') is not None:
                continue

            if subnet_v6['ips']:
                ipv6_is_available = True
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']
                dns_v6 = ' '.join([i['address'] for i in subnet_v6['dns']])

        net_info = {
            'name': 'eth%d' % ifc_num,
            'hwaddress': hwaddress,
            'address': address,
            'netmask': netmask,
            'gateway': gateway,
            'broadcast': broadcast,
            'dns': dns,
            'routes': routes,
            'address_v6': address_v6,
            'gateway_v6': gateway_v6,
            'netmask_v6': netmask_v6,
            'dns_v6': dns_v6,
        }
        nets.append(net_info)

    if not nets:
        return

    tmpl_path, tmpl_file = os.path.split(template)
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path),
                             trim_blocks=True)
    template = env.get_template(tmpl_file)
    return template.render({
        'interfaces': nets,
        'use_ipv6': ipv6_is_available,
        'libvirt_virt_type': libvirt_virt_type
    })
示例#5
0
def get_injected_network_template(network_info,
                                  use_ipv6=CONF.use_ipv6,
                                  template=CONF.injected_network_template):
    """Returns a rendered network template for the given network_info.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    :param template: Path to the interfaces template file.
    """
    if not (network_info and template):
        return

    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif['network'] or not vif['network']['subnets']:
            continue

        network = vif['network']
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = _get_first_network(network, 4)
        subnet_v6 = _get_first_network(network, 6)

        ifc_num += 1

        if not network.get_meta('injected'):
            continue

        address = None
        netmask = None
        gateway = ''
        broadcast = None
        dns = None
        if subnet_v4:
            if subnet_v4.get_meta('dhcp_server') is not None:
                continue

            if subnet_v4['ips']:
                ip = subnet_v4['ips'][0]
                address = ip['address']
                netmask = model.get_netmask(ip, subnet_v4)
                if subnet_v4['gateway']:
                    gateway = subnet_v4['gateway']['address']
                broadcast = str(subnet_v4.as_netaddr().broadcast)
                dns = ' '.join([i['address'] for i in subnet_v4['dns']])

        address_v6 = None
        gateway_v6 = ''
        netmask_v6 = None
        have_ipv6 = (use_ipv6 and subnet_v6)
        if have_ipv6:
            if subnet_v6.get_meta('dhcp_server') is not None:
                continue

            if subnet_v6['ips']:
                ipv6_is_available = True
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']

        net_info = {
            'name': 'eth%d' % ifc_num,
            'address': address,
            'netmask': netmask,
            'gateway': gateway,
            'broadcast': broadcast,
            'dns': dns,
            'address_v6': address_v6,
            'gateway_v6': gateway_v6,
            'netmask_v6': netmask_v6,
        }
        nets.append(net_info)

    if not nets:
        return

    return build_template(template, nets, ipv6_is_available)
示例#6
0
def get_non_legacy_network_template(network_info,
                                    use_ipv6=CONF.use_ipv6,
                                    template=CONF.injected_network_template):
    """A new version of get_injected_network_template that does not rely on
       legacy network info.

    Returns a rendered network template for the given network_info.  When
    libvirt's dependency on using legacy network info for network config
    injection goes away, this function can replace
    get_injected_network_template entirely.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    :param template: Path to the interfaces template file.
    """
    if not (network_info and template):
        return

    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif['network'] or not vif['network']['subnets']:
            continue

        network = vif['network']
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = [i for i in network['subnets'] if i['version'] == 4][0]
        subnet_v6 = [i for i in network['subnets'] if i['version'] == 6]
        if subnet_v6:
            subnet_v6 = subnet_v6[0]

        ifc_num += 1

        if (not network.get_meta('injected') or not subnet_v4['ips']
                or subnet_v4.get_meta('dhcp_server') is not None):
            continue

        ip = subnet_v4['ips'][0]
        address = ip['address']
        netmask = model.get_netmask(ip, subnet_v4)
        gateway = ''
        if subnet_v4['gateway']:
            gateway = subnet_v4['gateway']['address']
        broadcast = str(subnet_v4.as_netaddr().broadcast)
        dns = ' '.join([i['address'] for i in subnet_v4['dns']])
        # NOTE(bnemec): I don't think this code would handle a pure IPv6
        # environment properly, but I don't have such an environment in
        # which to test/fix that.
        address_v6 = None
        gateway_v6 = None
        netmask_v6 = None
        have_ipv6 = (use_ipv6 and subnet_v6)
        if have_ipv6:
            if subnet_v6['ips']:
                ipv6_is_available = True
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                gateway_v6 = ''
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']

        net_info = {
            'name': 'eth%d' % ifc_num,
            'address': address,
            'netmask': netmask,
            'gateway': gateway,
            'broadcast': broadcast,
            'dns': dns,
            'address_v6': address_v6,
            'gateway_v6': gateway_v6,
            'netmask_v6': netmask_v6,
        }
        nets.append(net_info)

    if not nets:
        return

    return build_template(template, nets, ipv6_is_available)
示例#7
0
def get_injected_network_template(network_info, use_ipv6=CONF.use_ipv6,
                                    template=CONF.injected_network_template):
    """Returns a rendered network template for the given network_info.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    :param template: Path to the interfaces template file.
    """
    if not (network_info and template):
        return

    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif['network'] or not vif['network']['subnets']:
            continue

        network = vif['network']
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = _get_first_network(network, 4)
        subnet_v6 = _get_first_network(network, 6)

        ifc_num += 1

        if not network.get_meta('injected'):
            continue

        address = None
        netmask = None
        gateway = ''
        broadcast = None
        dns = None
        if subnet_v4:
            if subnet_v4.get_meta('dhcp_server') is not None:
                continue

            if subnet_v4['ips']:
                ip = subnet_v4['ips'][0]
                address = ip['address']
                netmask = model.get_netmask(ip, subnet_v4)
                if subnet_v4['gateway']:
                    gateway = subnet_v4['gateway']['address']
                broadcast = str(subnet_v4.as_netaddr().broadcast)
                dns = ' '.join([i['address'] for i in subnet_v4['dns']])

        address_v6 = None
        gateway_v6 = ''
        netmask_v6 = None
        have_ipv6 = (use_ipv6 and subnet_v6)
        if have_ipv6:
            if subnet_v6.get_meta('dhcp_server') is not None:
                continue

            if subnet_v6['ips']:
                ipv6_is_available = True
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']

        net_info = {'name': 'eth%d' % ifc_num,
                    'address': address,
                    'netmask': netmask,
                    'gateway': gateway,
                    'broadcast': broadcast,
                    'dns': dns,
                    'address_v6': address_v6,
                    'gateway_v6': gateway_v6,
                    'netmask_v6': netmask_v6,
                   }
        nets.append(net_info)

    if not nets:
        return

    return build_template(template, nets, ipv6_is_available)
示例#8
0
def _get_network_entries(network_info, use_ipv6=CONF.use_ipv6):
    """
    THIS FUNCTION WAS REMOVED IN LIBERTY. PF9 FUNCTIONS IN LIBVIRT DRIVER
    STILL USE THIS FUNCTION HENCE KEEPING IT. DURING NEXT MERGE PLEASE MAKE
    SURE THAT FUNCTIONALITY IN FUNCTION - get_injected_network_template IS
    REPLICATED IN THIS FUNCTION AS WELL.
    Returns a tuple containing network entries for the given network_info,
    and a flag indicating whether IPv6 is available.
    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    """
    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif['network'] or not vif['network']['subnets']:
            continue

        network = vif['network']
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = _get_first_network(network, 4)
        subnet_v6 = _get_first_network(network, 6)

        ifc_num += 1

        if not network.get_meta('injected'):
            continue

        hwaddress = vif.get('address')
        address = None
        netmask = None
        gateway = ''
        broadcast = None
        dns = None
        routes = []
        if subnet_v4:
            if subnet_v4.get_meta('dhcp_server') is not None:
                continue

            if subnet_v4['ips']:
                ip = subnet_v4['ips'][0]
                address = ip['address']
                netmask = model.get_netmask(ip, subnet_v4)
                if subnet_v4['gateway']:
                    gateway = subnet_v4['gateway']['address']
                broadcast = str(subnet_v4.as_netaddr().broadcast)
                dns = ' '.join([i['address'] for i in subnet_v4['dns']])
                for route_ref in subnet_v4['routes']:
                    (net, mask) = get_net_and_mask(route_ref['cidr'])
                    route = {
                        'gateway': str(route_ref['gateway']['address']),
                        'cidr': str(route_ref['cidr']),
                        'network': net,
                        'netmask': mask
                    }
                    routes.append(route)
            else:
                # PF9: Configured by external DHCP server
                pass

        address_v6 = None
        gateway_v6 = ''
        netmask_v6 = None
        dns_v6 = None
        have_ipv6 = (use_ipv6 and subnet_v6)
        if have_ipv6:
            if subnet_v6.get_meta('dhcp_server') is not None:
                continue

            if subnet_v6['ips']:
                ipv6_is_available = True
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                dns_v6 = ' '.join([i['address'] for i in subnet_v6['dns']])
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']
            else:
                # Configured by external DHCP server
                pass

        net_info = {
            'name': 'eth%d' % ifc_num,
            'hwaddress': hwaddress,
            'address': address,
            'netmask': netmask,
            'gateway': gateway,
            'broadcast': broadcast,
            'dns': dns,
            'routes': routes,
            'address_v6': address_v6,
            'gateway_v6': gateway_v6,
            'netmask_v6': netmask_v6,
            'dns_v6': dns_v6,
            'has_ipv6': have_ipv6
        }
        nets.append(net_info)

    return nets
def get_non_legacy_network_template(network_info, use_ipv6=CONF.use_ipv6,
                                    template=CONF.injected_network_template):
    """A new version of get_injected_network_template that does not rely on
       legacy network info.

    Returns a rendered network template for the given network_info.  When
    libvirt's dependency on using legacy network info for network config
    injection goes away, this function can replace
    get_injected_network_template entirely.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    :param template: Path to the interfaces template file.
    """
    if not (network_info and template):
        return

    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif['network'] or not vif['network']['subnets']:
            continue

        network = vif['network']
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = [i for i in network['subnets'] if i['version'] == 4][0]
        subnet_v6 = [i for i in network['subnets'] if i['version'] == 6]
        if subnet_v6:
            subnet_v6 = subnet_v6[0]

        ifc_num += 1

        if (not network.get_meta('injected') or not subnet_v4['ips'] or
                subnet_v4.get_meta('dhcp_server') is not None):
            continue

        ip = subnet_v4['ips'][0]
        address = ip['address']
        netmask = model.get_netmask(ip, subnet_v4)
        gateway = ''
        if subnet_v4['gateway']:
            gateway = subnet_v4['gateway']['address']
        broadcast = str(subnet_v4.as_netaddr().broadcast)
        dns = ' '.join([i['address'] for i in subnet_v4['dns']])
        # NOTE(bnemec): I don't think this code would handle a pure IPv6
        # environment properly, but I don't have such an environment in
        # which to test/fix that.
        address_v6 = None
        gateway_v6 = None
        netmask_v6 = None
        have_ipv6 = (use_ipv6 and subnet_v6)
        if have_ipv6:
            if subnet_v6['ips']:
                ipv6_is_available = True
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                gateway_v6 = ''
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']

        net_info = {'name': 'eth%d' % ifc_num,
                    'address': address,
                    'netmask': netmask,
                    'gateway': gateway,
                    'broadcast': broadcast,
                    'dns': dns,
                    'address_v6': address_v6,
                    'gateway_v6': gateway_v6,
                    'netmask_v6': netmask_v6,
                   }
        nets.append(net_info)

    if not nets:
        return

    return build_template(template, nets, ipv6_is_available)
示例#10
0
def get_injected_network_template(network_info, use_ipv6=CONF.use_ipv6,
                                  template=CONF.injected_network_template):
    """Returns a rendered network template for the given network_info.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    :param template: Path to the interfaces template file.
    """
    if not (network_info and template):
        return

    nets = []
    ifc_num = -1

    for vif in network_info:
        if not vif['network']:
            continue

        network = vif['network']
        if not network.get_meta('injected'):
            continue

        subnet_v4 = None
        subnet_v6 = None
        methodv4 = None
        methodv6 = None

        if vif['network']['subnets']:
            subnet_v4 = base_netutils._get_first_network(network, 4)
            subnet_v6 = base_netutils._get_first_network(network, 6)
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.

        ifc_num += 1

        address = None
        netmask = None
        gateway = ''
        broadcast = None
        dns = None
        if subnet_v4:
            if subnet_v4.get_meta('dhcp_server') is not None:
                methodv4 = 'dhcp'
            elif subnet_v4['ips']:
                methodv4 = 'static'
                ip = subnet_v4['ips'][0]
                address = ip['address']
                netmask = model.get_netmask(ip, subnet_v4)
                if subnet_v4['gateway']:
                    gateway = subnet_v4['gateway']['address']
                broadcast = str(subnet_v4.as_netaddr().broadcast)
                dns = ' '.join([i['address'] for i in subnet_v4['dns']])
        else:
            # The VIF doesn't have a v4 subnet.  Check CONF values.
            if CONF.no_v4subnet_method_inject and \
                    CONF.no_v4subnet_method_inject.lower() == 'dhcp':
                methodv4 = 'dhcp'

        address_v6 = None
        gateway_v6 = ''
        netmask_v6 = None
        if subnet_v6:
            if subnet_v6.get_meta('dhcp_server') is not None:
                methodv6 = 'dhcp'
            elif subnet_v6['ips']:
                methodv6 = 'static'
                ip_v6 = subnet_v6['ips'][0]
                address_v6 = ip_v6['address']
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                if subnet_v6['gateway']:
                    gateway_v6 = subnet_v6['gateway']['address']

        elif use_ipv6:
            # request to use ipv6 but no subnet defined.
            # Check the CONF value for the method to use.
            if CONF.no_v6subnet_method_inject:
                if CONF.no_v6subnet_method_inject.lower() == 'dhcp':
                    methodv6 = 'dhcp'
                elif CONF.no_v6subnet_method_inject.lower() == 'auto':
                    methodv6 = 'auto'

        net_info = {'name': 'eth%d' % ifc_num,
                    'methodv4': methodv4,
                    'methodv6': methodv6,
                    'address': address,
                    'netmask': netmask,
                    'gateway': gateway,
                    'broadcast': broadcast,
                    'dns': dns,
                    'address_v6': address_v6,
                    'gateway_v6': gateway_v6,
                    'netmask_v6': netmask_v6,
                    }
        nets.append(net_info)

    if not nets:
        return

    return base_netutils.build_template(template, nets, use_ipv6)
示例#11
0
def get_injected_network_template(network_info, use_ipv6=None, template=None, libvirt_virt_type=None):
    """Returns a rendered network template for the given network_info.

    :param network_info:
        :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
    :param use_ipv6: If False, do not return IPv6 template information
        even if an IPv6 subnet is present in network_info.
    :param template: Path to the interfaces template file.
    :param libvirt_virt_type: The Libvirt `virt_type`, will be `None` for
        other hypervisors..
    """
    if use_ipv6 is None:
        use_ipv6 = CONF.use_ipv6

    if not template:
        template = CONF.injected_network_template

    if not (network_info and template):
        return

    nets = []
    ifc_num = -1
    ipv6_is_available = False

    for vif in network_info:
        if not vif["network"] or not vif["network"]["subnets"]:
            continue

        network = vif["network"]
        # NOTE(bnemec): The template only supports a single subnet per
        # interface and I'm not sure how/if that can be fixed, so this
        # code only takes the first subnet of the appropriate type.
        subnet_v4 = _get_first_network(network, 4)
        subnet_v6 = _get_first_network(network, 6)

        ifc_num += 1

        if not network.get_meta("injected"):
            continue

        hwaddress = vif.get("address")
        address = None
        netmask = None
        gateway = ""
        broadcast = None
        dns = None
        if subnet_v4:
            if subnet_v4.get_meta("dhcp_server") is not None:
                continue

            if subnet_v4["ips"]:
                ip = subnet_v4["ips"][0]
                address = ip["address"]
                netmask = model.get_netmask(ip, subnet_v4)
                if subnet_v4["gateway"]:
                    gateway = subnet_v4["gateway"]["address"]
                broadcast = str(subnet_v4.as_netaddr().broadcast)
                dns = " ".join([i["address"] for i in subnet_v4["dns"]])

        address_v6 = None
        gateway_v6 = ""
        netmask_v6 = None
        dns_v6 = None
        have_ipv6 = use_ipv6 and subnet_v6
        if have_ipv6:
            if subnet_v6.get_meta("dhcp_server") is not None:
                continue

            if subnet_v6["ips"]:
                ipv6_is_available = True
                ip_v6 = subnet_v6["ips"][0]
                address_v6 = ip_v6["address"]
                netmask_v6 = model.get_netmask(ip_v6, subnet_v6)
                if subnet_v6["gateway"]:
                    gateway_v6 = subnet_v6["gateway"]["address"]
                dns_v6 = " ".join([i["address"] for i in subnet_v6["dns"]])

        net_info = {
            "name": "eth%d" % ifc_num,
            "hwaddress": hwaddress,
            "address": address,
            "netmask": netmask,
            "gateway": gateway,
            "broadcast": broadcast,
            "dns": dns,
            "address_v6": address_v6,
            "gateway_v6": gateway_v6,
            "netmask_v6": netmask_v6,
            "dns_v6": dns_v6,
        }
        nets.append(net_info)

    if not nets:
        return

    tmpl_path, tmpl_file = os.path.split(CONF.injected_network_template)
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(tmpl_path), trim_blocks=True)
    template = env.get_template(tmpl_file)
    return template.render({"interfaces": nets, "use_ipv6": ipv6_is_available, "libvirt_virt_type": libvirt_virt_type})