def _has_libvirt_network_data(self, directory, device_name): def _parse_libvirt_file(libvirt_filename): try: xml_data = None if libvirt_filename not in self.libvirt_cache: stdout = utils.execute("cat", libvirt_filename, run_as_root=True)[0] xml_data = cElementTree.fromstring(stdout) self.libvirt_cache[libvirt_filename] = xml_data else: xml_data = self.libvirt_cache[libvirt_filename] return xml_data except Exception: """ file cannot be parsed as xml, so ignore it though log debug message """ LOG.debug("file %s cannot be parsed as xml, ignoring" % libvirt_filename) return None def _scan_for_bridge(libvirt_xml, device_name): net_list = libvirt_xml.findall("network") for network in net_list: bridge_list = network.findall("bridge") ip = network.find("ip") for bridge in bridge_list: name = bridge.get("name") LOG.debug("device name = %s, ip = %s" % (device_name, str(ip))) if name == device_name and ip: return True return False libvirt_dir = directory if len(self.libvirt_files) == 0: self.libvirt_files = [ f for f in commandlet._list_directory(libvirt_dir, run_as_root=True) if commandlet._check_file_exists("%s/%s" % (libvirt_dir, f)) ] LOG.debug("libvirt network files = %s" % self.libvirt_files) for libvirt_file in self.libvirt_files: fname = "%s/%s" % (libvirt_dir, libvirt_file) xml_elem = _parse_libvirt_file(fname) if not xml_elem: continue if _scan_for_bridge(xml_elem, device_name): return True return False
def _has_ifcfg_data(self, device_name): """ Determine if ifcfg data exists for device. :param device_name: A device name to look up such as eth0. :return: ifcfg_dict if data exists, {} otherwise. """ ifcfg_dir = "/etc/sysconfig/network-scripts" # The device_name is usually in the ifcfg file name, so check for this # case first. ifcfg_dict = {} fname = "%s/ifcfg-%s" % (ifcfg_dir, device_name) LOG.debug("Initial attempt to process %s using ifcfg file %s" % (device_name, fname)) if fname in self.ifcfg_cache: LOG.debug("Using cached ifcfg data") ifcfg_dict = self.ifcfg_cache[fname] elif commandlet._check_file_exists(fname): if fname not in self.ifcfg_cache: LOG.debug("Retrieving ifcfg data from system") stdout = utils.execute("cat", fname, run_as_root=True)[0] ifcfg_dict = self.commandparse.parse_ifcfg(device_name, stdout) self.ifcfg_cache[fname] = ifcfg_dict # make sure there are no quote around the device name we # get from the ifcfg file ifcfg_dev_name = re.sub(r'^"|"$', "", ifcfg_dict.get(agent.IFCFG_DEVICE, "")) if ifcfg_dev_name == device_name and self._has_ip_config(ifcfg_dict): LOG.debug("found ifcfg file in expected location.") return ifcfg_dict # The ifcfg with the desired device name either does not exist, or # contains a different internal device name. Search all ifcfg files # looking for the correct device. if len(self.ifcfg_files) == 0: self.ifcfg_files = [ f for f in commandlet._list_directory(ifcfg_dir) if commandlet._check_file_exists("%s/%s" % (ifcfg_dir, f)) ] for ifcfg_file in self.ifcfg_files: fname = "%s/%s" % (ifcfg_dir, ifcfg_file) LOG.debug("General attempt to process %s using ifcfg file %s" % (device_name, fname)) if os.access(fname, os.X_OK): LOG.debug("%s is executable, will not be processed for ifcfg " "data" % fname) self.ifcfg_files.remove(ifcfg_file) continue if fname not in self.ifcfg_cache: LOG.debug("For %s retrieving ifcfg file %s from filesystem" % (device_name, fname)) stdout = utils.execute("cat", fname, run_as_root=True)[0] ifcfg_dict = self.commandparse.parse_ifcfg(device_name, stdout) self.ifcfg_cache[fname] = ifcfg_dict else: LOG.debug("Using cached ifcfg data for file %s" % fname) # make sure there are no quote around the device name we # get from the ifcfg file ifcfg_dev_name = re.sub(r'^"|"$', "", self.ifcfg_cache[fname].get(agent.IFCFG_DEVICE, "")) LOG.debug( "check for match between %s and %s and " "ip data in %s" % (ifcfg_dev_name, device_name, str(ifcfg_dict)) ) if ifcfg_dev_name == device_name and self._has_ip_config(self.ifcfg_cache[fname]): LOG.debug("found ifcfg data in file %s for device %s." % (fname, device_name)) return self.ifcfg_cache[fname] return {}