Debian Codegraph @

tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py

raw
1 # I am an Ansible new-style Python module, but I lack an interpreter.
2 # See also custom_python_new_style_module, we should be updated in tandem.
3
4 import io
5 import json
6 import select
7 import signal
8 import sys
9 import warnings
10
11 # Ansible 2.7 changed how new style modules are invoked. It seems that module
12 # parameters are *sometimes* read before the module runs. Modules that try
13 # to read directly from stdin, such as this, are unable to. However it doesn't
14 # always fail, influences seem to include Ansible & Python version. As noted
15 # in ansible.module_utils.basic._load_params() we should probably use that.
16 # I think (medium confidence) I narrowed the inflection (with git bisect) to
17 # https://github.com/ansible/ansible/commit/52449cc01a71778ef94ea0237eed0284f5d75582
18
19 # As of Ansible 2.10, Ansible changed new-style detection: # https://github.com/ansible/ansible/pull/61196/files#diff-5675e463b6ce1fbe274e5e7453f83cd71e61091ea211513c93e7c0b4d527d637L828-R980
20 # NOTE: this import works for Mitogen, and the import below matches new-style Ansible 2.10
21 # TODO: find out why 1 import won't work for both Mitogen and Ansible
22 # from ansible.module_utils.
23 # import ansible.module_utils.
24
25 # These timeouts should prevent hard-to-attribute, 2+ hour CI job timeouts.
26 # Previously this module has waited on stdin forever (timeoutInMinutes=120).
27 SELECT_TIMEOUT = 5.0 # seconds
28 SIGNAL_TIMEOUT = 10 # seconds
29
30
31 def fail_json(msg, **kwargs):
32 kwargs.update(failed=True, msg=msg)
33 print(json.dumps(kwargs, sys.stdout, indent=2, sort_keys=True))
34 sys.exit(1)
35
36
37 def sigalrm_handler(signum, frame):
38 fail_json("Still executing after SIGNAL_TIMEOUT=%ds" % (SIGNAL_TIMEOUT,))
39
40
41 def usage():
42 sys.stderr.write('Usage: %s <input.json>\n' % (sys.argv[0],))
43 sys.exit(1)
44
45
46 # Wait SIGNAL_TIMEOUT seconds, exit with failure if still running.
47 signal.signal(signal.SIGALRM, sigalrm_handler)
48 signal.alarm(SIGNAL_TIMEOUT)
49
50 # Wait SELECT_TIMEOUT seconds, exit with failure if no data appears on stdin.
51 # TODO Combine select() & read() in a loop, to handle slow trickle of data.
52 # Consider buffering, line buffering, `f.read()` vs `f.read1()`.
53 # TODO Document that sys.stdin may be a StringIO under Ansible + Mitogen.
54 try:
55 inputs_ready, _, _ = select.select([sys.stdin], [], [], SELECT_TIMEOUT)
56 except (AttributeError, TypeError, io.UnsupportedOperation) as exc:
57 # sys.stdin.fileno() doesn't exist or can't return a real file descriptor.
58 warnings.warn("Could not wait on sys.stdin=%r: %r" % (sys.stdin, exc))
59 else:
60 if not inputs_ready:
61 fail_json("Gave up waiting on sys.stdin after SELECT_TIMEOUT=%ds"
62 % (SELECT_TIMEOUT,))
63
64 # Read all data on stdin. May block forever, if EOF is not reached.
65 input_json = sys.stdin.read()
66
67 print("{")
68 print(" \"changed\": false,")
69 print(" \"msg\": \"Here is my input\",")
70 print(" \"input\": [%s]" % (input_json,))
71 print("}")
72
73 # Ansible since 2.7.0/52449cc01a7 broke __file__ and *requires* the module
74 # process to exit itself. So needless.
75 sys.exit(0)