Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
moreati committed Sep 27, 2024
1 parent a42a459 commit e61af52
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 181 deletions.
75 changes: 0 additions & 75 deletions .ci/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,81 +41,6 @@ jobs:
- template: azure-pipelines-steps.yml
strategy:
matrix:
Mito_27_centos6:
tox.env: py27-mode_mitogen-distro_centos6
Mito_27_centos7:
tox.env: py27-mode_mitogen-distro_centos7
Mito_27_centos8:
tox.env: py27-mode_mitogen-distro_centos8
Mito_27_debian9:
tox.env: py27-mode_mitogen-distro_debian9
Mito_27_debian10:
tox.env: py27-mode_mitogen-distro_debian10
Mito_27_debian11:
tox.env: py27-mode_mitogen-distro_debian11
Mito_27_ubuntu1604:
tox.env: py27-mode_mitogen-distro_ubuntu1604
Mito_27_ubuntu1804:
tox.env: py27-mode_mitogen-distro_ubuntu1804
Mito_27_ubuntu2004:
tox.env: py27-mode_mitogen-distro_ubuntu2004

Mito_36_centos6:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_centos6
Mito_36_centos7:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_centos7
Mito_36_centos8:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_centos8
Mito_36_debian9:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_debian9
Mito_36_debian10:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_debian10
Mito_36_debian11:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_debian11
Mito_36_ubuntu1604:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_ubuntu1604
Mito_36_ubuntu1804:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_ubuntu1804
Mito_36_ubuntu2004:
python.version: '3.6'
tox.env: py36-mode_mitogen-distro_ubuntu2004

Mito_312_centos6:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_centos6
Mito_312_centos7:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_centos7
Mito_312_centos8:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_centos8
Mito_312_debian9:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_debian9
Mito_312_debian10:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_debian10
Mito_312_debian11:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_debian11
Mito_312_ubuntu1604:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_ubuntu1604
Mito_312_ubuntu1804:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_ubuntu1804
Mito_312_ubuntu2004:
python.version: '3.12'
tox.env: py312-mode_mitogen-distro_ubuntu2004

Ans_27_210:
tox.env: py27-mode_ansible-ansible2.10
Ans_27_4:
Expand Down
2 changes: 0 additions & 2 deletions ansible_mitogen/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import traceback

import ansible
import ansible.constants
import ansible.plugins
import ansible.plugins.action
import ansible.utils.unsafe_proxy
import ansible.vars.clean
Expand Down
6 changes: 2 additions & 4 deletions ansible_mitogen/plugins/connection/mitogen_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
import sys

try:
import ansible_mitogen.connection
import ansible_mitogen
except ImportError:
base_dir = os.path.dirname(__file__)
sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..')))
del base_dir
sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../../../..')))

import ansible_mitogen.connection
import ansible_mitogen.process
Expand Down
36 changes: 6 additions & 30 deletions ansible_mitogen/plugins/connection/mitogen_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,56 +32,32 @@
import os.path
import sys

import ansible.plugins.connection.ssh as _ansible_ssh

DOCUMENTATION = """
name: mitogen_ssh
author: David Wilson <[email protected]>
connection: mitogen_ssh
short_description: Connect over SSH via Mitogen
description:
- This connects using an OpenSSH client controlled by the Mitogen for
Ansible extension. It accepts every option the vanilla ssh plugin
accepts.
version_added: "2.5"
options:
ssh_args:
type: str
vars:
- name: ssh_args
- name: ansible_ssh_args
- name: ansible_mitogen_ssh_args
ssh_common_args:
type: str
vars:
- name: ssh_args
- name: ansible_ssh_common_args
- name: ansible_mitogen_ssh_common_args
ssh_extra_args:
type: str
vars:
- name: ssh_args
- name: ansible_ssh_extra_args
- name: ansible_mitogen_ssh_extra_args
"""
""" + _ansible_ssh.DOCUMENTATION.partition('options:\n')[2]

try:
import ansible_mitogen
except ImportError:
base_dir = os.path.dirname(__file__)
sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..')))
del base_dir
sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../../../..')))

import ansible_mitogen.connection
import ansible_mitogen.loaders


class Connection(ansible_mitogen.connection.Connection):
transport = 'ssh'
vanilla_class = ansible_mitogen.loaders.connection_loader__get(
'ssh',
class_only=True,
)

@staticmethod
def _create_control_path(*args, **kwargs):
"""Forward _create_control_path() to the implementation in ssh.py."""
# https://github.com/dw/mitogen/issues/342
return Connection.vanilla_class._create_control_path(*args, **kwargs)
return _ansible_ssh.Connection._create_control_path(*args, **kwargs)
10 changes: 4 additions & 6 deletions ansible_mitogen/plugins/strategy/mitogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@
# debuggers and isinstance() work predictably.
#

BASE_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__), '../../..')
)

if BASE_DIR not in sys.path:
sys.path.insert(0, BASE_DIR)
try:
import ansible_mitogen
except ImportError:
sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../../../..')))

import ansible_mitogen.strategy
import ansible.plugins.strategy.linear
Expand Down
10 changes: 4 additions & 6 deletions ansible_mitogen/plugins/strategy/mitogen_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@
# debuggers and isinstance() work predictably.
#

BASE_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__), '../../..')
)

if BASE_DIR not in sys.path:
sys.path.insert(0, BASE_DIR)
try:
import ansible_mitogen
except ImportError:
sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../../../..')))

import ansible_mitogen.loaders
import ansible_mitogen.strategy
Expand Down
93 changes: 42 additions & 51 deletions ansible_mitogen/transport_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
__metaclass__ = type

import abc
import logging
import os

import ansible.utils.shlex
import ansible.constants as C
import ansible.executor.interpreter_discovery
Expand All @@ -74,6 +76,8 @@
import mitogen.core


LOG = logging.getLogger(__name__)

def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_python):
"""
Triggers ansible python interpreter discovery if requested.
Expand Down Expand Up @@ -412,6 +416,29 @@ def __init__(self, connection, play_context, transport, inventory_name):
# used to run interpreter discovery
self._action = connection._action

