mitogen-hq / mitogen

Distributed self-replicating programs in Python
https://mitogen.networkgenomics.com/
BSD 3-Clause "New" or "Revised" License
2.32k stars 197 forks source link

httpapi connection support #1086

Open dvwh opened 2 months ago

dvwh commented 2 months ago

Currently, Mitogen supports several connection types like local, ssh, docker and more. However, it lacks support for httpapi.

Is it possible to reuse, adapt the existing connection plugins so httpapi can be used in Ansible plays with mitogen, or mitogen needs separate implementation of a connection plugin specifically for httpapi?

dvwh commented 2 months ago

Workaround that seems to work:

diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py
index 6bdf11ba..d266b394 100644
--- a/ansible_mitogen/connection.py
+++ b/ansible_mitogen/connection.py
@@ -41,6 +41,7 @@ import time
 import ansible.constants as C
 import ansible.errors
 import ansible.plugins.connection
+from ansible.plugins.action.normal import ActionModule

 import mitogen.core

@@ -585,7 +586,7 @@ class Connection(ansible.plugins.connection.ConnectionBase):
             if f.f_code.co_name == 'run':
                 f_locals = f.f_locals
                 f_self = f_locals.get('self')
-                if isinstance(f_self, ansible_mitogen.mixins.ActionModuleMixin):
+                if isinstance(f_self, ansible_mitogen.mixins.ActionModuleMixin) or (self._play_context.connection == 'httpapi' and isinstance(f_self, ActionModule)):
                     # backref for python interpreter discovery, should be safe because _get_task_vars
                     # is always called before running interpreter discovery
                     self._action = f_self
diff --git a/ansible_mitogen/plugins/connection/mitogen_ssh.py b/ansible_mitogen/plugins/connection/mitogen_ssh.py
index 75f2d42f..d977b6fd 100644
--- a/ansible_mitogen/plugins/connection/mitogen_ssh.py
+++ b/ansible_mitogen/plugins/connection/mitogen_ssh.py
@@ -75,7 +75,7 @@ import ansible_mitogen.loaders

 class Connection(ansible_mitogen.connection.Connection):
     transport = 'ssh'
-    vanilla_class = ansible_mitogen.loaders.connection_loader__get(
+    (vanilla_class, _) = ansible_mitogen.loaders.connection_loader__get(
         'ssh',
         class_only=True,
     )
timansky commented 2 months ago

Helped with zabbix module. Plz include in next release