def load_properties(self): Augeas.force_reload() for p in [ "bridge", "type", "bootproto", "ipaddr", "netmask", "gateway", "vlan", "device", "onboot", "hwaddr" ]: self.__dict__[p] = self.ifcfg_property(p.upper())
def commit(self): 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"): aug.remove(alias_entry, False) # ... and create a new one aliases = ["localhost", "localhost.localdomain"] if self.hostname: 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) fs.Config().persist("/etc/hosts") fs.Config().persist("/etc/hostname") fs.Config().persist("/etc/sysconfig/network") utils.network.reset_resolver()
def ui(self, *args, **kwargs): """ Sets a value in /etc/default/ovirt for the TUI to read required parameters: key, value """ if "key" not in kwargs or "value" not in kwargs: raise RuntimeError("A key and value are required to prompt " "for UI interaction") aug = AugeasWrapper() aug.set("/files/etc/default/ovirt/%s" % kwargs["key"], kwargs["value"])
def commit(self): from ovirt.node.utils import AugeasWrapper aug = AugeasWrapper() if OVIRT_VARS["OVIRT_SSH_PWAUTH"] == "yes": aug.set("/files/etc/ssh/sshd_config/PasswordAuthentication", "yes") elif OVIRT_VARS["OVIRT_SSH_PWAUTH"] == "no": aug.set("/files/etc/ssh/sshd_config/PasswordAuthentication", "no") Config().persist("/etc/ssh/sshd_config") process.call("service sshd restart &> /dev/null", shell=True)
def iface(iface): """Retuns the config of an iface Args: iface: Interface to retrieve the config for Returns: A dict of (nic-name, nic-infos-dict) """ LOGGER.debug("Getting configuration for '%s'" % iface) Augeas.force_reload() info = {} aug = Augeas() filepath = "/etc/sysconfig/network-scripts/ifcfg-%s" % iface augdevicepath = "/files%s" % filepath if not os.path.exists(filepath): LOGGER.debug("No config file %s" % filepath) # Type info["type"] = aug.get(augdevicepath + "/TYPE", True) # Bootprotocol info["bootproto"] = aug.get(augdevicepath + "/BOOTPROTO", True) # IPV4 for p in ["IPADDR", "NETMASK", "GATEWAY"]: info[p.lower()] = aug.get(augdevicepath + "/" + p, True) # FIXME IPv6 # Parent bridge info["bridge"] = aug.get(augdevicepath + "/BRIDGE", True) # VLAN info["is_vlan"] = aug.get(augdevicepath + "/VLAN", True) is not None name_says_vlan = "." in iface if info["is_vlan"] != name_says_vlan: LOGGER.warning("NIC config says the device is a VLAN, but the name" + "doesn't reflect that: %s (%s vs %s)" % (iface, info["is_vlan"], name_says_vlan)) if info["is_vlan"] is True: parts = iface.split(".") vlanid = parts[-1:][0] info["vlanid"] = vlanid info["vlan_parent"] = ".".join(parts[:-1]) info["type"] = "vlan" LOGGER.debug("Found VLAN %s on %s" % (str(vlanid), iface)) else: info["vlanid"] = None return info
def commit(self): m = Network().retrieve() aug = AugeasWrapper() bond = NicBonding().retrieve() if bond["slaves"]: NicBonding().transaction().commit() has_network = m["iface"] is not None if has_network: topology = NetworkLayout().retrieve()["layout"] if topology == "bridged": self.__write_bridged_config() else: self.__write_direct_config() else: topology = NetworkLayout().configure_direct() aug.set("/files/etc/sysconfig/network/NETWORKING", "yes" if has_network else "no") fs.Config().persist("/etc/sysconfig/network") fs.Config().persist("/etc/hosts")
def _aug_get_or_set(augpath, new_servers=None): """Get or set some servers """ aug = Augeas() servers = [] for path in aug.match(augpath): servers.append(aug.get(path)) if new_servers: itempath = lambda idx: "%s[%d]" % (augpath, idx + 1) for idx, server in enumerate(new_servers): LOGGER.debug("Setting server: %s" % server) aug.set(itempath(idx), server) if len(servers) > len(new_servers): LOGGER.debug("Less servers than before, removing old ones") for idx in range(len(servers) + 1, len(new_servers)): aug.remove(itempath(idx)) return servers
class Ifcfg(base.Base): """Object to access ifcfg-%ifname """ bridge = None type = None bootproto = None ipaddr = None netmask = None gateway = None vlan = None device = None hwaddr = None onboot = None def __init__(self, iface): self.iface = iface self.aug = Augeas() self.load_properties() def load_properties(self): Augeas.force_reload() for p in [ "bridge", "type", "bootproto", "ipaddr", "netmask", "gateway", "vlan", "device", "onboot", "hwaddr" ]: self.__dict__[p] = self.ifcfg_property(p.upper()) def ifcfg_property(self, name): filepath = "/etc/sysconfig/network-scripts/ifcfg-%s" % self.iface augdevicepath = "/files%s" % filepath value = None if os.path.exists(filepath): value = self.aug.get("%s/%s" % (augdevicepath, name), True) else: LOGGER.debug("No config file %s" % filepath) return value
class Ifcfg(base.Base): """Object to access ifcfg-%ifname """ bridge = None type = None bootproto = None ipaddr = None netmask = None gateway = None vlan = None device = None hwaddr = None onboot = None def __init__(self, iface): self.iface = iface self.aug = Augeas() self.load_properties() def load_properties(self): Augeas.force_reload() for p in ["bridge", "type", "bootproto", "ipaddr", "netmask", "gateway", "vlan", "device", "onboot", "hwaddr"]: self.__dict__[p] = self.ifcfg_property(p.upper()) def ifcfg_property(self, name): filepath = "/etc/sysconfig/network-scripts/ifcfg-%s" % self.iface augdevicepath = "/files%s" % filepath value = None if os.path.exists(filepath): value = self.aug.get("%s/%s" % (augdevicepath, name), True) else: LOGGER.debug("No config file %s" % filepath) return value
def __init__(self): from ovirt.node.utils import AugeasWrapper self.aug = AugeasWrapper() super(ImportConfigs, self).__init__()
class ImportConfigs(base.Base): """Import the real configs into Node's abstract config """ def __init__(self): from ovirt.node.utils import AugeasWrapper self.aug = AugeasWrapper() super(ImportConfigs, self).__init__() def translate_all(self, do_network=True): if do_network: self.migrate_network_layout() funcs = [getattr(self, func) for func in dir(self) if func.startswith("translate_") and not func.endswith("all")] for func in funcs: try: self.logger.debug("Calling %s" % func) func() except Exception as e: self.logger.info("Failed to translate %s: %s" % (func, e), exc_info=True) def translate_rsyslog(self): if self.__is_persisted("/etc/rsyslog.conf"): from ovirtnode import log rsyslog_server, rsyslog_port = log.get_rsyslog_config() if \ log.get_rsyslog_config() is not None else (None, 514) if rsyslog_server: self.aug.set("/files/etc/default/ovirt/OVIRT_SYSLOG_SERVER", rsyslog_server or "") self.aug.set("/files/etc/default/ovirt/OVIRT_SYSLOG_PORT", rsyslog_port or "") def translate_netconsole(self): if self.__is_persisted("/etc/sysconfig/netconsole"): netconsole_server = aug_unwrapped.get( "/files/etc/sysconfig/netconsole/SYSLOGADDR") netconsole_port = aug_unwrapped.get( "/files/etc/sysconfig/netconsole/SYSLOGPORT") if netconsole_server: self.aug.set( "/files/etc/default/ovirt/OVIRT_NETCONSOLE_SERVER", netconsole_server or "") self.aug.set("/files/etc/default/ovirt/OVIRT_NETCONSOLE_PORT", str(netconsole_port) or "") def translate_logrotate(self): if self.__is_persisted("/etc/logrotate.d/ovirt-node"): from ovirtnode import ovirtfunctions logrotate_size = ovirtfunctions.get_logrotate_size() if logrotate_size and logrotate_size is not 1024: self.aug.set( "/files/etc/default/ovirt/OVIRT_LOGROTATE_MAX_SIZE", str(logrotate_size) or "") def translate_ssh(self): from ovirt.node.utils import parse_bool from ovirt.node.utils.security import Ssh if self.__is_persisted("/etc/ssh/sshd_config"): pw_auth_enabled = aug_unwrapped.get( "/files/etc/ssh/sshd_config/PasswordAuthentication") rng_bytes, aes_disabled = Ssh().rng_status() rng_bytes = None if rng_bytes == 0 else rng_bytes aes_disabled = aes_disabled == 1 ssh_is_enabled = parse_bool(pw_auth_enabled) if rng_bytes: self.aug.set("/files/etc/default/ovirt/OVIRT_USE_STRONG_RNG", str(rng_bytes)) if aes_disabled: self.aug.set("/files/etc/default/ovirt/OVIRT_DISABLE_AES_NI", "true") if ssh_is_enabled: self.aug.set("/files/etc/default/ovirt/OVIRT_SSH_PWAUTH", "yes") def translate_network_servers(self): # For all of these, make sure it's not actually set already by # install parameters. If it isn't, we won't overwrite anything # by checking the actual values from the configuration files, which # will properly be set if self.aug.get('/files/etc/default/ovirt/OVIRT_DNS') is None and \ self.__is_persisted("/etc/resolv.conf"): dns = [aug_unwrapped.get("/files/etc/resolv.conf/nameserver[1]"), aug_unwrapped.get("/files/etc/resolv.conf/nameserver[2]")] self.aug.set("/files/etc/default/ovirt/OVIRT_DNS", ",".join((filter(None, dns)))) if self.aug.get('/files/etc/default/ovirt/OVIRT_NTP') is None and \ self.__is_persisted("/etc/ntp.conf"): ntp = [aug_unwrapped.get("/files/etc/ntp.conf/server[1]"), aug_unwrapped.get("/files/etc/ntp.conf/server[2]")] self.aug.set("/files/etc/default/ovirt/OVIRT_NTP", ",".join((filter(None, ntp)))) if self.aug.get('/files/etc/default/ovirt/OVIRT_HOSTNAME') is None \ and self.__is_persisted("/etc/hosts"): self.aug.set("/files/etc/default/ovirt/OVIRT_HOSTNAME", os.uname()[1]) def translate_kdump(self): if self.__is_persisted("/etc/kdump.conf"): kdump = self._get_kdump_config() if "nfs" in kdump: self.aug.set("/files/etc/default/ovirt/OVIRT_KDUMP_NFS", kdump["nfs"]) elif "ssh" in kdump: self.aug.set("/files/etc/default/ovirt/OVIRT_KDUMP_SSH", kdump["ssh"]) else: self.aug.set("/files/etc/default/ovirt/OVIRT_KDUMP_LOCAL", "true") def translate_snmp(self): if self.__is_persisted("/etc/snmp/snmpd.conf"): self.aug.set("/files/etc/default/ovirt/OVIRT_SNMP_ENABLED", "1") def translate_iscsi(self): if self.__is_persisted("/etc/iscsi/initiatorname.iscsi"): from ovirtnode import iscsi iscsi_initiator = iscsi.get_current_iscsi_initiator_name() if iscsi_initiator: self.aug.set("/files/etc/default/ovirt/OVIRT_ISCSI_NODE_NAME", iscsi_initiator or "") def translate_nfs(self): if self.__is_persisted("/etc/idmapd.conf"): nfsv4_domain = self._get_current_nfsv4_domain() if nfsv4_domain: self.aug.set("/files/etc/default/ovirt/OVIRT_NFSV4_DOMAIN", nfsv4_domain or "") def translate_rhn(self): try: self._translate_rhn() except: self.logger.debug("RHN plugin not available") def _translate_rhn(self): from ovirt.node.setup.rhn import rhn_page as rhn if self.__is_persisted("/etc/sysconfig/rhn/up2date") or \ self.__is_persisted("/etc/rhsm/rhsm.conf"): rhn_type = None rhn_url = None rhn_ca = None rhn_username = None rhn_profile = None rhn_activationkey = None rhn_org = None rhn_proxyurl = None rhn_proxyuser = None rhn_environment = None rhn_conf = rhn.get_rhn_config() status, rhn_type = rhn.get_rhn_status() RHN_XMLRPC_ADDR = "https://xmlrpc.rhn.redhat.com/XMLRPC" SAM_REG_ADDR = "subscription.rhn.redhat.com" CANDLEPIN_CERT_FILE = "/etc/rhsm/ca/candlepin-local.pem" if RHN_XMLRPC_ADDR not in rhn_conf["serverURL"] and not \ rhn.sam_check(): rhn_url = rhn_conf["serverURL"] rhn_ca = rhn_conf["sslCACert"] elif rhn.sam_check(): if SAM_REG_ADDR not in rhn_conf["hostname"]: rhn_url = "https://%s" % rhn_conf["hostname"] if os.path.exists(CANDLEPIN_CERT_FILE): rhn_ca = CANDLEPIN_CERT_FILE if "proxyUser" in rhn_conf and "proxyPassword" in rhn_conf: if len(rhn_conf["proxyUser"]) > 0: rhn_proxyuser = rhn_conf["proxyUser"] elif "proxy_user" in rhn_conf and "proxy_password" in rhn_conf: rhn_proxyuser = rhn_conf["proxy_user"] if rhn_conf["httpProxy"] is not None: try: proxy_hostname, proxy_port = rhn_conf[ "httpProxy"].split(':') rhn_proxyurl = "%s:%s" % (proxy_hostname, proxy_port) except ValueError: self.logger.debug("Bad proxy entry in old install %s" % rhn_conf["httpProxy"]) if rhn_conf["proxy_hostname"] is not None and rhn_conf[ "proxy_port"] is not None: rhn_proxyurl = "%s:%s" % (rhn_conf["proxy_hostname"], rhn_conf["proxy_port"]) self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_TYPE", rhn_type.lower() if rhn_type else "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_URL", rhn_url or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_CA_CERT", rhn_ca or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_USERNAME", rhn_username or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_PROFILE", rhn_profile or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_ACTIVATIONKEY", rhn_activationkey or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_ORG", rhn_org or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_ENVIRONMENT", rhn_environment or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_PROXY", rhn_proxyurl or "") self.aug.set("/files/etc/default/ovirt/OVIRT_RHN_PROXYUSER", rhn_proxyuser or "") def _get_kdump_config(self): kdump_type = {} try: kdump_config_file = open("/etc/kdump.conf") for line in kdump_config_file: if not line.startswith("#"): if line.startswith("net"): line = line.replace("net ", "") if "@" in line: kdump_type = {"ssh": line.strip()} elif ":" in line: kdump_type = {"nfs": line.strip()} elif "/dev/HostVG/Data" in line: kdump_type = {"local": None} kdump_config_file.close() except: pass return kdump_type def _get_current_nfsv4_domain(self): domain = None with open("/etc/idmapd.conf") as nfs_config: for line in nfs_config: if "Domain =" in line: domain = line.replace("Domain =", "").strip() break return domain def __is_persisted(self, path): return Config().exists(path) def migrate_network_layout(self): from ovirt.node.config import defaults from ovirt.node.utils import network bondcfg = defaults.NicBonding().retrieve() netlayoutcfg = defaults.NetworkLayout().retrieve() if bondcfg["name"] or netlayoutcfg["layout"]: # We can only reliably import pre node-3.0 # network configurations, therefor we abort # the import if it looks like a node-3.0 config self.logger.info("Looks like node-3.0 network, skipping import") return bridges = [x for x in network.Bridges().ifnames() if x.startswith("br")] bridged_nics = [x for x in network.all_ifaces() if network.NIC(x).config.bridge in bridges] self.logger.debug("Found bridges: %s" % bridges) self.logger.debug("Found bridged NICs: %s" % bridged_nics) def cfgset(k, v, prefix="OVIRT_"): if v: self.logger.debug(" Setting %s = %s" % (k, v)) self.aug.set("/files/etc/default/ovirt/%s%s" % (prefix, k), str(v)) found_mgmt = False for brn in ["rhevm", "ovirtmgmt"]: if brn in network.Bridges().ifnames(): self.logger.debug("Found managed nic: %s" % brn) cfgset("MANAGED_BY", "RHEV-M", "") cfgset("MANAGED_IFNAMES", brn, "") found_mgmt = True break self.logger.debug("Found management: %s" % found_mgmt) if not found_mgmt and bridges and bridged_nics: self.logger.debug("Assuming default bridged network") self.aug.set("/files/etc/default/ovirt/OVIRT_NETWORK_LAYOUT", "bridged") ifname = bridged_nics[0] br = bridges[0] vlanid = None self.logger.debug("Bridge and NIC: %s %s" % (br, ifname)) probably_vlan = "." in ifname if probably_vlan: ifname, vlanid = ifname.split(".", 1) self.logger.debug("Found VLAN setup, base NIC: %s %s" % (ifname, vlanid)) self.aug.set("/files/etc/default/ovirt/OVIRT_BOOTIF", ifname) def ifcfg(i, k): v = self.aug.get("/files/etc/sysconfig/network-" + "scripts/ifcfg-%s/%s" % (i, k)) self.logger.debug(" Getting %s.%s = %s" % (i, k, v)) return v proto = ifcfg(br, "BOOTPROTO") cfgset("BOOTPROTO", proto) addr = ifcfg(br, "IPADDR") if addr: cfgset("IP_ADDRESS", addr) cfgset("IP_GATEWAY", ifcfg(br, "GATEWAY")) cfgset("IP_NETMASK", ifcfg(br, "NETMASK")) if vlanid: cfgset("VLAN", vlanid)
def __init__(self): self.aug = AugeasWrapper() super(MigrateConfigs, self).__init__()
def commit(self): aug = AugeasWrapper() p = "/files/etc/ntp.conf" aug.remove(p, False) aug.set(p + "/driftfile", "/var/lib/ntp/drift", False) aug.set(p + "/includefile", "/etc/ntp/crypto/pw", False) aug.set(p + "/keys", "/etc/ntp/keys", False) aug.save() config.network.timeservers(servers) utils.fs.Config().persist("/etc/ntp.conf")
def _aug_get_or_set(augpath, new_servers=None): """Get or set some servers """ aug = Augeas() aug.save() aug.force_reload() servers = [] for path in aug.match(augpath): servers.append(aug.get(path)) LOGGER.debug("Current servers: %s" % servers) if new_servers is not None: itempath = lambda idx: "%s[%d]" % (augpath, idx + 1) LOGGER.debug("Removing old servers: %s" % servers) for idx, server in enumerate(servers): LOGGER.debug("Removing server %s: %s" % (itempath(idx), server)) aug.remove(itempath(idx)) LOGGER.debug("Setting new servers: %s" % new_servers) for idx, server in enumerate(new_servers): LOGGER.debug("Setting server %s: %s" % (itempath(idx), server)) aug.set(itempath(idx), server) return servers
def __init__(self): self.aug = AugeasWrapper() super(ImportConfigs, self).__init__()
def load_properties(self): Augeas.force_reload() for p in ["bridge", "type", "bootproto", "ipaddr", "netmask", "gateway", "vlan", "device", "onboot", "hwaddr"]: self.__dict__[p] = self.ifcfg_property(p.upper())
def __init__(self, iface): self.iface = iface self.aug = Augeas() self.load_properties()