From 01840fd0b0c4f152c9a9ebc72d14b7e5fd9de701 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 1 Feb 2019 02:42:49 +0000 Subject: [PATCH] issue #510: add mitogen_container_name= variable. --- ansible_mitogen/connection.py | 30 +++++++++++++++++++++++------ ansible_mitogen/transport_config.py | 13 +++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py index 8bfbb51d..61f8372d 100644 --- a/ansible_mitogen/connection.py +++ b/ansible_mitogen/connection.py @@ -123,7 +123,10 @@ def _connect_docker(spec): 'method': 'docker', 'kwargs': { 'username': spec.remote_user(), - 'container': spec.remote_addr(), + 'container': ( + spec.mitogen_container_name() or + spec.remote_addr() + ), 'python_path': spec.python_path(), 'connect_timeout': spec.ansible_ssh_timeout() or spec.timeout(), } @@ -137,7 +140,10 @@ def _connect_kubectl(spec): return { 'method': 'kubectl', 'kwargs': { - 'pod': spec.remote_addr(), + 'pod': ( + spec.mitogen_container_name() or + spec.remote_addr() + ), 'python_path': spec.python_path(), 'connect_timeout': spec.ansible_ssh_timeout() or spec.timeout(), 'kubectl_path': spec.mitogen_kubectl_path(), @@ -154,7 +160,10 @@ def _connect_jail(spec): 'method': 'jail', 'kwargs': { 'username': spec.remote_user(), - 'container': spec.remote_addr(), + 'container': ( + spec.mitogen_container_name() or + spec.remote_addr() + ), 'python_path': spec.python_path(), 'connect_timeout': spec.ansible_ssh_timeout() or spec.timeout(), } @@ -168,7 +177,10 @@ def _connect_lxc(spec): return { 'method': 'lxc', 'kwargs': { - 'container': spec.remote_addr(), + 'container': ( + spec.mitogen_container_name() or + spec.remote_addr() + ), 'python_path': spec.python_path(), 'lxc_attach_path': spec.mitogen_lxc_attach_path(), 'connect_timeout': spec.ansible_ssh_timeout() or spec.timeout(), @@ -183,7 +195,10 @@ def _connect_lxd(spec): return { 'method': 'lxd', 'kwargs': { - 'container': spec.remote_addr(), + 'container': ( + spec.mitogen_container_name() or + spec.remote_addr() + ), 'python_path': spec.python_path(), 'lxc_path': spec.mitogen_lxc_path(), 'connect_timeout': spec.ansible_ssh_timeout() or spec.timeout(), @@ -205,7 +220,10 @@ def _connect_setns(spec, kind=None): return { 'method': 'setns', 'kwargs': { - 'container': spec.remote_addr(), + 'container': ( + spec.mitogen_container_name() or + spec.remote_addr() + ), 'username': spec.remote_user(), 'python_path': spec.python_path(), 'kind': kind or spec.mitogen_kind(), diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index 92569c57..029feb97 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -273,6 +273,13 @@ class Spec(with_metaclass(abc.ABCMeta, object)): The SSH debug level. """ + @abc.abstractmethod + def mitogen_container_name(self): + """ + Highest precedence variable for specifying the name of a container to + connect to. + """ + @abc.abstractmethod def extra_args(self): """ @@ -398,6 +405,9 @@ class PlayContextSpec(Spec): def mitogen_ssh_debug_level(self): return self._connection.get_task_var('mitogen_ssh_debug_level') + def mitogen_container_name(self): + return self._connection.get_task_var('mitogen_container_name') + def extra_args(self): return self._connection.get_extra_args() @@ -577,5 +587,8 @@ class MitogenViaSpec(Spec): def mitogen_ssh_debug_level(self): return self._host_vars.get('mitogen_ssh_debug_level') + def mitogen_container_name(self): + return self._host_vars.get('mitogen_container_name') + def extra_args(self): return [] # TODO