From 1b8b2c8b1af8226b00ad2e26f0644c58f98c2fa2 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 23 Jan 2025 12:58:12 +0000 Subject: [PATCH] ansible_mitogen: Rename Mitogen interpreter discovery attributes This makes their nature and ownership/responsibility much more explicit. --- ansible_mitogen/mixins.py | 12 +++++++----- ansible_mitogen/transport_config.py | 10 +++++----- docs/changelog.rst | 2 ++ tests/ansible/tests/connection_test.py | 4 +++- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/ansible_mitogen/mixins.py b/ansible_mitogen/mixins.py index b916afe1..d1def2f6 100644 --- a/ansible_mitogen/mixins.py +++ b/ansible_mitogen/mixins.py @@ -103,8 +103,10 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): # required for python interpreter discovery connection.templar = self._templar - self._finding_python_interpreter = False - self._rediscovered_python = False + + self._mitogen_discovering_interpreter = False + self._mitogen_interpreter_candidate = None + self._mitogen_rediscovered_interpreter = False def run(self, tmp=None, task_vars=None): """ @@ -397,7 +399,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): # only cache discovered_interpreter if we're not running a rediscovery # rediscovery happens in places like docker connections that could have different # python interpreters than the main host - if not self._rediscovered_python: + if not self._mitogen_rediscovered_interpreter: result['ansible_facts'][self._discovered_interpreter_key] = self._discovered_interpreter if self._discovery_warnings: @@ -457,7 +459,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): # calling exec_command until we run into the right python we'll use # chicken-and-egg issue, mitogen needs a python to run low_level_execute_command # which is required by Ansible's discover_interpreter function - if self._finding_python_interpreter: + if self._mitogen_discovering_interpreter: possible_pythons = [ '/usr/bin/python', 'python3', @@ -484,7 +486,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): for possible_python in possible_pythons: try: - self._possible_python_interpreter = possible_python + self._mitogen_interpreter_candidate = possible_python rc, stdout, stderr = _run_cmd() # TODO: what exception is thrown? except: diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index effb4d62..22afd197 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -87,8 +87,8 @@ def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_pyth it could be different than what's ran on the host """ # keep trying different interpreters until we don't error - if action._finding_python_interpreter: - return action._possible_python_interpreter + if action._mitogen_discovering_interpreter: + return action._mitogen_interpreter_candidate if s in ['auto', 'auto_legacy', 'auto_silent', 'auto_legacy_silent']: # python is the only supported interpreter_name as of Ansible 2.8.8 @@ -102,13 +102,13 @@ def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_pyth # if we're rediscovering python then chances are we're running something like a docker connection # this will handle scenarios like running a playbook that does stuff + then dynamically creates a docker container, # then runs the rest of the playbook inside that container, and then rerunning the playbook again - action._rediscovered_python = True + action._mitogen_rediscovered_interpreter = True # blow away the discovered_interpreter_config cache and rediscover del task_vars['ansible_facts'][discovered_interpreter_config] if discovered_interpreter_config not in task_vars['ansible_facts']: - action._finding_python_interpreter = True + action._mitogen_discovering_interpreter = True # fake pipelining so discover_interpreter can be happy action._connection.has_pipelining = True s = ansible.executor.interpreter_discovery.discover_interpreter( @@ -128,7 +128,7 @@ def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_pyth action._discovered_interpreter_key = discovered_interpreter_config action._discovered_interpreter = s - action._finding_python_interpreter = False + action._mitogen_discovering_interpreter = False return s diff --git a/docs/changelog.rst b/docs/changelog.rst index 1b3df255..e5f3340b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -28,6 +28,8 @@ In progress (unreleased) * :gh:issue:`1213` tests: Fix unclosed file in fd_check script * :gh:issue:`1213` :mod:`ansible_mitogen`: Don't redeclare Ansible interpreter discovery attributes +* :gh:issue:`1213` :mod:`ansible_mitogen`: Rename Mitogen interpreter discovery + attributes v0.3.21 (2025-01-20) diff --git a/tests/ansible/tests/connection_test.py b/tests/ansible/tests/connection_test.py index 649f8b65..856c6b1c 100644 --- a/tests/ansible/tests/connection_test.py +++ b/tests/ansible/tests/connection_test.py @@ -45,7 +45,9 @@ class ConnectionMixin(MuxProcessMixin): conn = self.klass(play_context, new_stdin=False) # conn functions don't fetch ActionModuleMixin objs from _get_task_vars() # through the usual walk-the-stack approach so we'll not run interpreter discovery here - conn._action = mock.MagicMock(_possible_python_interpreter=testlib.base_executable()) + conn._action = mock.MagicMock( + _mitogen_interpreter_candidate=testlib.base_executable(), + ) conn.on_action_run( task_vars={}, delegate_to_hostname=None,