From 380ef7376db93bb6974fb70d30a0a7ba290b6cdf Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 2 Apr 2018 00:01:28 +0100 Subject: [PATCH] ansible: Add support for free strategy. --- ansible_mitogen/plugins/strategy/mitogen.py | 10 +++- .../plugins/strategy/mitogen_free.py | 60 +++++++++++++++++++ .../plugins/strategy/mitogen_linear.py | 60 +++++++++++++++++++ ansible_mitogen/strategy.py | 13 ++-- docs/ansible.rst | 8 +-- examples/playbook/ansible.cfg | 2 +- 6 files changed, 138 insertions(+), 15 deletions(-) create mode 100644 ansible_mitogen/plugins/strategy/mitogen_free.py create mode 100644 ansible_mitogen/plugins/strategy/mitogen_linear.py diff --git a/ansible_mitogen/plugins/strategy/mitogen.py b/ansible_mitogen/plugins/strategy/mitogen.py index 77d1e06e..3ef522b4 100644 --- a/ansible_mitogen/plugins/strategy/mitogen.py +++ b/ansible_mitogen/plugins/strategy/mitogen.py @@ -51,6 +51,10 @@ except ImportError: sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..'))) del base_dir -from ansible_mitogen.strategy import StrategyModule -del os -del sys +import ansible_mitogen.strategy +import ansible.plugins.strategy.linear + + +class StrategyModule(ansible_mitogen.strategy.StrategyMixin, + ansible.plugins.strategy.linear.StrategyModule): + pass diff --git a/ansible_mitogen/plugins/strategy/mitogen_free.py b/ansible_mitogen/plugins/strategy/mitogen_free.py new file mode 100644 index 00000000..b7ee03bc --- /dev/null +++ b/ansible_mitogen/plugins/strategy/mitogen_free.py @@ -0,0 +1,60 @@ +# 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. + +import os.path +import sys + +# +# This is not the real Strategy implementation module, it simply exists as a +# proxy to the real module, which is loaded using Python's regular import +# mechanism, to prevent Ansible's PluginLoader from making up a fake name that +# results in ansible_mitogen plugin modules being loaded twice: once by +# PluginLoader with a name like "ansible.plugins.strategy.mitogen", which is +# stuffed into sys.modules even though attempting to import it will trigger an +# ImportError, and once under its canonical name, "ansible_mitogen.strategy". +# +# Therefore we have a proxy module that imports it under the real name, and +# sets up the duff PluginLoader-imported module to just contain objects from +# the real module, so duplicate types don't exist in memory, and things like +# debuggers and isinstance() work predictably. +# + +try: + import ansible_mitogen +except ImportError: + base_dir = os.path.dirname(__file__) + sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..'))) + del base_dir + +import ansible_mitogen.strategy +import ansible.plugins.strategy.free + + +class StrategyModule(ansible_mitogen.strategy.StrategyMixin, + ansible.plugins.strategy.free.StrategyModule): + pass diff --git a/ansible_mitogen/plugins/strategy/mitogen_linear.py b/ansible_mitogen/plugins/strategy/mitogen_linear.py new file mode 100644 index 00000000..3ef522b4 --- /dev/null +++ b/ansible_mitogen/plugins/strategy/mitogen_linear.py @@ -0,0 +1,60 @@ +# 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. + +import os.path +import sys + +# +# This is not the real Strategy implementation module, it simply exists as a +# proxy to the real module, which is loaded using Python's regular import +# mechanism, to prevent Ansible's PluginLoader from making up a fake name that +# results in ansible_mitogen plugin modules being loaded twice: once by +# PluginLoader with a name like "ansible.plugins.strategy.mitogen", which is +# stuffed into sys.modules even though attempting to import it will trigger an +# ImportError, and once under its canonical name, "ansible_mitogen.strategy". +# +# Therefore we have a proxy module that imports it under the real name, and +# sets up the duff PluginLoader-imported module to just contain objects from +# the real module, so duplicate types don't exist in memory, and things like +# debuggers and isinstance() work predictably. +# + +try: + import ansible_mitogen +except ImportError: + base_dir = os.path.dirname(__file__) + sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..'))) + del base_dir + +import ansible_mitogen.strategy +import ansible.plugins.strategy.linear + + +class StrategyModule(ansible_mitogen.strategy.StrategyMixin, + ansible.plugins.strategy.linear.StrategyModule): + pass diff --git a/ansible_mitogen/strategy.py b/ansible_mitogen/strategy.py index 078ba921..1ad7099a 100644 --- a/ansible_mitogen/strategy.py +++ b/ansible_mitogen/strategy.py @@ -30,7 +30,6 @@ from __future__ import absolute_import import os import ansible.errors -import ansible.plugins.strategy.linear import ansible_mitogen.mixins import ansible_mitogen.process @@ -83,12 +82,12 @@ def wrap_connection_loader__get(name, play_context, new_stdin, **kwargs): return connection_loader__get(name, play_context, new_stdin, **kwargs) -class StrategyModule(ansible.plugins.strategy.linear.StrategyModule): +class StrategyMixin(object): """ - This strategy enhances the default "linear" strategy by arranging for - various Mitogen services to be initialized in the Ansible top-level - process, and for worker processes to grow support for using those top-level - services to communicate with and execute modules on remote hosts. + This mix-in enhances any built-in strategy by arranging for various Mitogen + services to be initialized in the Ansible top-level process, and for worker + processes to grow support for using those top-level services to communicate + with and execute modules on remote hosts. Mitogen: @@ -182,6 +181,6 @@ class StrategyModule(ansible.plugins.strategy.linear.StrategyModule): self._add_connection_plugin_path() self._install_wrappers() try: - return super(StrategyModule, self).run(iterator, play_context) + return super(StrategyMixin, self).run(iterator, play_context) finally: self._remove_wrappers() diff --git a/docs/ansible.rst b/docs/ansible.rst index 8d4dacc6..d164d85f 100644 --- a/docs/ansible.rst +++ b/docs/ansible.rst @@ -97,10 +97,12 @@ Installation [defaults] strategy_plugins = /path/to/mitogen-master/ansible_mitogen/plugins/strategy - strategy = mitogen + strategy = mitogen_linear The ``strategy`` key is optional. If omitted, you can set the - ``ANSIBLE_STRATEGY=mitogen`` environment variable on a per-run basis. + ``ANSIBLE_STRATEGY=mitogen_linear`` environment variable on a per-run basis. + Like ``mitogen_linear``, the ``mitogen_free`` strategy also exists to mimic + the built-in ``free`` strategy. 4. Cross your fingers and try it. @@ -146,8 +148,6 @@ Low Risk * Only the ``sudo`` become method is available, however adding new methods is straightforward, and eventually at least ``su`` will be included. -* The only supported strategy is ``linear``, which is Ansible's default. - * In some cases ``remote_tmp`` may not be respected. * The extension's performance benefits do not scale perfectly linearly with the diff --git a/examples/playbook/ansible.cfg b/examples/playbook/ansible.cfg index 81be60b1..479e42ab 100644 --- a/examples/playbook/ansible.cfg +++ b/examples/playbook/ansible.cfg @@ -1,7 +1,7 @@ [defaults] inventory = hosts strategy_plugins = ../../ansible_mitogen/plugins/strategy -strategy = mitogen +strategy = mitogen_linear library = modules retry_files_enabled = False forks = 50