Example #1
0
 def test_returns_interface_name_with_address(self):
     proc_path = self.make_dir()
     leases_path = self.make_dir()
     running_pids = set(random.randint(2, 999) for _ in range(3))
     self.patch(
         dhclient_module, "get_running_pids_with_command"
     ).return_value = running_pids
     interfaces = {}
     for pid in running_pids:
         interface_name = factory.make_name("eth")
         address = factory.make_ipv4_address()
         interfaces[interface_name] = address
         lease_path = os.path.join(leases_path, "%s.lease" % interface_name)
         lease_data = (
             dedent(
                 """\
             lease {
               interface "%s";
               fixed-address %s;
             }
             """
             )
             % (interface_name, address)
         )
         atomic_write(lease_data.encode("ascii"), lease_path)
         cmdline_path = os.path.join(proc_path, str(pid), "cmdline")
         cmdline = [
             "/sbin/dhclient",
             "-d",
             "-q",
             "-pf",
             "/run/dhclient-%s.pid" % interface_name,
             "-lf",
             lease_path,
             "-cf",
             "/var/lib/dhclient/dhclient-%s.conf" % interface_name,
             interface_name,
         ]
         cmdline = "\x00".join(cmdline) + "\x00"
         os.mkdir(os.path.join(proc_path, str(pid)))
         atomic_write(cmdline.encode("ascii"), cmdline_path)
     self.assertEquals(interfaces, get_dhclient_info(proc_path=proc_path))
Example #2
0
def get_all_interfaces_definition(
        annotate_with_monitored: bool = True) -> dict:
    """Return interfaces definition by parsing "ip addr" and the running
    "dhclient" processes on the machine.

    The interfaces definition is defined as a contract between the region and
    the rack controller. The region controller processes this resulting
    dictionary to update the interfaces model for the rack controller.

    :param annotate_with_monitored: If True, annotates the given interfaces
        with whether or not they should be monitored. (Default: True)
    """
    interfaces = {}
    dhclient_info = get_dhclient_info()
    iproute_info = get_ip_route()
    exclude_types = ["loopback", "ipip"]
    if not running_in_container():
        exclude_types.append("ethernet")
    ipaddr_info = {
        name: ipaddr
        for name, ipaddr in get_ip_addr().items()
        if (ipaddr["type"] not in exclude_types
            and not ipaddr["type"].startswith("unknown-"))
    }
    for name, ipaddr in ipaddr_info.items():
        iface_type = "physical"
        parents = []
        mac_address = None
        vid = None
        if ipaddr["type"] == "ethernet.bond":
            iface_type = "bond"
            mac_address = ipaddr["mac"]
            for bond_nic in ipaddr["bonded_interfaces"]:
                if bond_nic in interfaces or bond_nic in ipaddr_info:
                    parents.append(bond_nic)
        elif ipaddr["type"] == "ethernet.vlan":
            iface_type = "vlan"
            parents.append(ipaddr['parent'])
            vid = ipaddr["vid"]
        elif ipaddr["type"] == "ethernet.bridge":
            iface_type = "bridge"
            mac_address = ipaddr["mac"]
            for bridge_nic in ipaddr["bridged_interfaces"]:
                if bridge_nic in interfaces or bridge_nic in ipaddr_info:
                    parents.append(bridge_nic)
        else:
            mac_address = ipaddr["mac"]

        # Create the interface definition will links for both IPv4 and IPv6.
        interface = {
            "type": iface_type,
            "index": ipaddr['index'],
            "links": [],
            "enabled": True if 'UP' in ipaddr['flags'] else False,
            "parents": parents,
            "source": "ipaddr",
        }
        if mac_address is not None:
            interface["mac_address"] = mac_address
        if vid is not None:
            interface["vid"] = vid
        # Add the static and dynamic IP addresses assigned to the interface.
        dhcp_address = dhclient_info.get(name, None)
        for address in ipaddr.get("inet", []) + ipaddr.get("inet6", []):
            if str(IPNetwork(address).ip) == dhcp_address:
                interface["links"].append({
                    "mode": "dhcp",
                    "address": address,
                })
            else:
                interface["links"].append({
                    "mode": "static",
                    "address": address,
                })
        fix_link_addresses(interface["links"])
        fix_link_gateways(interface["links"], iproute_info)
        interfaces[name] = interface

        if annotate_with_monitored:
            annotate_with_default_monitored_interfaces(interfaces)

    return interfaces