| 1 |
# Copyright: (c) 2018 Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
from ansible import constants as C |
| 9 |
from ansible.errors import AnsibleError |
| 10 |
from ansible.utils.display import Display |
| 11 |
from ansible.utils.plugin_docs import get_versioned_doclink |
| 12 |
|
| 13 |
_FALLBACK_INTERPRETER = '/usr/bin/python3' |
| 14 |
|
| 15 |
display = Display() |
| 16 |
foundre = re.compile(r'FOUND(.*)ENDFOUND', flags=re.DOTALL) |
| 17 |
|
| 18 |
|
| 19 |
class InterpreterDiscoveryRequiredError(Exception): |
| 20 |
def __init__(self, message, interpreter_name, discovery_mode): |
| 21 |
super(InterpreterDiscoveryRequiredError, self).__init__(message) |
| 22 |
self.interpreter_name = interpreter_name |
| 23 |
self.discovery_mode = discovery_mode |
| 24 |
|
| 25 |
|
| 26 |
def discover_interpreter(action, interpreter_name, discovery_mode, task_vars): |
| 27 |
"""Probe the target host for a Python interpreter from the `INTERPRETER_PYTHON_FALLBACK` list, returning the first found or `/usr/bin/python3` if none.""" |
| 28 |
host = task_vars.get('inventory_hostname', 'unknown') |
| 29 |
res = None |
| 30 |
found_interpreters = [_FALLBACK_INTERPRETER] # fallback value |
| 31 |
is_silent = discovery_mode.endswith('_silent') |
| 32 |
|
| 33 |
try: |
| 34 |
bootstrap_python_list = C.config.get_config_value('INTERPRETER_PYTHON_FALLBACK', variables=task_vars) |
| 35 |
|
| 36 |
display.vvv(msg=f"Attempting {interpreter_name} interpreter discovery.", host=host) |
| 37 |
|
| 38 |
# not all command -v impls accept a list of commands, so we have to call it once per python |
| 39 |
command_list = ["command -v '%s'" % py for py in bootstrap_python_list] |
| 40 |
shell_bootstrap = "echo FOUND; {0}; echo ENDFOUND".format('; '.join(command_list)) |
| 41 |
|
| 42 |
# FUTURE: in most cases we probably don't want to use become, but maybe sometimes we do? |
| 43 |
res = action._low_level_execute_command(shell_bootstrap, sudoable=False) |
| 44 |
|
| 45 |
raw_stdout = res.get('stdout', u'') |
| 46 |
|
| 47 |
match = foundre.match(raw_stdout) |
| 48 |
|
| 49 |
if not match: |
| 50 |
display.debug(u'raw interpreter discovery output: {0}'.format(raw_stdout), host=host) |
| 51 |
raise ValueError('unexpected output from Python interpreter discovery') |
| 52 |
|
| 53 |
found_interpreters = [interp.strip() for interp in match.groups()[0].splitlines() if interp.startswith('/')] |
| 54 |
|
| 55 |
display.debug(u"found interpreters: {0}".format(found_interpreters), host=host) |
| 56 |
|
| 57 |
if not found_interpreters: |
| 58 |
if not is_silent: |
| 59 |
display.warning(msg=f'No python interpreters found for host {host!r} (tried {bootstrap_python_list!r}).') |
| 60 |
|
| 61 |
# this is lame, but returning None or throwing an exception is uglier |
| 62 |
return _FALLBACK_INTERPRETER |
| 63 |
except AnsibleError: |
| 64 |
raise |
| 65 |
except Exception as ex: |
| 66 |
if not is_silent: |
| 67 |
display.error_as_warning(msg=f'Unhandled error in Python interpreter discovery for host {host!r}.', exception=ex) |
| 68 |
|
| 69 |
if res and res.get('stderr'): # the current ssh plugin implementation always has stderr, making coverage of the false case difficult |
| 70 |
display.vvv(msg=f"Interpreter discovery remote stderr:\n{res.get('stderr')}", host=host) |
| 71 |
|
| 72 |
if not is_silent: |
| 73 |
display.warning( |
| 74 |
msg=( |
| 75 |
f"Host {host!r} is using the discovered Python interpreter at {found_interpreters[0]!r}, " |
| 76 |
"but future installation of another Python interpreter could cause a different interpreter to be discovered." |
| 77 |
), |
| 78 |
help_text=f"See {get_versioned_doclink('reference_appendices/interpreter_discovery.html')} for more information.", |
| 79 |
) |
| 80 |
|
| 81 |
return found_interpreters[0] |