2017-11-19 13:50:26 +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:
|
|
|
|
#
|
2018-02-27 11:28:26 +00:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2017-11-19 13:50:26 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2018-02-27 11:28:26 +00:00
|
|
|
# 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.
|
2017-11-19 13:50:26 +00:00
|
|
|
|
2018-02-14 16:45:35 +00:00
|
|
|
from __future__ import absolute_import
|
2018-04-17 16:40:45 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-01-20 18:42:02 +00:00
|
|
|
import errno
|
2018-02-18 07:47:09 +00:00
|
|
|
import logging
|
2018-02-14 23:10:56 +00:00
|
|
|
import os
|
2018-10-31 18:30:03 +00:00
|
|
|
import pprint
|
2018-09-11 02:44:17 +00:00
|
|
|
import random
|
2018-04-28 19:11:03 +00:00
|
|
|
import stat
|
2019-01-19 05:15:56 +00:00
|
|
|
import sys
|
2018-02-18 07:47:09 +00:00
|
|
|
import time
|
2017-09-15 06:24:41 +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
|
|
|
import jinja2.runtime
|
|
|
|
import ansible.constants as C
|
2017-09-21 18:48:50 +00:00
|
|
|
import ansible.errors
|
2017-09-15 06:24:41 +00:00
|
|
|
import ansible.plugins.connection
|
2018-06-19 17:46:30 +00:00
|
|
|
import ansible.utils.shlex
|
2018-02-17 11:41:15 +00:00
|
|
|
|
2018-11-05 14:28:19 +00:00
|
|
|
import mitogen.fork
|
2018-02-14 16:45:35 +00:00
|
|
|
import mitogen.unix
|
2018-04-05 19:14:14 +00:00
|
|
|
import mitogen.utils
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2018-07-17 22:45:28 +00:00
|
|
|
import ansible_mitogen.parsing
|
2018-03-16 23:39:30 +00:00
|
|
|
import ansible_mitogen.process
|
2018-04-22 01:48:06 +00:00
|
|
|
import ansible_mitogen.services
|
2018-07-17 22:45:28 +00:00
|
|
|
import ansible_mitogen.target
|
2018-02-15 09:43:19 +00:00
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2018-02-18 07:47:09 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-08-12 09:35:32 +00:00
|
|
|
def optional_secret(value):
|
|
|
|
"""
|
|
|
|
Wrap `value` in :class:`mitogen.core.Secret` if it is not :data:`None`,
|
|
|
|
otherwise return :data:`None`.
|
|
|
|
"""
|
|
|
|
if value is not None:
|
|
|
|
return mitogen.core.Secret(value)
|
|
|
|
|
|
|
|
|
2018-12-08 16:07:41 +00:00
|
|
|
def optional_int(value):
|
|
|
|
"""
|
|
|
|
Convert `value` to an integer if it is not :data:`None`, otherwise return
|
|
|
|
:data:`None`.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return int(value)
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-08-12 09:35:32 +00:00
|
|
|
def parse_python_path(s):
|
|
|
|
"""
|
|
|
|
Given the string set for ansible_python_interpeter, parse it using shell
|
|
|
|
syntax and return an appropriate argument vector.
|
|
|
|
"""
|
|
|
|
if s:
|
|
|
|
return ansible.utils.shlex.shlex_split(s)
|
|
|
|
|
|
|
|
|
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_local(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for a local connection.
|
|
|
|
"""
|
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
|
|
|
return {
|
|
|
|
'method': 'local',
|
|
|
|
'kwargs': {
|
|
|
|
'python_path': spec['python_path'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 00:22:46 +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 _connect_ssh(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for an SSH connection.
|
|
|
|
"""
|
2018-05-02 16:47:06 +00:00
|
|
|
if C.HOST_KEY_CHECKING:
|
|
|
|
check_host_keys = 'enforce'
|
|
|
|
else:
|
|
|
|
check_host_keys = 'ignore'
|
|
|
|
|
2018-10-30 14:58:35 +00:00
|
|
|
# #334: tilde-expand private_key_file to avoid implementation difference
|
|
|
|
# between Python and OpenSSH.
|
|
|
|
private_key_file = spec['private_key_file']
|
|
|
|
if private_key_file is not None:
|
|
|
|
private_key_file = os.path.expanduser(private_key_file)
|
|
|
|
|
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
|
|
|
return {
|
|
|
|
'method': 'ssh',
|
|
|
|
'kwargs': {
|
2018-05-02 16:47:06 +00:00
|
|
|
'check_host_keys': check_host_keys,
|
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
|
|
|
'hostname': spec['remote_addr'],
|
|
|
|
'username': spec['remote_user'],
|
2018-08-12 09:35:32 +00:00
|
|
|
'password': optional_secret(spec['password']),
|
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
|
|
|
'port': spec['port'],
|
|
|
|
'python_path': spec['python_path'],
|
2018-10-30 14:58:35 +00:00
|
|
|
'identity_file': private_key_file,
|
2018-08-19 21:11:24 +00:00
|
|
|
'identities_only': False,
|
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
|
|
|
'ssh_path': spec['ssh_executable'],
|
|
|
|
'connect_timeout': spec['ansible_ssh_timeout'],
|
|
|
|
'ssh_args': spec['ssh_args'],
|
2018-06-22 15:31:55 +00:00
|
|
|
'ssh_debug_level': spec['mitogen_ssh_debug_level'],
|
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_docker(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for a Docker connection.
|
|
|
|
"""
|
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
|
|
|
return {
|
|
|
|
'method': 'docker',
|
|
|
|
'kwargs': {
|
|
|
|
'username': spec['remote_user'],
|
|
|
|
'container': spec['remote_addr'],
|
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'connect_timeout': spec['ansible_ssh_timeout'] or spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-19 14:52:20 +00:00
|
|
|
def _connect_kubectl(spec):
|
|
|
|
"""
|
|
|
|
Return ContextService arguments for a Kubernetes connection.
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'method': 'kubectl',
|
|
|
|
'kwargs': {
|
|
|
|
'pod': spec['remote_addr'],
|
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'connect_timeout': spec['ansible_ssh_timeout'] or spec['timeout'],
|
2018-10-31 00:48:11 +00:00
|
|
|
'kubectl_path': spec['mitogen_kubectl_path'],
|
2018-10-02 18:58:23 +00:00
|
|
|
'kubectl_args': spec['extra_args'],
|
2018-09-19 14:52:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-27 05:21:10 +00:00
|
|
|
def _connect_jail(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for a FreeBSD jail connection.
|
|
|
|
"""
|
2018-04-27 05:21:10 +00:00
|
|
|
return {
|
|
|
|
'method': 'jail',
|
|
|
|
'kwargs': {
|
|
|
|
'username': spec['remote_user'],
|
|
|
|
'container': spec['remote_addr'],
|
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'connect_timeout': spec['ansible_ssh_timeout'] or spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-27 03:40:56 +00:00
|
|
|
def _connect_lxc(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for an LXC Classic container connection.
|
|
|
|
"""
|
2018-04-27 03:40:56 +00:00
|
|
|
return {
|
|
|
|
'method': 'lxc',
|
|
|
|
'kwargs': {
|
|
|
|
'container': spec['remote_addr'],
|
|
|
|
'python_path': spec['python_path'],
|
2018-10-31 12:40:08 +00:00
|
|
|
'lxc_attach_path': spec['mitogen_lxc_attach_path'],
|
2018-04-27 03:40:56 +00:00
|
|
|
'connect_timeout': spec['ansible_ssh_timeout'] or spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-10 13:21:10 +00:00
|
|
|
def _connect_lxd(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for an LXD container connection.
|
|
|
|
"""
|
2018-08-10 13:21:10 +00:00
|
|
|
return {
|
|
|
|
'method': 'lxd',
|
|
|
|
'kwargs': {
|
|
|
|
'container': spec['remote_addr'],
|
|
|
|
'python_path': spec['python_path'],
|
2018-10-31 12:40:08 +00:00
|
|
|
'lxc_path': spec['mitogen_lxc_path'],
|
2018-08-10 13:21:10 +00:00
|
|
|
'connect_timeout': spec['ansible_ssh_timeout'] or spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 23:31:20 +00:00
|
|
|
def _connect_machinectl(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for a machinectl connection.
|
|
|
|
"""
|
2018-04-28 23:31:20 +00:00
|
|
|
return _connect_setns(dict(spec, mitogen_kind='machinectl'))
|
|
|
|
|
|
|
|
|
|
|
|
def _connect_setns(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for a mitogen_setns connection.
|
|
|
|
"""
|
2018-04-28 23:31:20 +00:00
|
|
|
return {
|
|
|
|
'method': 'setns',
|
|
|
|
'kwargs': {
|
|
|
|
'container': spec['remote_addr'],
|
|
|
|
'username': spec['remote_user'],
|
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'kind': spec['mitogen_kind'],
|
|
|
|
'docker_path': spec['mitogen_docker_path'],
|
2018-10-31 12:40:08 +00:00
|
|
|
'lxc_path': spec['mitogen_lxc_path'],
|
2018-04-28 23:31:20 +00:00
|
|
|
'lxc_info_path': spec['mitogen_lxc_info_path'],
|
|
|
|
'machinectl_path': spec['mitogen_machinectl_path'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 02:49:05 +00:00
|
|
|
def _connect_su(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for su as a become method.
|
|
|
|
"""
|
2018-05-02 02:49:05 +00:00
|
|
|
return {
|
|
|
|
'method': 'su',
|
|
|
|
'enable_lru': True,
|
|
|
|
'kwargs': {
|
|
|
|
'username': spec['become_user'],
|
2018-08-12 09:35:32 +00:00
|
|
|
'password': optional_secret(spec['become_pass']),
|
2018-05-02 02:49:05 +00:00
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'su_path': spec['become_exe'],
|
|
|
|
'connect_timeout': spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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_sudo(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for sudo as a become method.
|
|
|
|
"""
|
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
|
|
|
return {
|
|
|
|
'method': 'sudo',
|
2018-04-29 08:53:16 +00:00
|
|
|
'enable_lru': True,
|
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
|
|
|
'kwargs': {
|
|
|
|
'username': spec['become_user'],
|
2018-08-12 09:35:32 +00:00
|
|
|
'password': optional_secret(spec['become_pass']),
|
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
|
|
|
'python_path': spec['python_path'],
|
2018-04-27 00:42:20 +00:00
|
|
|
'sudo_path': spec['become_exe'],
|
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
|
|
|
'connect_timeout': spec['timeout'],
|
|
|
|
'sudo_args': spec['sudo_args'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-12 18:54:41 +00:00
|
|
|
def _connect_doas(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for doas as a become method.
|
|
|
|
"""
|
2018-07-12 18:54:41 +00:00
|
|
|
return {
|
|
|
|
'method': 'doas',
|
|
|
|
'enable_lru': True,
|
|
|
|
'kwargs': {
|
|
|
|
'username': spec['become_user'],
|
2018-08-12 09:35:32 +00:00
|
|
|
'password': optional_secret(spec['become_pass']),
|
2018-07-12 18:54:41 +00:00
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'doas_path': spec['become_exe'],
|
|
|
|
'connect_timeout': spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 02:49:05 +00:00
|
|
|
def _connect_mitogen_su(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for su as a first class connection.
|
|
|
|
"""
|
2018-05-02 02:49:05 +00:00
|
|
|
return {
|
|
|
|
'method': 'su',
|
|
|
|
'kwargs': {
|
|
|
|
'username': spec['remote_user'],
|
2018-08-12 09:35:32 +00:00
|
|
|
'password': optional_secret(spec['password']),
|
2018-05-02 02:49:05 +00:00
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'su_path': spec['become_exe'],
|
|
|
|
'connect_timeout': spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-29 00:37:53 +00:00
|
|
|
def _connect_mitogen_sudo(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for sudo as a first class connection.
|
|
|
|
"""
|
2018-04-29 00:37:53 +00:00
|
|
|
return {
|
|
|
|
'method': 'sudo',
|
|
|
|
'kwargs': {
|
|
|
|
'username': spec['remote_user'],
|
2018-08-12 09:35:32 +00:00
|
|
|
'password': optional_secret(spec['password']),
|
2018-04-29 00:37:53 +00:00
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'sudo_path': spec['become_exe'],
|
|
|
|
'connect_timeout': spec['timeout'],
|
|
|
|
'sudo_args': spec['sudo_args'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-12 18:54:41 +00:00
|
|
|
def _connect_mitogen_doas(spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Return ContextService arguments for doas as a first class connection.
|
|
|
|
"""
|
2018-07-12 18:54:41 +00:00
|
|
|
return {
|
|
|
|
'method': 'doas',
|
|
|
|
'kwargs': {
|
|
|
|
'username': spec['remote_user'],
|
2018-08-12 09:35:32 +00:00
|
|
|
'password': optional_secret(spec['password']),
|
2018-07-12 18:54:41 +00:00
|
|
|
'python_path': spec['python_path'],
|
|
|
|
'doas_path': spec['become_exe'],
|
|
|
|
'connect_timeout': spec['timeout'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-12 09:35:32 +00:00
|
|
|
#: Mapping of connection method names to functions invoked as `func(spec)`
|
|
|
|
#: generating ContextService keyword arguments matching a connection
|
|
|
|
#: specification.
|
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
|
|
|
CONNECTION_METHOD = {
|
|
|
|
'docker': _connect_docker,
|
2018-09-19 14:52:20 +00:00
|
|
|
'kubectl': _connect_kubectl,
|
2018-04-27 05:21:10 +00:00
|
|
|
'jail': _connect_jail,
|
2018-04-27 03:40:56 +00:00
|
|
|
'local': _connect_local,
|
|
|
|
'lxc': _connect_lxc,
|
2018-08-10 13:21:10 +00:00
|
|
|
'lxd': _connect_lxd,
|
2018-04-28 23:31:20 +00:00
|
|
|
'machinectl': _connect_machinectl,
|
|
|
|
'setns': _connect_setns,
|
2018-04-27 03:40:56 +00:00
|
|
|
'ssh': _connect_ssh,
|
2018-05-02 02:49:05 +00:00
|
|
|
'su': _connect_su,
|
2018-04-27 03:40:56 +00:00
|
|
|
'sudo': _connect_sudo,
|
2018-07-12 18:54:41 +00:00
|
|
|
'doas': _connect_doas,
|
2018-05-02 02:49:05 +00:00
|
|
|
'mitogen_su': _connect_mitogen_su,
|
2018-04-29 00:37:53 +00:00
|
|
|
'mitogen_sudo': _connect_mitogen_sudo,
|
2018-07-12 18:54:41 +00:00
|
|
|
'mitogen_doas': _connect_mitogen_doas,
|
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 config_from_play_context(transport, inventory_name, connection):
|
|
|
|
"""
|
|
|
|
Return a dict representing all important connection configuration, allowing
|
|
|
|
the same functions to work regardless of whether configuration came from
|
|
|
|
play_context (direct connection) or host vars (mitogen_via=).
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'transport': transport,
|
|
|
|
'inventory_name': inventory_name,
|
|
|
|
'remote_addr': connection._play_context.remote_addr,
|
|
|
|
'remote_user': connection._play_context.remote_user,
|
|
|
|
'become': connection._play_context.become,
|
|
|
|
'become_method': connection._play_context.become_method,
|
|
|
|
'become_user': connection._play_context.become_user,
|
|
|
|
'become_pass': connection._play_context.become_pass,
|
|
|
|
'password': connection._play_context.password,
|
|
|
|
'port': connection._play_context.port,
|
2018-09-08 20:59:17 +00:00
|
|
|
'python_path': parse_python_path(
|
2018-09-08 21:35:42 +00:00
|
|
|
connection.get_task_var('ansible_python_interpreter',
|
|
|
|
default='/usr/bin/python')
|
2018-09-08 20:59:17 +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
|
|
|
'private_key_file': connection._play_context.private_key_file,
|
|
|
|
'ssh_executable': connection._play_context.ssh_executable,
|
|
|
|
'timeout': connection._play_context.timeout,
|
2018-09-08 20:59:17 +00:00
|
|
|
'ansible_ssh_timeout':
|
2018-09-08 21:35:42 +00:00
|
|
|
connection.get_task_var('ansible_ssh_timeout',
|
|
|
|
default=C.DEFAULT_TIMEOUT),
|
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
|
|
|
'ssh_args': [
|
2018-04-17 16:40:45 +00:00
|
|
|
mitogen.core.to_text(term)
|
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
|
|
|
for s in (
|
|
|
|
getattr(connection._play_context, 'ssh_args', ''),
|
|
|
|
getattr(connection._play_context, 'ssh_common_args', ''),
|
|
|
|
getattr(connection._play_context, 'ssh_extra_args', '')
|
|
|
|
)
|
2018-06-19 17:46:30 +00:00
|
|
|
for term in ansible.utils.shlex.shlex_split(s or '')
|
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
|
|
|
],
|
2018-04-27 00:42:20 +00:00
|
|
|
'become_exe': connection._play_context.become_exe,
|
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
|
|
|
'sudo_args': [
|
2018-04-17 16:40:45 +00:00
|
|
|
mitogen.core.to_text(term)
|
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
|
|
|
for s in (
|
|
|
|
connection._play_context.sudo_flags,
|
|
|
|
connection._play_context.become_flags
|
|
|
|
)
|
2018-06-19 17:46:30 +00:00
|
|
|
for term in ansible.utils.shlex.shlex_split(s or '')
|
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
|
|
|
],
|
2018-09-08 20:59:17 +00:00
|
|
|
'mitogen_via':
|
2018-09-08 21:35:42 +00:00
|
|
|
connection.get_task_var('mitogen_via'),
|
2018-09-08 20:59:17 +00:00
|
|
|
'mitogen_kind':
|
2018-09-08 21:35:42 +00:00
|
|
|
connection.get_task_var('mitogen_kind'),
|
2018-09-08 20:59:17 +00:00
|
|
|
'mitogen_docker_path':
|
2018-09-08 21:35:42 +00:00
|
|
|
connection.get_task_var('mitogen_docker_path'),
|
2018-09-19 14:52:20 +00:00
|
|
|
'mitogen_kubectl_path':
|
|
|
|
connection.get_task_var('mitogen_kubectl_path'),
|
2018-10-31 12:40:08 +00:00
|
|
|
'mitogen_lxc_path':
|
|
|
|
connection.get_task_var('mitogen_lxc_path'),
|
|
|
|
'mitogen_lxc_attach_path':
|
|
|
|
connection.get_task_var('mitogen_lxc_attach_path'),
|
2018-09-08 20:59:17 +00:00
|
|
|
'mitogen_lxc_info_path':
|
2018-09-08 21:35:42 +00:00
|
|
|
connection.get_task_var('mitogen_lxc_info_path'),
|
2018-09-08 20:59:17 +00:00
|
|
|
'mitogen_machinectl_path':
|
2018-09-08 21:35:42 +00:00
|
|
|
connection.get_task_var('mitogen_machinectl_path'),
|
2018-09-08 20:59:17 +00:00
|
|
|
'mitogen_ssh_debug_level':
|
2018-12-08 16:07:41 +00:00
|
|
|
optional_int(
|
|
|
|
connection.get_task_var('mitogen_ssh_debug_level')
|
|
|
|
),
|
2018-10-02 18:58:23 +00:00
|
|
|
'extra_args':
|
|
|
|
connection.get_extra_args(),
|
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 config_from_hostvars(transport, inventory_name, connection,
|
|
|
|
hostvars, become_user):
|
|
|
|
"""
|
|
|
|
Override config_from_play_context() to take equivalent information from
|
|
|
|
host vars.
|
|
|
|
"""
|
|
|
|
config = config_from_play_context(transport, inventory_name, connection)
|
|
|
|
hostvars = dict(hostvars)
|
|
|
|
return dict(config, **{
|
2018-08-12 09:37:32 +00:00
|
|
|
'remote_addr': hostvars.get('ansible_host', inventory_name),
|
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
|
|
|
'become': bool(become_user),
|
|
|
|
'become_user': become_user,
|
|
|
|
'become_pass': None,
|
|
|
|
'remote_user': hostvars.get('ansible_user'), # TODO
|
|
|
|
'password': (hostvars.get('ansible_ssh_pass') or
|
|
|
|
hostvars.get('ansible_password')),
|
|
|
|
'port': hostvars.get('ansible_port'),
|
2018-07-17 22:45:28 +00:00
|
|
|
'python_path': parse_python_path(hostvars.get('ansible_python_interpreter')),
|
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
|
|
|
'private_key_file': (hostvars.get('ansible_ssh_private_key_file') or
|
|
|
|
hostvars.get('ansible_private_key_file')),
|
|
|
|
'mitogen_via': hostvars.get('mitogen_via'),
|
2018-04-28 23:31:20 +00:00
|
|
|
'mitogen_kind': hostvars.get('mitogen_kind'),
|
|
|
|
'mitogen_docker_path': hostvars.get('mitogen_docker_path'),
|
2018-09-19 14:52:20 +00:00
|
|
|
'mitogen_kubectl_path': hostvars.get('mitogen_kubectl_path'),
|
2018-10-31 12:40:08 +00:00
|
|
|
'mitogen_lxc_path': hostvars.get('mitogen_lxc_path'),
|
|
|
|
'mitogen_lxc_attach_path': hostvars.get('mitogen_lxc_attach_path'),
|
2018-04-28 23:31:20 +00:00
|
|
|
'mitogen_lxc_info_path': hostvars.get('mitogen_lxc_info_path'),
|
|
|
|
'mitogen_machinectl_path': hostvars.get('mitogen_machinctl_path'),
|
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
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-09-09 19:29:02 +00:00
|
|
|
class CallChain(mitogen.parent.CallChain):
|
2018-10-29 19:35:17 +00:00
|
|
|
"""
|
|
|
|
Extend :class:`mitogen.parent.CallChain` to additionally cause the
|
|
|
|
associated :class:`Connection` to be reset if a ChannelError occurs.
|
|
|
|
|
|
|
|
This only catches failures that occur while a call is pnding, it is a
|
|
|
|
stop-gap until a more general method is available to notice connection in
|
|
|
|
every situation.
|
|
|
|
"""
|
2018-09-09 19:29:02 +00:00
|
|
|
call_aborted_msg = (
|
|
|
|
'Mitogen was disconnected from the remote environment while a call '
|
|
|
|
'was in-progress. If you feel this is in error, please file a bug. '
|
|
|
|
'Original error was: %s'
|
|
|
|
)
|
|
|
|
|
2018-10-29 19:35:17 +00:00
|
|
|
def __init__(self, connection, context, pipelined=False):
|
|
|
|
super(CallChain, self).__init__(context, pipelined)
|
|
|
|
#: The connection to reset on CallError.
|
|
|
|
self._connection = connection
|
|
|
|
|
2018-09-09 19:29:02 +00:00
|
|
|
def _rethrow(self, recv):
|
|
|
|
try:
|
|
|
|
return recv.get().unpickle()
|
|
|
|
except mitogen.core.ChannelError as e:
|
2018-10-29 19:35:17 +00:00
|
|
|
self._connection.reset()
|
2018-09-09 19:29:02 +00:00
|
|
|
raise ansible.errors.AnsibleConnectionFailure(
|
|
|
|
self.call_aborted_msg % (e,)
|
|
|
|
)
|
|
|
|
|
|
|
|
def call(self, func, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Like :meth:`mitogen.parent.CallChain.call`, but log timings.
|
|
|
|
"""
|
|
|
|
t0 = time.time()
|
|
|
|
try:
|
|
|
|
recv = self.call_async(func, *args, **kwargs)
|
|
|
|
return self._rethrow(recv)
|
|
|
|
finally:
|
|
|
|
LOG.debug('Call took %d ms: %r', 1000 * (time.time() - t0),
|
|
|
|
mitogen.parent.CallSpec(func, args, kwargs))
|
|
|
|
|
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
class Connection(ansible.plugins.connection.ConnectionBase):
|
2018-03-14 16:14:16 +00:00
|
|
|
#: mitogen.master.Broker for this worker.
|
|
|
|
broker = None
|
|
|
|
|
2018-02-17 10:12:29 +00:00
|
|
|
#: mitogen.master.Router for this worker.
|
2018-02-14 16:32:42 +00:00
|
|
|
router = None
|
2018-02-17 10:12:29 +00:00
|
|
|
|
2018-07-24 02:11:23 +00:00
|
|
|
#: mitogen.parent.Context representing the parent Context, which is
|
2018-06-09 21:11:26 +00:00
|
|
|
#: presently always the connection multiplexer process.
|
2018-02-17 10:12:29 +00:00
|
|
|
parent = None
|
|
|
|
|
2018-07-24 04:43:06 +00:00
|
|
|
#: mitogen.parent.Context for the target account on the target, possibly
|
|
|
|
#: reached via become.
|
2017-09-15 06:24:41 +00:00
|
|
|
context = None
|
|
|
|
|
2018-09-08 21:31:19 +00:00
|
|
|
#: Context for the login account on the target. This is always the login
|
|
|
|
#: account, even when become=True.
|
2018-07-24 04:43:06 +00:00
|
|
|
login_context = None
|
|
|
|
|
2018-09-09 19:29:02 +00:00
|
|
|
#: Only sudo, su, and doas are supported for now.
|
|
|
|
become_methods = ['sudo', 'su', 'doas']
|
|
|
|
|
2018-09-11 02:44:17 +00:00
|
|
|
#: Dict containing init_child() return value as recorded at startup by
|
2018-09-08 21:31:19 +00:00
|
|
|
#: ContextService. Contains:
|
|
|
|
#:
|
|
|
|
#: fork_context: Context connected to the fork parent : process in the
|
|
|
|
#: target account.
|
|
|
|
#: home_dir: Target context's home directory.
|
2018-09-11 02:44:17 +00:00
|
|
|
#: good_temp_dir: A writeable directory where new temporary directories
|
|
|
|
#: can be created.
|
2018-09-08 21:31:19 +00:00
|
|
|
init_child_result = None
|
2018-06-09 21:11:26 +00:00
|
|
|
|
2018-09-11 02:44:17 +00:00
|
|
|
#: A :class:`mitogen.parent.CallChain` for calls made to the target
|
|
|
|
#: account, to ensure subsequent calls fail with the original exception if
|
|
|
|
#: pipelined directory creation or file transfer fails.
|
2018-09-09 19:29:02 +00:00
|
|
|
chain = None
|
2018-02-17 10:12:29 +00:00
|
|
|
|
2018-08-20 14:43:56 +00:00
|
|
|
#
|
|
|
|
# Note: any of the attributes below may be :data:`None` if the connection
|
|
|
|
# plugin was constructed directly by a non-cooperative action, such as in
|
|
|
|
# the case of the synchronize module.
|
|
|
|
#
|
|
|
|
|
2018-09-08 21:35:42 +00:00
|
|
|
#: Set to the host name as it appears in inventory by on_action_run().
|
|
|
|
inventory_hostname = None
|
|
|
|
|
2018-09-08 20:59:17 +00:00
|
|
|
#: Set to task_vars by on_action_run().
|
2018-09-08 21:35:42 +00:00
|
|
|
_task_vars = 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
|
|
|
|
|
|
|
#: Set to 'hostvars' by on_action_run()
|
|
|
|
host_vars = None
|
|
|
|
|
2018-08-20 14:43:56 +00:00
|
|
|
#: Set by on_action_run()
|
|
|
|
delegate_to_hostname = None
|
|
|
|
|
2018-08-17 10:31:28 +00:00
|
|
|
#: Set to '_loader.get_basedir()' by on_action_run(). Used by mitogen_local
|
|
|
|
#: to change the working directory to that of the current playbook,
|
|
|
|
#: matching vanilla Ansible behaviour.
|
2018-07-10 16:56:05 +00:00
|
|
|
loader_basedir = 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
|
|
|
def __init__(self, play_context, new_stdin, **kwargs):
|
2018-03-16 23:39:30 +00:00
|
|
|
assert ansible_mitogen.process.MuxProcess.unix_listener_path, (
|
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 connection types may only be instantiated '
|
2018-04-17 16:40:45 +00:00
|
|
|
'while the "mitogen" strategy is active.'
|
2018-02-17 10:12:29 +00:00
|
|
|
)
|
2018-02-16 04:28:26 +00:00
|
|
|
super(Connection, self).__init__(play_context, new_stdin)
|
|
|
|
|
2018-03-14 16:14:16 +00:00
|
|
|
def __del__(self):
|
|
|
|
"""
|
|
|
|
Ansible cannot be trusted to always call close() e.g. the synchronize
|
|
|
|
action constructs a local connection like this. So provide a destructor
|
|
|
|
in the hopes of catching these cases.
|
|
|
|
"""
|
|
|
|
# https://github.com/dw/mitogen/issues/140
|
|
|
|
self.close()
|
|
|
|
|
2018-08-17 10:31:28 +00:00
|
|
|
def on_action_run(self, task_vars, delegate_to_hostname, loader_basedir):
|
2018-02-27 18:22:11 +00:00
|
|
|
"""
|
|
|
|
Invoked by ActionModuleMixin to indicate a new task is about to start
|
|
|
|
executing. We use the opportunity to grab relevant bits from the
|
|
|
|
task-specific data.
|
2018-08-17 10:31:28 +00:00
|
|
|
|
|
|
|
:param dict task_vars:
|
|
|
|
Task variable dictionary.
|
|
|
|
:param str delegate_to_hostname:
|
|
|
|
:data:`None`, or the template-expanded inventory hostname this task
|
|
|
|
is being delegated to. A similar variable exists on PlayContext
|
|
|
|
when ``delegate_to:`` is active, however it is unexpanded.
|
|
|
|
:param str loader_basedir:
|
|
|
|
Loader base directory; see :attr:`loader_basedir`.
|
2018-02-27 18:22:11 +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
|
|
|
self.inventory_hostname = task_vars['inventory_hostname']
|
2018-09-08 21:35:42 +00:00
|
|
|
self._task_vars = task_vars
|
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.host_vars = task_vars['hostvars']
|
2018-08-17 10:31:28 +00:00
|
|
|
self.delegate_to_hostname = delegate_to_hostname
|
2018-07-10 16:56:05 +00:00
|
|
|
self.loader_basedir = loader_basedir
|
2018-10-31 16:35:25 +00:00
|
|
|
self._mitogen_reset(mode='put')
|
2018-04-12 11:15:02 +00:00
|
|
|
|
2018-09-08 21:35:42 +00:00
|
|
|
def get_task_var(self, key, default=None):
|
|
|
|
if self._task_vars and key in self._task_vars:
|
|
|
|
return self._task_vars[key]
|
|
|
|
return default
|
|
|
|
|
2018-04-01 14:54:17 +00:00
|
|
|
@property
|
|
|
|
def homedir(self):
|
|
|
|
self._connect()
|
2018-09-08 21:31:19 +00:00
|
|
|
return self.init_child_result['home_dir']
|
2018-04-01 14:54:17 +00:00
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
@property
|
|
|
|
def connected(self):
|
2018-04-12 11:15:02 +00:00
|
|
|
return self.context is not None
|
2017-09-15 06:24:41 +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 _config_from_via(self, via_spec):
|
2018-08-12 09:35:32 +00:00
|
|
|
"""
|
|
|
|
Produce a dict connection specifiction given a string `via_spec`, of
|
|
|
|
the form `[become_user@]inventory_hostname`.
|
|
|
|
"""
|
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
|
|
|
become_user, _, inventory_name = via_spec.rpartition('@')
|
|
|
|
via_vars = self.host_vars[inventory_name]
|
|
|
|
if isinstance(via_vars, jinja2.runtime.Undefined):
|
|
|
|
raise ansible.errors.AnsibleConnectionFailure(
|
|
|
|
self.unknown_via_msg % (
|
2018-09-08 20:59:17 +00:00
|
|
|
via_spec,
|
2018-06-10 00:22:46 +00:00
|
|
|
inventory_name,
|
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
|
|
|
)
|
|
|
|
)
|
2018-04-05 19:14:14 +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
|
|
|
return config_from_hostvars(
|
|
|
|
transport=via_vars.get('ansible_connection', 'ssh'),
|
|
|
|
inventory_name=inventory_name,
|
|
|
|
connection=self,
|
|
|
|
hostvars=via_vars,
|
|
|
|
become_user=become_user or None,
|
|
|
|
)
|
2018-04-05 19:14:14 +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
|
|
|
unknown_via_msg = 'mitogen_via=%s of %s specifies an unknown hostname'
|
|
|
|
via_cycle_msg = 'mitogen_via=%s of %s creates a cycle (%s)'
|
|
|
|
|
|
|
|
def _stack_from_config(self, config, stack=(), seen_names=()):
|
|
|
|
if config['inventory_name'] in seen_names:
|
|
|
|
raise ansible.errors.AnsibleConnectionFailure(
|
|
|
|
self.via_cycle_msg % (
|
|
|
|
config['mitogen_via'],
|
|
|
|
config['inventory_name'],
|
|
|
|
' -> '.join(reversed(
|
|
|
|
seen_names + (config['inventory_name'],)
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
)
|
2018-04-05 19:14:14 +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
|
|
|
if config['mitogen_via']:
|
|
|
|
stack, seen_names = self._stack_from_config(
|
|
|
|
self._config_from_via(config['mitogen_via']),
|
|
|
|
stack=stack,
|
|
|
|
seen_names=seen_names + (config['inventory_name'],)
|
|
|
|
)
|
2018-02-14 23:10:56 +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
|
|
|
stack += (CONNECTION_METHOD[config['transport']](config),)
|
|
|
|
if config['become']:
|
|
|
|
stack += (CONNECTION_METHOD[config['become_method']](config),)
|
2018-02-17 10:12:29 +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
|
|
|
return stack, seen_names
|
2018-02-16 01:38:52 +00:00
|
|
|
|
2018-08-12 09:39:50 +00:00
|
|
|
def _connect_broker(self):
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
2018-08-12 09:39:50 +00:00
|
|
|
Establish a reference to the Broker, Router and parent context used for
|
|
|
|
connections.
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
2018-04-12 11:15:02 +00:00
|
|
|
if not self.broker:
|
|
|
|
self.broker = mitogen.master.Broker()
|
|
|
|
self.router, self.parent = mitogen.unix.connect(
|
|
|
|
path=ansible_mitogen.process.MuxProcess.unix_listener_path,
|
|
|
|
broker=self.broker,
|
|
|
|
)
|
2018-02-16 01:38:52 +00:00
|
|
|
|
2018-08-17 10:31:28 +00:00
|
|
|
def _config_from_direct_connection(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
return config_from_play_context(
|
|
|
|
transport=self.transport,
|
|
|
|
inventory_name=self.inventory_hostname,
|
|
|
|
connection=self
|
|
|
|
)
|
|
|
|
|
|
|
|
def _config_from_delegate_to(self):
|
|
|
|
return config_from_hostvars(
|
|
|
|
transport=self._play_context.connection,
|
|
|
|
inventory_name=self.delegate_to_hostname,
|
|
|
|
connection=self,
|
2018-08-20 21:54:55 +00:00
|
|
|
hostvars=self.host_vars[self.delegate_to_hostname],
|
2018-08-17 10:31:28 +00:00
|
|
|
become_user=(self._play_context.become_user
|
|
|
|
if self._play_context.become
|
|
|
|
else None),
|
|
|
|
)
|
|
|
|
|
2018-08-12 09:39:50 +00:00
|
|
|
def _build_stack(self):
|
|
|
|
"""
|
|
|
|
Construct a list of dictionaries representing the connection
|
2018-08-12 10:53:06 +00:00
|
|
|
configuration between the controller and the target. This is
|
|
|
|
additionally used by the integration tests "mitogen_get_stack" action
|
|
|
|
to fetch the would-be connection configuration.
|
2018-08-12 09:39:50 +00:00
|
|
|
"""
|
2018-08-17 10:31:28 +00:00
|
|
|
if self.delegate_to_hostname is not None:
|
|
|
|
target_config = self._config_from_delegate_to()
|
2018-05-18 18:00:57 +00:00
|
|
|
else:
|
2018-08-17 10:31:28 +00:00
|
|
|
target_config = self._config_from_direct_connection()
|
|
|
|
|
2018-05-18 18:00:57 +00:00
|
|
|
stack, _ = self._stack_from_config(target_config)
|
2018-08-12 09:39:50 +00:00
|
|
|
return stack
|
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
|
|
|
|
2018-08-12 09:39:50 +00:00
|
|
|
def _connect_stack(self, stack):
|
|
|
|
"""
|
|
|
|
Pass `stack` to ContextService, requesting a copy of the context object
|
|
|
|
representing the target. If no connection exists yet, ContextService
|
|
|
|
will establish it before returning it or throwing an error.
|
|
|
|
"""
|
2018-10-31 18:30:03 +00:00
|
|
|
try:
|
|
|
|
dct = self.parent.call_service(
|
|
|
|
service_name='ansible_mitogen.services.ContextService',
|
|
|
|
method_name='get',
|
|
|
|
stack=mitogen.utils.cast(list(stack)),
|
|
|
|
)
|
|
|
|
except mitogen.core.CallError:
|
|
|
|
LOG.warning('Connection failed; stack configuration was:\n%s',
|
|
|
|
pprint.pformat(stack))
|
|
|
|
raise
|
2018-03-16 18:22:46 +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
|
|
|
if dct['msg']:
|
|
|
|
if dct['method_name'] in self.become_methods:
|
|
|
|
raise ansible.errors.AnsibleModuleError(dct['msg'])
|
|
|
|
raise ansible.errors.AnsibleConnectionFailure(dct['msg'])
|
2018-03-16 18:22:46 +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
|
|
|
self.context = dct['context']
|
2018-10-29 19:35:17 +00:00
|
|
|
self.chain = CallChain(self, self.context, pipelined=True)
|
2018-07-24 04:43:06 +00:00
|
|
|
if self._play_context.become:
|
|
|
|
self.login_context = dct['via']
|
|
|
|
else:
|
|
|
|
self.login_context = self.context
|
|
|
|
|
2018-09-08 21:31:19 +00:00
|
|
|
self.init_child_result = dct['init_child_result']
|
2018-08-19 14:59:35 +00:00
|
|
|
|
2018-09-11 02:44:17 +00:00
|
|
|
def get_good_temp_dir(self):
|
|
|
|
self._connect()
|
|
|
|
return self.init_child_result['good_temp_dir']
|
|
|
|
|
|
|
|
def _generate_tmp_path(self):
|
|
|
|
return os.path.join(
|
|
|
|
self.get_good_temp_dir(),
|
|
|
|
'ansible_mitogen_action_%016x' % (
|
|
|
|
random.getrandbits(8*8),
|
|
|
|
)
|
2018-09-09 22:39:08 +00:00
|
|
|
)
|
2018-09-11 02:44:17 +00:00
|
|
|
|
|
|
|
def _make_tmp_path(self):
|
|
|
|
assert getattr(self._shell, 'tmpdir', None) is None
|
|
|
|
self._shell.tmpdir = self._generate_tmp_path()
|
|
|
|
LOG.debug('Temporary directory: %r', self._shell.tmpdir)
|
|
|
|
self.get_chain().call_no_reply(os.mkdir, self._shell.tmpdir)
|
|
|
|
return self._shell.tmpdir
|
2018-04-01 09:48:20 +00:00
|
|
|
|
2018-10-29 19:34:50 +00:00
|
|
|
def _reset_tmp_path(self):
|
|
|
|
"""
|
2018-10-31 16:35:25 +00:00
|
|
|
Called by _mitogen_reset(); ask the remote context to delete any
|
|
|
|
temporary directory created for the action. CallChain is not used here
|
|
|
|
to ensure exception is logged by the context on failure, since the
|
|
|
|
CallChain itself is about to be destructed.
|
2018-10-29 19:34:50 +00:00
|
|
|
"""
|
|
|
|
if getattr(self._shell, 'tmpdir', None) is not None:
|
|
|
|
self.context.call_no_reply(
|
|
|
|
ansible_mitogen.target.prune_tree,
|
|
|
|
self._shell.tmpdir,
|
|
|
|
)
|
|
|
|
self._shell.tmpdir = None
|
|
|
|
|
2018-08-12 09:39:50 +00:00
|
|
|
def _connect(self):
|
|
|
|
"""
|
|
|
|
Establish a connection to the master process's UNIX listener socket,
|
|
|
|
constructing a mitogen.master.Router to communicate with the master,
|
|
|
|
and a mitogen.parent.Context to represent it.
|
|
|
|
|
|
|
|
Depending on the original transport we should emulate, trigger one of
|
|
|
|
the _connect_*() service calls defined above to cause the master
|
|
|
|
process to establish the real connection on our behalf, or return a
|
|
|
|
reference to the existing one.
|
|
|
|
"""
|
|
|
|
if self.connected:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._connect_broker()
|
|
|
|
stack = self._build_stack()
|
|
|
|
self._connect_stack(stack)
|
|
|
|
|
2018-10-31 16:35:25 +00:00
|
|
|
def _mitogen_reset(self, mode):
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
2018-10-31 16:35:25 +00:00
|
|
|
Forget everything we know about the connected context. This function
|
|
|
|
cannot be called _reset() since that name is used as a public API by
|
|
|
|
Ansible 2.4 wait_for_connection plug-in.
|
2018-10-29 19:34:50 +00:00
|
|
|
|
|
|
|
:param str mode:
|
|
|
|
Name of ContextService method to use to discard the context, either
|
|
|
|
'put' or 'reset'.
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
2018-10-29 19:34:50 +00:00
|
|
|
if not self.context:
|
|
|
|
return
|
2018-09-11 02:44:17 +00:00
|
|
|
|
2018-10-29 19:34:50 +00:00
|
|
|
self._reset_tmp_path()
|
|
|
|
self.chain.reset()
|
|
|
|
self.parent.call_service(
|
|
|
|
service_name='ansible_mitogen.services.ContextService',
|
|
|
|
method_name=mode,
|
|
|
|
context=self.context
|
|
|
|
)
|
2018-04-13 12:31:25 +00:00
|
|
|
|
2018-04-12 11:15:02 +00:00
|
|
|
self.context = None
|
2018-09-09 19:29:02 +00:00
|
|
|
self.login_context = None
|
2018-09-08 21:31:19 +00:00
|
|
|
self.init_child_result = None
|
2018-09-09 19:29:02 +00:00
|
|
|
self.chain = None
|
2018-10-29 19:34:50 +00:00
|
|
|
|
2018-11-05 14:28:19 +00:00
|
|
|
def _shutdown_broker(self):
|
2018-10-29 19:34:50 +00:00
|
|
|
"""
|
2018-11-05 14:28:19 +00:00
|
|
|
Shutdown the broker thread during :meth:`close` or :meth:`reset`.
|
2018-10-29 19:34:50 +00:00
|
|
|
"""
|
|
|
|
if self.broker:
|
2018-03-14 16:14:16 +00:00
|
|
|
self.broker.shutdown()
|
|
|
|
self.broker.join()
|
|
|
|
self.broker = None
|
2018-02-17 10:12:29 +00:00
|
|
|
self.router = None
|
|
|
|
|
2018-11-05 14:28:19 +00:00
|
|
|
# #420: Ansible executes "meta" actions in the top-level process,
|
|
|
|
# meaning "reset_connection" will cause :class:`mitogen.core.Latch` FDs
|
|
|
|
# to be cached and subsequently erroneously shared by children on
|
|
|
|
# subsequent task forks. To handle that, call on_fork() to ensure any
|
|
|
|
# shared state is discarded.
|
|
|
|
mitogen.fork.on_fork()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Arrange for the mitogen.master.Router running in the worker to
|
|
|
|
gracefully shut down, and wait for shutdown to complete. Safe to call
|
|
|
|
multiple times.
|
|
|
|
"""
|
|
|
|
self._mitogen_reset(mode='put')
|
|
|
|
self._shutdown_broker()
|
|
|
|
|
2019-01-19 05:15:56 +00:00
|
|
|
def _reset_find_task_vars(self):
|
|
|
|
"""
|
|
|
|
Monsterous hack: since "meta: reset_connection" does not run from an
|
|
|
|
action, we cannot capture task variables via :meth:`on_action_run`.
|
|
|
|
Instead walk the parent frames searching for the `all_vars` local from
|
|
|
|
StrategyBase._execute_meta(). If this fails, just leave task_vars
|
|
|
|
unset, likely causing the wrong configuration to be created.
|
|
|
|
"""
|
|
|
|
frame = sys._getframe()
|
|
|
|
while frame and not self._task_vars:
|
|
|
|
self._task_vars = frame.f_locals.get('all_vars')
|
|
|
|
frame = frame.f_back
|
|
|
|
|
2018-10-31 18:30:03 +00:00
|
|
|
reset_compat_msg = (
|
|
|
|
'Mitogen only supports "reset_connection" on Ansible 2.5.6 or later'
|
|
|
|
)
|
|
|
|
|
2018-10-29 19:34:50 +00:00
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Explicitly terminate the connection to the remote host. This discards
|
|
|
|
any local state we hold for the connection, returns the Connection to
|
|
|
|
the 'disconnected' state, and informs ContextService the connection is
|
|
|
|
bad somehow, and should be shut down and discarded.
|
|
|
|
"""
|
2019-01-19 05:15:56 +00:00
|
|
|
if self._task_vars is None:
|
|
|
|
self._reset_find_task_vars()
|
|
|
|
|
2018-10-31 18:30:03 +00:00
|
|
|
if self._play_context.remote_addr is None:
|
|
|
|
# <2.5.6 incorrectly populate PlayContext for reset_connection
|
|
|
|
# https://github.com/ansible/ansible/issues/27520
|
|
|
|
raise ansible.errors.AnsibleConnectionFailure(
|
|
|
|
self.reset_compat_msg
|
|
|
|
)
|
|
|
|
|
2018-10-29 19:34:50 +00:00
|
|
|
self._connect()
|
2018-10-31 16:35:25 +00:00
|
|
|
self._mitogen_reset(mode='reset')
|
2018-11-05 14:28:19 +00:00
|
|
|
self._shutdown_broker()
|
2018-10-31 16:35:25 +00:00
|
|
|
|
|
|
|
# Compatibility with Ansible 2.4 wait_for_connection plug-in.
|
|
|
|
_reset = reset
|
2018-10-29 19:34:50 +00:00
|
|
|
|
2018-09-09 19:29:02 +00:00
|
|
|
def get_chain(self, use_login=False, use_fork=False):
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
2018-09-09 19:29:02 +00:00
|
|
|
Return the :class:`mitogen.parent.CallChain` to use for executing
|
|
|
|
function calls.
|
2018-08-20 13:11:46 +00:00
|
|
|
|
2018-09-09 19:29:02 +00:00
|
|
|
:param bool use_login:
|
|
|
|
If :data:`True`, always return the chain for the login account
|
|
|
|
rather than any active become user.
|
|
|
|
:param bool use_fork:
|
|
|
|
If :data:`True`, return the chain for the fork parent.
|
|
|
|
:returns mitogen.parent.CallChain:
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
2018-02-14 16:32:42 +00:00
|
|
|
self._connect()
|
2018-09-09 19:29:02 +00:00
|
|
|
if use_login:
|
|
|
|
return self.login_context.default_call_chain
|
|
|
|
if use_fork:
|
|
|
|
return self.init_child_result['fork_context'].default_call_chain
|
|
|
|
return self.chain
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2018-06-09 21:11:26 +00:00
|
|
|
def create_fork_child(self):
|
|
|
|
"""
|
|
|
|
Fork a new child off the target context. The actual fork occurs from
|
|
|
|
the 'virginal fork parent', which does not any Ansible modules prior to
|
|
|
|
fork, to avoid conflicts resulting from custom module_utils paths.
|
|
|
|
|
|
|
|
:returns:
|
|
|
|
mitogen.core.Context of the new child.
|
|
|
|
"""
|
2018-09-09 19:29:02 +00:00
|
|
|
return self.get_chain(use_fork=True).call(
|
|
|
|
ansible_mitogen.target.create_fork_child
|
|
|
|
)
|
2018-06-09 21:11:26 +00:00
|
|
|
|
2018-10-02 18:58:23 +00:00
|
|
|
def get_extra_args(self):
|
|
|
|
"""
|
|
|
|
Overridden by connections/mitogen_kubectl.py to a list of additional
|
|
|
|
arguments for the command.
|
|
|
|
"""
|
|
|
|
# TODO: maybe use this for SSH too.
|
|
|
|
return []
|
|
|
|
|
2018-07-10 16:56:05 +00:00
|
|
|
def get_default_cwd(self):
|
|
|
|
"""
|
|
|
|
Overridden by connections/mitogen_local.py to emulate behaviour of CWD
|
2018-07-10 17:29:49 +00:00
|
|
|
being fixed to that of ActionBase._loader.get_basedir().
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_default_env(self):
|
|
|
|
"""
|
|
|
|
Overridden by connections/mitogen_local.py to emulate behaviour of
|
|
|
|
WorkProcess environment inherited from WorkerProcess.
|
2018-07-10 16:56:05 +00:00
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
2018-04-04 23:41:14 +00:00
|
|
|
def exec_command(self, cmd, in_data='', sudoable=True, mitogen_chdir=None):
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
|
|
|
Implement exec_command() by calling the corresponding
|
2018-04-06 16:22:45 +00:00
|
|
|
ansible_mitogen.target function in the target.
|
2018-02-17 10:12:29 +00:00
|
|
|
|
|
|
|
:param str cmd:
|
|
|
|
Shell command to execute.
|
|
|
|
:param bytes in_data:
|
|
|
|
Data to supply on ``stdin`` of the process.
|
|
|
|
:returns:
|
|
|
|
(return code, stdout bytes, stderr bytes)
|
|
|
|
"""
|
2018-04-04 23:41:14 +00:00
|
|
|
emulate_tty = (not in_data and sudoable)
|
2018-09-10 00:31:15 +00:00
|
|
|
rc, stdout, stderr = self.get_chain().call(
|
2018-04-06 16:22:45 +00:00
|
|
|
ansible_mitogen.target.exec_command,
|
2018-04-05 19:14:14 +00:00
|
|
|
cmd=mitogen.utils.cast(cmd),
|
|
|
|
in_data=mitogen.utils.cast(in_data),
|
2018-07-10 16:56:05 +00:00
|
|
|
chdir=mitogen_chdir or self.get_default_cwd(),
|
2018-04-04 23:41:14 +00:00
|
|
|
emulate_tty=emulate_tty,
|
|
|
|
)
|
|
|
|
|
2018-11-05 13:15:20 +00:00
|
|
|
stderr += b'Shared connection to %s closed.%s' % (
|
|
|
|
self._play_context.remote_addr.encode(),
|
|
|
|
(b'\r\n' if emulate_tty else b'\n'),
|
2018-04-04 23:41:14 +00:00
|
|
|
)
|
|
|
|
return rc, stdout, stderr
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def fetch_file(self, in_path, out_path):
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
|
|
|
Implement fetch_file() by calling the corresponding
|
2018-04-06 16:22:45 +00:00
|
|
|
ansible_mitogen.target function in the target.
|
2018-02-17 10:12:29 +00:00
|
|
|
|
|
|
|
:param str in_path:
|
|
|
|
Remote filesystem path to read.
|
|
|
|
:param str out_path:
|
|
|
|
Local filesystem path to write.
|
|
|
|
"""
|
2018-09-09 19:29:02 +00:00
|
|
|
output = self.get_chain().call(
|
|
|
|
ansible_mitogen.target.read_path,
|
|
|
|
mitogen.utils.cast(in_path),
|
|
|
|
)
|
2018-04-06 16:22:45 +00:00
|
|
|
ansible_mitogen.target.write_path(out_path, output)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2018-04-28 19:11:03 +00:00
|
|
|
def put_data(self, out_path, data, mode=None, utimes=None):
|
2018-03-08 02:29:02 +00:00
|
|
|
"""
|
2018-08-20 13:11:46 +00:00
|
|
|
Implement put_file() by caling the corresponding ansible_mitogen.target
|
2018-09-09 19:29:02 +00:00
|
|
|
function in the target, transferring small files inline. This is
|
|
|
|
pipelined and will return immediately; failed transfers are reported as
|
|
|
|
exceptions in subsequent functon calls.
|
2018-03-08 02:29:02 +00:00
|
|
|
|
|
|
|
:param str out_path:
|
|
|
|
Remote filesystem path to write.
|
2018-04-13 12:31:25 +00:00
|
|
|
:param byte data:
|
|
|
|
File contents to put.
|
2018-03-08 02:29:02 +00:00
|
|
|
"""
|
2018-09-09 19:29:02 +00:00
|
|
|
self.get_chain().call_no_reply(
|
|
|
|
ansible_mitogen.target.write_path,
|
|
|
|
mitogen.utils.cast(out_path),
|
|
|
|
mitogen.core.Blob(data),
|
|
|
|
mode=mode,
|
|
|
|
utimes=utimes,
|
|
|
|
)
|
|
|
|
|
|
|
|
#: Maximum size of a small file before switching to streaming
|
2018-08-20 13:11:46 +00:00
|
|
|
#: transfer. This should really be the same as
|
|
|
|
#: mitogen.services.FileService.IO_SIZE, however the message format has
|
|
|
|
#: slightly more overhead, so just randomly subtract 4KiB.
|
|
|
|
SMALL_FILE_LIMIT = mitogen.core.CHUNK_SIZE - 4096
|
2018-03-08 02:29:02 +00:00
|
|
|
|
2019-01-20 18:42:02 +00:00
|
|
|
def _throw_io_error(self, e, path):
|
|
|
|
if e.args[0] == errno.ENOENT:
|
|
|
|
s = 'file or module does not exist: ' + path
|
|
|
|
raise ansible.errors.AnsibleFileNotFound(s)
|
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
def put_file(self, in_path, out_path):
|
2018-02-17 10:12:29 +00:00
|
|
|
"""
|
2018-04-22 01:48:06 +00:00
|
|
|
Implement put_file() by streamily transferring the file via
|
|
|
|
FileService.
|
2018-02-17 10:12:29 +00:00
|
|
|
|
|
|
|
:param str in_path:
|
|
|
|
Local filesystem path to read.
|
|
|
|
:param str out_path:
|
|
|
|
Remote filesystem path to write.
|
|
|
|
"""
|
2019-01-20 18:42:02 +00:00
|
|
|
try:
|
|
|
|
st = os.stat(in_path)
|
|
|
|
except OSError as e:
|
|
|
|
self._throw_io_error(e, in_path)
|
|
|
|
raise
|
|
|
|
|
2018-04-28 19:11:03 +00:00
|
|
|
if not stat.S_ISREG(st.st_mode):
|
|
|
|
raise IOError('%r is not a regular file.' % (in_path,))
|
|
|
|
|
|
|
|
# If the file is sufficiently small, just ship it in the argument list
|
|
|
|
# rather than introducing an extra RTT for the child to request it from
|
|
|
|
# FileService.
|
2018-08-20 13:11:46 +00:00
|
|
|
if st.st_size <= self.SMALL_FILE_LIMIT:
|
2018-04-28 19:11:03 +00:00
|
|
|
try:
|
2019-01-20 18:42:02 +00:00
|
|
|
fp = open(in_path, 'rb')
|
|
|
|
try:
|
|
|
|
s = fp.read(self.SMALL_FILE_LIMIT + 1)
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
except OSError:
|
|
|
|
self._throw_io_error(e, in_path)
|
|
|
|
raise
|
2018-04-28 19:11:03 +00:00
|
|
|
|
2018-08-20 13:11:46 +00:00
|
|
|
# Ensure did not grow during read.
|
2018-04-28 19:11:03 +00:00
|
|
|
if len(s) == st.st_size:
|
|
|
|
return self.put_data(out_path, s, mode=st.st_mode,
|
|
|
|
utimes=(st.st_atime, st.st_mtime))
|
|
|
|
|
2019-01-20 18:42:02 +00:00
|
|
|
self._connect()
|
2018-05-28 04:09:19 +00:00
|
|
|
self.parent.call_service(
|
2018-05-29 16:07:58 +00:00
|
|
|
service_name='mitogen.service.FileService',
|
2018-05-28 04:09:19 +00:00
|
|
|
method_name='register',
|
|
|
|
path=mitogen.utils.cast(in_path)
|
2018-04-22 01:48:06 +00:00
|
|
|
)
|
2018-09-09 23:31:16 +00:00
|
|
|
|
2018-09-10 18:05:27 +00:00
|
|
|
# For now this must remain synchronous, as the action plug-in may have
|
|
|
|
# passed us a temporary file to transfer. A future FileService could
|
|
|
|
# maintain an LRU list of open file descriptors to keep the temporary
|
|
|
|
# file alive, but that requires more work.
|
|
|
|
self.get_chain().call(
|
2018-04-22 01:48:06 +00:00
|
|
|
ansible_mitogen.target.transfer_file,
|
|
|
|
context=self.parent,
|
|
|
|
in_path=in_path,
|
|
|
|
out_path=out_path
|
|
|
|
)
|