summaryrefslogtreecommitdiffstats
path: root/src/lib/Client/Tools
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Client/Tools')
-rw-r--r--src/lib/Client/Tools/APT.py5
-rw-r--r--src/lib/Client/Tools/POSIX.py132
-rw-r--r--src/lib/Client/Tools/Pacman.py3
-rw-r--r--src/lib/Client/Tools/RPMng.py3
-rw-r--r--src/lib/Client/Tools/YUM24.py8
-rw-r--r--src/lib/Client/Tools/YUMng.py86
6 files changed, 182 insertions, 55 deletions
diff --git a/src/lib/Client/Tools/APT.py b/src/lib/Client/Tools/APT.py
index a838f5e27..2b8cc3304 100644
--- a/src/lib/Client/Tools/APT.py
+++ b/src/lib/Client/Tools/APT.py
@@ -9,6 +9,8 @@ warnings.filterwarnings("ignore", "Accessed deprecated property Package.installe
warnings.filterwarnings("ignore", "Accessed deprecated property Package.candidateVersion, please see the Version class for alternatives.", DeprecationWarning)
warnings.filterwarnings("ignore", "Deprecated, please use 'is_installed' instead", DeprecationWarning)
warnings.filterwarnings("ignore", "Attribute 'IsUpgradable' of the 'apt_pkg.DepCache' object is deprecated, use 'is_upgradable' instead.", DeprecationWarning)
+warnings.filterwarnings("ignore", "Attribute 'VersionList' of the 'apt_pkg.Package' object is deprecated, use 'version_list' instead.", DeprecationWarning)
+warnings.filterwarnings("ignore", "Attribute 'VerStr' of the 'apt_pkg.Version' object is deprecated, use 'ver_str' instead.", DeprecationWarning)
import apt.cache
import os
@@ -71,7 +73,8 @@ class APT(Bcfg2.Client.Tools.Tool):
self.cmd.run("%s clean" % APTGET)
try:
self.pkg_cache = apt.cache.Cache()
- except SystemError, e:
+ except SystemError:
+ e = sys.exc_info()[1]
self.logger.info("Failed to initialize APT cache: %s" % e)
raise Bcfg2.Client.Tools.toolInstantiationError
self.pkg_cache.update()
diff --git a/src/lib/Client/Tools/POSIX.py b/src/lib/Client/Tools/POSIX.py
index af3d1a473..faec2e251 100644
--- a/src/lib/Client/Tools/POSIX.py
+++ b/src/lib/Client/Tools/POSIX.py
@@ -14,7 +14,12 @@ import os
import pwd
import shutil
import stat
+import sys
import time
+# py3k compatibility
+if sys.hexversion >= 0x03000000:
+ unicode = str
+
import Bcfg2.Client.Tools
import Bcfg2.Options
from Bcfg2.Client import XML
@@ -55,7 +60,8 @@ def normGid(entry):
except:
return int(grp.getgrnam(entry.get('group'))[2])
except (OSError, KeyError):
- log.error('GID normalization failed for %s' % (entry.get('name')))
+ log.error('GID normalization failed for %s. Does group %s exist?'
+ % (entry.get('name'), entry.get('group')))
return False
@@ -70,7 +76,23 @@ def normUid(entry):
except:
return int(pwd.getpwnam(entry.get('owner'))[2])
except (OSError, KeyError):
- log.error('UID normalization failed for %s' % (entry.get('name')))
+ log.error('UID normalization failed for %s. Does owner %s exist?'
+ % (entry.get('name'), entry.get('owner')))
+ return False
+
+
+def isString(strng, encoding):
+ """
+ Returns true if the string contains no ASCII control characters
+ and can be decoded from the specified encoding.
+ """
+ for char in strng:
+ if ord(char) < 9 or ord(char) > 13 and ord(char) < 32:
+ return False
+ try:
+ strng.decode(encoding)
+ return True
+ except:
return False
@@ -127,7 +149,8 @@ class POSIX(Bcfg2.Client.Tools.Tool):
try:
content = open(entry.get('name')).read()
entry.set('current_bfile', binascii.b2a_base64(content))
- except IOError, error:
+ except IOError:
+ error = sys.exc_info()[1]
self.logger.error("Failed to read %s: %s" % (error.filename,
error.strerror))
@@ -439,12 +462,14 @@ class POSIX(Bcfg2.Client.Tools.Tool):
if type(tempdata) == unicode:
try:
tempdata = tempdata.encode(self.setup['encoding'])
- except UnicodeEncodeError, e:
+ except UnicodeEncodeError:
+ e = sys.exc_info()[1]
self.logger.error("Error encoding file %s:\n %s" % \
(entry.get('name'), e))
try:
content = open(entry.get('name')).read()
- except IOError, error:
+ except IOError:
+ error = sys.exc_info()[1]
if error.strerror == "No such file or directory":
# print diff for files that don't exist (yet)
content = ''
@@ -456,12 +481,7 @@ class POSIX(Bcfg2.Client.Tools.Tool):
# md5sum so it would be faster for big binary files
contentStatus = content == tempdata
if not contentStatus:
- try:
- content.decode('ascii')
- isstring = True
- except:
- isstring = False
- if tbin or not isstring:
+ if tbin or not isString(content, self.setup['encoding']):
entry.set('current_bfile', binascii.b2a_base64(content))
nqtext = entry.get('qtext', '')
nqtext += '\nBinary file, no printable diff'
@@ -491,15 +511,15 @@ class POSIX(Bcfg2.Client.Tools.Tool):
difflib.unified_diff(content.split('\n'), \
tempdata.split('\n'))])
try:
- eudiff = udiff.encode('ascii')
+ dudiff = udiff.decode(self.setup['encoding'])
except:
- eudiff = "Binary file: no diff printed"
+ dudiff = "Binary file: no diff printed"
nqtext = entry.get('qtext', '')
if nqtext:
nqtext += '\n'
- nqtext += eudiff
+ nqtext += dudiff
else:
entry.set('current_bfile', binascii.b2a_base64(content))
nqtext = entry.get('qtext', '')
@@ -547,8 +567,14 @@ class POSIX(Bcfg2.Client.Tools.Tool):
(entry.get('current_exists', 'true') == 'false'):
bkupnam = entry.get('name').replace('/', '_')
# current list of backups for this file
- bkuplist = [f for f in os.listdir(self.ppath) if
- f.startswith(bkupnam)]
+ try:
+ bkuplist = [f for f in os.listdir(self.ppath) if
+ f.startswith(bkupnam)]
+ except OSError:
+ e = sys.exc_info()[1]
+ self.logger.error("Failed to create backup list in %s: %s" %
+ (self.ppath, e.strerror))
+ return False
bkuplist.sort()
while len(bkuplist) >= int(self.max_copies):
# remove the oldest backup available
@@ -567,7 +593,8 @@ class POSIX(Bcfg2.Client.Tools.Tool):
datetime.isoformat(datetime.now())))
self.logger.info("Backup of %s saved to %s" %
(entry.get('name'), self.ppath))
- except IOError, e:
+ except IOError:
+ e = sys.exc_info()[1]
self.logger.error("Failed to create backup file for %s" % \
(entry.get('name')))
self.logger.error(e)
@@ -603,7 +630,8 @@ class POSIX(Bcfg2.Client.Tools.Tool):
% (entry.get('name')))
return False
return True
- except (OSError, IOError), err:
+ except (OSError, IOError):
+ err = sys.exc_info()[1]
if err.errno == errno.EACCES:
self.logger.info("Failed to open %s for writing" % (entry.get('name')))
else:
@@ -683,7 +711,8 @@ class POSIX(Bcfg2.Client.Tools.Tool):
return False
try:
shutil.rmtree(ename)
- except OSError, e:
+ except OSError:
+ e = sys.exc_info()[1]
self.logger.error('Failed to remove %s: %s' % (ename,
e.strerror))
else:
@@ -691,20 +720,63 @@ class POSIX(Bcfg2.Client.Tools.Tool):
try:
os.rmdir(ename)
return True
- except OSError, e:
+ except OSError:
+ e = sys.exc_info()[1]
self.logger.error('Failed to remove %s: %s' % (ename,
e.strerror))
return False
try:
os.remove(ename)
return True
- except OSError, e:
+ except OSError:
+ e = sys.exc_info()[1]
self.logger.error('Failed to remove %s: %s' % (ename,
e.strerror))
return False
def Verifypermissions(self, entry, _):
"""Verify Path type='permissions' entry"""
+ if entry.get('perms') == None or \
+ entry.get('owner') == None or \
+ entry.get('group') == None:
+ self.logger.error('Entry %s not completely specified. '
+ 'Try running bcfg2-lint.' % (entry.get('name')))
+ return False
+ if entry.get('recursive') in ['True', 'true']:
+ # verify ownership information recursively
+ owner = normUid(entry)
+ group = normGid(entry)
+
+ for root, dirs, files in os.walk(entry.get('name')):
+ for p in dirs + files:
+ path = os.path.join(root, p)
+ pstat = os.stat(path)
+ if owner != pstat.st_uid:
+ # owner mismatch for path
+ entry.set('current_owner', str(pstat.st_uid))
+ self.logger.debug("%s %s ownership wrong" % \
+ (entry.tag, path))
+ nqtext = entry.get('qtext', '') + '\n'
+ nqtext += ("Owner for path %s is incorrect. "
+ "Current owner is %s but should be %s\n" % \
+ (path, pstat.st_uid, entry.get('owner')))
+ nqtext += ("\nInstall %s %s: (y/N): " %
+ (entry.tag, entry.get('name')))
+ entry.set('qtext', nqtext)
+ return False
+ if group != pstat.st_gid:
+ # group mismatch for path
+ entry.set('current_group', str(pstat.st_gid))
+ self.logger.debug("%s %s group wrong" % \
+ (entry.tag, path))
+ nqtext = entry.get('qtext', '') + '\n'
+ nqtext += ("Group for path %s is incorrect. "
+ "Current group is %s but should be %s\n" % \
+ (path, pstat.st_gid, entry.get('group')))
+ nqtext += ("\nInstall %s %s: (y/N): " %
+ (entry.tag, entry.get('name')))
+ entry.set('qtext', nqtext)
+ return False
return self.Verifydirectory(entry, _)
def Installpermissions(self, entry):
@@ -715,9 +787,23 @@ class POSIX(Bcfg2.Client.Tools.Tool):
self.logger.error('Entry %s not completely specified. '
'Try running bcfg2-lint.' % (entry.get('name')))
return False
+ plist = [entry.get('name')]
+ if entry.get('recursive') in ['True', 'true']:
+ # verify ownership information recursively
+ owner = normUid(entry)
+ group = normGid(entry)
+
+ for root, dirs, files in os.walk(entry.get('name')):
+ for p in dirs + files:
+ path = os.path.join(root, p)
+ pstat = os.stat(path)
+ if owner != pstat.st_uid or group != pstat.st_gid:
+ # owner mismatch for path
+ plist.append(path)
try:
- os.chown(entry.get('name'), normUid(entry), normGid(entry))
- os.chmod(entry.get('name'), calcPerms(S_IFDIR, entry.get('perms')))
+ for p in plist:
+ os.chown(p, normUid(entry), normGid(entry))
+ os.chmod(p, calcPerms(S_IFDIR, entry.get('perms')))
return True
except (OSError, KeyError):
self.logger.error('Permission fixup failed for %s' % \
diff --git a/src/lib/Client/Tools/Pacman.py b/src/lib/Client/Tools/Pacman.py
index 082897934..c8c05061c 100644
--- a/src/lib/Client/Tools/Pacman.py
+++ b/src/lib/Client/Tools/Pacman.py
@@ -78,5 +78,6 @@ class Pacman(Bcfg2.Client.Tools.PkgTool):
try:
self.logger.debug("Running : %s -S %s" % (self.pkgtool, pkgline))
self.cmd.run("%s -S %s" % (self.pkgtool, pkgline))
- except Exception, e:
+ except Exception:
+ e = sys.exc_info()[1]
self.logger.error("Error occurred during installation: %s" % e)
diff --git a/src/lib/Client/Tools/RPMng.py b/src/lib/Client/Tools/RPMng.py
index a1e14b3a6..5376118c2 100644
--- a/src/lib/Client/Tools/RPMng.py
+++ b/src/lib/Client/Tools/RPMng.py
@@ -2,11 +2,12 @@
__revision__ = '$Revision$'
-import ConfigParser
import os.path
import rpm
import rpmtools
import Bcfg2.Client.Tools
+# Compatibility import
+from Bcfg2.Bcfg2Py3k import ConfigParser
# Fix for python2.3
try:
diff --git a/src/lib/Client/Tools/YUM24.py b/src/lib/Client/Tools/YUM24.py
index 04d9f5c07..66768fb34 100644
--- a/src/lib/Client/Tools/YUM24.py
+++ b/src/lib/Client/Tools/YUM24.py
@@ -1,13 +1,14 @@
"""This provides bcfg2 support for yum."""
__revision__ = '$Revision: $'
-import ConfigParser
import copy
import os.path
import sys
import yum
import Bcfg2.Client.XML
import Bcfg2.Client.Tools.RPMng
+# Compatibility import
+from Bcfg2.Bcfg2Py3k import ConfigParser
# Fix for python2.3
try:
@@ -76,8 +77,6 @@ class YUM24(Bcfg2.Client.Tools.RPMng.RPMng):
__new_gpg_ireq__ = {'Package': ['name'],
'Instance': ['version', 'release']}
- conflicts = ['YUMng', 'RPMng']
-
def __init__(self, logger, setup, config):
Bcfg2.Client.Tools.RPMng.RPMng.__init__(self, logger, setup, config)
self.__important__ = self.__important__ + \
@@ -153,7 +152,8 @@ class YUM24(Bcfg2.Client.Tools.RPMng.RPMng):
try:
pkgDict = dict([(i.name, i) for i in \
self.yb.returnPackagesByDep(entry.get('name'))])
- except yum.Errors.YumBaseError, e:
+ except yum.Errors.YumBaseError:
+ e = sys.exc_info()[1]
self.logger.error('Yum Error Depsolving for %s: %s' % \
(entry.get('name'), str(e)))
pkgDict = {}
diff --git a/src/lib/Client/Tools/YUMng.py b/src/lib/Client/Tools/YUMng.py
index c9e7aa15e..24605ca44 100644
--- a/src/lib/Client/Tools/YUMng.py
+++ b/src/lib/Client/Tools/YUMng.py
@@ -1,9 +1,9 @@
"""This provides bcfg2 support for yum."""
__revision__ = '$Revision$'
-import ConfigParser
import copy
import os.path
+import sys
import yum
import yum.packages
import yum.rpmtrans
@@ -13,6 +13,8 @@ import yum.misc
import rpmUtils.arch
import Bcfg2.Client.XML
import Bcfg2.Client.Tools
+# Compatibility import
+from Bcfg2.Bcfg2Py3k import ConfigParser
# Fix for python2.3
try:
@@ -141,7 +143,7 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
'Path': ['type']}
__ireq__ = {'Package': ['name']}
- conflicts = ['RPMng']
+ conflicts = ['YUM24', 'RPMng']
def __init__(self, logger, setup, config):
self.yb = yum.YumBase()
@@ -167,10 +169,12 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
self.yb.doConfigSetup()
self.yb.doTsSetup()
self.yb.doRpmDBSetup()
- except yum.Errors.RepoError, e:
+ except yum.Errors.RepoError:
+ e = sys.exc_info()[1]
self.logger.error("YUMng Repository error: %s" % e)
raise Bcfg2.Client.Tools.toolInstantiationError
- except yum.Errors.YumBaseError, e:
+ except Exception:
+ e = sys.exc_info()[1]
self.logger.error("YUMng error: %s" % e)
raise Bcfg2.Client.Tools.toolInstantiationError
@@ -447,8 +451,13 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
verify_flags = inst.get('verify_flags', self.verifyFlags)
verify_flags = verify_flags.lower().replace(' ', ',').split(',')
- if len(POs) == 0:
- # Package not installed
+ if 'arch' in nevra:
+ # If arch is specified use it to select the package
+ _POs = [ p for p in POs if p.arch == nevra['arch'] ]
+ else:
+ _POs = POs
+ if len(_POs) == 0:
+ # Package (name, arch) not installed
self.logger.debug(" %s is not installed" % nevraString(nevra))
stat['installed'] = False
package_fail = True
@@ -485,6 +494,9 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
qtext_versions.append("U(%s)" % str(POs[0]))
continue
+ if self.setup.get('quick', False):
+ # Passed -q on the command line
+ continue
if not (pkg_verify and \
inst.get('pkg_verify', 'true').lower() == 'true'):
continue
@@ -502,7 +514,8 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
try:
vResult = self._verifyHelper(_POs[0])
- except Exception, e:
+ except Exception:
+ e = sys.exc_info()[1]
# Unknown Yum exception
self.logger.warning(" Verify Exception: %s" % str(e))
package_fail = True
@@ -668,38 +681,58 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
return True
def _runYumTransaction(self):
+ def cleanup():
+ self.yb.closeRpmDB()
+ self.RefreshPackages()
+
rDisplay = RPMDisplay(self.logger)
yDisplay = YumDisplay(self.logger)
# Run the Yum Transaction
- rescode, restring = self.yb.buildTransaction()
+ try:
+ rescode, restring = self.yb.buildTransaction()
+ except yum.Errors.YumBaseError:
+ e = sys.exc_info()[1]
+ self.logger.error("Yum transaction error: %s" % str(e))
+ cleanup()
+ return
+
self.logger.debug("Initial Yum buildTransaction() run said:")
self.logger.debug(" resultcode: %s, msgs: %s" \
% (rescode, restring))
if rescode != 1:
# Transaction built successfully, run it
- self.yb.processTransaction(callback=yDisplay,
- rpmDisplay=rDisplay)
- self.logger.info("Single Pass for Install Succeeded")
+ try:
+ self.yb.processTransaction(callback=yDisplay,
+ rpmDisplay=rDisplay)
+ self.logger.info("Single Pass for Install Succeeded")
+ except yum.Errors.YumBaseError:
+ e = sys.exc_info()[1]
+ self.logger.error("Yum transaction error: %s" % str(e))
+ cleanup()
+ return
else:
# The yum command failed. No packages installed.
# Try installing instances individually.
self.logger.error("Single Pass Install of Packages Failed")
skipBroken = self.yb.conf.skip_broken
self.yb.conf.skip_broken = True
- rescode, restring = self.yb.buildTransaction()
- if rescode != 1:
- self.yb.processTransaction(callback=yDisplay,
- rpmDisplay=rDisplay)
- self.logger.debug(
- "Second pass install did not install all packages")
- else:
- self.logger.error("Second pass yum install failed.")
- self.logger.debug(" %s" % restring)
+ try:
+ rescode, restring = self.yb.buildTransaction()
+ if rescode != 1:
+ self.yb.processTransaction(callback=yDisplay,
+ rpmDisplay=rDisplay)
+ self.logger.debug(
+ "Second pass install did not install all packages")
+ else:
+ self.logger.error("Second pass yum install failed.")
+ self.logger.debug(" %s" % restring)
+ except yum.Errors.YumBaseError, e:
+ self.logger.error("Yum transaction error: %s" % str(e))
+
self.yb.conf.skip_broken = skipBroken
- self.yb.closeRpmDB()
- self.RefreshPackages()
+ cleanup()
def Install(self, packages, states):
"""
@@ -801,7 +834,8 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
pkg_arg = self.instance_status[inst].get('pkg').get('name')
try:
self.yb.install(**build_yname(pkg_arg, inst))
- except yum.Errors.YumBaseError, yume:
+ except yum.Errors.YumBaseError:
+ yume = sys.exc_info()[1]
self.logger.error("Error installing some packages: %s" % yume)
if len(upgrade_pkgs) > 0:
@@ -811,7 +845,8 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
pkg_arg = self.instance_status[inst].get('pkg').get('name')
try:
self.yb.update(**build_yname(pkg_arg, inst))
- except yum.Errors.YumBaseError, yume:
+ except yum.Errors.YumBaseError:
+ yume = sys.exc_info()[1]
self.logger.error("Error upgrading some packages: %s" % yume)
if len(reinstall_pkgs) > 0:
@@ -820,7 +855,8 @@ class YUMng(Bcfg2.Client.Tools.PkgTool):
pkg_arg = self.instance_status[inst].get('pkg').get('name')
try:
self.yb.reinstall(**build_yname(pkg_arg, inst))
- except yum.Errors.YumBaseError, yume:
+ except yum.Errors.YumBaseError:
+ yume = sys.exc_info()[1]
self.logger.error("Error upgrading some packages: %s" \
% yume)