diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Client/Debian.py | 4 | ||||
-rw-r--r-- | src/lib/Client/Gentoo.py | 57 | ||||
-rw-r--r-- | src/lib/Client/Redhat.py | 32 | ||||
-rw-r--r-- | src/lib/Client/Toolset.py | 27 |
4 files changed, 61 insertions, 59 deletions
diff --git a/src/lib/Client/Debian.py b/src/lib/Client/Debian.py index d02252180..fc5e3b2bc 100644 --- a/src/lib/Client/Debian.py +++ b/src/lib/Client/Debian.py @@ -71,13 +71,13 @@ class ToolsetImpl(Bcfg2.Client.Toolset.Toolset): if entry.attrib['status'] == 'off': if self.setup['dryrun']: - print "Disabling service %s" % (entry.get('name')) + self.logger.info("Disabling service %s" % (entry.get('name'))) else: self.saferun("/etc/init.d/%s stop" % (entry.get('name'))) cmdrc = self.saferun("/usr/sbin/update-rc.d -f %s remove" % entry.get('name'))[0] else: if self.setup['dryrun']: - print "Enabling service %s" % (entry.attrib['name']) + self.logger.info("Enabling service %s" % (entry.attrib['name'])) else: cmdrc = self.saferun("/usr/sbin/update-rc.d %s defaults" % (entry.attrib['name']))[0] if cmdrc: diff --git a/src/lib/Client/Gentoo.py b/src/lib/Client/Gentoo.py index a42d4b1b6..99390f19e 100644 --- a/src/lib/Client/Gentoo.py +++ b/src/lib/Client/Gentoo.py @@ -1,11 +1,7 @@ '''This provides bcfg2 support for Gentoo''' __revision__ = '$Revision$' -from os import popen, system, stat -from popen2 import Popen4 -from glob import glob -from re import match, compile as regcompile - +import glob, os, re from Bcfg2.Client.Toolset import Toolset class Gentoo(Toolset): @@ -19,35 +15,33 @@ class Gentoo(Toolset): self.installed = {} self.extra_services = [] self.Refresh() - system("emerge sync") + self.saferun("emerge sync") def Refresh(self): '''Refresh memory hashes of packages''' self.installed = {} - splitter = regcompile('([\w\-\+]+)-([\d].*)') + splitter = re.compile('([\w\-\+]+)-([\d].*)') # Build list of packages instp = [splitter.match(fname.split('/')[-1].replace('.ebuild','')).groups() - for fname in glob('/var/db/pkg/*/*/*.ebuild')] + for fname in glob.glob('/var/db/pkg/*/*/*.ebuild')] for info in instp: self.installed["%s-%s" % info] = info[1] def VerifyService(self, entry): '''Verify Service status for entry''' - runlevels = popen("/bin/rc-status --list").readlines() + runlevels = self.saferun("/bin/rc-status --list")[1] try: - crunlevels = popen("/sbin/rc-update show | grep %s" % entry.attrib['name']).readlines()[0].split()[2:] + crl = self.saferun("/sbin/rc-update show | grep %s" % entry.attrib['name'])[1][0].split()[2:] except IndexError: # Ocurrs when no lines are returned (service not installed) return False if entry.get('status') == 'off': - if len(crunlevels) == 0: - return True + return len(crl) == 0 elif entry.get('status') == 'on': - if len(crunlevels) > 0: - return True + return len(crl) > 0 return False def InstallService(self, entry): @@ -55,21 +49,21 @@ class Gentoo(Toolset): self.logger.info("Installing Service %s" % (entry.get('name'))) try: - stat('/etc/init.d/%s' % entry.get('name')) + os.stat('/etc/init.d/%s' % entry.get('name')) except OSError: self.logger.debug("Init script for service %s does not exist" % entry.get('name')) return False if entry.attrib['status'] == 'off': if self.setup['dryrun']: - print "Disabling Service %s" % (entry.get('name')) + self.logger.info("Disabling Service %s" % (entry.get('name'))) else: - cmdrc = system("/sbin/rc-update del %s %s" % (entry.attrib['name'], entry.attrib['runlevels'])) + cmdrc = self.saferun("/sbin/rc-update del %s %s" % (entry.attrib['name'], entry.attrib['runlevels']))[0] else: if self.setup['dryrun']: - print "Enabling Service %s" % (entry.attrib['name']) + self.logger.info("Enabling Service %s" % (entry.attrib['name'])) else: - cmdrc = system("/sbin/rc-update add %s %s" % (entry.attrib['name'], entry.attrib['runlevels'])) + cmdrc = self.saferun("/sbin/rc-update add %s %s" % (entry.attrib['name'], entry.attrib['runlevels']))[0] if cmdrc: return False return True @@ -77,11 +71,11 @@ class Gentoo(Toolset): def VerifyPackage(self, entry, modlist): '''Verify Package status for entry''' if not (entry.get('name') and entry.get('version')): - print "Can't verify package, not enough data." + self.logger.error("Can't verify package, not enough data.") return False - installed_package = popen("/usr/bin/qpkg --no-color --installed --verbose %s-%s" % - (entry.get('name'), entry.get('version'))).readlines() + installed_package = self.saferun("/usr/bin/qpkg --no-color --installed --verbose %s-%s" % + (entry.get('name'), entry.get('version')))[1] if installed_package: installed_package = installed_package[0].strip("\n").split('/')[-1] if installed_package != "%s-%s" % (entry.get('name'), entry.get('version')): @@ -89,17 +83,11 @@ class Gentoo(Toolset): if entry.attrib.get('verify', 'true') == 'true': if self.setup['quick']: return True - verp = Popen4("/usr/bin/qpkg --no-color --check %s-%s" % - (entry.get('name'), entry.get('version')), bufsize=16384) - odata = verp.fromchild.read() - vstat = verp.poll() - while vstat == -1: - odata += verp.fromchild.read() - vstat = verp.poll() - output = [line for line in odata.split("\n") if line] + output = self.saferun("/usr/bin/qpkg --no-color --check %s-%s" % + (entry.get('name'), entry.get('version')))[1] differences = output[-1] - if match("^0/", differences): + if re.match("^0/", differences): return True else: for line in output[1:-1]: @@ -112,7 +100,7 @@ class Gentoo(Toolset): def Inventory(self): '''Do standard inventory plus debian extra service check''' Toolset.Inventory(self) - allsrv = [ srv.split('/')[-1] for srv in glob('/etc/init.d/*')] + allsrv = [ srv.split('/')[-1] for srv in glob.glob('/etc/init.d/*')] csrv = self.cfg.findall(".//Service") [allsrv.remove(svc.get('name')) for svc in csrv if svc.get('status') == 'on' and svc.get('name') in allsrv] self.extra_services = allsrv @@ -122,13 +110,14 @@ class Gentoo(Toolset): if len(self.pkgwork) > 0: if self.setup['remove'] in ['all', 'packages']: self.logger.info("Removing packages: %s" % (self.pkgwork['remove'])) - system("/usr/bin/emerge --quiet --nospinner unmerge %s" % " ".join(self.pkgwork['remove'])) + cmd = "/usr/bin/emerge --quiet --nospinner unmerge %s" % " ".join(self.pkgwork['remove']) + self.saferun(cmd) else: self.logger.info("Need to remove packages: %s" % (self.pkgwork['remove'])) if len(self.extra_services) > 0: if self.setup['remove'] in ['all', 'services']: self.logger.info("Removing services: %s" % (self.extra_services)) for service in self.extra_services: - system("/sbin/rc-update del %s" % service) + self.saferun("/sbin/rc-update del %s" % service) else: self.logger.info("Need to remove services: %s" % (self.extra_services)) diff --git a/src/lib/Client/Redhat.py b/src/lib/Client/Redhat.py index ee08be18e..3d5e3c73c 100644 --- a/src/lib/Client/Redhat.py +++ b/src/lib/Client/Redhat.py @@ -4,8 +4,6 @@ '''This is redhat client support''' __revision__ = '$Revision$' -from os import popen, system - from Bcfg2.Client.Toolset import Toolset class ToolsetImpl(Toolset): @@ -33,17 +31,14 @@ class ToolsetImpl(Toolset): def Refresh(self): '''Refresh memory hashes of packages''' self.installed = {} - - # Build list of packages - instp = popen("rpm -qa --qf '%{NAME} %{VERSION}-%{RELEASE}\n'") - for line in instp: + for line in self.saferun("rpm -qa --qf '%{NAME} %{VERSION}-%{RELEASE}\n'")[1]: [name, version] = line.split(' ') self.installed[name] = version[:-1] def VerifyService(self, entry): '''Verify Service status for entry''' try: - srvdata = popen("/sbin/chkconfig --list %s"%entry.attrib['name']).readlines()[0].split() + srvdata = self.saferun("/sbin/chkconfig --list %s"%entry.attrib['name'])[1][0].split() except IndexError: # Ocurrs when no lines are returned (service not installed) return False @@ -60,28 +55,29 @@ class ToolsetImpl(Toolset): def InstallService(self, entry): '''Install Service entry''' - system("/sbin/chkconfig --add %s"%(entry.attrib['name'])) + self.saferun("/sbin/chkconfig --add %s"%(entry.attrib['name'])) self.logger.info("Installing Service %s" % (entry.get('name'))) if not entry.get('status'): - print "Can't install service %s, not enough data" % (entry.get('name')) + self.logger.error("Can't install service %s, not enough data" % (entry.get('name'))) return False if entry.attrib['status'] == 'off': if self.setup['dryrun']: - print "Disabling server %s" % (entry.get('name')) + self.logger.info("Disabling server %s" % (entry.get('name'))) else: - cmdrc = system("/sbin/chkconfig %s %s" % (entry.attrib['name'], entry.attrib['status'])) + cmdrc = self.saferun("/sbin/chkconfig %s %s" % (entry.attrib['name'], + entry.attrib['status']))[0] else: if self.setup['dryrun']: - print "Enabling server %s" % (entry.get('name')) + self.logger.info("Enabling server %s" % (entry.get('name'))) else: - cmdrc = system("/sbin/chkconfig %s %s" % - (entry.attrib['name'], entry.attrib['status'])) + cmdrc = self.saferun("/sbin/chkconfig %s %s" % + (entry.attrib['name'], entry.attrib['status']))[0] return cmdrc == 0 def VerifyPackage(self, entry, modlist): '''Verify Package status for entry''' if not entry.get('version'): - print "Can't install package %s, not enough data." % (entry.get('name')) + self.logger.error("Can't install package %s, not enough data." % (entry.get('name'))) return False if self.installed.has_key(entry.get('name')): if entry.get('version') == self.installed[entry.get('name')]: @@ -107,7 +103,7 @@ class ToolsetImpl(Toolset): if len(self.pkgwork) > 0: if self.setup['remove'] in ['all', 'packages']: self.logger.info("Removing packages: %s" % self.pkgwork['remove']) - if not system("rpm --quiet -e %s" % " ".join(self.pkgwork['remove'])): + if not self.saferun("rpm --quiet -e %s" % " ".join(self.pkgwork['remove']))[0]: self.pkgwork['remove'] = [] self.Refresh() self.Inventory() @@ -119,7 +115,7 @@ class ToolsetImpl(Toolset): self.logger.info('Removing services:') self.logger.info(self.extra_services) for service in self.extra_services: - if not system("/sbin/chkconfig --level 123456 %s off" % service): + if not self.saferun("/sbin/chkconfig --level 123456 %s off" % service)[0]: self.extra_services.remove(service) self.logger.info("Failed to remove service %s" % (service)) else: @@ -129,7 +125,7 @@ class ToolsetImpl(Toolset): def Inventory(self): '''Do standard inventory plus debian extra service check''' Toolset.Inventory(self) - allsrv = [line.split()[0] for line in popen("/sbin/chkconfig --list|grep :on").readlines()] + allsrv = [line.split()[0] for line in self.saferun("/sbin/chkconfig --list|grep :on")[1]] self.logger.debug('Found active services:') self.logger.debug(allsrv) csrv = self.cfg.findall(".//Service") diff --git a/src/lib/Client/Toolset.py b/src/lib/Client/Toolset.py index aedccce35..744074cd2 100644 --- a/src/lib/Client/Toolset.py +++ b/src/lib/Client/Toolset.py @@ -21,6 +21,24 @@ def calcPerms(initial, perms): tempperms |= perm return tempperms +class readonlypipe(popen2.Popen4): + '''This pipe sets up stdin --> /dev/null''' + def __init__(self, cmd, bufsize=-1): + popen2._cleanup() + c2pread, c2pwrite = os.pipe() + null = open('/dev/null', 'w+') + self.pid = os.fork() + if self.pid == 0: + # Child + os.dup2(null.fileno(), sys.__stdin__.fileno()) + #os.dup2(p2cread, 0) + os.dup2(c2pwrite, 1) + os.dup2(c2pwrite, 2) + self._run_child(cmd) + os.close(c2pwrite) + self.fromchild = os.fdopen(c2pread, 'r', bufsize) + popen2._active.append(self) + class Toolset(object): '''The toolset class contains underlying command support and all states''' __important__ = [] @@ -49,12 +67,11 @@ class Toolset(object): '''Run a command in a pipe dealing with stdout buffer overloads''' self.logger.debug('> %s' % command) - runpipe = popen2.Popen4(command, bufsize=16384) - output = runpipe.fromchild.read() - if len(output) > 0: - self.logger.debug('< %s' % output) - cmdstat = runpipe.poll() + runpipe = readonlypipe(command, bufsize=16384) + output = '' + cmdstat = -1 while cmdstat == -1: + runpipe.fromchild.flush() moreOutput = runpipe.fromchild.read() if len(moreOutput) > 0: self.logger.debug('< %s' % moreOutput) |