def assign_ipv4_link_local(nic=None): """Bring up NIC using an address using link-local (ip4LL) IPs. On DigitalOcean, the link-local domain is per-droplet routed, so there is no risk of collisions. However, to be more safe, the ip4LL address is random. """ if not nic: for cdev in sorted(cloudnet.get_devicelist()): if cloudnet.is_physical(cdev): nic = cdev LOG.debug("assigned nic '%s' for link-local discovery", nic) break if not nic: raise RuntimeError("unable to find interfaces to access the" "meta-data server. This droplet is broken.") addr = "169.254.{0}.{1}/16".format(random.randint(1, 168), random.randint(0, 255)) ip_addr_cmd = ['ip', 'addr', 'add', addr, 'dev', nic] ip_link_cmd = ['ip', 'link', 'set', 'dev', nic, 'up'] if not util.which('ip'): raise RuntimeError("No 'ip' command available to configure ip4LL " "address") try: (result, _err) = util.subp(ip_addr_cmd) LOG.debug("assigned ip4LL address '%s' to '%s'", addr, nic) (result, _err) = util.subp(ip_link_cmd) LOG.debug("brought device '%s' up", nic) except Exception: util.logexc( LOG, "ip4LL address assignment of '%s' to '%s' failed." " Droplet networking will be broken", addr, nic) raise return nic
def assign_ipv4_link_local(nic=None): """Bring up NIC using an address using link-local (ip4LL) IPs. On DigitalOcean, the link-local domain is per-droplet routed, so there is no risk of collisions. However, to be more safe, the ip4LL address is random. """ if not nic: for cdev in sorted(cloudnet.get_devicelist()): if cloudnet.is_physical(cdev): nic = cdev LOG.debug("assigned nic '%s' for link-local discovery", nic) break if not nic: raise RuntimeError("unable to find interfaces to access the" "meta-data server. This droplet is broken.") addr = "169.254.{0}.{1}/16".format(random.randint(1, 168), random.randint(0, 255)) ip_addr_cmd = ['ip', 'addr', 'add', addr, 'dev', nic] ip_link_cmd = ['ip', 'link', 'set', 'dev', nic, 'up'] if not util.which('ip'): raise RuntimeError("No 'ip' command available to configure ip4LL " "address") try: (result, _err) = util.subp(ip_addr_cmd) LOG.debug("assigned ip4LL address '%s' to '%s'", addr, nic) (result, _err) = util.subp(ip_link_cmd) LOG.debug("brought device '%s' up", nic) except Exception: util.logexc(LOG, "ip4LL address assignment of '%s' to '%s' failed." " Droplet networking will be broken", addr, nic) raise return nic
def test_is_physical(self): """is_physical is True when /sys/net/devname/device exists.""" self.assertFalse(net.is_physical('eth0')) ensure_file(os.path.join(self.sysdir, 'eth0', 'device')) self.assertTrue(net.is_physical('eth0'))
def get_physical_nics_by_mac(): devs = net.get_interfaces_by_mac() return dict([(m, n) for m, n in devs.items() if net.is_physical(n)])
def get_link_local_nic(): nics = [f for f in cloudnet.get_devicelist() if cloudnet.is_physical(f)] if not nics: return None return min(nics, key=lambda d: cloudnet.read_sys_net_int(d, 'ifindex'))
def is_physical(self, devname: DeviceName) -> bool: return net.is_physical(devname)
def get_first_physical_interface(): devs = [f for f in cloudnet.get_devicelist() if cloudnet.is_physical(f)] if not devs: raise RuntimeError("No interfaces find to configure link-local address") return min(devs, key=lambda d: cloudnet.read_sys_net_int(d, 'ifindex'))