From 4a2276bb4be779a0721624d5632ada13d6debae8 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 13 Mar 2013 18:11:23 -0400 Subject: [PATCH 1/2] now catches lock exceptoins on apt and returns friendly message instead of traceback Signed-off-by: Brian Coca --- library/apt | 64 ++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/library/apt b/library/apt index 743e380bb5..73fa17e970 100644 --- a/library/apt +++ b/library/apt @@ -226,41 +226,45 @@ def main(): p = module.params install_recommends = p['install_recommends'] - cache = apt.Cache() - if p['default_release']: - apt_pkg.config['APT::Default-Release'] = p['default_release'] - # reopen cache w/ modified config - cache.open(progress=None) + try: + cache = apt.Cache() + if p['default_release']: + apt_pkg.config['APT::Default-Release'] = p['default_release'] + # reopen cache w/ modified config + cache.open(progress=None) - if p['update_cache']: - cache.update() - cache.open(progress=None) - if not p['package']: - module.exit_json(changed=False) + if p['update_cache']: + cache.update() + cache.open(progress=None) + if not p['package']: + module.exit_json(changed=False) - force_yes = p['force'] + force_yes = p['force'] - if p['upgrade']: - upgrade(module, p['upgrade']) + if p['upgrade']: + upgrade(module, p['upgrade']) - packages = p['package'].split(',') - latest = p['state'] == 'latest' - for package in packages: - if package.count('=') > 1: - module.fail_json(msg="invalid package spec: %s" % package) - if latest and '=' in package: - module.fail_json(msg='version number inconsistent with state=latest: %s' % package) + packages = p['package'].split(',') + latest = p['state'] == 'latest' + for package in packages: + if package.count('=') > 1: + module.fail_json(msg="invalid package spec: %s" % package) + if latest and '=' in package: + module.fail_json(msg='version number inconsistent with state=latest: %s' % package) - if p['state'] == 'latest': - install(module, packages, cache, upgrade=True, - default_release=p['default_release'], - install_recommends=install_recommends, - force=force_yes) - elif p['state'] in [ 'installed', 'present' ]: - install(module, packages, cache, default_release=p['default_release'], - install_recommends=install_recommends,force=force_yes) - elif p['state'] in [ 'removed', 'absent' ]: - remove(module, packages, cache, p['purge']) + if p['state'] == 'latest': + install(module, packages, cache, upgrade=True, + default_release=p['default_release'], + install_recommends=install_recommends, + force=force_yes) + elif p['state'] in [ 'installed', 'present' ]: + install(module, packages, cache, default_release=p['default_release'], + install_recommends=install_recommends,force=force_yes) + elif p['state'] in [ 'removed', 'absent' ]: + remove(module, packages, cache, p['purge']) + + except LockFailedException: + module.fail_json(msg="Failed to lock apt for exclusive operation") # this is magic, see lib/ansible/module_common.py #<> From 4f9c61d6739e217ffdf2fc56b1697af144e8ebe8 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 13 Mar 2013 18:21:16 -0400 Subject: [PATCH 2/2] now actually import exception to be usable --- library/apt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/apt b/library/apt index 73fa17e970..5731d4f8e5 100644 --- a/library/apt +++ b/library/apt @@ -263,7 +263,7 @@ def main(): elif p['state'] in [ 'removed', 'absent' ]: remove(module, packages, cache, p['purge']) - except LockFailedException: + except apt.cache.LockFailedException: module.fail_json(msg="Failed to lock apt for exclusive operation") # this is magic, see lib/ansible/module_common.py