2018-03-30 10:10:38 +00:00
|
|
|
# Copyright 2017, David Wilson
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
# may be used to endorse or promote products derived from this software without
|
|
|
|
# specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2018-04-01 10:29:52 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
These classes implement execution for each style of Ansible module. They are
|
2018-04-06 16:22:45 +00:00
|
|
|
instantiated in the target context by way of target.py::run_module().
|
2018-04-01 10:29:52 +00:00
|
|
|
|
|
|
|
Each class in here has a corresponding Planner class in planners.py that knows
|
|
|
|
how to build arguments for it, preseed related data, etc.
|
|
|
|
"""
|
|
|
|
|
2018-03-30 10:10:38 +00:00
|
|
|
from __future__ import absolute_import
|
2018-04-01 20:10:04 +00:00
|
|
|
import cStringIO
|
2018-03-30 10:10:38 +00:00
|
|
|
import json
|
2018-04-01 17:19:34 +00:00
|
|
|
import logging
|
2018-03-30 10:10:38 +00:00
|
|
|
import os
|
2018-04-04 13:05:57 +00:00
|
|
|
import shutil
|
2018-04-01 20:10:04 +00:00
|
|
|
import sys
|
2018-03-30 10:10:38 +00:00
|
|
|
import tempfile
|
2018-04-01 20:10:04 +00:00
|
|
|
import types
|
2018-03-30 10:10:38 +00:00
|
|
|
|
2018-04-08 22:50:57 +00:00
|
|
|
import mitogen.service
|
2018-04-06 16:22:45 +00:00
|
|
|
import ansible_mitogen.target # TODO: circular import
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from shlex import quote as shlex_quote
|
|
|
|
except ImportError:
|
|
|
|
from pipes import quote as shlex_quote
|
|
|
|
|
|
|
|
# Prevent accidental import of an Ansible module from hanging on stdin read.
|
|
|
|
import ansible.module_utils.basic
|
|
|
|
ansible.module_utils.basic._ANSIBLE_ARGS = '{}'
|
|
|
|
|
|
|
|
|
2018-04-01 17:19:34 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-04-20 19:47:07 +00:00
|
|
|
def utf8(s):
|
|
|
|
"""
|
|
|
|
Coerce an object to bytes if it is Unicode.
|
|
|
|
"""
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
s = s.encode('utf-8')
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2018-04-02 07:59:36 +00:00
|
|
|
def reopen_readonly(fp):
|
|
|
|
"""
|
|
|
|
Replace the file descriptor belonging to the file object `fp` with one
|
|
|
|
open on the same file (`fp.name`), but opened with :py:data:`os.O_RDONLY`.
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
This enables temporary files to be executed on Linux, which usually throws
|
2018-04-02 07:59:36 +00:00
|
|
|
``ETXTBUSY`` if any writeable handle exists pointing to a file passed to
|
|
|
|
`execve()`.
|
|
|
|
"""
|
|
|
|
fd = os.open(fp.name, os.O_RDONLY)
|
|
|
|
os.dup2(fd, fp.fileno())
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
|
2018-03-31 04:37:11 +00:00
|
|
|
class Runner(object):
|
|
|
|
"""
|
|
|
|
Ansible module runner. After instantiation (with kwargs supplied by the
|
|
|
|
corresponding Planner), `.run()` is invoked, upon which `setup()`,
|
|
|
|
`_run()`, and `revert()` are invoked, with the return value of `_run()`
|
|
|
|
returned by `run()`.
|
|
|
|
|
|
|
|
Subclasses may override `_run`()` and extend `setup()` and `revert()`.
|
|
|
|
"""
|
2018-05-04 14:30:51 +00:00
|
|
|
def __init__(self, module, service_context, emulate_tty=None,
|
2018-05-04 15:35:35 +00:00
|
|
|
args=None, env=None):
|
2018-03-31 04:37:11 +00:00
|
|
|
if args is None:
|
|
|
|
args = {}
|
|
|
|
|
2018-04-21 19:09:38 +00:00
|
|
|
self.module = utf8(module)
|
2018-04-08 22:50:57 +00:00
|
|
|
self.service_context = service_context
|
2018-04-20 13:20:05 +00:00
|
|
|
self.emulate_tty = emulate_tty
|
2018-03-31 04:37:11 +00:00
|
|
|
self.args = args
|
|
|
|
self.env = env
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""
|
|
|
|
Prepare the current process for running a module. The base
|
|
|
|
implementation simply prepares the environment.
|
|
|
|
"""
|
|
|
|
self._env = TemporaryEnvironment(self.env)
|
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
"""
|
|
|
|
Revert any changes made to the process after running a module. The base
|
|
|
|
implementation simply restores the original environment.
|
|
|
|
"""
|
|
|
|
self._env.revert()
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
self._cleanup_temp()
|
|
|
|
|
|
|
|
def _cleanup_temp(self):
|
|
|
|
for name in os.listdir(ansible_mitogen.target.temp_dir):
|
|
|
|
if name in ('.', '..'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
path = os.path.join(ansible_mitogen.target.temp_dir, name)
|
|
|
|
LOG.debug('Deleting %r', path)
|
|
|
|
ansible_mitogen.target.prune_tree(path)
|
2018-03-31 04:37:11 +00:00
|
|
|
|
|
|
|
def _run(self):
|
2018-04-01 11:56:53 +00:00
|
|
|
"""
|
|
|
|
The _run() method is expected to return a dictionary in the form of
|
|
|
|
ActionBase._low_level_execute_command() output, i.e. having::
|
|
|
|
|
|
|
|
{
|
|
|
|
"rc": int,
|
|
|
|
"stdout": "stdout data",
|
|
|
|
"stderr": "stderr data"
|
|
|
|
}
|
|
|
|
"""
|
2018-03-31 04:37:11 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Set up the process environment in preparation for running an Ansible
|
|
|
|
module. This monkey-patches the Ansible libraries in various places to
|
|
|
|
prevent it from trying to kill the process on completion, and to
|
|
|
|
prevent it from reading sys.stdin.
|
|
|
|
|
|
|
|
:returns:
|
|
|
|
Module result dictionary.
|
|
|
|
"""
|
|
|
|
self.setup()
|
|
|
|
try:
|
2018-04-20 13:20:05 +00:00
|
|
|
return self._run()
|
2018-03-31 04:37:11 +00:00
|
|
|
finally:
|
|
|
|
self.revert()
|
|
|
|
|
|
|
|
|
2018-03-30 10:10:38 +00:00
|
|
|
class TemporaryEnvironment(object):
|
|
|
|
def __init__(self, env=None):
|
|
|
|
self.original = os.environ.copy()
|
|
|
|
self.env = env or {}
|
|
|
|
os.environ.update((k, str(v)) for k, v in self.env.iteritems())
|
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(self.original)
|
|
|
|
|
|
|
|
|
2018-04-01 20:10:04 +00:00
|
|
|
class TemporaryArgv(object):
|
|
|
|
def __init__(self, argv):
|
|
|
|
self.original = sys.argv[:]
|
2018-05-04 00:50:48 +00:00
|
|
|
sys.argv[:] = map(str, argv)
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
def revert(self):
|
2018-04-01 20:10:04 +00:00
|
|
|
sys.argv[:] = self.original
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
|
2018-04-01 20:10:04 +00:00
|
|
|
class NewStyleStdio(object):
|
2018-03-30 10:10:38 +00:00
|
|
|
"""
|
2018-03-30 17:20:17 +00:00
|
|
|
Patch ansible.module_utils.basic argument globals.
|
2018-03-30 10:10:38 +00:00
|
|
|
"""
|
|
|
|
def __init__(self, args):
|
2018-04-01 20:10:04 +00:00
|
|
|
self.original_stdout = sys.stdout
|
|
|
|
self.original_stderr = sys.stderr
|
|
|
|
self.original_stdin = sys.stdin
|
|
|
|
sys.stdout = cStringIO.StringIO()
|
|
|
|
sys.stderr = cStringIO.StringIO()
|
2018-03-30 10:10:38 +00:00
|
|
|
ansible.module_utils.basic._ANSIBLE_ARGS = json.dumps({
|
|
|
|
'ANSIBLE_MODULE_ARGS': args
|
|
|
|
})
|
2018-04-01 20:10:04 +00:00
|
|
|
sys.stdin = cStringIO.StringIO(
|
|
|
|
ansible.module_utils.basic._ANSIBLE_ARGS
|
|
|
|
)
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
def revert(self):
|
2018-04-01 20:10:04 +00:00
|
|
|
sys.stdout = self.original_stdout
|
|
|
|
sys.stderr = self.original_stderr
|
|
|
|
sys.stdin = self.original_stdin
|
|
|
|
ansible.module_utils.basic._ANSIBLE_ARGS = '{}'
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
|
2018-04-01 17:19:34 +00:00
|
|
|
class ProgramRunner(Runner):
|
2018-04-08 22:50:57 +00:00
|
|
|
def __init__(self, path, **kwargs):
|
2018-04-01 17:19:34 +00:00
|
|
|
super(ProgramRunner, self).__init__(**kwargs)
|
2018-03-30 17:20:17 +00:00
|
|
|
self.path = path
|
|
|
|
|
2018-03-30 10:10:38 +00:00
|
|
|
def setup(self):
|
2018-04-01 17:19:34 +00:00
|
|
|
super(ProgramRunner, self).setup()
|
2018-04-01 10:32:45 +00:00
|
|
|
self._setup_program()
|
2018-03-30 10:10:38 +00:00
|
|
|
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
program_fp = None
|
|
|
|
|
2018-03-30 10:10:38 +00:00
|
|
|
def _setup_program(self):
|
|
|
|
"""
|
|
|
|
Create a temporary file containing the program code. The code is
|
2018-04-01 10:32:45 +00:00
|
|
|
fetched via :meth:`_get_program`.
|
2018-03-30 10:10:38 +00:00
|
|
|
"""
|
2018-05-04 00:50:48 +00:00
|
|
|
name = 'ansible_module_' + os.path.basename(self.path)
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
path = os.path.join(ansible_mitogen.target.temp_dir, name)
|
2018-05-04 00:50:48 +00:00
|
|
|
self.program_fp = open(path, 'wb')
|
2018-04-01 17:19:34 +00:00
|
|
|
self.program_fp.write(self._get_program())
|
|
|
|
self.program_fp.flush()
|
|
|
|
os.chmod(self.program_fp.name, int('0700', 8))
|
2018-04-02 07:59:36 +00:00
|
|
|
reopen_readonly(self.program_fp)
|
2018-03-30 10:10:38 +00:00
|
|
|
|
2018-04-01 17:19:34 +00:00
|
|
|
def _get_program(self):
|
2018-03-30 10:10:38 +00:00
|
|
|
"""
|
2018-04-01 17:19:34 +00:00
|
|
|
Fetch the module binary from the master if necessary.
|
2018-03-30 10:10:38 +00:00
|
|
|
"""
|
2018-04-06 16:22:45 +00:00
|
|
|
return ansible_mitogen.target.get_file(
|
2018-04-01 17:19:34 +00:00
|
|
|
context=self.service_context,
|
|
|
|
path=self.path,
|
2018-03-30 10:10:38 +00:00
|
|
|
)
|
2018-04-01 17:19:34 +00:00
|
|
|
|
|
|
|
def _get_program_args(self):
|
ansible: use _ansible_shell_executable in ScriptRunner.
Now we match Ansible error output and exit status. Ansible:
$ ansible localhost -e end=2 -m custom_binary_single_null
localhost | FAILED! => {
"changed": false,
"module_stderr": "Shared connection to localhost closed.\r\n",
"module_stdout": "/bin/sh: /Users/dmw/.ansible/tmp/ansible-tmp-1522661797.42-158833651208060/custom_binary_single_null: cannot execute binary file\r\n",
"msg": "MODULE FAILURE",
"rc": 126
}
Mitogen now:
localhost | FAILED! => {
"changed": false,
"module_stderr": "/bin/sh: /var/folders/gw/f6w3dgy16fsg5y4kdthbqycw0000gn/T/ansible_mitogenAYF8LM-binary: cannot execute binary file\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 126
}
Previously:
localhost | FAILED! => {
"changed": false,
"module_stderr": "<type 'exceptions.OSError'>: [Errno 8] Exec format error",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
2018-04-02 09:37:23 +00:00
|
|
|
return [
|
|
|
|
self.args['_ansible_shell_executable'],
|
|
|
|
'-c',
|
|
|
|
self.program_fp.name
|
|
|
|
]
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
"""
|
2018-04-01 17:19:34 +00:00
|
|
|
Delete the temporary program file.
|
2018-03-30 10:10:38 +00:00
|
|
|
"""
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
if self.program_fp:
|
|
|
|
self.program_fp.close()
|
2018-04-04 13:05:57 +00:00
|
|
|
super(ProgramRunner, self).revert()
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
def _run(self):
|
2018-03-30 17:20:17 +00:00
|
|
|
try:
|
2018-04-06 16:22:45 +00:00
|
|
|
rc, stdout, stderr = ansible_mitogen.target.exec_args(
|
2018-04-01 17:19:34 +00:00
|
|
|
args=self._get_program_args(),
|
2018-04-20 13:20:05 +00:00
|
|
|
emulate_tty=self.emulate_tty,
|
2018-03-30 17:20:17 +00:00
|
|
|
)
|
|
|
|
except Exception, e:
|
ansible: use _ansible_shell_executable in ScriptRunner.
Now we match Ansible error output and exit status. Ansible:
$ ansible localhost -e end=2 -m custom_binary_single_null
localhost | FAILED! => {
"changed": false,
"module_stderr": "Shared connection to localhost closed.\r\n",
"module_stdout": "/bin/sh: /Users/dmw/.ansible/tmp/ansible-tmp-1522661797.42-158833651208060/custom_binary_single_null: cannot execute binary file\r\n",
"msg": "MODULE FAILURE",
"rc": 126
}
Mitogen now:
localhost | FAILED! => {
"changed": false,
"module_stderr": "/bin/sh: /var/folders/gw/f6w3dgy16fsg5y4kdthbqycw0000gn/T/ansible_mitogenAYF8LM-binary: cannot execute binary file\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 126
}
Previously:
localhost | FAILED! => {
"changed": false,
"module_stderr": "<type 'exceptions.OSError'>: [Errno 8] Exec format error",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
2018-04-02 09:37:23 +00:00
|
|
|
LOG.exception('While running %s', self._get_program_args())
|
2018-03-31 04:37:11 +00:00
|
|
|
return {
|
2018-04-01 11:56:53 +00:00
|
|
|
'rc': 1,
|
|
|
|
'stdout': '',
|
|
|
|
'stderr': '%s: %s' % (type(e), e),
|
2018-03-31 04:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'rc': rc,
|
|
|
|
'stdout': stdout,
|
|
|
|
'stderr': stderr
|
|
|
|
}
|
2018-03-30 10:10:38 +00:00
|
|
|
|
|
|
|
|
2018-04-01 17:19:34 +00:00
|
|
|
class ArgsFileRunner(Runner):
|
|
|
|
def setup(self):
|
|
|
|
super(ArgsFileRunner, self).setup()
|
|
|
|
self._setup_args()
|
|
|
|
|
|
|
|
def _setup_args(self):
|
|
|
|
"""
|
|
|
|
Create a temporary file containing the module's arguments. The
|
|
|
|
arguments are formatted via :meth:`_get_args`.
|
|
|
|
"""
|
|
|
|
self.args_fp = tempfile.NamedTemporaryFile(
|
|
|
|
prefix='ansible_mitogen',
|
|
|
|
suffix='-args',
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
dir=ansible_mitogen.target.temp_dir,
|
2018-04-01 17:19:34 +00:00
|
|
|
)
|
|
|
|
self.args_fp.write(self._get_args_contents())
|
|
|
|
self.args_fp.flush()
|
2018-04-02 07:59:36 +00:00
|
|
|
reopen_readonly(self.program_fp)
|
2018-04-01 17:19:34 +00:00
|
|
|
|
|
|
|
def _get_args_contents(self):
|
|
|
|
"""
|
|
|
|
Return the module arguments formatted as JSON.
|
|
|
|
"""
|
|
|
|
return json.dumps(self.args)
|
|
|
|
|
|
|
|
def _get_program_args(self):
|
ansible: use _ansible_shell_executable in ScriptRunner.
Now we match Ansible error output and exit status. Ansible:
$ ansible localhost -e end=2 -m custom_binary_single_null
localhost | FAILED! => {
"changed": false,
"module_stderr": "Shared connection to localhost closed.\r\n",
"module_stdout": "/bin/sh: /Users/dmw/.ansible/tmp/ansible-tmp-1522661797.42-158833651208060/custom_binary_single_null: cannot execute binary file\r\n",
"msg": "MODULE FAILURE",
"rc": 126
}
Mitogen now:
localhost | FAILED! => {
"changed": false,
"module_stderr": "/bin/sh: /var/folders/gw/f6w3dgy16fsg5y4kdthbqycw0000gn/T/ansible_mitogenAYF8LM-binary: cannot execute binary file\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 126
}
Previously:
localhost | FAILED! => {
"changed": false,
"module_stderr": "<type 'exceptions.OSError'>: [Errno 8] Exec format error",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
2018-04-02 09:37:23 +00:00
|
|
|
return [
|
|
|
|
self.args['_ansible_shell_executable'],
|
|
|
|
'-c',
|
|
|
|
"%s %s" % (self.program_fp.name, self.args_fp.name),
|
|
|
|
]
|
2018-04-01 17:19:34 +00:00
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
"""
|
|
|
|
Delete the temporary argument file.
|
|
|
|
"""
|
|
|
|
self.args_fp.close()
|
2018-04-04 13:05:57 +00:00
|
|
|
super(ArgsFileRunner, self).revert()
|
2018-04-01 17:19:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BinaryRunner(ArgsFileRunner, ProgramRunner):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptRunner(ProgramRunner):
|
|
|
|
def __init__(self, interpreter, interpreter_arg, **kwargs):
|
|
|
|
super(ScriptRunner, self).__init__(**kwargs)
|
|
|
|
self.interpreter = interpreter
|
|
|
|
self.interpreter_arg = interpreter_arg
|
|
|
|
|
|
|
|
b_ENCODING_STRING = b'# -*- coding: utf-8 -*-'
|
|
|
|
|
2018-04-01 10:32:45 +00:00
|
|
|
def _get_program(self):
|
2018-04-01 17:19:34 +00:00
|
|
|
return self._rewrite_source(
|
|
|
|
super(ScriptRunner, self)._get_program()
|
|
|
|
)
|
|
|
|
|
|
|
|
def _rewrite_source(self, s):
|
|
|
|
"""
|
|
|
|
Mutate the source according to the per-task parameters.
|
|
|
|
"""
|
|
|
|
# Couldn't find shebang, so let shell run it, because shell assumes
|
|
|
|
# executables like this are just shell scripts.
|
|
|
|
if not self.interpreter:
|
|
|
|
return s
|
|
|
|
|
2018-04-20 19:47:07 +00:00
|
|
|
shebang = '#!' + utf8(self.interpreter)
|
2018-04-01 17:19:34 +00:00
|
|
|
if self.interpreter_arg:
|
2018-04-20 19:47:07 +00:00
|
|
|
shebang += ' ' + utf8(self.interpreter_arg)
|
2018-04-01 17:19:34 +00:00
|
|
|
|
|
|
|
new = [shebang]
|
|
|
|
if os.path.basename(self.interpreter).startswith('python'):
|
|
|
|
new.append(self.b_ENCODING_STRING)
|
|
|
|
|
|
|
|
_, _, rest = s.partition('\n')
|
|
|
|
new.append(rest)
|
|
|
|
return '\n'.join(new)
|
|
|
|
|
|
|
|
|
2018-04-01 20:10:04 +00:00
|
|
|
class NewStyleRunner(ScriptRunner):
|
|
|
|
"""
|
|
|
|
Execute a new-style Ansible module, where Module Replacer-related tricks
|
|
|
|
aren't required.
|
|
|
|
"""
|
2018-04-01 20:40:06 +00:00
|
|
|
#: path => new-style module bytecode.
|
|
|
|
_code_by_path = {}
|
|
|
|
|
2018-04-01 20:10:04 +00:00
|
|
|
def setup(self):
|
|
|
|
super(NewStyleRunner, self).setup()
|
|
|
|
self._stdio = NewStyleStdio(self.args)
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
# It is possible that not supplying the script filename will break some
|
|
|
|
# module, but this has never been a bug report. Instead act like an
|
|
|
|
# interpreter that had its script piped on stdin.
|
|
|
|
self._argv = TemporaryArgv([''])
|
2018-04-01 20:10:04 +00:00
|
|
|
|
|
|
|
def revert(self):
|
2018-04-01 20:40:06 +00:00
|
|
|
self._argv.revert()
|
2018-04-01 20:10:04 +00:00
|
|
|
self._stdio.revert()
|
2018-04-01 20:40:06 +00:00
|
|
|
super(NewStyleRunner, self).revert()
|
2018-04-01 20:10:04 +00:00
|
|
|
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
def _setup_args(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _setup_program(self):
|
|
|
|
pass
|
|
|
|
|
2018-04-01 20:40:06 +00:00
|
|
|
def _get_code(self):
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
self.source = ansible_mitogen.target.get_file(
|
|
|
|
context=self.service_context,
|
|
|
|
path=self.path,
|
|
|
|
)
|
|
|
|
|
2018-04-01 20:40:06 +00:00
|
|
|
try:
|
|
|
|
return self._code_by_path[self.path]
|
|
|
|
except KeyError:
|
|
|
|
return self._code_by_path.setdefault(self.path, compile(
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
source=self.source,
|
2018-04-01 20:40:06 +00:00
|
|
|
filename=self.path,
|
|
|
|
mode='exec',
|
|
|
|
dont_inherit=True,
|
|
|
|
))
|
2018-04-01 20:10:04 +00:00
|
|
|
|
|
|
|
def _run(self):
|
2018-04-01 20:40:06 +00:00
|
|
|
code = self._get_code()
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
|
2018-04-01 20:10:04 +00:00
|
|
|
mod = types.ModuleType('__main__')
|
2018-04-22 12:27:41 +00:00
|
|
|
mod.__package__ = None
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
# Some Ansible modules use __file__ to find the Ansiballz temporary
|
|
|
|
# directory. We must provide some temporary path in __file__, but we
|
|
|
|
# don't want to pointlessly write the module to disk when it never
|
|
|
|
# actually needs to exist. So just pass the filename as it would exist.
|
|
|
|
mod.__file__ = os.path.join(
|
|
|
|
ansible_mitogen.target.temp_dir,
|
|
|
|
'ansible_module_' + os.path.basename(self.path),
|
|
|
|
)
|
2018-04-01 20:10:04 +00:00
|
|
|
e = None
|
|
|
|
|
|
|
|
try:
|
issue #199: ansible: stop writing temp files for new style modules
While adding support for non-new style module types, NewStyleRunner
began writing modules to a temporary file, and sys.argv was patched to
actually include the script filename. The argv change was never required
to fix any particular bug, and a search of the standard modules reveals
no argv users. Update argv[0] to be '', like an interactive interpreter
would have.
While fixing #210, new style runner began setting __file__ to the
temporary file path in order to allow apt.py to discover the Ansiballz
temporary directory. 5 out of 1,516 standard modules follow this
pattern, but in each case, none actually attempt to access __file__,
they just call dirname on it. Therefore do not write the contents of
file, simply set it to the path as it would exist, within a real
temporary directory.
Finally move temporary directory creation out of runner and into target.
Now a single directory exists for the duration of a run, and is emptied
by runner.py as necessary after each task invocation.
This could be further extended to stop rewriting non-new-style modules
in a with_items loop, but that's another step.
Finally the last bullet point in the documentation almost isn't a lie
again.
2018-05-04 05:16:25 +00:00
|
|
|
exec code in vars(mod)
|
2018-04-01 20:10:04 +00:00
|
|
|
except SystemExit, e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return {
|
|
|
|
'rc': e[0] if e else 2,
|
|
|
|
'stdout': sys.stdout.getvalue(),
|
|
|
|
'stderr': sys.stderr.getvalue(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-01 20:20:58 +00:00
|
|
|
class JsonArgsRunner(ScriptRunner):
|
2018-04-01 17:19:34 +00:00
|
|
|
JSON_ARGS = '<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>'
|
|
|
|
|
|
|
|
def _get_args_contents(self):
|
2018-04-01 20:20:58 +00:00
|
|
|
return json.dumps(self.args)
|
2018-04-01 17:19:34 +00:00
|
|
|
|
|
|
|
def _rewrite_source(self, s):
|
|
|
|
return (
|
2018-04-01 20:20:58 +00:00
|
|
|
super(JsonArgsRunner, self)._rewrite_source(s)
|
2018-04-01 17:19:34 +00:00
|
|
|
.replace(self.JSON_ARGS, self._get_args_contents())
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class WantJsonRunner(ArgsFileRunner, ScriptRunner):
|
|
|
|
pass
|
|
|
|
|
2018-03-30 10:10:38 +00:00
|
|
|
|
2018-04-01 18:01:18 +00:00
|
|
|
class OldStyleRunner(ArgsFileRunner, ScriptRunner):
|
2018-04-01 17:19:34 +00:00
|
|
|
def _get_args_contents(self):
|
2018-03-30 10:10:38 +00:00
|
|
|
"""
|
|
|
|
Mimic the argument formatting behaviour of
|
|
|
|
ActionBase._execute_module().
|
|
|
|
"""
|
|
|
|
return ' '.join(
|
|
|
|
'%s=%s' % (key, shlex_quote(str(self.args[key])))
|
|
|
|
for key in self.args
|
2018-04-01 20:10:04 +00:00
|
|
|
) + ' ' # Bug-for-bug :(
|