Fabian Deutsch has uploaded a new change for review. Change subject: [DRAFT] Replace legacy network backend with new backend ......................................................................
[DRAFT] Replace legacy network backend with new backend After creating the backend this patch replaces all occurrences of the old backend code with the new backend. Change-Id: I30378deb42c270d615353110fcf86ec79d18a345 Signed-off-by: Fabian Deutsch <[email protected]> --- M src/ovirt/node/config/defaults.py M src/ovirt/node/setup/core/kdump_page.py M src/ovirt/node/utils/console.py M src/ovirt/node/utils/storage.py M src/ovirtnode/network.py 5 files changed, 86 insertions(+), 291 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/14/15514/1 diff --git a/src/ovirt/node/config/defaults.py b/src/ovirt/node/config/defaults.py index a5faa47..c5656a7 100644 --- a/src/ovirt/node/config/defaults.py +++ b/src/ovirt/node/config/defaults.py @@ -239,35 +239,6 @@ def transaction(self): """Return all transactions to re-configure networking """ - return self.__transaction() - - def __legacy_transaction(self): - class ConfigureNIC(utils.Transaction.Element): - title = "Configuring NIC" - - def prepare(self): - self.logger.debug("Psuedo preparing ovirtnode.Network") - - def commit(self): - from ovirtnode.network import Network as oNetwork - net = oNetwork() - net.configure_interface() - net.save_network_configuration() - #utils.AugeasWrapper.force_reload() - - class ReloadNetworkConfiguration(utils.Transaction.Element): - title = "Reloading network configuration" - - def commit(self): - utils.AugeasWrapper.force_reload() - utils.network.reset_resolver() - - tx = utils.Transaction("Applying new network configuration") - tx.append(ConfigureNIC()) - tx.append(ReloadNetworkConfiguration()) - return tx - - def __transaction(self): services = ["network", "ntpd", "ntpdate", "rpcbind", "nfslock", "rpcidmapd", "rpcgssd"] @@ -537,21 +508,37 @@ self.hostname = hostname def commit(self): - from ovirtnode import network as onet, ovirtfunctions - network = onet.Network() + aug= AugeasWrapper() + localhost_entry = None + for entry in aug.match("/files/etc/hosts/*"): + if aug.get(entry + "/ipaddr") == "127.0.0.1": + localhost_entry = entry + break + + if not localhost_entry: + raise RuntimeError("Couldn't find entry for localhost") + + # Remove all aliases + for alias_entry in aug.match(localhost_entry + "/alias", + False): + aug.remove(alias_entry, False) + + # ... and create a new one + aliases = ["localhost", "localhost.localdomain"] if self.hostname: - network.remove_non_localhost() - network.add_localhost_alias(self.hostname) - else: - network.remove_non_localhost() - self.hostname = "localhost.localdomain" + aliases.append(self.hostname) + + for _idx, alias in enumerate(aliases): + idx = _idx + 1 + p = "%s/alias[%s]" % (localhost_entry, idx) + aug.set(p, alias, False) config.network.hostname(self.hostname) - ovirtfunctions.ovirt_store_config("/etc/hosts") - ovirtfunctions.ovirt_store_config("/etc/hostname") - ovirtfunctions.ovirt_store_config("/etc/sysconfig/network") + fs.Config().persist("/etc/hosts") + fs.Config().persist("/etc/hostname") + fs.Config().persist("/etc/sysconfig/network") utils.network.reset_resolver() @@ -1143,6 +1130,10 @@ nfsv4 = storage.NFSv4() nfsv4.domain(domain) + fs.Config().persist(nfsv4.configfilename) + process.check_call("service rpcidmapd restart") + process.check_call("nfsidmap -c") + tx = utils.Transaction("Configuring NFSv4") tx.append(ConfigureNfsv4()) return tx diff --git a/src/ovirt/node/setup/core/kdump_page.py b/src/ovirt/node/setup/core/kdump_page.py index 3017520..8995ec3 100644 --- a/src/ovirt/node/setup/core/kdump_page.py +++ b/src/ovirt/node/setup/core/kdump_page.py @@ -137,7 +137,8 @@ with self.application.ui.suspended(): utils.process.call("reset") - progress_dialog = console.TransactionProgress(txs, self) + is_dry = self.application.args.dry + progress_dialog = console.TransactionProgress(txs, is_dry) progress_dialog.run() console.writeln("\nPlease press any key to continue") console.wait_for_keypress() diff --git a/src/ovirt/node/utils/console.py b/src/ovirt/node/utils/console.py index bdb22b4..9c7f5fe 100644 --- a/src/ovirt/node/utils/console.py +++ b/src/ovirt/node/utils/console.py @@ -49,9 +49,13 @@ class TransactionProgress(base.Base): """Display the progress of a transaction on a console """ - def __init__(self, transaction, plugin, initial_text=""): + transaction = None + texts = None + is_dry = False + + def __init__(self, transaction, is_dry, initial_text=""): self.transaction = transaction - self.plugin = plugin + self.is_dry = is_dry self.texts = [initial_text, ""] super(TransactionProgress, self).__init__() @@ -88,7 +92,10 @@ # Sometimes a tx_element is wrapping some code that # writes to stdout/stderr which scrambles the screen, # therefore we are capturing this - self.plugin.dry_or(lambda: e.commit()) + if self.is_dry: + self.logger.debug("In dry mode: %s" % e) + else: + e.commit() self.add_update("\nAll changes were applied successfully.") except Exception as e: self.add_update("\nAn error occurred while applying the changes:") diff --git a/src/ovirt/node/utils/storage.py b/src/ovirt/node/utils/storage.py index 77eb4a6..a28cbe4 100644 --- a/src/ovirt/node/utils/storage.py +++ b/src/ovirt/node/utils/storage.py @@ -20,6 +20,8 @@ # also available at http://www.gnu.org/copyleft/gpl.html. from ovirt.node import base +from ovirt.node.utils import process +from ovirt.node.utils.fs import File import os @@ -39,12 +41,30 @@ class NFSv4(base.Base): """A class to deal some external NFSv4 related functionality """ - def domain(self, domain=None): - import ovirtnode.network as onet - if domain: - onet.set_nfsv4_domain(domain) - return onet.get_current_nfsv4_domain() + configfilename= "/etc/idmapd.conf" + def domain(self, domain=None): + if domain: + self.__set_domain(domain) + return self.__get_domain() + + def __set_domain(self, domain): + current_domain = self.__get_domain() + if current_domain.startswith("#"): + current_domain = "#Domain = %s" % current_domain.replace("# ","") + process.check_call("sed -i 's/%s/Domain = %s/g' %s" \ + % (current_domain, domain, self.configfilename)) + else: + process.check_call("sed -i '/^Domain/ s/%s/%s/g' %s" \ + % (current_domain, domain, self.configfilename)) + + def __get_domain(self): + domain = None + nfs_config = File(self.configfilename) + for line in nfs_config: + if "Domain =" in line: + domain = line.replace("Domain =", "").strip() + return domain class Devices(base.Base): """A class to retrieve available storage devices diff --git a/src/ovirtnode/network.py b/src/ovirtnode/network.py index 41e10ff..e1b8d26 100644 --- a/src/ovirtnode/network.py +++ b/src/ovirtnode/network.py @@ -171,84 +171,10 @@ pass return True - def get_localhost_entry(self): - entries = _functions.augtool("match", "/files/etc/hosts/*", "") - for entry in entries: - ipaddr = _functions.augtool("get", entry + "/ipaddr", "") - if ipaddr == "127.0.0.1": - return entry - return None - - def get_num_localhost_aliases(self): - if self.localhost_entry: - aliases = _functions.augtool("match", self.localhost_entry + \ - "/alias", "") - return len(aliases) - return 0 - - def remove_non_localhost(self): - last_alias = _functions.augtool("get", self.localhost_entry + \ - "/alias[" + \ - str(self.alias_count) + "]", "") - while self.alias_count != 0: - if last_alias == "localhost": - break - elif last_alias == "localhost.localdomain": - break - _functions.augtool("rm", self.localhost_entry + "/alias[" + \ - str(self.alias_count) + "]", "") - self.alias_count = self.alias_count - 1 - - def add_localhost_alias(self, alias): - self.alias_count = self.alias_count + 1 - _functions.augtool("set", self.localhost_entry + "/alias[" + \ - str(self.alias_count) + "]", alias) - def configure_dns(self): - logger.warn("Configuring DNS") - OVIRT_VARS = _functions.parse_defaults() - have_peerdns = True - DNS = "" - if "OVIRT_DNS" in OVIRT_VARS: - DNS = OVIRT_VARS["OVIRT_DNS"] - logger.debug("Found DNS key with value '%s'" % DNS) - try: - # Write resolv.conf any way, sometimes without servers - tui_cmt = ("Please make changes through the TUI. " + \ - "Manual edits to this file will be " + \ - "lost on reboot") - _functions.augtool("set", \ - "/files/etc/resolv.conf/#comment[1]", \ - tui_cmt) - DNS = [s for s in DNS.split(",") if s] - i = 1 - for server in DNS: - logger.debug("Setting DNS server %d: %s" % (i, server)) - setting = "/files/etc/resolv.conf/nameserver[%s]" % i - _functions.augtool("set", setting, server) - # PEERDNS=no is required with manual DNS servers - have_peerdns = False - i = i + i - _functions.ovirt_store_config("/etc/resolv.conf") - except: - logger.warn("Failed to set DNS servers") - - # Remove all spare DNS servers - logger.debug("Removing DNS servers") - if len(DNS) < 2: - _functions.augtool("rm", "/files/etc/resolv.conf/nameserver[2]", "") - if len(DNS) < 1: - _functions.augtool("rm", "/files/etc/resolv.conf/nameserver[1]", "") - - # Set or remove PEERDNS for all ifcfg-* - for nic in glob("/etc/sysconfig/network-scripts/ifcfg-*"): - if "ifcfg-lo" in nic: - continue - path = "/files%s/PEERDNS" % nic - if have_peerdns: - _functions.augtool("rm", path, "") - else: - _functions.augtool("set", path, "no") + from ovirt.node.config.defaults import Network + mnet = Network() + mnet.transaction() def configure_ntp(self): if "OVIRT_NTP" in OVIRT_VARS: @@ -433,166 +359,6 @@ _functions.test_ntp_configuration() -def get_system_nics(): - # Re-trigger udev for rhbz#866584 - for sysfspath in glob("/sys/class/net/*"): - _functions.system_closefds("udevadm test %s > /dev/null 2> /dev/null" % quote(sysfspath)) - - client = _functions.gudev.Client(['net']) - configured_nics = 0 - ntp_dhcp = 0 - nic_dict = {} - for device in client.query_by_subsystem("net"): - try: - dev_interface = device.get_property("INTERFACE") - dev_vendor = device.get_property("ID_VENDOR_FROM_DATABASE") - dev_type = device.get_property("DEVTYPE") - dev_path = device.get_property("DEVPATH") - - if (dev_interface == "lo" or \ - dev_interface.startswith("bond") or \ - dev_interface.startswith("sit") or \ - dev_interface.startswith("vnet") or \ - "." in dev_interface or \ - dev_type == "bridge"): - logger.info("Skipping interface '%s'" % dev_interface) - continue - else: - logger.info("Gathering informations for '%s'" % dev_interface) - - try: - dev_vendor = dev_vendor.replace(",", "") - except AttributeError: - logger.debug("2. vendor approach: %s" % dev_vendor) - try: - # rhevh workaround since udev version - # doesn't have vendor info - dev_path = dev_path.split('/') - if "virtio" in dev_path[4]: - pci_dev = dev_path[3].replace("0000:", "") - else: - pci_dev = dev_path[4].replace("0000:", "") - pci_lookup_cmd = (("lspci|grep '%s'|awk -F \":\" " % pci_dev) + - "{'print $3'}") - pci_lookup = _functions.subprocess_closefds(pci_lookup_cmd, - shell=True, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) - dev_vendor = pci_lookup.stdout.read().strip() - except: - dev_vendor = "unknown" - logger.debug("3. vendor approach: %s" % dev_vendor) - try: - dev_vendor = dev_vendor.replace(",", "") - except AttributeError: - dev_vendor = "unknown" - logger.debug("4. vendor approach: %s" % dev_vendor) - - dev_vendor = _functions.pad_or_trim(25, dev_vendor) - dev_driver = "" - try: - dev_driver = os.readlink("/sys/class/net/" + dev_interface + \ - "/device/driver") - dev_driver = os.path.basename(dev_driver) - except Exception as e: - logger.debug("Exception while determining NIC driver: %s" % ( - repr(e))) - nic_addr_file = open("/sys/class/net/" + dev_interface + \ - "/address") - dev_address = nic_addr_file.read().strip() - cmd = ("/files/etc/sysconfig/network-scripts/" + \ - "ifcfg-%s/BOOTPROTO") % str(dev_interface) - dev_bootproto = _functions.augtool_get(cmd) - type_cmd = ("/files/etc/sysconfig/network-scripts/" + \ - "ifcfg-%s/TYPE") % str(dev_interface) - bridge_cmd = ("/files/etc/sysconfig/network-scripts/" + \ - "ifcfg-%s/BRIDGE") % str(dev_interface) - dev_bridge = _functions.augtool_get(bridge_cmd) - - # check for vlans - logger.debug("checking for vlan") - vlans = glob("/etc/sysconfig/network-scripts/ifcfg-%s.*" % - dev_interface) - if (len(vlans) > 0): - dev_conf_status = "Configured " - vlanid = vlans[0].split(".")[-1] - logger.debug("found vlan %s" % vlanid) - - # if no bridge in nic, check clan-nic for bridge - if not dev_bridge: - vlancfg = "ifcfg-%s.%s" % (str(dev_interface), vlanid) - cmd = ("/files/etc/sysconfig/network-scripts/%s/" + - "BRIDGE") % vlancfg - dev_bridge = augtool_get(cmd) - logger.debug("Getting bridge '%s' from vlan: %s" % ( - dev_bridge, cmd)) - - if dev_bootproto is None: - logger.debug("Looking for bootproto in %s" % dev_bridge) - cmd = ("/files/etc/sysconfig/network-scripts/" + \ - "ifcfg-%s/BOOTPROTO") % str(dev_bridge) - dev_bootproto = _functions.augtool_get(cmd) - if dev_bootproto is None: - dev_bootproto = "Disabled" - dev_conf_status = "Unconfigured" - else: - dev_conf_status = "Configured " - else: - dev_conf_status = "Configured " - if dev_conf_status == "Configured ": - configured_nics = configured_nics + 1 - except Exception as e: - logger.warning("Error while determining NICs: %s" % repr(e)) - - nic_info = "%s,%s,%s,%s,%s,%s,%s" % ( \ - dev_interface, dev_bootproto, \ - dev_vendor, dev_address, \ - dev_driver, dev_conf_status, \ - dev_bridge) - logger.debug("NIC info: %s" % nic_info) - nic_dict[dev_interface] = nic_info - - if dev_bootproto == "dhcp": - ntp_dhcp = 1 - return nic_dict, configured_nics, ntp_dhcp - - -def get_system_vlans(): - """Retrieves a list of VLANs on this host - """ - vlandir = "/proc/net/vlan/" - vlans = [] - if os.path.exists(vlandir): - vlans = os.listdir(vlandir) - vlans.remove("config") - return vlans - -def get_current_nfsv4_domain(): - domain = None - with open("/etc/idmapd.conf") as nfs_config: - for line in nfs_config: - if "Domain =" in line: - domain = line.replace("Domain =", "").strip() - nfs_config.close() - return domain - -def set_nfsv4_domain(domain): - idmap_conf = "/etc/idmapd.conf" - current_domain = get_current_nfsv4_domain() - _functions.unmount_config(idmap_conf) - if current_domain.startswith("#"): - current_domain = "#Domain = %s" % current_domain.replace("# ","") - _functions.system("sed -i 's/%s/Domain = %s/g' %s" \ - % (current_domain, domain, idmap_conf)) - else: - _functions.system("sed -i '/^Domain/ s/%s/%s/g' %s" \ - % (current_domain, domain, idmap_conf)) - if _functions.ovirt_store_config(idmap_conf): - logger.info("NFSv4 domain set as: " + domain) - else: - logger.warning("Setting nfsv4 domain failed") - _functions.system_closefds("service rpcidmapd restart") - _functions.system_closefds("nfsidmap -c &>/dev/null") - def convert_to_biosdevname(): if not "BIOSDEVNAMES_CONVERSION" in OVIRT_VARS: # check for appropriate bios version @@ -650,12 +416,22 @@ def network_auto(): try: - network = Network() - network.configure_interface() - network.configure_dns() - network.configure_ntp() - network.save_ntp_configuration() - network.save_network_configuration() + from ovirt.node.config.defaults import Network, Nameservers, \ + Timeservers + from ovirt.node.utils.console import TransactionProgress + + txs = [] + + mnet = Network() + txs += mnet.transaction() + + mdns = Nameservers() + txs += mdns.transaction() + + mntp = Timeservers() + txs += mntp.transaction() + + TransactionProgress(txs, is_dry=False) except: logger.warn("Network Configuration Failed....") return False -- To view, visit http://gerrit.ovirt.org/15514 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I30378deb42c270d615353110fcf86ec79d18a345 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-node Gerrit-Branch: master Gerrit-Owner: Fabian Deutsch <[email protected]> _______________________________________________ node-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/node-patches
