def _status(self, verbose=False): """ Evaluate the ip resource status. """ try: self.getaddr() except Exception as exc: self.status_log(str(exc)) if "not allocated" in str(exc): return core.status.DOWN else: return core.status.WARN ifconfig = self.get_ifconfig() intf = ifconfig.interface(self.ipdev) mode = getattr(self, "mode") if hasattr(self, "mode") else None if intf is None and "dedicated" not in self.tags and mode != "dedicated": self.status_log("interface %s not found" % self.ipdev) return core.status.DOWN try: if self.is_up() and self.has_carrier(intf) is not False: return core.status.UP else: return core.status.DOWN except ex.NotSupported: self.status_log("not supported", "info") return core.status.NA except ex.Error as exc: self.status_log(str(exc), "error") return core.status.WARN
def allow_start(self): """ Do sanity checks before allowing the start. """ if self.is_up() is True: self.log.info("%s is already up on %s", self.addr, self.ipdev) raise ex.IpAlreadyUp(self.addr) ifconfig = self.get_ifconfig() intf = ifconfig.interface(self.ipdev) if self.has_carrier(intf) is False and not self.svc.options.force: self.log.error("interface %s no-carrier.", self.ipdev) raise ex.IpDevDown(self.ipdev) if intf is None: self.log.error("interface %s not found. Cannot stack over it.", self.ipdev) raise ex.IpDevDown(self.ipdev) if not intf.flag_up: if hasattr(intf, 'groupname') and intf.groupname != "": l = [_intf for _intf in ifconfig.get_matching_interfaces('groupname', intf.groupname) if _intf.flag_up] if len(l) == 1: self.log.info("switch %s to valid alternate path %s", self.ipdev, l[0].name) intf = l[0] self.ipdev = l[0].name try: self.start_link() except ex.MissImpl: self.log.error("interface %s is not up. Cannot stack over it.", self.ipdev) raise ex.IpDevDown(self.ipdev) if not self.svc.abort_start_done and self.check_ping(): self.log.error("%s is already up on another host", self.addr) raise ex.IpConflict(self.addr) return
def get_ip_address(ifname): ifconfig = utilities.ifconfig.Ifconfig() intf = ifconfig.interface(ifname) if intf is None: raise AttributeError("interface %s not found" % ifname) if isinstance(intf.ipaddr, list): addr = intf.ipaddr[0] else: addr = intf.ipaddr return addr
def has_ip(self): ifconfig = self.get_ifconfig() if ifconfig is None: return False iface = ifconfig.interface(self.ipdev) if iface is None: return False if len(iface.ipaddr) == 0: return False return True
def get_network_interface(self): ifconfig = utilities.ifconfig.Ifconfig() intf = ifconfig.interface(self.ipdev) ips = set(intf.ipaddr + intf.ip6addr) instance_data = self.get_instance_data(refresh=True) if instance_data is None: raise ex.Error("can't find instance data") for eni in instance_data["NetworkInterfaces"]: _ips = set( [pa["PrivateIpAddress"] for pa in eni["PrivateIpAddresses"]]) if len(ips & _ips) > 0: return eni["NetworkInterfaceId"]
def get_mask(self, ifconfig=None): if ifconfig is None: ifconfig = self.get_ifconfig() if self.netmask is None: intf = ifconfig.interface(self.ipdev) if intf is None: raise ex.Error("netmask parameter is mandatory with 'noalias' tag") self.netmask = intf.mask if not self.netmask: if "noaction" not in self.tags: self.netmask = None raise ex.Error("No netmask set on parent interface %s" % self.ipdev) if isinstance(self.netmask, list): try: self.netmask = self.netmask[0] except IndexError: self.netmask = None
def allow_start(self): retry = 1 interval = 0 import time ok = False if 'noalias' not in self.tags: for i in range(retry): ifconfig = utilities.ifconfig.Ifconfig() intf = ifconfig.interface(self.ipdev) if intf is not None and intf.flag_up: ok = True break time.sleep(interval) if not ok: self.log.error("Interface %s is not up. Cannot stack over it." % self.ipdev) raise ex.IpDevDown(self.ipdev) if self.is_up() is True: self.log.info("%s is already up on %s" % (self.addr, self.ipdev)) raise ex.IpAlreadyUp(self.addr) if not hasattr(self, 'abort_start_done') and 'nonrouted' not in self.tags and self.check_ping(): self.log.error("%s is already up on another host" % (self.addr)) raise ex.IpConflict(self.addr) return
def allocate(self): """ Request an ip in the ipdev network from the collector. """ if self.svc.node.collector_env.dbopensvc is None: return try: self.conf_get("ipname") self.log.info("skip allocate: an ip is already defined") return except ex.RequiredOptNotFound: pass except ex.OptNotFound: pass if self.ipdev is None: self.log.info("skip allocate: ipdev is not set") return try: # explicit network setting network = self.conf_get("network") except ex.OptNotFound: network = None if network is None: # implicit network: the network of the first ipdev ip ifconfig = self.get_ifconfig() intf = ifconfig.interface(self.ipdev) try: if isinstance(intf.ipaddr, list): baseaddr = intf.ipaddr[0] else: baseaddr = intf.ipaddr network = str( utilities.net.ipaddress.IPv4Interface( baseaddr).network.network_address) except (ValueError, IndexError): self.log.info( "skip allocate: ipdev %s has no configured address " "and network is not set", self.ipdev) return post_data = { "network": network, } if self.dns_name_suffix: post_data["name"] = self.dns_name_suffix else: self.log.debug("allocate: dns_name_suffix is not set") try: data = self.svc.node.collector_rest_post( "/networks/%s/allocate" % network, post_data, path=self.dns_rec_name(), ) except Exception as exc: raise ex.Error("ip allocation failed: " + str(exc)) if "error" in data: raise ex.Error(data["error"]) if "info" in data: self.log.info(data["info"]) self.ipname = data["data"]["ip"] self.addr = self.ipname self.set_label() self.svc._set(self.rid, "ipname", self.ipname) if self.gateway in (None, ""): gateway = data.get("data", {}).get("network", {}).get("gateway") if gateway: self.log.info("set gateway=%s", gateway) self.svc._set(self.rid, "gateway", gateway) self.gateway = gateway if self.netmask in (None, ""): netmask = data.get("data", {}).get("network", {}).get("netmask") if netmask: self.log.info("set netmask=%s", netmask) self.svc._set(self.rid, "netmask", netmask) self.netmask = str(netmask) self.log.info("ip %s allocated", self.ipname) record_name = data["data"].get("record_name") if record_name: self.log.info("record %s created", record_name)
def get_ipdev(self): ifconfig = self.get_ifconfig() if ifconfig is None: return return ifconfig.interface(self.ipdev)