From e39c602fd32510ae9cebdf5caa720b7c1f193234 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Tue, 17 Jul 2018 23:45:28 +0100 Subject: [PATCH] issue #291: support UNIX hashbang syntax for ansible_*_interpreter. Closes #291. --- ansible_mitogen/connection.py | 21 ++++- ansible_mitogen/parsing.py | 84 +++++++++++++++++++ ansible_mitogen/planner.py | 31 +------ tests/ansible/integration/runner/all.yml | 1 + .../runner/custom_bash_hashbang_argument.yml | 19 +++++ .../modules/custom_bash_old_style_module.sh | 1 + 6 files changed, 125 insertions(+), 32 deletions(-) create mode 100644 ansible_mitogen/parsing.py create mode 100644 tests/ansible/integration/runner/custom_bash_hashbang_argument.yml diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py index 2b5c4356..464a9d81 100644 --- a/ansible_mitogen/connection.py +++ b/ansible_mitogen/connection.py @@ -44,9 +44,10 @@ import ansible.utils.shlex import mitogen.unix import mitogen.utils -import ansible_mitogen.target +import ansible_mitogen.parsing import ansible_mitogen.process import ansible_mitogen.services +import ansible_mitogen.target LOG = logging.getLogger(__name__) @@ -248,6 +249,20 @@ CONNECTION_METHOD = { } +def parse_python_path(s): + """ + Given the string set for ansible_python_interpeter, parse it as hashbang + syntax and return an appropriate argument vector. + """ + if not s: + return None + + interpreter, arg = ansible_mitogen.parsing.parse_script_interpreter(s) + if arg: + return [interpreter, arg] + return [interpreter] + + def config_from_play_context(transport, inventory_name, connection): """ Return a dict representing all important connection configuration, allowing @@ -265,7 +280,7 @@ def config_from_play_context(transport, inventory_name, connection): 'become_pass': connection._play_context.become_pass, 'password': connection._play_context.password, 'port': connection._play_context.port, - 'python_path': connection.python_path, + 'python_path': parse_python_path(connection.python_path), 'private_key_file': connection._play_context.private_key_file, 'ssh_executable': connection._play_context.ssh_executable, 'timeout': connection._play_context.timeout, @@ -314,7 +329,7 @@ def config_from_hostvars(transport, inventory_name, connection, 'password': (hostvars.get('ansible_ssh_pass') or hostvars.get('ansible_password')), 'port': hostvars.get('ansible_port'), - 'python_path': hostvars.get('ansible_python_interpreter'), + 'python_path': parse_python_path(hostvars.get('ansible_python_interpreter')), 'private_key_file': (hostvars.get('ansible_ssh_private_key_file') or hostvars.get('ansible_private_key_file')), 'mitogen_via': hostvars.get('mitogen_via'), diff --git a/ansible_mitogen/parsing.py b/ansible_mitogen/parsing.py new file mode 100644 index 00000000..fa79282a --- /dev/null +++ b/ansible_mitogen/parsing.py @@ -0,0 +1,84 @@ +# 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. + +""" +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. + +[0] "Ansible Module Architecture", developing_program_flow_modules.html +""" + +from __future__ import absolute_import +from __future__ import unicode_literals + +import mitogen.core + + +def parse_script_interpreter(source): + """ + Parse the script interpreter portion of a UNIX hashbang using the rules + Linux uses. + + :param str source: String like "/usr/bin/env python". + + :returns: + Tuple of `(interpreter, arg)`, where `intepreter` is the script + interpreter and `arg` is its sole argument if present, otherwise + :py:data:`None`. + """ + # Find terminating newline. Assume last byte of binprm_buf if absent. + nl = source.find(b'\n', 0, 128) + 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[0:nl].strip().split(None, 1) + if len(bits) == 1: + return mitogen.core.to_text(bits[0]), None + return mitogen.core.to_text(bits[0]), mitogen.core.to_text(bits[1]) + + +def parse_hashbang(source): + """ + Parse a UNIX "hashbang line" using the syntax supported by Linux. + + :param str source: String like "#!/usr/bin/env python". + + :returns: + Tuple of `(interpreter, arg)`, where `intepreter` is the script + interpreter and `arg` is its sole argument if present, otherwise + :py:data:`None`. + """ + # Linux requires first 2 bytes with no whitespace, pretty sure it's the + # same everywhere. See binfmt_script.c. + if not source.startswith(b'#!'): + return None, None + + return parse_script_interpreter(source[2:]) diff --git a/ansible_mitogen/planner.py b/ansible_mitogen/planner.py index 3542756b..c5636773 100644 --- a/ansible_mitogen/planner.py +++ b/ansible_mitogen/planner.py @@ -48,6 +48,7 @@ import ansible.module_utils import mitogen.core import ansible_mitogen.loaders +import ansible_mitogen.parsing import ansible_mitogen.target @@ -56,34 +57,6 @@ NO_METHOD_MSG = 'Mitogen: no invocation method found for: ' NO_INTERPRETER_MSG = 'module (%s) is missing interpreter line' -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 - interpreter and `arg` is its sole argument if present, otherwise - :py:data:`None`. - """ - # Linux requires first 2 bytes with no whitespace, pretty sure it's the - # same everywhere. See binfmt_script.c. - if not source.startswith(b'#!'): - return None, None - - # Find terminating newline. Assume last byte of binprm_buf if absent. - nl = source.find(b'\n', 0, 128) - 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: - return mitogen.core.to_text(bits[0]), None - return mitogen.core.to_text(bits[0]), mitogen.core.to_text(bits[1]) - - class Invocation(object): """ Collect up a module's execution environment then use it to invoke @@ -215,7 +188,7 @@ class ScriptPlanner(BinaryPlanner): detection and rewrite. """ def _get_interpreter(self): - interpreter, arg = parse_script_interpreter( + interpreter, arg = ansible_mitogen.parsing.parse_hashbang( self._inv.module_source ) if interpreter is None: diff --git a/tests/ansible/integration/runner/all.yml b/tests/ansible/integration/runner/all.yml index eaeb46d5..5242a405 100644 --- a/tests/ansible/integration/runner/all.yml +++ b/tests/ansible/integration/runner/all.yml @@ -1,6 +1,7 @@ - import_playbook: builtin_command_module.yml - import_playbook: custom_bash_old_style_module.yml - import_playbook: custom_bash_want_json_module.yml +- import_playbook: custom_bash_hashbang_argument.yml - import_playbook: custom_binary_producing_json.yml - import_playbook: custom_binary_producing_junk.yml - import_playbook: custom_binary_single_null.yml diff --git a/tests/ansible/integration/runner/custom_bash_hashbang_argument.yml b/tests/ansible/integration/runner/custom_bash_hashbang_argument.yml new file mode 100644 index 00000000..f02b8419 --- /dev/null +++ b/tests/ansible/integration/runner/custom_bash_hashbang_argument.yml @@ -0,0 +1,19 @@ +# https://github.com/dw/mitogen/issues/291 +- name: integration/runner/custom_bash_hashbang_argument.yml + hosts: test-targets + any_errors_fatal: true + tasks: + + - custom_bash_old_style_module: + foo: true + with_sequence: start=1 end={{end|default(1)}} + register: out + vars: + ansible_bash_interpreter: "/usr/bin/env RUN_VIA_ENV=yes bash" + + - assert: + that: | + (not out.changed) and + (not out.results[0].changed) and + out.results[0].msg == 'Here is my input' and + out.results[0].run_via_env == "yes" diff --git a/tests/ansible/lib/modules/custom_bash_old_style_module.sh b/tests/ansible/lib/modules/custom_bash_old_style_module.sh index 9f80dc28..04f1bcd9 100755 --- a/tests/ansible/lib/modules/custom_bash_old_style_module.sh +++ b/tests/ansible/lib/modules/custom_bash_old_style_module.sh @@ -16,5 +16,6 @@ echo "{" echo " \"changed\": false," echo " \"msg\": \"Here is my input\"," echo " \"filename\": \"$INPUT\"," +echo " \"run_via_env\": \"$RUN_VIA_ENV\"," echo " \"input\": [\"$(cat $INPUT | tr \" \' )\"]" echo "}"