def port(self, port=None): augpath = "/files/etc/ssh/sshd_config/Port" aug = utils.AugeasWrapper() if port is not None and not isinstance(port, int): try: int(port) except ValueError: raise RuntimeError("Port must be an integer") if port is not None: if int(port) in range(1024, 65536) or int(port) == 22: self.logger.debug("Setting SSH port to %s" % port) aug.set(augpath, port) self.restart() else: raise RuntimeError("Port must be in the range [1024-65536] \ or 22") state = str(aug.get(augpath)).lower() if state != "none": if int(state) in range(1024, 65536) or int(state) == 22: self.logger.debug("SSH port %s" % state) else: raise RuntimeError("Failed to set SSH port: value is %s" % state) return state
def transaction(self): """Derives the nameserver config from OVIRT_DNS 1. Parse nameservers from defaults 2. Update resolv.conf 3. Update ifcfg- (peerdns=no if manual resolv.conf) 4. Persist resolv.conf Args: servers: List of servers (str) """ aug = utils.AugeasWrapper() m = Nameservers().retrieve() tx = utils.Transaction("Configuring DNS") if not m["servers"]: self.logger.debug("No DNS server entry in default config") return tx servers = m["servers"] if servers is None or servers == "": self.logger.debug("No DNS servers configured " + "in default config") class UpdateResolvConf(utils.Transaction.Element): title = "Updating resolv.conf" def commit(self): # Write resolv.conf any way, sometimes without servers comment = ("Please make changes through the TUI. " + "Manual edits to this file will be " + "lost on reboot") aug.set("/files/etc/resolv.conf/#comment[1]", comment) # Now set the nameservers config.network.nameservers(servers) utils.fs.Config().persist("/etc/resolv.conf") utils.network.reset_resolver() class UpdatePeerDNS(utils.Transaction.Element): title = "Update PEERDNS statement in ifcfg-* files" def commit(self): # Set or remove PEERDNS for all ifcfg-* for nic in glob.glob("/etc/sysconfig/network-scripts/ifcfg-*"): if "ifcfg-lo" in nic: continue path = "/files%s/PEERDNS" % nic if len(servers) > 0: aug.set(path, "no") else: aug.remove(path) # FIXME what about restarting NICs to pickup peerdns? tx += [UpdateResolvConf(), UpdatePeerDNS()] return tx
def load(self): aug = utils.AugeasWrapper() augg = lambda k: aug.get("\n%s/%s\n" % (self._version_filename, k), strip_quotes=True) # read product / version info self.PRODUCT_SHORT = augg("PRODUCT_SHORT") or "oVirt" self.VERSION = augg("VERSION") self.RELEASE = augg("RELEASE")
def password_authentication(self, enable=None): augpath = "/files/etc/ssh/sshd_config/PasswordAuthentication" aug = utils.AugeasWrapper() if enable in [True, False]: import ovirtnode.ovirtfunctions as ofunc value = "yes" if enable else "no" self.logger.debug("Setting SSH PasswordAuthentication to " + "%s" % value) aug.set(augpath, value) ofunc.ovirt_store_config("/etc/ssh/sshd_config") self.restart() return aug.get(augpath)
def __legacy_hostname(new_hostname=None): """The legacy way of setting a hostname. """ aug = utils.AugeasWrapper() augpath = "/files/etc/sysconfig/network/HOSTNAME" sys_hostname = None if new_hostname: aug.set(augpath, new_hostname) sys_hostname = utils.network.hostname(new_hostname) cfg_hostname = aug.get(augpath) if sys_hostname and (sys_hostname != cfg_hostname): # A trivial check: Check that the configured hostname equals the # configured one (only check if we are configuring a new hostname) raise RuntimeError(("A new hostname was configured (%s) but the " + "systems hostname (%s) wasn't set accordingly.") % (cfg_hostname, sys_hostname)) return cfg_hostname
def password_authentication(self, enable=None): """Get or set the ssh password authentication Args: enable: (optional) If given the auth is set Returns: True if password authentication is enabled, False otherwise """ augpath = "/files/etc/ssh/sshd_config/PasswordAuthentication" aug = utils.AugeasWrapper() if enable in [True, False]: value = "yes" if enable else "no" self.logger.debug("Setting SSH PasswordAuthentication to " + "%s" % value) aug.set(augpath, value) self.restart() state = str(aug.get(augpath)).lower() if state not in ["yes", "no", "none"]: raise RuntimeError("Failed to set SSH password authentication" + "(%s)" % state) return state == "yes"
# # Copyright (C) 2015 # # Douglas Schilling Landgraf <*****@*****.**> # # This software is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 # USA from ovirt.node import utils # Example of setting new value for driftfile in /etc/ntp.conf aug = utils.AugeasWrapper() ntp_conf_drift = "/files/etc/ntp.conf/driftfile" if aug.match(ntp_conf_drift): print("Current value of drift %s" % aug.get(ntp_conf_drift)) aug.set(ntp_conf_drift, "/lib/ntp/drift") print("New value of driftfile %s" % aug.get(ntp_conf_drift))
class Syslog(base.Base): aug = utils.AugeasWrapper() def __get_index(self): index = None m = self.aug.match("/files/etc/rsyslog.conf/*/action/hostname") group = m[0] if m else None pat = re.compile(r'.*?entry\[(\d+)\].*') if group: index = int(pat.sub(r'\1', group)) elif self.aug.get("/augeas/files/etc/rsyslog.conf/error"): self.logger.error("Augeas could not parse rsyslog.conf. " "Please check " "/augeas/files/etc/rsyslog.conf/error " "with augtool") raise RuntimeError("Augeas could not parse rsyslog.conf") else: group = \ self.aug.match('/files/etc/rsyslog.conf/entry[last()]/*')[0] index = int(pat.sub(r'\1', group)) + 1 return index def clear_config(self): self.logger.info("Clearing rsyslog config") sel = self.aug.match("/files/etc/rsyslog.conf/entry[%d]" % self.__get_index()) self.aug.remove_many(sel) def configure(self, server, port): # I know this doesn't make any sense, but augeas is incredibly # finicky about the lenses, and if these aren't inserted in exactly # the right order, it will fail config = [{ "/selector/facility": "*" }, { "/selector/level": "*" }, { "/action/hostname": server }, { "/action/port": port }] path = "/files/etc/rsyslog.conf/entry[%d]" % self.__get_index() for i in config: for k, v in i.items(): k = path + k self.aug.set(k, v, do_save=False) try: self.aug.save() except: self.logger.error("Augeas failed to save values, check " "lenses versus values") self.logger.error( self.aug.get_many(self.aug.match("%s/*/*" % path))) self.logger.error( self.aug.get_many( self.aug.match("/augeas/files/etc/rsyslog.conf/error/*"))) self.logger.error( self.aug.get_many( self.aug.match( "/augeas/files/etc/rsyslog.conf/error/*/*"))) raise RuntimeError("Augeas failed to save rsyslog.conf")