mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
add _load_params debug overrides for module args/file passed on cmdline
Updated python module wrapper explode method to drop 'args' file next to module. Both execute() and excommunicate() debug methods now pass the module args via file to enable debuggers that are picky about stdin. Updated unit tests to use a context manager for masking/restoring default streams and argv.
This commit is contained in:
@@ -142,6 +142,7 @@ def debug(command, zipped_mod, json_params):
|
||||
|
||||
# Okay to use __file__ here because we're running from a kept file
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
args_path = os.path.join(basedir, 'args')
|
||||
if command == 'explode':
|
||||
# transform the ZIPDATA into an exploded directory of code and then
|
||||
# print the path to the code. This is an easy way for people to look
|
||||
@@ -163,6 +164,11 @@ def debug(command, zipped_mod, json_params):
|
||||
f.write(z.read(filename))
|
||||
f.close()
|
||||
|
||||
# write the args file
|
||||
f = open(args_path, 'w')
|
||||
f.write(json_params)
|
||||
f.close()
|
||||
|
||||
print('Module expanded into:')
|
||||
print('%%s' %% os.path.join(basedir, 'ansible'))
|
||||
exitcode = 0
|
||||
@@ -171,7 +177,29 @@ def debug(command, zipped_mod, json_params):
|
||||
# Execute the exploded code instead of executing the module from the
|
||||
# embedded ZIPDATA. This allows people to easily run their modified
|
||||
# code on the remote machine to see how changes will affect it.
|
||||
exitcode = invoke_module(os.path.join(basedir, 'ansible_module_%(ansible_module)s.py'), basedir, json_params)
|
||||
# This differs slightly from default Ansible execution of Python modules
|
||||
# as it passes the arguments to the module via a file instead of stdin.
|
||||
|
||||
pythonpath = os.environ.get('PYTHONPATH')
|
||||
if pythonpath:
|
||||
os.environ['PYTHONPATH'] = ':'.join((basedir, pythonpath))
|
||||
else:
|
||||
os.environ['PYTHONPATH'] = basedir
|
||||
|
||||
p = subprocess.Popen(['%(interpreter)s', 'ansible_module_%(ansible_module)s.py', args_path], env=os.environ, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
(stdout, stderr) = p.communicate()
|
||||
|
||||
if not isinstance(stderr, (bytes, unicode)):
|
||||
stderr = stderr.read()
|
||||
if not isinstance(stdout, (bytes, unicode)):
|
||||
stdout = stdout.read()
|
||||
if PY3:
|
||||
sys.stderr.buffer.write(stderr)
|
||||
sys.stdout.buffer.write(stdout)
|
||||
else:
|
||||
sys.stderr.write(stderr)
|
||||
sys.stdout.write(stdout)
|
||||
return p.returncode
|
||||
|
||||
elif command == 'excommunicate':
|
||||
# This attempts to run the module in-process (by importing a main
|
||||
@@ -182,8 +210,9 @@ def debug(command, zipped_mod, json_params):
|
||||
# when using this that are only artifacts of how we're invoking here,
|
||||
# not actual bugs (as they don't affect the real way that we invoke
|
||||
# ansible modules)
|
||||
sys.stdin = IOStream(json_params)
|
||||
sys.path.insert(0, basedir)
|
||||
|
||||
# stub the
|
||||
sys.argv = ['%(ansible_module)s', args_path]
|
||||
from ansible_module_%(ansible_module)s import main
|
||||
main()
|
||||
print('WARNING: Module returned to wrapper instead of exiting')
|
||||
|
||||
@@ -1435,11 +1435,23 @@ class AnsibleModule(object):
|
||||
|
||||
def _load_params(self):
|
||||
''' read the input and set the params attribute. Sets the constants as well.'''
|
||||
# debug overrides to read args from file or cmdline
|
||||
|
||||
# Avoid tracebacks when locale is non-utf8
|
||||
if sys.version_info < (3,):
|
||||
buffer = sys.stdin.read()
|
||||
if len(sys.argv) > 1:
|
||||
if os.path.isfile(sys.argv[1]):
|
||||
fd = open(sys.argv[1], 'rb')
|
||||
buffer = fd.read()
|
||||
fd.close()
|
||||
else:
|
||||
buffer = sys.argv[1]
|
||||
# default case, read from stdin
|
||||
else:
|
||||
buffer = sys.stdin.buffer.read()
|
||||
if sys.version_info < (3,):
|
||||
buffer = sys.stdin.read()
|
||||
else:
|
||||
buffer = sys.stdin.buffer.read()
|
||||
|
||||
try:
|
||||
params = json.loads(buffer.decode('utf-8'))
|
||||
except ValueError:
|
||||
|
||||
Reference in New Issue
Block a user