Debian Codegraph @

tests/ansible/lib/modules/custom_python_new_style_module.py

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