wait_for, fallback to read for non mmapable files (#82064) (#82233)

* wait_for, fallback to read for non mmapable files (#82064)

* also handle oserror, added debug jic

(cherry picked from commit 8b102dca4a)

* skip problem versions
This commit is contained in:
Brian Coca
2023-11-27 08:28:03 -08:00
committed by GitHub
parent 2f7376ce06
commit fb5d254a79
3 changed files with 29 additions and 5 deletions
+2
View File
@@ -0,0 +1,2 @@
bugfixes:
- wait_for should not handle 'non mmapable files' again.
+17 -4
View File
@@ -238,7 +238,7 @@ import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.sys_info import get_platform_subclass
from ansible.module_utils.common.text.converters import to_bytes
from ansible.module_utils.common.text.converters import to_bytes, to_native
from ansible.module_utils.compat.datetime import utcnow
@@ -585,17 +585,30 @@ def main():
if not b_compiled_search_re:
# nope, succeed!
break
try:
with open(b_path, 'rb') as f:
with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)) as mm:
search = b_compiled_search_re.search(mm)
try:
with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)) as mm:
search = b_compiled_search_re.search(mm)
if search:
if search.groupdict():
match_groupdict = search.groupdict()
if search.groups():
match_groups = search.groups()
break
except (ValueError, OSError) as e:
module.debug('wait_for failed to use mmap on "%s": %s. Falling back to file read().' % (path, to_native(e)))
# cannot mmap this file, try normal read
search = re.search(b_compiled_search_re, f.read())
if search:
if search.groupdict():
match_groupdict = search.groupdict()
if search.groups():
match_groups = search.groups()
break
except Exception as e:
module.warn('wait_for failed on "%s", unexpected exception(%s): %s.).' % (path, to_native(e.__class__), to_native(e)))
except IOError:
pass
elif port:
@@ -91,7 +91,7 @@
wait_for:
path: "{{remote_tmp_dir}}/wait_for_keyword"
search_regex: completed (?P<foo>\w+) ([0-9]+)
timeout: 5
timeout: 25
register: waitfor
- name: verify test wait for keyword in file with match groups
@@ -114,6 +114,15 @@
path: "{{remote_tmp_dir}}/utf16.txt"
search_regex: completed
- name: test non mmapable file
wait_for:
path: "/sys/class/net/lo/carrier"
search_regex: "1"
timeout: 30
when:
- ansible_facts['os_family'] not in ['FreeBSD', 'Darwin']
- not (ansible_facts['os_family'] in ['RedHat', 'CentOS'] and ansible_facts['distribution_major_version'] is version('7', '<='))
- name: test wait for port timeout
wait_for:
port: 12121