added apt lock_timeout (#74095)

* added apt lock_timeout

should help control issues with apt db being locked out
also cleanup imports
This commit is contained in:
Brian Coca 2021-04-12 14:10:50 -04:00 committed by GitHub
parent c7473828c7
commit bb7b17fded
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 151 additions and 131 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- apt, added a 'lock_timeout' to be more resilient when encountering the apt db already locked and handle it w/o haveing to rerun task.

View file

@ -158,6 +158,13 @@ options:
type: bool type: bool
default: 'no' default: 'no'
version_added: "2.4" version_added: "2.4"
lock_timeout:
description:
- How many seconds will this action wait to aquire a lock on the apt db.
- Sometimes there is a transitory lock and this will retry at least until timeout is hit.
type: int
default: 60
version_added: "2.12"
requirements: requirements:
- python-apt (python 2) - python-apt (python 2)
- python3-apt (python 3) - python3-apt (python 3)
@ -312,13 +319,12 @@ import datetime
import fnmatch import fnmatch
import itertools import itertools
import os import os
import shutil import random
import re import re
import shutil
import sys import sys
import tempfile import tempfile
import time import time
import random
import time
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.respawn import has_respawned, probe_interpreters_for_module, respawn_module from ansible.module_utils.common.respawn import has_respawned, probe_interpreters_for_module, respawn_module
@ -1079,6 +1085,7 @@ def main():
only_upgrade=dict(type='bool', default=False), only_upgrade=dict(type='bool', default=False),
force_apt_get=dict(type='bool', default=False), force_apt_get=dict(type='bool', default=False),
allow_unauthenticated=dict(type='bool', default=False, aliases=['allow-unauthenticated']), allow_unauthenticated=dict(type='bool', default=False, aliases=['allow-unauthenticated']),
lock_timeout=dict(type='int', default=60),
), ),
mutually_exclusive=[['deb', 'package', 'upgrade']], mutually_exclusive=[['deb', 'package', 'upgrade']],
required_one_of=[['autoremove', 'deb', 'package', 'update_cache', 'upgrade']], required_one_of=[['autoremove', 'deb', 'package', 'update_cache', 'upgrade']],
@ -1166,7 +1173,13 @@ def main():
fail_on_autoremove = p['fail_on_autoremove'] fail_on_autoremove = p['fail_on_autoremove']
autoclean = p['autoclean'] autoclean = p['autoclean']
# Get the cache object # max times we'll retry
deadline = time.time() + p['lock_timeout']
# keep running on lock issues unless timeout or resolution is hit.
while True:
# Get the cache object, this has 3 retries built in
cache = get_cache(module) cache = get_cache(module)
try: try:
@ -1301,10 +1314,15 @@ def main():
remove(module, packages, cache, p['purge'], force=force_yes, dpkg_options=dpkg_options, autoremove=autoremove) remove(module, packages, cache, p['purge'], force=force_yes, dpkg_options=dpkg_options, autoremove=autoremove)
except apt.cache.LockFailedException as lockFailedException: except apt.cache.LockFailedException as lockFailedException:
if time.time() < deadline:
continue
module.fail_json(msg="Failed to lock apt for exclusive operation: %s" % lockFailedException) module.fail_json(msg="Failed to lock apt for exclusive operation: %s" % lockFailedException)
except apt.cache.FetchFailedException as fetchFailedException: except apt.cache.FetchFailedException as fetchFailedException:
module.fail_json(msg="Could not fetch updated apt files: %s" % fetchFailedException) module.fail_json(msg="Could not fetch updated apt files: %s" % fetchFailedException)
# got here w/o exception and/or exit???
module.exit_json({'failed': True, 'msg': 'Unexpected code path taken,we really should have exited before, this is a bug'})
if __name__ == "__main__": if __name__ == "__main__":
main() main()