|
|
@ -738,29 +738,38 @@ def main():
|
|
|
|
|
|
|
|
|
|
|
|
if changed and not module.check_mode:
|
|
|
|
if changed and not module.check_mode:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
|
|
|
|
err = ''
|
|
|
|
sourceslist.save()
|
|
|
|
sourceslist.save()
|
|
|
|
if update_cache:
|
|
|
|
if update_cache:
|
|
|
|
err = ''
|
|
|
|
|
|
|
|
update_cache_retries = module.params.get('update_cache_retries')
|
|
|
|
update_cache_retries = module.params.get('update_cache_retries')
|
|
|
|
update_cache_retry_max_delay = module.params.get('update_cache_retry_max_delay')
|
|
|
|
update_cache_retry_max_delay = module.params.get('update_cache_retry_max_delay')
|
|
|
|
randomize = random.randint(0, 1000) / 1000.0
|
|
|
|
randomize = random.randint(0, 1000) / 1000.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cache = apt.Cache()
|
|
|
|
for retry in range(update_cache_retries):
|
|
|
|
for retry in range(update_cache_retries):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
cache = apt.Cache()
|
|
|
|
|
|
|
|
cache.update()
|
|
|
|
cache.update()
|
|
|
|
break
|
|
|
|
break
|
|
|
|
except apt.cache.FetchFailedException as e:
|
|
|
|
except apt.cache.FetchFailedException as fetch_failed_exc:
|
|
|
|
err = to_native(e)
|
|
|
|
err = fetch_failed_exc
|
|
|
|
|
|
|
|
module.warn(
|
|
|
|
|
|
|
|
f"Failed to update cache after {retry + 1} due "
|
|
|
|
|
|
|
|
f"to {to_native(fetch_failed_exc)} retry, retrying"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Use exponential backoff with a max fail count, plus a little bit of randomness
|
|
|
|
# Use exponential backoff with a max fail count, plus a little bit of randomness
|
|
|
|
delay = 2 ** retry + randomize
|
|
|
|
delay = 2 ** retry + randomize
|
|
|
|
if delay > update_cache_retry_max_delay:
|
|
|
|
if delay > update_cache_retry_max_delay:
|
|
|
|
delay = update_cache_retry_max_delay + randomize
|
|
|
|
delay = update_cache_retry_max_delay + randomize
|
|
|
|
time.sleep(delay)
|
|
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
module.warn(f"Sleeping for {int(round(delay))} seconds, before attempting to update the cache again")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
revert_sources_list(sources_before, sources_after, sourceslist_before)
|
|
|
|
revert_sources_list(sources_before, sources_after, sourceslist_before)
|
|
|
|
module.fail_json(msg='Failed to update apt cache: %s' % (err if err else 'unknown reason'))
|
|
|
|
msg = (
|
|
|
|
|
|
|
|
f"Failed to update apt cache after {update_cache_retries} retries: "
|
|
|
|
|
|
|
|
f"{err if err else 'unknown reason'}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
module.fail_json(msg=msg)
|
|
|
|
|
|
|
|
|
|
|
|
except (OSError, IOError) as ex:
|
|
|
|
except (OSError, IOError) as ex:
|
|
|
|
revert_sources_list(sources_before, sources_after, sourceslist_before)
|
|
|
|
revert_sources_list(sources_before, sources_after, sourceslist_before)
|
|
|
|