issue #291: support UNIX hashbang syntax for ansible_*_interpreter.

Closes #291.
pull/322/head
David Wilson 6 years ago
parent f977be2868
commit e39c602fd3

@ -44,9 +44,10 @@ import ansible.utils.shlex
import mitogen.unix import mitogen.unix
import mitogen.utils import mitogen.utils
import ansible_mitogen.target import ansible_mitogen.parsing
import ansible_mitogen.process import ansible_mitogen.process
import ansible_mitogen.services import ansible_mitogen.services
import ansible_mitogen.target
LOG = logging.getLogger(__name__) 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): def config_from_play_context(transport, inventory_name, connection):
""" """
Return a dict representing all important connection configuration, allowing 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, 'become_pass': connection._play_context.become_pass,
'password': connection._play_context.password, 'password': connection._play_context.password,
'port': connection._play_context.port, '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, 'private_key_file': connection._play_context.private_key_file,
'ssh_executable': connection._play_context.ssh_executable, 'ssh_executable': connection._play_context.ssh_executable,
'timeout': connection._play_context.timeout, 'timeout': connection._play_context.timeout,
@ -314,7 +329,7 @@ def config_from_hostvars(transport, inventory_name, connection,
'password': (hostvars.get('ansible_ssh_pass') or 'password': (hostvars.get('ansible_ssh_pass') or
hostvars.get('ansible_password')), hostvars.get('ansible_password')),
'port': hostvars.get('ansible_port'), '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 'private_key_file': (hostvars.get('ansible_ssh_private_key_file') or
hostvars.get('ansible_private_key_file')), hostvars.get('ansible_private_key_file')),
'mitogen_via': hostvars.get('mitogen_via'), 'mitogen_via': hostvars.get('mitogen_via'),

@ -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:])

@ -48,6 +48,7 @@ import ansible.module_utils
import mitogen.core import mitogen.core
import ansible_mitogen.loaders import ansible_mitogen.loaders
import ansible_mitogen.parsing
import ansible_mitogen.target 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' 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): class Invocation(object):
""" """
Collect up a module's execution environment then use it to invoke Collect up a module's execution environment then use it to invoke
@ -215,7 +188,7 @@ class ScriptPlanner(BinaryPlanner):
detection and rewrite. detection and rewrite.
""" """
def _get_interpreter(self): def _get_interpreter(self):
interpreter, arg = parse_script_interpreter( interpreter, arg = ansible_mitogen.parsing.parse_hashbang(
self._inv.module_source self._inv.module_source
) )
if interpreter is None: if interpreter is None:

@ -1,6 +1,7 @@
- import_playbook: builtin_command_module.yml - import_playbook: builtin_command_module.yml
- import_playbook: custom_bash_old_style_module.yml - import_playbook: custom_bash_old_style_module.yml
- import_playbook: custom_bash_want_json_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_json.yml
- import_playbook: custom_binary_producing_junk.yml - import_playbook: custom_binary_producing_junk.yml
- import_playbook: custom_binary_single_null.yml - import_playbook: custom_binary_single_null.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"

@ -16,5 +16,6 @@ echo "{"
echo " \"changed\": false," echo " \"changed\": false,"
echo " \"msg\": \"Here is my input\"," echo " \"msg\": \"Here is my input\","
echo " \"filename\": \"$INPUT\"," echo " \"filename\": \"$INPUT\","
echo " \"run_via_env\": \"$RUN_VIA_ENV\","
echo " \"input\": [\"$(cat $INPUT | tr \" \' )\"]" echo " \"input\": [\"$(cat $INPUT | tr \" \' )\"]"
echo "}" echo "}"

Loading…
Cancel
Save