2018-02-28 18:59:17 +00:00
|
|
|
# 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.
|
|
|
|
|
2018-04-09 01:50:37 +00:00
|
|
|
"""
|
|
|
|
Classes in this file define Mitogen 'services' that run (initially) within the
|
|
|
|
connection multiplexer process that is forked off the top-level controller
|
|
|
|
process.
|
|
|
|
|
|
|
|
Once a worker process connects to a multiplexer process
|
|
|
|
(Connection._connect()), it communicates with these services to establish new
|
|
|
|
connections, grant access to files by children, and register for notification
|
|
|
|
when a child has completed a job.
|
|
|
|
"""
|
|
|
|
|
2018-02-28 18:59:17 +00:00
|
|
|
from __future__ import absolute_import
|
2018-04-17 16:40:45 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2018-03-29 08:07:19 +00:00
|
|
|
import logging
|
2018-04-13 14:16:54 +00:00
|
|
|
import os
|
2018-04-01 14:54:17 +00:00
|
|
|
import os.path
|
2018-04-13 14:16:54 +00:00
|
|
|
import sys
|
2018-04-08 22:50:57 +00:00
|
|
|
import threading
|
2018-03-29 08:07:19 +00:00
|
|
|
|
2018-08-19 17:50:53 +00:00
|
|
|
import ansible.constants
|
|
|
|
|
2018-03-29 08:07:19 +00:00
|
|
|
import mitogen
|
2018-02-28 18:59:17 +00:00
|
|
|
import mitogen.service
|
2018-08-19 17:50:53 +00:00
|
|
|
import mitogen.utils
|
|
|
|
import ansible_mitogen.loaders
|
2018-05-12 13:14:35 +00:00
|
|
|
import ansible_mitogen.module_finder
|
2018-04-08 22:50:57 +00:00
|
|
|
import ansible_mitogen.target
|
2018-02-28 18:59:17 +00:00
|
|
|
|
|
|
|
|
2018-03-29 08:07:19 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2018-08-19 18:03:20 +00:00
|
|
|
# Force load of plugin to ensure ConfigManager has definitions loaded. Done
|
|
|
|
# during module import to ensure a single-threaded environment; PluginLoader
|
|
|
|
# is not thread-safe.
|
|
|
|
ansible_mitogen.loaders.shell_loader.get('sh')
|
|
|
|
|
2018-03-29 08:07:19 +00:00
|
|
|
|
2018-04-17 16:40:45 +00:00
|
|
|
if sys.version_info[0] == 3:
|
|
|
|
def reraise(tp, value, tb):
|
|
|
|
if value is None:
|
|
|
|
value = tp()
|
|
|
|
if value.__traceback__ is not tb:
|
|
|
|
raise value.with_traceback(tb)
|
|
|
|
raise value
|
|
|
|
else:
|
|
|
|
exec(
|
|
|
|
"def reraise(tp, value, tb=None):\n"
|
|
|
|
" raise tp, value, tb\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-08-19 17:50:53 +00:00
|
|
|
def _get_candidate_temp_dirs():
|
|
|
|
options = ansible.constants.config.get_plugin_options('shell', 'sh')
|
|
|
|
|
|
|
|
# Pre 2.5 this came from ansible.constants.
|
|
|
|
remote_tmp = (options.get('remote_tmp') or
|
|
|
|
ansible.constants.DEFAULT_REMOTE_TMP)
|
|
|
|
dirs = list(options.get('system_tmpdirs', ('/var/tmp', '/tmp')))
|
|
|
|
dirs.insert(0, remote_tmp)
|
|
|
|
return mitogen.utils.cast(dirs)
|
|
|
|
|
|
|
|
|
2018-10-29 15:27:20 +00:00
|
|
|
def key_from_dict(**kwargs):
|
|
|
|
"""
|
|
|
|
Return a unique string representation of a dict as quickly as possible.
|
|
|
|
Used to generated deduplication keys from a request.
|
|
|
|
"""
|
|
|
|
out = []
|
|
|
|
stack = [kwargs]
|
|
|
|
while stack:
|
|
|
|
obj = stack.pop()
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
stack.extend(sorted(obj.items()))
|
|
|
|
elif isinstance(obj, (list, tuple)):
|
|
|
|
stack.extend(obj)
|
|
|
|
else:
|
|
|
|
out.append(str(obj))
|
|
|
|
return ''.join(out)
|
|
|
|
|
|
|
|
|
2018-04-08 22:50:57 +00:00
|
|
|
class Error(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-04-13 12:31:25 +00:00
|
|
|
class ContextService(mitogen.service.Service):
|
2018-02-28 18:59:17 +00:00
|
|
|
"""
|
2018-04-09 01:50:37 +00:00
|
|
|
Used by workers to fetch the single Context instance corresponding to a
|
|
|
|
connection configuration, creating the matching connection if it does not
|
|
|
|
exist.
|
2018-02-28 18:59:17 +00:00
|
|
|
|
2018-04-09 01:50:37 +00:00
|
|
|
For connection methods and their parameters, see:
|
2018-03-10 21:22:19 +00:00
|
|
|
https://mitogen.readthedocs.io/en/latest/api.html#context-factories
|
2018-02-28 18:59:17 +00:00
|
|
|
|
2018-04-09 01:50:37 +00:00
|
|
|
This concentrates connections in the top-level process, which may become a
|
|
|
|
bottleneck. The bottleneck can be removed using per-CPU connection
|
|
|
|
processes and arranging for the worker to select one according to a hash of
|
|
|
|
the connection parameters (sharding).
|
2018-02-28 18:59:17 +00:00
|
|
|
"""
|
2018-04-13 14:16:54 +00:00
|
|
|
max_interpreters = int(os.getenv('MITOGEN_MAX_INTERPRETERS', '20'))
|
2018-04-13 12:31:25 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ContextService, self).__init__(*args, **kwargs)
|
|
|
|
self._lock = threading.Lock()
|
2018-04-16 13:42:53 +00:00
|
|
|
#: Records the :meth:`get` result dict for successful calls, returned
|
2018-10-29 15:27:20 +00:00
|
|
|
#: for identical subsequent calls. Keyed by :meth:`key_from_dict`.
|
2018-04-13 12:31:25 +00:00
|
|
|
self._response_by_key = {}
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
#: List of :class:`mitogen.core.Latch` awaiting the result for a
|
|
|
|
#: particular key.
|
|
|
|
self._latches_by_key = {}
|
2018-04-16 13:42:53 +00:00
|
|
|
#: Mapping of :class:`mitogen.core.Context` -> reference count. Each
|
|
|
|
#: call to :meth:`get` increases this by one. Calls to :meth:`put`
|
|
|
|
#: decrease it by one.
|
2018-04-13 12:31:25 +00:00
|
|
|
self._refs_by_context = {}
|
2018-04-16 13:42:53 +00:00
|
|
|
#: List of contexts in creation order by via= parameter. When
|
|
|
|
#: :attr:`max_interpreters` is reached, the most recently used context
|
|
|
|
#: is destroyed to make room for any additional context.
|
2018-04-17 11:42:53 +00:00
|
|
|
self._lru_by_via = {}
|
2018-10-29 15:27:20 +00:00
|
|
|
#: :func:`key_from_dict` result by Context.
|
2018-04-16 13:42:53 +00:00
|
|
|
self._key_by_context = {}
|
2018-10-29 19:30:56 +00:00
|
|
|
#: Mapping of Context -> parent Context
|
|
|
|
self._via_by_context = {}
|
|
|
|
|
|
|
|
@mitogen.service.expose(mitogen.service.AllowParents())
|
|
|
|
@mitogen.service.arg_spec({
|
|
|
|
'context': mitogen.core.Context
|
|
|
|
})
|
|
|
|
def reset(self, context):
|
|
|
|
"""
|
|
|
|
Return a reference, forcing close and discard of the underlying
|
|
|
|
connection. Used for 'meta: reset_connection' or when some other error
|
|
|
|
is detected.
|
|
|
|
"""
|
|
|
|
LOG.debug('%r.reset(%r)', self, context)
|
|
|
|
self._lock.acquire()
|
|
|
|
try:
|
|
|
|
self._shutdown_unlocked(context)
|
|
|
|
finally:
|
|
|
|
self._lock.release()
|
2018-02-28 18:59:17 +00:00
|
|
|
|
2018-04-08 22:50:57 +00:00
|
|
|
@mitogen.service.expose(mitogen.service.AllowParents())
|
|
|
|
@mitogen.service.arg_spec({
|
2018-04-13 12:31:25 +00:00
|
|
|
'context': mitogen.core.Context
|
2018-04-08 22:50:57 +00:00
|
|
|
})
|
2018-04-13 12:31:25 +00:00
|
|
|
def put(self, context):
|
2018-04-09 01:50:37 +00:00
|
|
|
"""
|
2018-04-13 12:31:25 +00:00
|
|
|
Return a reference, making it eligable for recycling once its reference
|
|
|
|
count reaches zero.
|
|
|
|
"""
|
|
|
|
LOG.debug('%r.put(%r)', self, context)
|
2018-09-11 23:34:53 +00:00
|
|
|
self._lock.acquire()
|
|
|
|
try:
|
|
|
|
if self._refs_by_context.get(context, 0) == 0:
|
|
|
|
LOG.warning('%r.put(%r): refcount was 0. shutdown_all called?',
|
|
|
|
self, context)
|
|
|
|
return
|
|
|
|
self._refs_by_context[context] -= 1
|
|
|
|
finally:
|
|
|
|
self._lock.release()
|
2018-04-09 01:50:37 +00:00
|
|
|
|
2018-04-13 12:31:25 +00:00
|
|
|
def _produce_response(self, key, response):
|
2018-04-16 13:42:53 +00:00
|
|
|
"""
|
|
|
|
Reply to every waiting request matching a configuration key with a
|
|
|
|
response dictionary, deleting the list of waiters when done.
|
|
|
|
|
|
|
|
:param str key:
|
2018-10-29 15:27:20 +00:00
|
|
|
Result of :meth:`key_from_dict`
|
2018-04-16 13:42:53 +00:00
|
|
|
:param dict response:
|
|
|
|
Response dictionary
|
|
|
|
:returns:
|
|
|
|
Number of waiters that were replied to.
|
|
|
|
"""
|
2018-04-13 12:31:25 +00:00
|
|
|
self._lock.acquire()
|
|
|
|
try:
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
latches = self._latches_by_key.pop(key)
|
|
|
|
count = len(latches)
|
|
|
|
for latch in latches:
|
|
|
|
latch.put(response)
|
2018-04-13 12:31:25 +00:00
|
|
|
finally:
|
|
|
|
self._lock.release()
|
|
|
|
return count
|
|
|
|
|
2018-10-29 19:30:56 +00:00
|
|
|
def _forget_context_unlocked(self, context):
|
|
|
|
key = self._key_by_context.get(context)
|
|
|
|
if key is None:
|
|
|
|
LOG.debug('%r: attempt to forget unknown %r', self, context)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._response_by_key.pop(key, None)
|
|
|
|
self._latches_by_key.pop(key, None)
|
|
|
|
self._key_by_context.pop(context, None)
|
|
|
|
self._refs_by_context.pop(context, None)
|
|
|
|
self._via_by_context.pop(context, None)
|
|
|
|
self._lru_by_via.pop(context, None)
|
|
|
|
|
2018-09-12 17:56:06 +00:00
|
|
|
def _shutdown_unlocked(self, context, lru=None, new_context=None):
|
2018-04-17 11:42:53 +00:00
|
|
|
"""
|
|
|
|
Arrange for `context` to be shut down, and optionally add `new_context`
|
|
|
|
to the LRU list while holding the lock.
|
|
|
|
"""
|
2018-09-12 17:56:06 +00:00
|
|
|
LOG.info('%r._shutdown_unlocked(): shutting down %r', self, context)
|
2018-04-17 11:42:53 +00:00
|
|
|
context.shutdown()
|
2018-10-29 19:30:56 +00:00
|
|
|
via = self._via_by_context.get(context)
|
|
|
|
if via:
|
|
|
|
lru = self._lru_by_via.get(via)
|
|
|
|
if lru:
|
|
|
|
if context in lru:
|
|
|
|
lru.remove(context)
|
|
|
|
if new_context:
|
|
|
|
lru.append(new_context)
|
|
|
|
self._forget_context_unlocked(context)
|
2018-04-17 11:42:53 +00:00
|
|
|
|
2018-09-12 17:56:06 +00:00
|
|
|
def _update_lru_unlocked(self, new_context, spec, via):
|
2018-04-16 13:42:53 +00:00
|
|
|
"""
|
|
|
|
Update the LRU ("MRU"?) list associated with the connection described
|
|
|
|
by `kwargs`, destroying the most recently created context if the list
|
|
|
|
is full. Finally add `new_context` to the list.
|
|
|
|
"""
|
2018-04-17 11:42:53 +00:00
|
|
|
lru = self._lru_by_via.setdefault(via, [])
|
2018-04-13 14:16:54 +00:00
|
|
|
if len(lru) < self.max_interpreters:
|
2018-04-13 12:31:25 +00:00
|
|
|
lru.append(new_context)
|
|
|
|
return
|
|
|
|
|
|
|
|
for context in reversed(lru):
|
|
|
|
if self._refs_by_context[context] == 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
LOG.warning('via=%r reached maximum number of interpreters, '
|
|
|
|
'but they are all marked as in-use.', via)
|
|
|
|
return
|
|
|
|
|
2018-10-29 19:30:56 +00:00
|
|
|
self._via_by_context[new_context] = via
|
2018-09-12 17:56:06 +00:00
|
|
|
self._shutdown_unlocked(context, lru=lru, new_context=new_context)
|
|
|
|
|
|
|
|
def _update_lru(self, new_context, spec, via):
|
|
|
|
self._lock.acquire()
|
|
|
|
try:
|
|
|
|
self._update_lru_unlocked(new_context, spec, via)
|
|
|
|
finally:
|
|
|
|
self._lock.release()
|
2018-04-13 12:31:25 +00:00
|
|
|
|
2018-04-17 11:42:53 +00:00
|
|
|
@mitogen.service.expose(mitogen.service.AllowParents())
|
|
|
|
def shutdown_all(self):
|
|
|
|
"""
|
|
|
|
For testing use, arrange for all connections to be shut down.
|
|
|
|
"""
|
2018-09-12 17:56:06 +00:00
|
|
|
self._lock.acquire()
|
|
|
|
try:
|
|
|
|
for context in list(self._key_by_context):
|
|
|
|
self._shutdown_unlocked(context)
|
|
|
|
finally:
|
|
|
|
self._lock.release()
|
2018-04-13 12:31:25 +00:00
|
|
|
|
2018-04-21 17:21:24 +00:00
|
|
|
def _on_stream_disconnect(self, stream):
|
|
|
|
"""
|
|
|
|
Respond to Stream disconnection by deleting any record of contexts
|
|
|
|
reached via that stream. This method runs in the Broker thread and must
|
|
|
|
not to block.
|
|
|
|
"""
|
|
|
|
# TODO: there is a race between creation of a context and disconnection
|
|
|
|
# of its related stream. An error reply should be sent to any message
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
# in _latches_by_key below.
|
2018-04-21 17:21:24 +00:00
|
|
|
self._lock.acquire()
|
|
|
|
try:
|
2018-10-24 12:02:46 +00:00
|
|
|
routes = self.router.route_monitor.get_routes(stream)
|
2018-10-29 19:30:56 +00:00
|
|
|
for context in list(self._key_by_context):
|
2018-10-24 12:02:46 +00:00
|
|
|
if context.context_id in routes:
|
2018-04-21 17:21:24 +00:00
|
|
|
LOG.info('Dropping %r due to disconnect of %r',
|
|
|
|
context, stream)
|
2018-10-29 19:30:56 +00:00
|
|
|
self._forget_context_unlocked(context)
|
2018-04-21 17:21:24 +00:00
|
|
|
finally:
|
|
|
|
self._lock.release()
|
|
|
|
|
2018-05-29 12:34:20 +00:00
|
|
|
ALWAYS_PRELOAD = (
|
2018-06-07 15:45:02 +00:00
|
|
|
'ansible.module_utils.basic',
|
2018-05-29 12:34:20 +00:00
|
|
|
'ansible.module_utils.json_utils',
|
2018-06-07 15:45:02 +00:00
|
|
|
'ansible.release',
|
2018-05-29 12:34:20 +00:00
|
|
|
'ansible_mitogen.runner',
|
2018-06-07 15:45:02 +00:00
|
|
|
'ansible_mitogen.target',
|
2018-05-29 12:34:20 +00:00
|
|
|
'mitogen.fork',
|
2018-06-07 15:45:02 +00:00
|
|
|
'mitogen.service',
|
2018-05-29 12:34:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def _send_module_forwards(self, context):
|
2018-09-08 15:39:45 +00:00
|
|
|
self.router.responder.forward_modules(context, self.ALWAYS_PRELOAD)
|
2018-05-29 12:34:20 +00:00
|
|
|
|
2018-08-19 17:50:53 +00:00
|
|
|
_candidate_temp_dirs = None
|
|
|
|
|
|
|
|
def _get_candidate_temp_dirs(self):
|
|
|
|
"""
|
|
|
|
Return a list of locations to try to create the single temporary
|
|
|
|
directory used by the run. This simply caches the (expensive) plugin
|
|
|
|
load of :func:`_get_candidate_temp_dirs`.
|
|
|
|
"""
|
|
|
|
if self._candidate_temp_dirs is None:
|
|
|
|
self._candidate_temp_dirs = _get_candidate_temp_dirs()
|
|
|
|
return self._candidate_temp_dirs
|
|
|
|
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
def _connect(self, key, spec, via=None):
|
2018-04-16 13:42:53 +00:00
|
|
|
"""
|
|
|
|
Actual connect implementation. Arranges for the Mitogen connection to
|
|
|
|
be created and enqueues an asynchronous call to start the forked task
|
|
|
|
parent in the remote context.
|
|
|
|
|
|
|
|
:param key:
|
|
|
|
Deduplication key representing the connection configuration.
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
:param spec:
|
|
|
|
Connection specification.
|
2018-04-16 13:42:53 +00:00
|
|
|
:returns:
|
|
|
|
Dict like::
|
|
|
|
|
|
|
|
{
|
|
|
|
'context': mitogen.core.Context or None,
|
2018-07-24 04:43:06 +00:00
|
|
|
'via': mitogen.core.Context or None,
|
2018-06-09 21:11:26 +00:00
|
|
|
'init_child_result': {
|
|
|
|
'fork_context': mitogen.core.Context,
|
|
|
|
'home_dir': str or None,
|
|
|
|
},
|
2018-04-16 13:42:53 +00:00
|
|
|
'msg': str or None
|
|
|
|
}
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
Where `context` is a reference to the newly constructed context,
|
|
|
|
`init_child_result` is the result of executing
|
|
|
|
:func:`ansible_mitogen.target.init_child` in that context, `msg` is
|
|
|
|
an error message and the remaining fields are :data:`None`, or
|
|
|
|
`msg` is :data:`None` and the remaining fields are set.
|
2018-04-16 13:42:53 +00:00
|
|
|
"""
|
2018-04-05 19:14:14 +00:00
|
|
|
try:
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
method = getattr(self.router, spec['method'])
|
|
|
|
except AttributeError:
|
|
|
|
raise Error('unsupported method: %(transport)s' % spec)
|
|
|
|
|
2018-05-08 15:10:44 +00:00
|
|
|
context = method(via=via, unidirectional=True, **spec['kwargs'])
|
2018-04-29 08:53:16 +00:00
|
|
|
if via and spec.get('enable_lru'):
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
self._update_lru(context, spec, via)
|
2018-04-21 17:21:24 +00:00
|
|
|
else:
|
|
|
|
# For directly connected contexts, listen to the associated
|
|
|
|
# Stream's disconnect event and use it to invalidate dependent
|
|
|
|
# Contexts.
|
|
|
|
stream = self.router.stream_by_id(context.context_id)
|
|
|
|
mitogen.core.listen(stream, 'disconnect',
|
|
|
|
lambda: self._on_stream_disconnect(stream))
|
|
|
|
|
2018-05-29 12:34:20 +00:00
|
|
|
self._send_module_forwards(context)
|
2018-08-19 17:50:53 +00:00
|
|
|
init_child_result = context.call(
|
|
|
|
ansible_mitogen.target.init_child,
|
|
|
|
log_level=LOG.getEffectiveLevel(),
|
|
|
|
candidate_temp_dirs=self._get_candidate_temp_dirs(),
|
|
|
|
)
|
2018-04-21 17:21:24 +00:00
|
|
|
|
2018-04-18 22:37:42 +00:00
|
|
|
if os.environ.get('MITOGEN_DUMP_THREAD_STACKS'):
|
2018-04-20 13:20:05 +00:00
|
|
|
from mitogen import debug
|
|
|
|
context.call(debug.dump_to_logger)
|
2018-04-21 17:21:24 +00:00
|
|
|
|
2018-04-16 13:42:53 +00:00
|
|
|
self._key_by_context[context] = key
|
2018-04-13 12:31:25 +00:00
|
|
|
self._refs_by_context[context] = 0
|
2018-04-05 19:14:14 +00:00
|
|
|
return {
|
|
|
|
'context': context,
|
2018-07-24 04:43:06 +00:00
|
|
|
'via': via,
|
2018-06-09 21:11:26 +00:00
|
|
|
'init_child_result': init_child_result,
|
2018-04-05 19:14:14 +00:00
|
|
|
'msg': None,
|
|
|
|
}
|
2018-03-29 08:07:19 +00:00
|
|
|
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
def _wait_or_start(self, spec, via=None):
|
|
|
|
latch = mitogen.core.Latch()
|
2018-10-29 15:27:20 +00:00
|
|
|
key = key_from_dict(via=via, **spec)
|
2018-04-13 12:31:25 +00:00
|
|
|
self._lock.acquire()
|
|
|
|
try:
|
|
|
|
response = self._response_by_key.get(key)
|
|
|
|
if response is not None:
|
|
|
|
self._refs_by_context[response['context']] += 1
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
latch.put(response)
|
|
|
|
return latch
|
2018-04-13 12:31:25 +00:00
|
|
|
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
latches = self._latches_by_key.setdefault(key, [])
|
|
|
|
first = len(latches) == 0
|
|
|
|
latches.append(latch)
|
2018-04-13 12:31:25 +00:00
|
|
|
finally:
|
|
|
|
self._lock.release()
|
|
|
|
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
if first:
|
|
|
|
# I'm the first requestee, so I will create the connection.
|
|
|
|
try:
|
|
|
|
response = self._connect(key, spec, via=via)
|
|
|
|
count = self._produce_response(key, response)
|
2018-04-16 13:42:53 +00:00
|
|
|
# Only record the response for non-error results.
|
2018-04-13 12:31:25 +00:00
|
|
|
self._response_by_key[key] = response
|
2018-04-16 13:42:53 +00:00
|
|
|
# Set the reference count to the number of waiters.
|
2018-04-13 12:31:25 +00:00
|
|
|
self._refs_by_context[response['context']] += count
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
except Exception:
|
|
|
|
self._produce_response(key, sys.exc_info())
|
|
|
|
|
|
|
|
return latch
|
|
|
|
|
2018-09-07 22:46:56 +00:00
|
|
|
disconnect_msg = (
|
|
|
|
'Channel was disconnected while connection attempt was in progress; '
|
|
|
|
'this may be caused by an abnormal Ansible exit, or due to an '
|
|
|
|
'unreliable target.'
|
|
|
|
)
|
|
|
|
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
@mitogen.service.expose(mitogen.service.AllowParents())
|
|
|
|
@mitogen.service.arg_spec({
|
|
|
|
'stack': list
|
|
|
|
})
|
|
|
|
def get(self, msg, stack):
|
|
|
|
"""
|
|
|
|
Return a Context referring to an established connection with the given
|
|
|
|
configuration, establishing new connections as necessary.
|
|
|
|
|
|
|
|
:param list stack:
|
|
|
|
Connection descriptions. Each element is a dict containing 'method'
|
|
|
|
and 'kwargs' keys describing the Router method and arguments.
|
|
|
|
Subsequent elements are proxied via the previous.
|
|
|
|
|
|
|
|
:returns dict:
|
2018-07-24 02:11:23 +00:00
|
|
|
* context: mitogen.parent.Context or None.
|
2018-06-09 21:11:26 +00:00
|
|
|
* init_child_result: Result of :func:`init_child`.
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
* msg: StreamError exception text or None.
|
|
|
|
* method_name: string failing method name.
|
|
|
|
"""
|
|
|
|
via = None
|
|
|
|
for spec in stack:
|
|
|
|
try:
|
|
|
|
result = self._wait_or_start(spec, via=via).get()
|
|
|
|
if isinstance(result, tuple): # exc_info()
|
2018-04-17 16:40:45 +00:00
|
|
|
reraise(*result)
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
via = result['context']
|
2018-09-07 22:46:56 +00:00
|
|
|
except mitogen.core.ChannelError:
|
|
|
|
return {
|
|
|
|
'context': None,
|
|
|
|
'init_child_result': None,
|
|
|
|
'method_name': spec['method'],
|
|
|
|
'msg': self.disconnect_msg,
|
|
|
|
}
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
except mitogen.core.StreamError as e:
|
|
|
|
return {
|
|
|
|
'context': None,
|
2018-06-09 21:11:26 +00:00
|
|
|
'init_child_result': None,
|
ansible: connection delegation v1
This implements the first edition of Connection Delegation, where
delegating connection establishment is initially single-threaded.
ansible_mitogen/strategy.py:
ansible_mitogen/plugins/connection/*:
Begin splitting connection.Connection into subclasses, exposing them
directly as "mitogen_ssh", "mitogen_local", etc. connection types.
This is far from removing strategy.py, but it's a tiny start.
ansible_mitogen/connection.py:
* config_from_play_context() and config_from_host_vars() build up a
huge dictionary containing either more or less PlayContext contents,
or our best attempt at reconstructing a host's connection config
from its hostvars, where that config is not the current
WorkerProcess target.
They both produce the same format with the same keys, allowing
remaining code to have a single input format.
These dicts contain fields named after how Ansible refers to them,
e.g. "sudo_exe".
* _config_from_via() parses a basic connection specification like
"username@inventory_name" into one of the aforementioned dicts.
* _stack_from_config() produces a list of dicts describing the order
in which (Mitogen) connections should be established, such that each
element is proxied via= the previous element. The dicts produced by
this function use Mitogen keyword arguments, the former di.
These dicts contain fields named after how Mitogen refers to them,
e.g. "sudo_path".
* Pass the stack to ContextService, which is responsible for actual
setup of the full chain.
ansible_mitogen/services.py:
Teach get() to walk the supplied stack, establishing each connection
in turn, creating refounts for it before continuing.
TODO: refcounting is broken in a variety of cases.
2018-04-24 21:42:02 +00:00
|
|
|
'method_name': spec['method'],
|
|
|
|
'msg': str(e),
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2018-04-13 12:31:25 +00:00
|
|
|
|
2018-03-29 08:07:19 +00:00
|
|
|
|
2018-05-12 13:14:35 +00:00
|
|
|
class ModuleDepService(mitogen.service.Service):
|
|
|
|
"""
|
|
|
|
Scan a new-style module and produce a cached mapping of module_utils names
|
|
|
|
to their resolved filesystem paths.
|
|
|
|
"""
|
2018-07-31 20:05:07 +00:00
|
|
|
invoker_class = mitogen.service.SerializedInvoker
|
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ModuleDepService, self).__init__(*args, **kwargs)
|
2018-05-12 13:14:35 +00:00
|
|
|
self._cache = {}
|
|
|
|
|
2018-05-29 13:45:12 +00:00
|
|
|
def _get_builtin_names(self, builtin_path, resolved):
|
|
|
|
return [
|
|
|
|
fullname
|
|
|
|
for fullname, path, is_pkg in resolved
|
|
|
|
if os.path.abspath(path).startswith(builtin_path)
|
|
|
|
]
|
|
|
|
|
|
|
|
def _get_custom_tups(self, builtin_path, resolved):
|
|
|
|
return [
|
|
|
|
(fullname, path, is_pkg)
|
|
|
|
for fullname, path, is_pkg in resolved
|
|
|
|
if not os.path.abspath(path).startswith(builtin_path)
|
|
|
|
]
|
|
|
|
|
2018-05-12 13:14:35 +00:00
|
|
|
@mitogen.service.expose(policy=mitogen.service.AllowParents())
|
|
|
|
@mitogen.service.arg_spec({
|
2018-04-17 16:40:45 +00:00
|
|
|
'module_name': mitogen.core.UnicodeType,
|
|
|
|
'module_path': mitogen.core.FsPathTypes,
|
2018-05-12 13:14:35 +00:00
|
|
|
'search_path': tuple,
|
2018-04-17 16:40:45 +00:00
|
|
|
'builtin_path': mitogen.core.FsPathTypes,
|
2018-05-29 13:45:12 +00:00
|
|
|
'context': mitogen.core.Context,
|
2018-05-12 13:14:35 +00:00
|
|
|
})
|
2018-05-29 13:45:12 +00:00
|
|
|
def scan(self, module_name, module_path, search_path, builtin_path, context):
|
|
|
|
key = (module_name, search_path)
|
|
|
|
if key not in self._cache:
|
2018-05-12 13:14:35 +00:00
|
|
|
resolved = ansible_mitogen.module_finder.scan(
|
|
|
|
module_name=module_name,
|
|
|
|
module_path=module_path,
|
2018-05-13 02:18:22 +00:00
|
|
|
search_path=tuple(search_path) + (builtin_path,),
|
2018-05-12 13:14:35 +00:00
|
|
|
)
|
2018-05-13 02:18:22 +00:00
|
|
|
builtin_path = os.path.abspath(builtin_path)
|
2018-05-29 13:45:12 +00:00
|
|
|
builtin = self._get_builtin_names(builtin_path, resolved)
|
|
|
|
custom = self._get_custom_tups(builtin_path, resolved)
|
|
|
|
self._cache[key] = {
|
|
|
|
'builtin': builtin,
|
|
|
|
'custom': custom,
|
|
|
|
}
|
2018-06-07 15:48:42 +00:00
|
|
|
return self._cache[key]
|