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
This commit is contained in:
jctanner 2016-11-01 19:30:50 -04:00 committed by Matt Clay
parent ba6bc5382d
commit 60dd2f1fff

View file

@ -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']