From 619f4dee07346454d754b069dba48f9134317932 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 2 Aug 2019 04:05:19 +0100 Subject: [PATCH 1/4] [linear2] merge fallout: restore optimization from #491 / 7b129e857 --- ansible_mitogen/connection.py | 9 --------- ansible_mitogen/process.py | 11 ++++++++++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py index ef21f606..532ef042 100644 --- a/ansible_mitogen/connection.py +++ b/ansible_mitogen/connection.py @@ -405,15 +405,6 @@ CONNECTION_METHOD = { } -class Broker(mitogen.master.Broker): - """ - WorkerProcess maintains at most 2 file descriptors, therefore does not need - the exuberant syscall expense of EpollPoller, so override it and restore - the poll() poller. - """ - poller_class = mitogen.core.Poller - - class CallChain(mitogen.parent.CallChain): """ Extend :class:`mitogen.parent.CallChain` to additionally cause the diff --git a/ansible_mitogen/process.py b/ansible_mitogen/process.py index 1f7741f6..b09b54eb 100644 --- a/ansible_mitogen/process.py +++ b/ansible_mitogen/process.py @@ -294,6 +294,15 @@ def get_cpu_count(default=None): return cpu_count +class Broker(mitogen.master.Broker): + """ + WorkerProcess maintains at most 2 file descriptors, therefore does not need + the exuberant syscall expense of EpollPoller, so override it and restore + the poll() poller. + """ + poller_class = mitogen.core.Poller + + class Binding(object): """ Represent a bound connection for a particular inventory hostname. When @@ -530,7 +539,7 @@ class ClassicWorkerModel(WorkerModel): See WorkerModel.get_binding(). """ if self.broker is None: - self.broker = mitogen.master.Broker() + self.broker = Broker() path = self._listener_for_name(inventory_name) if path != self.listener_path: From 6b4bcf4fe01fa13113584209af88987b028febb5 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 2 Aug 2019 04:05:28 +0100 Subject: [PATCH 2/4] ansible: remove cutpasted docstring --- ansible_mitogen/parsing.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ansible_mitogen/parsing.py b/ansible_mitogen/parsing.py index 525e60cf..27fca7cd 100644 --- a/ansible_mitogen/parsing.py +++ b/ansible_mitogen/parsing.py @@ -26,14 +26,6 @@ # 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 From 33bceb6eb4468ec2df005a4525c403a5f6f8ba11 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 2 Aug 2019 04:05:34 +0100 Subject: [PATCH 3/4] issue #602: recover task_vars for synchronize and meta: reset_connection --- ansible_mitogen/connection.py | 51 ++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py index 532ef042..c08df611 100644 --- a/ansible_mitogen/connection.py +++ b/ansible_mitogen/connection.py @@ -46,6 +46,7 @@ import mitogen.core import mitogen.fork import mitogen.utils +import ansible_mitogen.mixins import ansible_mitogen.parsing import ansible_mitogen.process import ansible_mitogen.services @@ -533,6 +534,47 @@ class Connection(ansible.plugins.connection.ConnectionBase): self.loader_basedir = loader_basedir self._mitogen_reset(mode='put') + def _get_task_vars(self): + """ + More information is needed than normally provided to an Ansible + connection. For proxied connections, intermediary configuration must + be inferred, and for any connection the configured Python interpreter + must be known. + + There is no clean way to access this information that would not deviate + from the running Ansible version. The least invasive method known is to + reuse the running task's task_vars dict. + + This method walks the stack to find task_vars of the Action plugin's + run(), or if no Action is present, from Strategy's _execute_meta(), as + in the case of 'meta: reset_connection'. The stack is walked in + addition to subclassing Action.run()/on_action_run(), as it is possible + for new connections to be constructed in addition to the preconstructed + connection passed into any running action. + """ + f = sys._getframe() + + while f: + if f.f_code.co_name == 'run': + f_locals = f.f_locals + f_self = f_locals.get('self') + if isinstance(f_self, ansible_mitogen.mixins.ActionModuleMixin): + task_vars = f_locals.get('task_vars') + if task_vars: + LOG.debug('recovered task_vars from Action') + return task_vars + elif f.f_code.co_name == '_execute_meta': + f_all_vars = f.f_locals.get('all_vars') + if isinstance(f_all_vars, dict): + LOG.debug('recovered task_vars from meta:') + return f_all_vars + + f = f.f_back + + LOG.warning('could not recover task_vars. This means some connection ' + 'settings may erroneously be reset to their defaults. ' + 'Please report a bug if you encounter this message.') + def get_task_var(self, key, default=None): """ Fetch the value of a task variable related to connection configuration, @@ -544,12 +586,13 @@ class Connection(ansible.plugins.connection.ConnectionBase): does not make sense to extract connection-related configuration for the delegated-to machine from them. """ - if self._task_vars: + task_vars = self._task_vars or self._get_task_vars() + if task_vars is not None: if self.delegate_to_hostname is None: - if key in self._task_vars: - return self._task_vars[key] + if key in task_vars: + return task_vars[key] else: - delegated_vars = self._task_vars['ansible_delegated_vars'] + delegated_vars = task_vars['ansible_delegated_vars'] if self.delegate_to_hostname in delegated_vars: task_vars = delegated_vars[self.delegate_to_hostname] if key in task_vars: From 7629ff9e6da96d70c9bd12140d1ced53727791d4 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 2 Aug 2019 04:10:34 +0100 Subject: [PATCH 4/4] issue #602: update Changelog --- docs/changelog.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 2e37e87f..267bc0db 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -94,6 +94,11 @@ Mitogen for Ansible for Unicode file contents. Now the file may contain data in any single byte encoding. +* `#602 `_: connection configuration + is more accurately inferred for `meta: reset_connection`, the `synchronize` + module, and for any other action plug-ins that establish new connections of + their own. + * `7ae926b3 `_: the ``lineinfile`` module began leaking writable temporary file descriptors since Ansible 2.7.0. When ``lineinfile`` was used to create or modify a script, and @@ -157,6 +162,7 @@ Mitogen would not be possible without the support of users. A huge thanks for bug reports, testing, features and fixes in this release contributed by `Andreas Hubert `_. `Anton Markelov `_, +`Dave Cottlehuber `_, `Nigel Metheringham `_, `Orion Poplawski `_, `Pieter Voet `_,