diff --git a/ansible_mitogen/runner.py b/ansible_mitogen/runner.py index b602065a..30c0ca7b 100644 --- a/ansible_mitogen/runner.py +++ b/ansible_mitogen/runner.py @@ -114,8 +114,9 @@ class Runner(object): def setup(self): """ - Prepare the current process for running a module. The base - implementation simply prepares the environment. + Prepare for running a module, including fetching necessary dependencies + from the parent, as :meth:`run` may detach prior to beginning + execution. The base implementation simply prepares the environment. """ self._env = TemporaryEnvironment(self.env) @@ -234,6 +235,14 @@ class ProgramRunner(Runner): super(ProgramRunner, self).setup() self._setup_program() + def _get_program_filename(self): + """ + Return the filename used for program on disk. Ansible uses the original + filename for non-Ansiballz runs, and "ansible_module_+filename for + Ansiballz runs. + """ + return os.path.basename(self.path) + program_fp = None def _setup_program(self): @@ -241,8 +250,8 @@ class ProgramRunner(Runner): Create a temporary file containing the program code. The code is fetched via :meth:`_get_program`. """ - name = 'ansible_module_' + os.path.basename(self.path) - path = os.path.join(ansible_mitogen.target.temp_dir, name) + filename = self._get_program_filename() + path = os.path.join(ansible_mitogen.target.temp_dir, filename) self.program_fp = open(path, 'wb') self.program_fp.write(self._get_program()) self.program_fp.flush() @@ -394,6 +403,12 @@ class NewStyleRunner(ScriptRunner): self._stdio.revert() super(NewStyleRunner, self).revert() + def _get_program_filename(self): + """ + See ProgramRunner._get_program_filename(). + """ + return 'ansible_module_' + os.path.basename(self.path) + def _setup_args(self): pass diff --git a/ansible_mitogen/target.py b/ansible_mitogen/target.py index 6259bdec..5dc1dfa7 100644 --- a/ansible_mitogen/target.py +++ b/ansible_mitogen/target.py @@ -235,6 +235,7 @@ def _on_broker_shutdown(): prune_tree(temp_dir) +@mitogen.core.takes_econtext def reset_temp_dir(econtext): """ Create one temporary directory to be reused by all runner.py invocations @@ -277,6 +278,7 @@ def init_child(econtext): def start_fork_child(wrap_async, kwargs, econtext): mitogen.parent.upgrade_router(econtext) context = econtext.router.fork() + context.call(reset_temp_dir) if not wrap_async: try: return context.call(run_module, kwargs) @@ -386,7 +388,6 @@ def run_module_async(job_id, kwargs, econtext): """ try: try: - reset_temp_dir(econtext) _run_module_async(job_id, kwargs, econtext) except Exception: LOG.exception('_run_module_async crashed') diff --git a/tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py b/tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py index 4d1bb23f..26022ff3 100644 --- a/tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py +++ b/tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py @@ -11,16 +11,10 @@ def usage(): sys.stderr.write('Usage: %s \n' % (sys.argv[0],)) sys.exit(1) -# Also must slurp in our own source code, to verify the encoding string was -# added. -with open(sys.argv[0]) as fp: - me = fp.read() - input_json = sys.stdin.read() print "{" print " \"changed\": false," print " \"msg\": \"Here is my input\"," -print " \"source\": [%s]," % (json.dumps(me),) print " \"input\": [%s]" % (input_json,) print "}" diff --git a/tests/ansible/lib/modules/custom_python_new_style_module.py b/tests/ansible/lib/modules/custom_python_new_style_module.py index 4bc9794d..69b4dae0 100755 --- a/tests/ansible/lib/modules/custom_python_new_style_module.py +++ b/tests/ansible/lib/modules/custom_python_new_style_module.py @@ -12,11 +12,6 @@ def usage(): sys.stderr.write('Usage: %s \n' % (sys.argv[0],)) sys.exit(1) -# Also must slurp in our own source code, to verify the encoding string was -# added. -with open(sys.argv[0]) as fp: - me = fp.read() - input_json = sys.stdin.read() print "{" @@ -27,6 +22,5 @@ print " \"__file__\": \"%s\"," % (__file__,) # Python sets this during a regular import. print " \"__package__\": \"%s\"," % (__package__,) print " \"msg\": \"Here is my input\"," -print " \"source\": [%s]," % (json.dumps(me),) print " \"input\": [%s]" % (input_json,) print "}"