2018-03-30 10:21:23 +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-03-31 04:37:11 +00:00
|
|
|
Classes to detect each case from [0] and prepare arguments necessary for the
|
|
|
|
corresponding Runner class within the target, including preloading requisite
|
|
|
|
files/modules known missing.
|
2018-03-30 10:21:23 +00:00
|
|
|
|
|
|
|
[0] "Ansible Module Architecture", developing_program_flow_modules.html
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
2018-04-17 16:40:45 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
import json
|
2018-03-31 04:37:11 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2018-06-09 21:11:26 +00:00
|
|
|
import random
|
2018-03-31 04:37:11 +00:00
|
|
|
|
2018-03-30 10:21:23 +00:00
|
|
|
from ansible.executor import module_common
|
2018-03-31 04:37:11 +00:00
|
|
|
import ansible.errors
|
2018-05-12 13:14:35 +00:00
|
|
|
import ansible.module_utils
|
2018-04-17 16:40:45 +00:00
|
|
|
import mitogen.core
|
2018-03-31 04:37:11 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from ansible.plugins.loader import module_loader
|
2018-05-12 13:14:35 +00:00
|
|
|
from ansible.plugins.loader import module_utils_loader
|
2018-03-31 04:37:11 +00:00
|
|
|
except ImportError: # Ansible <2.4
|
|
|
|
from ansible.plugins import module_loader
|
2018-05-12 13:14:35 +00:00
|
|
|
from ansible.plugins import module_utils_loader
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-04-06 16:22:45 +00:00
|
|
|
import ansible_mitogen.target
|
2018-03-30 10:21:23 +00:00
|
|
|
|
|
|
|
|
2018-03-31 04:37:11 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2018-04-02 10:35:33 +00:00
|
|
|
NO_METHOD_MSG = 'Mitogen: no invocation method found for: '
|
|
|
|
NO_INTERPRETER_MSG = 'module (%s) is missing interpreter line'
|
2018-03-31 04:37:11 +00:00
|
|
|
|
|
|
|
|
2018-04-01 17:19:34 +00:00
|
|
|
def parse_script_interpreter(source):
|
|
|
|
"""
|
|
|
|
Extract the script interpreter and its sole argument from the module
|
|
|
|
source code.
|
|
|
|
|
|
|
|
:returns:
|
|
|
|
Tuple of `(interpreter, arg)`, where `intepreter` is the script
|
2018-04-02 10:35:33 +00:00
|
|
|
interpreter and `arg` is its sole argument if present, otherwise
|
2018-04-01 17:19:34 +00:00
|
|
|
:py:data:`None`.
|
|
|
|
"""
|
|
|
|
# Linux requires first 2 bytes with no whitespace, pretty sure it's the
|
|
|
|
# same everywhere. See binfmt_script.c.
|
2018-04-17 16:40:45 +00:00
|
|
|
if not source.startswith(b'#!'):
|
2018-04-01 17:19:34 +00:00
|
|
|
return None, None
|
|
|
|
|
|
|
|
# Find terminating newline. Assume last byte of binprm_buf if absent.
|
2018-04-17 16:40:45 +00:00
|
|
|
nl = source.find(b'\n', 0, 128)
|
2018-04-01 17:19:34 +00:00
|
|
|
if nl == -1:
|
|
|
|
nl = min(128, len(source))
|
|
|
|
|
|
|
|
# Split once on the first run of whitespace. If no whitespace exists,
|
|
|
|
# bits just contains the interpreter filename.
|
|
|
|
bits = source[2:nl].strip().split(None, 1)
|
|
|
|
if len(bits) == 1:
|
2018-04-17 16:40:45 +00:00
|
|
|
return mitogen.core.to_text(bits[0]), None
|
|
|
|
return mitogen.core.to_text(bits[0]), mitogen.core.to_text(bits[1])
|
2018-04-01 17:19:34 +00:00
|
|
|
|
|
|
|
|
2018-03-31 04:37:11 +00:00
|
|
|
class Invocation(object):
|
|
|
|
"""
|
|
|
|
Collect up a module's execution environment then use it to invoke
|
2018-04-06 16:22:45 +00:00
|
|
|
target.run_module() or helpers.run_module_async() in the target context.
|
2018-03-31 04:37:11 +00:00
|
|
|
"""
|
|
|
|
def __init__(self, action, connection, module_name, module_args,
|
2018-06-10 01:11:41 +00:00
|
|
|
task_vars, templar, env, wrap_async, timeout_secs):
|
2018-04-01 10:24:57 +00:00
|
|
|
#: ActionBase instance invoking the module. Required to access some
|
|
|
|
#: output postprocessing methods that don't belong in ActionBase at
|
|
|
|
#: all.
|
2018-03-31 04:37:11 +00:00
|
|
|
self.action = action
|
2018-04-01 10:32:45 +00:00
|
|
|
#: Ansible connection to use to contact the target. Must be an
|
|
|
|
#: ansible_mitogen connection.
|
2018-03-31 04:37:11 +00:00
|
|
|
self.connection = connection
|
2018-04-01 10:32:45 +00:00
|
|
|
#: Name of the module ('command', 'shell', etc.) to execute.
|
2018-03-31 04:37:11 +00:00
|
|
|
self.module_name = module_name
|
2018-04-01 10:32:45 +00:00
|
|
|
#: Final module arguments.
|
2018-03-31 04:37:11 +00:00
|
|
|
self.module_args = module_args
|
2018-04-01 17:19:34 +00:00
|
|
|
#: Task variables, needed to extract ansible_*_interpreter.
|
|
|
|
self.task_vars = task_vars
|
|
|
|
#: Templar, needed to extract ansible_*_interpreter.
|
|
|
|
self.templar = templar
|
2018-04-01 10:32:45 +00:00
|
|
|
#: Final module environment.
|
2018-03-31 04:37:11 +00:00
|
|
|
self.env = env
|
2018-04-01 10:32:45 +00:00
|
|
|
#: Boolean, if :py:data:`True`, launch the module asynchronously.
|
2018-03-31 04:37:11 +00:00
|
|
|
self.wrap_async = wrap_async
|
2018-06-10 01:11:41 +00:00
|
|
|
#: Integer, if >0, limit the time an asynchronous job may run for.
|
|
|
|
self.timeout_secs = timeout_secs
|
2018-04-01 10:32:45 +00:00
|
|
|
#: Initially ``None``, but set by :func:`invoke`. The path on the
|
|
|
|
#: master to the module's implementation file.
|
|
|
|
self.module_path = None
|
|
|
|
#: Initially ``None``, but set by :func:`invoke`. The raw source or
|
|
|
|
#: binary contents of the module.
|
|
|
|
self.module_source = None
|
|
|
|
|
2018-03-31 04:37:11 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return 'Invocation(module_name=%s)' % (self.module_name,)
|
|
|
|
|
|
|
|
|
2018-03-30 10:21:23 +00:00
|
|
|
class Planner(object):
|
|
|
|
"""
|
|
|
|
A Planner receives a module name and the contents of its implementation
|
|
|
|
file, indicates whether or not it understands how to run the module, and
|
|
|
|
exports a method to run the module.
|
|
|
|
"""
|
2018-06-09 21:11:26 +00:00
|
|
|
def __init__(self, invocation):
|
|
|
|
self._inv = invocation
|
|
|
|
|
|
|
|
def detect(self):
|
2018-04-01 21:31:45 +00:00
|
|
|
"""
|
|
|
|
Return true if the supplied `invocation` matches the module type
|
|
|
|
implemented by this planner.
|
|
|
|
"""
|
2018-03-31 04:37:11 +00:00
|
|
|
raise NotImplementedError()
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def should_fork(self):
|
2018-04-09 02:20:48 +00:00
|
|
|
"""
|
|
|
|
Asynchronous tasks must always be forked.
|
|
|
|
"""
|
2018-06-09 21:11:26 +00:00
|
|
|
return self._inv.wrap_async
|
|
|
|
|
|
|
|
def get_push_files(self):
|
|
|
|
"""
|
|
|
|
Return a list of files that should be propagated to the target context
|
|
|
|
using PushFileService. The default implementation pushes nothing.
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_module_deps(self):
|
|
|
|
"""
|
|
|
|
Return a list of the Python module names imported by the module.
|
|
|
|
"""
|
|
|
|
return []
|
2018-04-08 22:50:57 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_kwargs(self, **kwargs):
|
2018-04-01 21:31:45 +00:00
|
|
|
"""
|
|
|
|
If :meth:`detect` returned :data:`True`, plan for the module's
|
|
|
|
execution, including granting access to or delivering any files to it
|
|
|
|
that are known to be absent, and finally return a dict::
|
|
|
|
|
|
|
|
{
|
|
|
|
# Name of the class from runners.py that implements the
|
|
|
|
# target-side execution of this module type.
|
|
|
|
"runner_name": "...",
|
|
|
|
|
|
|
|
# Remaining keys are passed to the constructor of the class
|
|
|
|
# named by `runner_name`.
|
|
|
|
}
|
|
|
|
"""
|
2018-04-17 16:40:45 +00:00
|
|
|
new = dict((mitogen.core.UnicodeType(k), kwargs[k])
|
|
|
|
for k in kwargs)
|
|
|
|
new.setdefault('emulate_tty', True)
|
|
|
|
new.setdefault('service_context', self._inv.connection.parent)
|
|
|
|
return new
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-04-21 17:19:55 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '%s()' % (type(self).__name__,)
|
|
|
|
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-04-01 10:32:45 +00:00
|
|
|
class BinaryPlanner(Planner):
|
2018-03-30 10:21:23 +00:00
|
|
|
"""
|
2018-04-01 10:32:45 +00:00
|
|
|
Binary modules take their arguments and will return data to Ansible in the
|
|
|
|
same way as want JSON modules.
|
2018-03-30 10:21:23 +00:00
|
|
|
"""
|
2018-04-01 10:32:45 +00:00
|
|
|
runner_name = 'BinaryRunner'
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def detect(self):
|
|
|
|
return module_common._is_binary(self._inv.module_source)
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_push_files(self):
|
|
|
|
return [self._inv.module_path]
|
2018-05-13 00:47:11 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_kwargs(self, **kwargs):
|
|
|
|
return super(BinaryPlanner, self).get_kwargs(
|
2018-04-08 22:50:57 +00:00
|
|
|
runner_name=self.runner_name,
|
2018-06-09 21:11:26 +00:00
|
|
|
module=self._inv.module_name,
|
|
|
|
path=self._inv.module_path,
|
2018-04-17 16:40:45 +00:00
|
|
|
json_args=json.dumps(self._inv.module_args),
|
2018-06-09 21:11:26 +00:00
|
|
|
env=self._inv.env,
|
2018-04-08 22:50:57 +00:00
|
|
|
**kwargs
|
2018-04-01 10:32:45 +00:00
|
|
|
)
|
2018-03-30 10:21:23 +00:00
|
|
|
|
|
|
|
|
2018-04-01 17:19:34 +00:00
|
|
|
class ScriptPlanner(BinaryPlanner):
|
|
|
|
"""
|
|
|
|
Common functionality for script module planners -- handle interpreter
|
|
|
|
detection and rewrite.
|
|
|
|
"""
|
2018-06-09 21:11:26 +00:00
|
|
|
def _get_interpreter(self):
|
|
|
|
interpreter, arg = parse_script_interpreter(
|
|
|
|
self._inv.module_source
|
|
|
|
)
|
2018-04-02 08:08:14 +00:00
|
|
|
if interpreter is None:
|
|
|
|
raise ansible.errors.AnsibleError(NO_INTERPRETER_MSG % (
|
2018-06-09 21:11:26 +00:00
|
|
|
self._inv.module_name,
|
2018-04-02 08:08:14 +00:00
|
|
|
))
|
|
|
|
|
2018-04-21 19:55:40 +00:00
|
|
|
key = u'ansible_%s_interpreter' % os.path.basename(interpreter).strip()
|
|
|
|
try:
|
2018-06-09 21:11:26 +00:00
|
|
|
template = self._inv.task_vars[key].strip()
|
|
|
|
return self._inv.templar.template(template), arg
|
2018-04-21 19:55:40 +00:00
|
|
|
except KeyError:
|
|
|
|
return interpreter, arg
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_kwargs(self, **kwargs):
|
|
|
|
interpreter, arg = self._get_interpreter()
|
|
|
|
return super(ScriptPlanner, self).get_kwargs(
|
2018-04-01 21:04:05 +00:00
|
|
|
interpreter_arg=arg,
|
2018-04-21 19:55:40 +00:00
|
|
|
interpreter=interpreter,
|
2018-04-08 22:50:57 +00:00
|
|
|
**kwargs
|
2018-04-01 17:19:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class JsonArgsPlanner(ScriptPlanner):
|
2018-03-30 10:21:23 +00:00
|
|
|
"""
|
2018-04-01 10:32:45 +00:00
|
|
|
Script that has its interpreter directive and the task arguments
|
|
|
|
substituted into its source as a JSON string.
|
2018-03-30 10:21:23 +00:00
|
|
|
"""
|
2018-04-01 10:32:45 +00:00
|
|
|
runner_name = 'JsonArgsRunner'
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def detect(self):
|
|
|
|
return module_common.REPLACER_JSONARGS in self._inv.module_source
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-04-01 10:32:45 +00:00
|
|
|
|
2018-04-01 17:19:34 +00:00
|
|
|
class WantJsonPlanner(ScriptPlanner):
|
2018-04-01 10:32:45 +00:00
|
|
|
"""
|
|
|
|
If a module has the string WANT_JSON in it anywhere, Ansible treats it as a
|
|
|
|
non-native module that accepts a filename as its only command line
|
|
|
|
parameter. The filename is for a temporary file containing a JSON string
|
|
|
|
containing the module's parameters. The module needs to open the file, read
|
|
|
|
and parse the parameters, operate on the data, and print its return data as
|
|
|
|
a JSON encoded dictionary to stdout before exiting.
|
|
|
|
|
|
|
|
These types of modules are self-contained entities. As of Ansible 2.1,
|
|
|
|
Ansible only modifies them to change a shebang line if present.
|
|
|
|
"""
|
|
|
|
runner_name = 'WantJsonRunner'
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def detect(self):
|
2018-04-17 16:40:45 +00:00
|
|
|
return b'WANT_JSON' in self._inv.module_source
|
2018-03-30 10:21:23 +00:00
|
|
|
|
|
|
|
|
2018-04-01 20:10:04 +00:00
|
|
|
class NewStylePlanner(ScriptPlanner):
|
2018-03-30 10:21:23 +00:00
|
|
|
"""
|
|
|
|
The Ansiballz framework differs from module replacer in that it uses real
|
|
|
|
Python imports of things in ansible/module_utils instead of merely
|
|
|
|
preprocessing the module.
|
|
|
|
"""
|
2018-04-01 18:01:18 +00:00
|
|
|
runner_name = 'NewStyleRunner'
|
2018-04-17 16:40:45 +00:00
|
|
|
marker = b'from ansible.module_utils.'
|
2018-04-01 18:01:18 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def detect(self):
|
2018-04-17 16:40:45 +00:00
|
|
|
return self.marker in self._inv.module_source
|
2018-06-09 21:11:26 +00:00
|
|
|
|
|
|
|
def _get_interpreter(self):
|
2018-04-21 19:55:40 +00:00
|
|
|
return None, None
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_push_files(self):
|
|
|
|
return super(NewStylePlanner, self).get_push_files() + [
|
|
|
|
path
|
|
|
|
for fullname, path, is_pkg in self.get_module_map()['custom']
|
|
|
|
]
|
2018-05-13 00:47:11 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_module_deps(self):
|
|
|
|
return self.get_module_map()['builtin']
|
|
|
|
|
2018-06-22 03:43:28 +00:00
|
|
|
#: Module names appearing in this set always require forking, usually due
|
|
|
|
#: to some terminal leakage that cannot be worked around in any sane
|
|
|
|
#: manner.
|
|
|
|
ALWAYS_FORK_MODULES = frozenset([
|
|
|
|
'dnf', # issue #280; py-dnf/hawkey need therapy
|
|
|
|
])
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def should_fork(self):
|
2018-04-09 02:20:48 +00:00
|
|
|
"""
|
|
|
|
In addition to asynchronous tasks, new-style modules should be forked
|
2018-06-22 03:43:28 +00:00
|
|
|
if:
|
|
|
|
|
|
|
|
* the user specifies mitogen_task_isolation=fork, or
|
|
|
|
* the new-style module has a custom module search path, or
|
|
|
|
* the module is known to leak like a sieve.
|
2018-04-09 02:20:48 +00:00
|
|
|
"""
|
2018-04-08 22:50:57 +00:00
|
|
|
return (
|
2018-06-09 21:11:26 +00:00
|
|
|
super(NewStylePlanner, self).should_fork() or
|
|
|
|
(self._inv.task_vars.get('mitogen_task_isolation') == 'fork') or
|
2018-06-22 03:43:28 +00:00
|
|
|
(self._inv.module_name in self.ALWAYS_FORK_MODULES) or
|
2018-06-09 21:11:26 +00:00
|
|
|
(len(self.get_module_map()['custom']) > 0)
|
2018-04-08 22:50:57 +00:00
|
|
|
)
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_search_path(self):
|
2018-05-13 02:18:22 +00:00
|
|
|
return tuple(
|
2018-05-12 13:14:35 +00:00
|
|
|
path
|
|
|
|
for path in module_utils_loader._get_paths(subdirs=False)
|
|
|
|
if os.path.isdir(path)
|
2018-05-13 02:18:22 +00:00
|
|
|
)
|
2018-05-12 13:14:35 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
_module_map = None
|
2018-05-28 04:09:19 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def get_module_map(self):
|
|
|
|
if self._module_map is None:
|
|
|
|
self._module_map = self._inv.connection.parent.call_service(
|
|
|
|
service_name='ansible_mitogen.services.ModuleDepService',
|
|
|
|
method_name='scan',
|
2018-05-12 13:14:35 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
module_name='ansible_module_%s' % (self._inv.module_name,),
|
|
|
|
module_path=self._inv.module_path,
|
|
|
|
search_path=self.get_search_path(),
|
|
|
|
builtin_path=module_common._MODULE_UTILS_PATH,
|
|
|
|
context=self._inv.connection.context,
|
2018-06-07 15:48:42 +00:00
|
|
|
)
|
2018-06-09 21:11:26 +00:00
|
|
|
return self._module_map
|
|
|
|
|
|
|
|
def get_kwargs(self):
|
|
|
|
return super(NewStylePlanner, self).get_kwargs(
|
|
|
|
module_map=self.get_module_map(),
|
2018-05-12 13:14:35 +00:00
|
|
|
)
|
|
|
|
|
2018-03-30 10:21:23 +00:00
|
|
|
|
2018-04-01 18:01:18 +00:00
|
|
|
class ReplacerPlanner(NewStylePlanner):
|
2018-05-12 13:09:54 +00:00
|
|
|
"""
|
|
|
|
The Module Replacer framework is the original framework implementing
|
|
|
|
new-style modules. It is essentially a preprocessor (like the C
|
|
|
|
Preprocessor for those familiar with that programming language). It does
|
|
|
|
straight substitutions of specific substring patterns in the module file.
|
|
|
|
There are two types of substitutions.
|
|
|
|
|
|
|
|
* Replacements that only happen in the module file. These are public
|
|
|
|
replacement strings that modules can utilize to get helpful boilerplate
|
|
|
|
or access to arguments.
|
|
|
|
|
|
|
|
"from ansible.module_utils.MOD_LIB_NAME import *" is replaced with the
|
|
|
|
contents of the ansible/module_utils/MOD_LIB_NAME.py. These should only
|
|
|
|
be used with new-style Python modules.
|
|
|
|
|
|
|
|
"#<<INCLUDE_ANSIBLE_MODULE_COMMON>>" is equivalent to
|
|
|
|
"from ansible.module_utils.basic import *" and should also only apply to
|
|
|
|
new-style Python modules.
|
|
|
|
|
|
|
|
"# POWERSHELL_COMMON" substitutes the contents of
|
|
|
|
"ansible/module_utils/powershell.ps1". It should only be used with
|
|
|
|
new-style Powershell modules.
|
|
|
|
"""
|
2018-04-01 18:01:18 +00:00
|
|
|
runner_name = 'ReplacerRunner'
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def detect(self):
|
|
|
|
return module_common.REPLACER in self._inv.module_source
|
2018-04-01 18:01:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OldStylePlanner(ScriptPlanner):
|
|
|
|
runner_name = 'OldStyleRunner'
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def detect(self):
|
2018-04-01 18:01:18 +00:00
|
|
|
# Everything else.
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-03-30 10:21:23 +00:00
|
|
|
_planners = [
|
2018-04-01 18:01:18 +00:00
|
|
|
BinaryPlanner,
|
|
|
|
# ReplacerPlanner,
|
|
|
|
NewStylePlanner,
|
2018-04-01 20:20:58 +00:00
|
|
|
JsonArgsPlanner,
|
2018-04-01 17:19:34 +00:00
|
|
|
WantJsonPlanner,
|
2018-04-01 18:01:18 +00:00
|
|
|
OldStylePlanner,
|
2018-03-30 10:21:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-03-31 04:37:11 +00:00
|
|
|
def get_module_data(name):
|
|
|
|
path = module_loader.find_plugin(name, '')
|
|
|
|
with open(path, 'rb') as fp:
|
|
|
|
source = fp.read()
|
2018-04-17 16:40:45 +00:00
|
|
|
return mitogen.core.to_text(path), source
|
2018-03-31 04:37:11 +00:00
|
|
|
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def _propagate_deps(invocation, planner, context):
|
|
|
|
invocation.connection.parent.call_service(
|
|
|
|
service_name='mitogen.service.PushFileService',
|
|
|
|
method_name='propagate_paths_and_modules',
|
|
|
|
context=context,
|
|
|
|
paths=planner.get_push_files(),
|
|
|
|
modules=planner.get_module_deps(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _invoke_async_task(invocation, planner):
|
|
|
|
job_id = '%016x' % random.randint(0, 2**64)
|
|
|
|
context = invocation.connection.create_fork_child()
|
|
|
|
_propagate_deps(invocation, planner, context)
|
|
|
|
context.call_no_reply(
|
|
|
|
ansible_mitogen.target.run_module_async,
|
|
|
|
job_id=job_id,
|
2018-06-10 01:11:41 +00:00
|
|
|
timeout_secs=invocation.timeout_secs,
|
2018-06-09 21:11:26 +00:00
|
|
|
kwargs=planner.get_kwargs(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'stdout': json.dumps({
|
|
|
|
# modules/utilities/logic/async_wrapper.py::_run_module().
|
|
|
|
'changed': True,
|
|
|
|
'started': 1,
|
|
|
|
'finished': 0,
|
|
|
|
'ansible_job_id': job_id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _invoke_forked_task(invocation, planner):
|
|
|
|
context = invocation.connection.create_fork_child()
|
|
|
|
_propagate_deps(invocation, planner, context)
|
|
|
|
try:
|
|
|
|
return context.call(
|
|
|
|
ansible_mitogen.target.run_module,
|
|
|
|
kwargs=planner.get_kwargs(),
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
context.shutdown()
|
|
|
|
|
2018-03-31 04:37:11 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def _get_planner(invocation):
|
2018-03-31 04:37:11 +00:00
|
|
|
for klass in _planners:
|
2018-06-09 21:11:26 +00:00
|
|
|
planner = klass(invocation)
|
|
|
|
if planner.detect():
|
2018-04-17 16:02:52 +00:00
|
|
|
LOG.debug('%r accepted %r (filename %r)', planner,
|
|
|
|
invocation.module_name, invocation.module_path)
|
2018-06-09 21:11:26 +00:00
|
|
|
return planner
|
2018-04-17 15:16:24 +00:00
|
|
|
LOG.debug('%r rejected %r', planner, invocation.module_name)
|
2018-04-20 13:20:05 +00:00
|
|
|
raise ansible.errors.AnsibleError(NO_METHOD_MSG + repr(invocation))
|
2018-06-09 21:11:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def invoke(invocation):
|
|
|
|
"""
|
|
|
|
Find a Planner subclass corresnding to `invocation` and use it to invoke
|
|
|
|
the module.
|
|
|
|
|
|
|
|
:param Invocation invocation:
|
|
|
|
:returns:
|
|
|
|
Module return dict.
|
|
|
|
:raises ansible.errors.AnsibleError:
|
|
|
|
Unrecognized/unsupported module type.
|
|
|
|
"""
|
|
|
|
(invocation.module_path,
|
|
|
|
invocation.module_source) = get_module_data(invocation.module_name)
|
|
|
|
planner = _get_planner(invocation)
|
|
|
|
|
|
|
|
if invocation.wrap_async:
|
|
|
|
response = _invoke_async_task(invocation, planner)
|
|
|
|
elif planner.should_fork():
|
|
|
|
response = _invoke_forked_task(invocation, planner)
|
|
|
|
else:
|
|
|
|
_propagate_deps(invocation, planner, invocation.connection.context)
|
|
|
|
response = invocation.connection.call(
|
|
|
|
ansible_mitogen.target.run_module,
|
|
|
|
kwargs=planner.get_kwargs(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return invocation.action._postprocess_response(response)
|