diff options
-rw-r--r-- | src/lib/Client/Redhat.py | 63 | ||||
-rw-r--r-- | src/lib/Client/Toolset.py | 63 |
2 files changed, 64 insertions, 62 deletions
diff --git a/src/lib/Client/Redhat.py b/src/lib/Client/Redhat.py index c3c0da4ac..c6d55bc48 100644 --- a/src/lib/Client/Redhat.py +++ b/src/lib/Client/Redhat.py @@ -1,18 +1,14 @@ # This is the bcfg2 support for redhat # $Id: $ -__revision__ = '$Revision$' '''This is redhat client support''' +__revision__ = '$Revision$' from os import popen, system from popen2 import Popen4 from Bcfg2.Client.Toolset import Toolset -def Detect(): - # until the code works - return False - class Redhat(Toolset): '''This class implelements support for rpm packages and standard chkconfig services''' rpmcmd = "rpm --oldpackage --replacepkgs --quiet -U %s" @@ -22,44 +18,47 @@ class Redhat(Toolset): self.pkgtodo = [] def VerifyService(self, entry): + '''Verify Service status for entry''' srvdata = popen("/sbin/chkconfig --list %s"%entry.attrib['name']).readlines()[0].split() if entry.attrib['type'] == 'xinetd': if entry.attrib['status'] == srvdata[1]: return True else: return False + + # chkconfig/init.d service + if entry.attrib['status'] == 'off': + for level in srvdata[1:]: + if level.split(':')[1] != 'off': + return False + return True else: - # chkconfig/init.d service - if entry.attrib['status'] == 'off': - for level in srvdata[1:]: - if level.split(':')[1] != 'off': + # services should be on for 2345 + for level in srvdata[1:]: + [num, status] = level.split(':') + if num in '2345': + if status == 'off': return False - return True - else: - # services should be on for 2345 - for level in srvdata[1:]: - [num, status] = level.split(':') - if num in '2345': - if status == 'off': - return False - else: - if status == 'on': - return False - return True + else: + if status == 'on': + return False + return True def InstallService(self, entry): + '''Install Service entry''' system("/sbin/chkconfig --add %s"%(entry.attrib['name'])) if entry.attrib['status'] == 'off': - rc = system("/sbin/chkconfig --level 0123456 %s %s" % (entry.attrib['name'], entry.attrib['status'])) + cmdrc = system("/sbin/chkconfig --level 0123456 %s %s" % (entry.attrib['name'], entry.attrib['status'])) else: - rc = system("/sbin/chkconfig %s %s" % + cmdrc = system("/sbin/chkconfig %s %s" % (entry.attrib['name'], entry.attrib['status'])) - if rc == 0: + if cmdrc == 0: return True else: return False def VerifyPackage(self, entry, modlist = []): + '''Verify Package status for entry''' instp = Popen4("rpm -qi %s-%s" % (entry.attrib['name'], entry.attrib['version'])) istat = instp.wait()/256 if istat == 0: @@ -73,22 +72,24 @@ class Redhat(Toolset): while vstat == -1: odata += verp.fromchild.read() vstat = verp.poll() >> 8 - output = [x for x in odata.split("\n") if x] + output = [line for line in odata.split("\n") if line] if vstat == 0: return True else: - if len([x for x in output if x.split()[-1] not in modlist]) == 0: + if len([name for name in output if name.split()[-1] not in modlist]): return True return False def InstallPackage(self, entry): + '''Install Package entry''' self.pkgtodo.append(entry) return False def Install(self): + '''Fix detected misconfigurations''' # try single install - rc = system(self.rpmcmd % (" ".join([x.attrib['url'] for x in self.pkgtodo]))) - if rc == 0: + cmdrc = system(self.rpmcmd % (" ".join([pkg.get('url') for pkg in self.pkgtodo]))) + if cmdrc == 0: # set state == True for all just-installed packages for pkg in self.pkgtodo: self.states[pkg] = True @@ -99,12 +100,12 @@ class Redhat(Toolset): while oldlen > len(self.pkgtodo): oldlen = len(self.pkgtodo) for entry in self.pkgtodo: - rc = system(self.rpmcmd%(entry.attrib['url'])) - if rc == 0: + cmdrc = system(self.rpmcmd % (entry.get('url'))) + if cmdrc == 0: self.states[entry] = True self.pkgtodo.remove(entry) else: if self.setup['verbose']: - print "package %s-%s failed to install" % (entry.attrib['name'], entry.attrib['version']) + print "package %s-%s failed to install" % (entry.get('name'), entry.get('version')) diff --git a/src/lib/Client/Toolset.py b/src/lib/Client/Toolset.py index 9b73432e3..09b19ea23 100644 --- a/src/lib/Client/Toolset.py +++ b/src/lib/Client/Toolset.py @@ -18,22 +18,16 @@ from elementtree.ElementTree import Element, SubElement, tostring def calc_perms(initial, perms): '''This compares ondisk permissions with specified ones''' + pdisp = [{1:S_ISVTX, 2:S_ISGID, 4:S_ISUID}, {1:S_IXUSR, 2:S_IWUSR, 4:S_IRUSR}, + {1:S_IXGRP, 2:S_IWGRP, 4:S_IRGRP}, {1:S_IXOTH, 2:S_IWOTH, 4:S_IROTH}] tempperms = initial if len(perms) == 3: perms = '0%s' % (perms) - [suid, user, group, other] = [int(perms[x]) for x in range(4)] - for (num, perm) in {1:S_ISVTX, 2:S_ISGID, 4:S_ISUID}.iteritems(): - if suid & num: - tempperms |= perm - for (num, perm) in {1:S_IXUSR, 2:S_IWUSR, 4:S_IRUSR}.iteritems(): - if user & num: - tempperms |= perm - for (num, perm) in {1:S_IXGRP, 2:S_IWGRP, 4:S_IRGRP}.iteritems(): - if group & num: - tempperms |= perm - for (num, perm) in {1:S_IXOTH, 2:S_IWOTH, 4:S_IROTH}.iteritems(): - if other & num: - tempperms |= perm + pdigits = [int(perms[digit]) for digit in range(4)] + for index in range(4): + for (num, perm) in pdisp[index].iteritems(): + if pdigits[index] & num: + tempperms |= perm return tempperms class Toolset(object): @@ -58,11 +52,11 @@ class Toolset(object): def LogFailure(self, area, entry): '''Print tracebacks in unexpected cases''' print "Failure in %s for entry: %s" % (area, tostring(entry)) - (t, v, tb) = exc_info() - for line in extract_tb(tb): + (ttype, value, trace) = exc_info() + for line in extract_tb(trace): print "File %s, line %i, in %s\n %s\n" % (line) - print "%s: %s\n" % (t, v) - del t, v, tb + print "%s: %s\n" % (ttype, value) + del ttype, v, trace def print_failure(self): '''Display curses style failure message''' @@ -77,16 +71,17 @@ class Toolset(object): # These next functions form the external API def Inventory(self): + '''Build up workqueue for installation''' # build initial set of states - unexamined = [(x, []) for x in self.cfg.getchildren()] + unexamined = [(child, []) for child in self.cfg.getchildren()] while unexamined: - (r, modlist) = unexamined.pop() - if r.tag not in ['Bundle', 'Independant']: - self.VerifyEntry(r, modlist) + (entry, modlist) = unexamined.pop() + if entry.tag not in ['Bundle', 'Independant']: + self.VerifyEntry(entry, modlist) else: - modlist = [x.get('name') for x in r.getchildren() if x.tag == 'ConfigFile'] - unexamined += [(x, modlist) for x in r.getchildren()] - self.structures[r] = False + modlist = [cfile.get('name') for cfile in entry.getchildren() if cfile.tag == 'ConfigFile'] + unexamined += [(child, modlist) for child in entry.getchildren()] + self.structures[entry] = False for structure in self.cfg.getchildren(): self.CheckStructure(structure) @@ -156,15 +151,17 @@ class Toolset(object): # All remaining operations implement the mechanics of POSIX cfg elements def VerifySymLink(self, entry): + '''Verify SymLink Entry''' try: - s = readlink(entry.get('name')) - if s == entry.get('to'): + sloc = readlink(entry.get('name')) + if sloc == entry.get('to'): return True return False except OSError: return False def InstallSymLink(self, entry): + '''Install SymLink Entry''' try: fmode = lstat(entry.get('name'))[ST_MODE] if S_ISREG(fmode) or S_ISLNK(fmode): @@ -177,10 +174,11 @@ class Toolset(object): print "Symlink %s cleanup failed" % (entry.get('name')) try: symlink(entry.get('to'), entry.get('name')) - except OSError, e: + except OSError: return False def VerifyDirectory(self, entry): + '''Verify Directory Entry''' try: ondisk = stat(entry.get('name')) except OSError: @@ -200,6 +198,7 @@ class Toolset(object): return False def InstallDirectory(self, entry): + '''Install Directory Entry''' try: fmode = lstat(entry.get('name')) if not S_ISDIR(fmode[0]): @@ -221,6 +220,7 @@ class Toolset(object): return False def VerifyConfigFile(self, entry): + '''Install ConfigFile Entry''' try: ondisk = stat(entry.get('name')) except OSError: @@ -245,6 +245,7 @@ class Toolset(object): return False def InstallConfigFile(self, entry): + '''Install ConfigFile Entry''' if self.setup['dryrun'] or self.setup['verbose']: print "Installing ConfigFile %s" % (entry.get('name')) if self.setup['dryrun']: @@ -252,9 +253,9 @@ class Toolset(object): parent = "/".join(entry.get('name').split('/')[:-1]) if parent: try: - s = lstat(parent) + sloc = lstat(parent) try: - if not S_ISDIR(s[ST_MODE]): + if not S_ISDIR(sloc[ST_MODE]): unlink(parent) mkdir(parent) except OSError: @@ -281,7 +282,7 @@ class Toolset(object): system("cp %s /var/cache/bcfg2/%s" % (entry.get('name'))) rename(newfile.name, entry.get('name')) return True - except (OSError, IOError), e: - print e + except (OSError, IOError), errmsg: + print errmsg return False |