def _become_option(self, name):
plugin = self._connection.become
if plugin is not None:
return plugin.get_option(
name, hostvars=self._task_vars, playcontext=self._play_context,
)
else:
# FIXME BecomeBase.get_option() only does this for become_user,
# become_pass, become_flags, & become_exe.
LOG.warning(
'%r: Used play_context fallback for option %r', self, name,
)
return getattr(self._play_context, name)

def _connection_option(self, name):
try:
return self._connection.get_option(name, hostvars=self._task_vars)
except KeyError:
LOG.warning(
'%r: Used play_context fallback for option %r', self, name,
)
return getattr(self._play_context, name)

def transport(self):
return self._transport

Expand All @@ -428,31 +455,20 @@ def become(self):
return self._play_context.become

def become_method(self):
# TODO self._connection.become.name?
return self._play_context.become_method

def become_user(self):
return self._play_context.become_user
return self._become_option('become_user')

def become_pass(self):
# become_pass is owned/provided by the active become plugin. However
# PlayContext is intertwined with it. Known complications
# - ansible_become_password is higher priority than ansible_become_pass,
# `play_context.become_pass` doesn't obey this (atleast with Mitgeon).
# - `meta: reset_connection` runs `connection.reset()` but
# `ansible_mitogen.connection.Connection.reset()` recreates the
# connection object, setting `connection.become = None`.
become_plugin = self._connection.become
try:
become_pass = become_plugin.get_option('become_pass', playcontext=self._play_context)
except AttributeError:
become_pass = self._play_context.become_pass
return optional_secret(become_pass)
return optional_secret(self._become_option('become_pass'))

def password(self):
return optional_secret(self._play_context.password)
return optional_secret(self._connection_option('password'))

def port(self):
return self._play_context.port
return self._connection_option('port')

def python_path(self, rediscover_python=False):
s = self._connection.get_task_var('ansible_python_interpreter')
Expand All @@ -466,21 +482,16 @@ def python_path(self, rediscover_python=False):
rediscover_python=rediscover_python)

def host_key_checking(self):
def candidates():
yield self._connection.get_task_var('ansible_ssh_host_key_checking')
yield self._connection.get_task_var('ansible_host_key_checking')
yield C.HOST_KEY_CHECKING
val = next((v for v in candidates() if v is not None), True)
return boolean(val)
return self._connection_option('host_key_checking')

def private_key_file(self):
return self._play_context.private_key_file
return self._connection_option('private_key_file')

def ssh_executable(self):
return C.config.get_config_value("ssh_executable", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {}))
return self._connection_option('ssh_executable')

def timeout(self):
return self._play_context.timeout
return self._connection_option('timeout')

def ansible_ssh_timeout(self):
return (
Expand All @@ -490,42 +501,22 @@ def ansible_ssh_timeout(self):
)

def ssh_args(self):
local_vars = self._task_vars.get("hostvars", {}).get(self._inventory_name, {})
return [
mitogen.core.to_text(term)
for s in (
C.config.get_config_value("ssh_args", plugin_type="connection", plugin_name="ssh", variables=local_vars),
C.config.get_config_value("ssh_common_args", plugin_type="connection", plugin_name="ssh", variables=local_vars),
C.config.get_config_value("ssh_extra_args", plugin_type="connection", plugin_name="ssh", variables=local_vars)
self._connection_option('ssh_args'),
self._connection_option('ssh_common_args'),
self._connection_option('ssh_extra_args'),
)
for term in ansible.utils.shlex.shlex_split(s or '')
]

def become_exe(self):
# In Ansible 2.8, PlayContext.become_exe always has a default value due
# to the new options mechanism. Previously it was only set if a value
# ("somewhere") had been specified for the task.
# For consistency in the tests, here we make older Ansibles behave like
# newer Ansibles.
exe = self._play_context.become_exe
if exe is None and self._play_context.become_method == 'sudo':
exe = 'sudo'
return exe
return self._become_option('become_exe')

def sudo_args(self):
return [
mitogen.core.to_text(term)
for term in ansible.utils.shlex.shlex_split(
first_true((
self._play_context.become_flags,
# Ansible <=2.7.
getattr(self._play_context, 'sudo_flags', ''),
# Ansible <=2.3.
getattr(C, 'DEFAULT_BECOME_FLAGS', ''),
getattr(C, 'DEFAULT_SUDO_FLAGS', '')
), default='')
)
]
become_flags = self._become_option('become_flags')
return ansible.utils.shlex.shlex_split(become_flags or '')

def mitogen_via(self):
return self._connection.get_task_var('mitogen_via')
Expand Down
Loading

0 comments on commit e61af52

Please sign in to comment.