2018-02-15 09:43:19 +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:
|
|
|
|
#
|
2018-02-27 11:28:26 +00:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2018-02-15 09:43:19 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2018-02-27 11:28:26 +00:00
|
|
|
# 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-02-15 09:43:19 +00:00
|
|
|
|
2018-02-17 11:41:15 +00:00
|
|
|
from __future__ import absolute_import
|
2018-03-08 14:37:08 +00:00
|
|
|
import logging
|
2018-02-15 09:43:19 +00:00
|
|
|
import os
|
2018-02-18 08:08:00 +00:00
|
|
|
import pwd
|
|
|
|
import shutil
|
2018-02-27 11:28:26 +00:00
|
|
|
import traceback
|
2018-02-15 09:43:19 +00:00
|
|
|
|
2018-04-17 16:40:45 +00:00
|
|
|
try:
|
|
|
|
from shlex import quote as shlex_quote
|
|
|
|
except ImportError:
|
|
|
|
from pipes import quote as shlex_quote
|
|
|
|
|
2018-03-10 12:20:40 +00:00
|
|
|
from ansible.module_utils._text import to_bytes
|
|
|
|
from ansible.parsing.utils.jsonify import jsonify
|
|
|
|
|
2018-02-15 09:43:19 +00:00
|
|
|
import ansible
|
2018-04-04 19:19:46 +00:00
|
|
|
import ansible.constants
|
2018-02-17 11:41:15 +00:00
|
|
|
import ansible.plugins
|
2018-02-15 09:43:19 +00:00
|
|
|
import ansible.plugins.action
|
|
|
|
|
2018-02-17 11:41:15 +00:00
|
|
|
import mitogen.core
|
2018-05-24 22:32:07 +00:00
|
|
|
import mitogen.select
|
2018-04-30 14:21:26 +00:00
|
|
|
import mitogen.utils
|
2018-02-17 11:41:15 +00:00
|
|
|
|
2018-02-28 19:00:52 +00:00
|
|
|
import ansible_mitogen.connection
|
2018-03-31 04:37:11 +00:00
|
|
|
import ansible_mitogen.planner
|
2018-04-06 16:22:45 +00:00
|
|
|
import ansible_mitogen.target
|
2018-02-15 09:43:19 +00:00
|
|
|
from ansible.module_utils._text import to_text
|
2018-02-17 11:41:15 +00:00
|
|
|
|
|
|
|
|
2018-03-08 14:37:08 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-02-15 09:43:19 +00:00
|
|
|
class ActionModuleMixin(ansible.plugins.action.ActionBase):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
The Mitogen-patched PluginLoader dynamically mixes this into every action
|
|
|
|
class that Ansible attempts to load. It exists to override all the
|
|
|
|
assumptions built into the base action class that should really belong in
|
|
|
|
some middle layer, or at least in the connection layer.
|
|
|
|
|
|
|
|
Functionality is defined here for:
|
|
|
|
|
|
|
|
* Capturing the final set of task variables and giving Connection a chance
|
|
|
|
to update its idea of the correct execution environment, before any
|
|
|
|
attempt is made to call a Connection method. While it's not expected for
|
|
|
|
the interpreter to change on a per-task basis, Ansible permits this, and
|
|
|
|
so it must be supported.
|
|
|
|
|
|
|
|
* Overriding lots of methods that try to call out to shell for mundane
|
|
|
|
reasons, such as copying files around, changing file permissions,
|
|
|
|
creating temporary directories and suchlike.
|
|
|
|
|
|
|
|
* Short-circuiting any use of Ansiballz or related code for executing a
|
|
|
|
module remotely using shell commands and SSH.
|
|
|
|
|
|
|
|
* Short-circuiting most of the logic in dealing with the fact that Ansible
|
|
|
|
always runs become: tasks across at least the SSH user account and the
|
|
|
|
destination user account, and handling the security permission issues
|
|
|
|
that crop up due to this. Mitogen always runs a task completely within
|
|
|
|
the target user account, so it's not a problem for us.
|
|
|
|
"""
|
2018-02-28 19:00:52 +00:00
|
|
|
def __init__(self, task, connection, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Verify the received connection is really a Mitogen connection. If not,
|
|
|
|
transmute this instance back into the original unadorned base class.
|
|
|
|
|
|
|
|
This allows running the Mitogen strategy in mixed-target playbooks,
|
|
|
|
where some targets use SSH while others use WinRM or some fancier UNIX
|
|
|
|
connection plug-in. That's because when the Mitogen strategy is active,
|
|
|
|
ActionModuleMixin is unconditionally mixed into any action module that
|
|
|
|
is instantiated, and there is no direct way for the monkey-patch to
|
|
|
|
know what kind of connection will be used upfront.
|
|
|
|
"""
|
|
|
|
super(ActionModuleMixin, self).__init__(task, connection, *args, **kwargs)
|
|
|
|
if not isinstance(connection, ansible_mitogen.connection.Connection):
|
|
|
|
_, self.__class__ = type(self).__bases__
|
|
|
|
|
2018-02-27 18:22:11 +00:00
|
|
|
def run(self, tmp=None, task_vars=None):
|
|
|
|
"""
|
2018-02-28 17:52:32 +00:00
|
|
|
Override run() to notify Connection of task-specific data, so it has a
|
|
|
|
chance to know e.g. the Python interpreter in use.
|
2018-02-27 18:22:11 +00:00
|
|
|
"""
|
2018-07-10 16:56:05 +00:00
|
|
|
self._connection.on_action_run(
|
|
|
|
task_vars=task_vars,
|
2018-08-17 10:31:28 +00:00
|
|
|
delegate_to_hostname=self._task.delegate_to,
|
2018-07-10 16:56:05 +00:00
|
|
|
loader_basedir=self._loader.get_basedir(),
|
|
|
|
)
|
2018-02-27 18:22:11 +00:00
|
|
|
return super(ActionModuleMixin, self).run(tmp, task_vars)
|
|
|
|
|
2018-02-15 09:43:19 +00:00
|
|
|
def call(self, func, *args, **kwargs):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Arrange for a Python function to be called in the target context, which
|
|
|
|
should be some function from the standard library or
|
2018-04-06 16:22:45 +00:00
|
|
|
ansible_mitogen.target module. This junction point exists mainly as a
|
2018-02-28 17:52:32 +00:00
|
|
|
nice place to insert print statements during debugging.
|
|
|
|
"""
|
2018-02-15 09:43:19 +00:00
|
|
|
return self._connection.call(func, *args, **kwargs)
|
|
|
|
|
|
|
|
COMMAND_RESULT = {
|
|
|
|
'rc': 0,
|
|
|
|
'stdout': '',
|
|
|
|
'stdout_lines': [],
|
|
|
|
'stderr': ''
|
|
|
|
}
|
|
|
|
|
|
|
|
def fake_shell(self, func, stdout=False):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Execute a function and decorate its return value in the style of
|
|
|
|
_low_level_execute_command(). This produces a return value that looks
|
|
|
|
like some shell command was run, when really func() was implemented
|
|
|
|
entirely in Python.
|
|
|
|
|
|
|
|
If the function raises :py:class:`mitogen.core.CallError`, this will be
|
|
|
|
translated into a failed shell command with a non-zero exit status.
|
|
|
|
|
|
|
|
:param func:
|
|
|
|
Function invoked as `func()`.
|
|
|
|
:returns:
|
|
|
|
See :py:attr:`COMMAND_RESULT`.
|
|
|
|
"""
|
2018-02-15 09:43:19 +00:00
|
|
|
dct = self.COMMAND_RESULT.copy()
|
|
|
|
try:
|
|
|
|
rc = func()
|
|
|
|
if stdout:
|
|
|
|
dct['stdout'] = repr(rc)
|
|
|
|
except mitogen.core.CallError:
|
2018-03-08 14:37:08 +00:00
|
|
|
LOG.exception('While emulating a shell command')
|
2018-02-15 09:43:19 +00:00
|
|
|
dct['rc'] = 1
|
|
|
|
dct['stderr'] = traceback.format_exc()
|
|
|
|
|
|
|
|
return dct
|
|
|
|
|
|
|
|
def _remote_file_exists(self, path):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Determine if `path` exists by directly invoking os.path.exists() in the
|
|
|
|
target user account.
|
|
|
|
"""
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_remote_file_exists(%r)', path)
|
2018-04-05 18:35:03 +00:00
|
|
|
return self.call(os.path.exists, mitogen.utils.cast(path))
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
def _configure_module(self, module_name, module_args, task_vars=None):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Mitogen does not use the Ansiballz framework. This call should never
|
|
|
|
happen when ActionMixin is active, so crash if it does.
|
|
|
|
"""
|
2018-02-15 09:43:19 +00:00
|
|
|
assert False, "_configure_module() should never be called."
|
|
|
|
|
|
|
|
def _is_pipelining_enabled(self, module_style, wrap_async=False):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Mitogen does not use SSH pipelining. This call should never happen when
|
|
|
|
ActionMixin is active, so crash if it does.
|
|
|
|
"""
|
|
|
|
assert False, "_is_pipelining_enabled() should never be called."
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
def _make_tmp_path(self, remote_user=None):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-08-19 14:59:35 +00:00
|
|
|
Return the temporary directory created by the persistent interpreter at
|
|
|
|
startup.
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_make_tmp_path(remote_user=%r)', remote_user)
|
2018-04-01 14:58:44 +00:00
|
|
|
# _make_tmp_path() is basically a global stashed away as Shell.tmpdir.
|
2018-08-19 14:59:35 +00:00
|
|
|
self._connection._shell.tmpdir = self._connection.get_temp_dir()
|
2018-04-01 14:58:44 +00:00
|
|
|
LOG.debug('Temporary directory: %r', self._connection._shell.tmpdir)
|
2018-02-18 08:08:00 +00:00
|
|
|
self._cleanup_remote_tmp = True
|
2018-04-01 14:58:44 +00:00
|
|
|
return self._connection._shell.tmpdir
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
def _remove_tmp_path(self, tmp_path):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-08-19 14:59:35 +00:00
|
|
|
Stub out the base implementation's invocation of rm -rf, replacing it
|
|
|
|
with nothing, as the persistent interpreter automatically cleans up
|
|
|
|
after itself without introducing roundtrips.
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_remove_tmp_path(%r)', tmp_path)
|
2018-08-19 14:59:35 +00:00
|
|
|
self._connection._shell.tmpdir = None
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
def _transfer_data(self, remote_path, data):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-03-08 02:29:02 +00:00
|
|
|
Used by the base _execute_module(), and in <2.4 also by the template
|
|
|
|
action module, and probably others.
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-03-10 12:20:40 +00:00
|
|
|
if isinstance(data, dict):
|
|
|
|
data = jsonify(data)
|
|
|
|
if not isinstance(data, bytes):
|
|
|
|
data = to_bytes(data, errors='surrogate_or_strict')
|
|
|
|
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_transfer_data(%r, %s ..%d bytes)',
|
|
|
|
remote_path, type(data), len(data))
|
2018-03-08 02:29:02 +00:00
|
|
|
self._connection.put_data(remote_path, data)
|
2018-03-08 21:57:31 +00:00
|
|
|
return remote_path
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
def _fixup_perms2(self, remote_paths, remote_user=None, execute=True):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Mitogen always executes ActionBase helper methods in the context of the
|
|
|
|
target user account, so it is never necessary to modify permissions
|
|
|
|
except to ensure the execute bit is set if requested.
|
|
|
|
"""
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_fixup_perms2(%r, remote_user=%r, execute=%r)',
|
|
|
|
remote_paths, remote_user, execute)
|
2018-02-27 11:14:36 +00:00
|
|
|
if execute:
|
2018-02-27 09:30:41 +00:00
|
|
|
return self._remote_chmod(remote_paths, mode='u+x')
|
|
|
|
return self.COMMAND_RESULT.copy()
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
def _remote_chmod(self, paths, mode, sudoable=False):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Issue an asynchronous set_file_mode() call for every path in `paths`,
|
|
|
|
then format the resulting return value list with fake_shell().
|
|
|
|
"""
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_remote_chmod(%r, mode=%r, sudoable=%r)',
|
|
|
|
paths, mode, sudoable)
|
2018-05-24 22:32:07 +00:00
|
|
|
return self.fake_shell(lambda: mitogen.select.Select.all(
|
2018-02-28 17:52:32 +00:00
|
|
|
self._connection.call_async(
|
2018-04-06 16:22:45 +00:00
|
|
|
ansible_mitogen.target.set_file_mode, path, mode
|
2018-02-28 17:52:32 +00:00
|
|
|
)
|
2018-02-15 09:43:19 +00:00
|
|
|
for path in paths
|
|
|
|
))
|
|
|
|
|
|
|
|
def _remote_chown(self, paths, user, sudoable=False):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Issue an asynchronous os.chown() call for every path in `paths`, then
|
|
|
|
format the resulting return value list with fake_shell().
|
|
|
|
"""
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_remote_chown(%r, user=%r, sudoable=%r)',
|
|
|
|
paths, user, sudoable)
|
2018-02-15 09:43:19 +00:00
|
|
|
ent = self.call(pwd.getpwnam, user)
|
2018-05-24 22:32:07 +00:00
|
|
|
return self.fake_shell(lambda: mitogen.select.Select.all(
|
2018-02-28 17:52:32 +00:00
|
|
|
self._connection.call_async(
|
|
|
|
os.chown, path, ent.pw_uid, ent.pw_gid
|
|
|
|
)
|
2018-02-15 09:43:19 +00:00
|
|
|
for path in paths
|
|
|
|
))
|
|
|
|
|
|
|
|
def _remote_expand_user(self, path, sudoable=True):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
Replace the base implementation's attempt to emulate
|
|
|
|
os.path.expanduser() with an actual call to os.path.expanduser().
|
2018-07-25 19:13:27 +00:00
|
|
|
|
|
|
|
:param bool sudoable:
|
|
|
|
If :data:`True`, indicate unqualified tilde ("~" with no username)
|
|
|
|
should be evaluated in the context of the login account, not any
|
|
|
|
become_user.
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-04-01 14:54:17 +00:00
|
|
|
LOG.debug('_remote_expand_user(%r, sudoable=%r)', path, sudoable)
|
|
|
|
if not path.startswith('~'):
|
|
|
|
# /home/foo -> /home/foo
|
|
|
|
return path
|
2018-07-25 19:13:27 +00:00
|
|
|
if sudoable or not self._play_context.become:
|
|
|
|
if path == '~':
|
|
|
|
# ~ -> /home/dmw
|
|
|
|
return self._connection.homedir
|
|
|
|
if path.startswith('~/'):
|
|
|
|
# ~/.ansible -> /home/dmw/.ansible
|
|
|
|
return os.path.join(self._connection.homedir, path[2:])
|
|
|
|
# ~root/.ansible -> /root/.ansible
|
|
|
|
return self.call(os.path.expanduser, mitogen.utils.cast(path),
|
|
|
|
use_login_context=not sudoable)
|
2018-02-15 09:43:19 +00:00
|
|
|
|
2018-06-10 01:11:41 +00:00
|
|
|
def get_task_timeout_secs(self):
|
|
|
|
"""
|
|
|
|
Return the task "async:" value, portable across 2.4-2.5.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self._task.async_val
|
|
|
|
except AttributeError:
|
|
|
|
return getattr(self._task, 'async')
|
|
|
|
|
2018-02-15 09:43:19 +00:00
|
|
|
def _execute_module(self, module_name=None, module_args=None, tmp=None,
|
|
|
|
task_vars=None, persist_files=False,
|
|
|
|
delete_remote_tmp=True, wrap_async=False):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
|
|
|
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
|
2018-02-28 17:52:32 +00:00
|
|
|
context.
|
|
|
|
"""
|
2018-03-07 14:19:05 +00:00
|
|
|
if module_name is None:
|
|
|
|
module_name = self._task.action
|
|
|
|
if module_args is None:
|
|
|
|
module_args = self._task.args
|
2018-03-31 04:37:11 +00:00
|
|
|
if task_vars is None:
|
|
|
|
task_vars = {}
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
self._update_module_args(module_name, module_args, task_vars)
|
2018-02-27 19:57:37 +00:00
|
|
|
env = {}
|
|
|
|
self._compute_environment_string(env)
|
|
|
|
|
2018-08-19 14:59:35 +00:00
|
|
|
# Always set _ansible_tmpdir regardless of whether _make_remote_tmp()
|
|
|
|
# has ever been called. This short-circuits all the .tmpdir logic in
|
|
|
|
# module_common and ensures no second temporary directory or atexit
|
|
|
|
# handler is installed.
|
2018-05-29 13:45:12 +00:00
|
|
|
self._connection._connect()
|
2018-08-19 17:50:53 +00:00
|
|
|
if not module_args.get('_ansible_tmpdir', object()):
|
|
|
|
module_args['_ansible_tmpdir'] = self._connection.get_temp_dir()
|
2018-08-19 14:59:35 +00:00
|
|
|
|
2018-03-31 04:37:11 +00:00
|
|
|
return ansible_mitogen.planner.invoke(
|
|
|
|
ansible_mitogen.planner.Invocation(
|
|
|
|
action=self,
|
|
|
|
connection=self._connection,
|
2018-04-17 16:40:45 +00:00
|
|
|
module_name=mitogen.core.to_text(module_name),
|
2018-03-31 04:37:11 +00:00
|
|
|
module_args=mitogen.utils.cast(module_args),
|
2018-04-01 17:19:34 +00:00
|
|
|
task_vars=task_vars,
|
|
|
|
templar=self._templar,
|
2018-03-31 04:37:11 +00:00
|
|
|
env=mitogen.utils.cast(env),
|
|
|
|
wrap_async=wrap_async,
|
2018-06-10 01:11:41 +00:00
|
|
|
timeout_secs=self.get_task_timeout_secs(),
|
2018-03-31 04:37:11 +00:00
|
|
|
)
|
2018-02-15 09:43:19 +00:00
|
|
|
)
|
|
|
|
|
2018-04-01 11:56:53 +00:00
|
|
|
def _postprocess_response(self, result):
|
2018-03-31 04:37:11 +00:00
|
|
|
"""
|
|
|
|
Apply fixups mimicking ActionBase._execute_module(); this is copied
|
|
|
|
verbatim from action/__init__.py, the guts of _parse_returned_data are
|
|
|
|
garbage and should be removed or reimplemented once tests exist.
|
2018-04-01 11:56:53 +00:00
|
|
|
|
|
|
|
:param dict result:
|
|
|
|
Dictionary with format::
|
|
|
|
|
|
|
|
{
|
|
|
|
"rc": int,
|
|
|
|
"stdout": "stdout data",
|
|
|
|
"stderr": "stderr data"
|
|
|
|
}
|
2018-03-31 04:37:11 +00:00
|
|
|
"""
|
2018-04-01 11:56:53 +00:00
|
|
|
data = self._parse_returned_data(result)
|
2018-02-15 09:43:19 +00:00
|
|
|
|
2018-02-28 17:52:32 +00:00
|
|
|
# Cutpasted from the base implementation.
|
2018-02-15 09:43:19 +00:00
|
|
|
if 'stdout' in data and 'stdout_lines' not in data:
|
2018-02-28 17:52:32 +00:00
|
|
|
data['stdout_lines'] = (data['stdout'] or u'').splitlines()
|
2018-02-15 09:43:19 +00:00
|
|
|
if 'stderr' in data and 'stderr_lines' not in data:
|
2018-02-28 17:52:32 +00:00
|
|
|
data['stderr_lines'] = (data['stderr'] or u'').splitlines()
|
2018-02-15 09:43:19 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def _low_level_execute_command(self, cmd, sudoable=True, in_data=None,
|
|
|
|
executable=None,
|
2018-02-27 07:54:40 +00:00
|
|
|
encoding_errors='surrogate_then_replace',
|
|
|
|
chdir=None):
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-03-31 04:37:11 +00:00
|
|
|
Override the base implementation by simply calling
|
2018-04-06 16:22:45 +00:00
|
|
|
target.exec_command() in the target context.
|
2018-02-28 17:52:32 +00:00
|
|
|
"""
|
2018-03-08 21:57:06 +00:00
|
|
|
LOG.debug('_low_level_execute_command(%r, in_data=%r, exe=%r, dir=%r)',
|
|
|
|
cmd, type(in_data), executable, chdir)
|
2018-02-27 07:54:40 +00:00
|
|
|
if executable is None: # executable defaults to False
|
|
|
|
executable = self._play_context.executable
|
|
|
|
if executable:
|
2018-04-17 16:40:45 +00:00
|
|
|
cmd = executable + ' -c ' + shlex_quote(cmd)
|
2018-02-27 07:54:40 +00:00
|
|
|
|
2018-04-04 23:41:14 +00:00
|
|
|
rc, stdout, stderr = self._connection.exec_command(
|
2018-04-05 01:55:17 +00:00
|
|
|
cmd=cmd,
|
|
|
|
in_data=in_data,
|
2018-04-04 23:41:14 +00:00
|
|
|
sudoable=sudoable,
|
2018-04-05 01:55:17 +00:00
|
|
|
mitogen_chdir=chdir,
|
2018-02-15 09:43:19 +00:00
|
|
|
)
|
2018-02-27 11:28:26 +00:00
|
|
|
stdout_text = to_text(stdout, errors=encoding_errors)
|
|
|
|
|
2018-02-15 09:43:19 +00:00
|
|
|
return {
|
|
|
|
'rc': rc,
|
2018-02-27 11:28:26 +00:00
|
|
|
'stdout': stdout_text,
|
|
|
|
'stdout_lines': stdout_text.splitlines(),
|
2018-02-15 09:43:19 +00:00
|
|
|
'stderr': stderr,
|
|
|
|
}
|