diff options
author | jctanner <tanner.jc@gmail.com> | 2016-11-01 19:30:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-01 19:30:50 -0400 |
commit | 5890fba5dcdb47aba19857b640cab8c5ec793230 (patch) | |
tree | c48210fd60a6ed53f5ece490929fd236af068169 | |
parent | 859180d0133343751efade55d1799d9da365d417 (diff) | |
download | ansible-modules-core-5890fba5dcdb47aba19857b640cab8c5ec793230.tar.gz |
apt: update cache until corrupt package lists are fixed (#5448)
* apt: If the cache object fails to lost due to a corrupt file, try to update the cache until it is fixed.
* Append -q to the update parameters
* Remove unused variable
* Use a string that doesn't rely on internationalization
* Use py24 exception style
* Use get_exception
Fixes #2951
-rw-r--r-- | packaging/os/apt.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/packaging/os/apt.py b/packaging/os/apt.py index 3983b6bd..be8e1016 100644 --- a/packaging/os/apt.py +++ b/packaging/os/apt.py @@ -755,6 +755,31 @@ def get_updated_cache_time(): return mtimestamp, updated_cache_time +# https://github.com/ansible/ansible-modules-core/issues/2951 +def get_cache(module): + '''Attempt to get the cache object and update till it works''' + cache = None + try: + cache = apt.Cache() + except SystemError: + e = get_exception() + if '/var/lib/apt/lists/' in str(e).lower(): + # update cache until files are fixed or retries exceeded + retries = 0 + while retries < 2: + (rc, so, se) = module.run_command(['apt-get', 'update', '-q']) + retries += 1 + if rc == 0: + break + if rc != 0: + module.fail_json(msg='Updating the cache to correct corrupt package lists failed:\n%s\n%s' % (str(e), str(so) + str(se))) + # try again + cache = apt.Cache() + else: + module.fail_json(msg=str(e)) + return cache + + def main(): module = AnsibleModule( argument_spec = dict( @@ -821,8 +846,10 @@ def main(): if p['state'] == 'removed': p['state'] = 'absent' + # Get the cache object + cache = get_cache(module) + try: - cache = apt.Cache() if p['default_release']: try: apt_pkg.config['APT::Default-Release'] = p['default_release'] |