-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ansible_mitogen: Fix usage of connection_loader__get. #1215
base: master
Are you sure you want to change the base?
Changes from all commits
9a01aef
0f8575d
2fab44d
c1ac3ab
994060b
9982198
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,9 @@ | |
import ansible.executor.process.worker | ||
import ansible.template | ||
import ansible.utils.sentinel | ||
import ansible.playbook.play_context | ||
|
||
from ansible.plugins.loader import get_with_context_result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personal preference, use absolute imports when possible. It makes it easier to distinguish what belongs to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha - I had some issues getting an absolute import working in this file due to other imports masking things. Tried a few more ways just now but didn't get anywhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried some variations in moreati@25da0f7. We could eliminate this import entirely by using result = ansible_mitogen.loaders.action_loader__get_with_context(...)
...
return result._replace(object=adorned_klass) It loses a little clarity. I'm in favour of the tradeoff, but ready to be convinced otherwise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not very keen on this alternative for a few reasons - clarity, as you mentioned, but also the fact that we're now relying on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On reflection I agree. I'd still like to use an absolute import. I'll investigate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolute import seems to have worked in 8273eac |
||
|
||
|
||
def _patch_awx_callback(): | ||
|
@@ -76,12 +79,12 @@ def patch_add_local(self, **kwargs): | |
_patch_awx_callback() | ||
|
||
|
||
def wrap_action_loader__get(name, *args, **kwargs): | ||
def wrap_action_loader__get_with_context(name, *args, **kwargs): | ||
""" | ||
While the mitogen strategy is active, trap action_loader.get() calls, | ||
augmenting any fetched class with ActionModuleMixin, which replaces various | ||
helper methods inherited from ActionBase with implementations that avoid | ||
the use of shell fragments wherever possible. | ||
While the mitogen strategy is active, trap action_loader.get_with_context() | ||
calls, augmenting any fetched class with ActionModuleMixin, which replaces | ||
various helper methods inherited from ActionBase with implementations that | ||
avoid the use of shell fragments wherever possible. | ||
|
||
This is used instead of static subclassing as it generalizes to third party | ||
action plugins outside the Ansible tree. | ||
|
@@ -91,13 +94,19 @@ def wrap_action_loader__get(name, *args, **kwargs): | |
name = 'mitogen_' + name | ||
get_kwargs['collection_list'] = kwargs.pop('collection_list', None) | ||
|
||
klass = ansible_mitogen.loaders.action_loader__get(name, **get_kwargs) | ||
(klass, context) = ansible_mitogen.loaders.action_loader__get_with_context( | ||
name, | ||
**get_kwargs | ||
) | ||
|
||
if klass: | ||
bases = (ansible_mitogen.mixins.ActionModuleMixin, klass) | ||
adorned_klass = type(str(name), bases, {}) | ||
if kwargs.get('class_only'): | ||
return adorned_klass | ||
return adorned_klass(*args, **kwargs) | ||
return get_with_context_result(adorned_klass, context) | ||
return get_with_context_result(adorned_klass(*args, **kwargs), context) | ||
|
||
return get_with_context_result(None, context) | ||
|
||
|
||
REDIRECTED_CONNECTION_PLUGINS = ( | ||
|
@@ -115,15 +124,26 @@ def wrap_action_loader__get(name, *args, **kwargs): | |
) | ||
|
||
|
||
def wrap_connection_loader__get(name, *args, **kwargs): | ||
def wrap_connection_loader__get_with_context(name, *args, **kwargs): | ||
""" | ||
While a Mitogen strategy is active, rewrite connection_loader.get() calls | ||
for some transports into requests for a compatible Mitogen transport. | ||
While a Mitogen strategy is active, rewrite | ||
connection_loader.get_with_context() calls for some transports into | ||
requests for a compatible Mitogen transport. | ||
""" | ||
if name in REDIRECTED_CONNECTION_PLUGINS: | ||
is_play_using_mitogen_connection = None | ||
if len(args) > 0 and isinstance(args[0], ansible.playbook.play_context.PlayContext): | ||
play_context = args[0] | ||
is_play_using_mitogen_connection = play_context.connection in REDIRECTED_CONNECTION_PLUGINS | ||
|
||
# assume true if we're not in a play context since we're using a Mitogen strategy | ||
if is_play_using_mitogen_connection is None: | ||
is_play_using_mitogen_connection = True | ||
|
||
redirect_connection = name in REDIRECTED_CONNECTION_PLUGINS and is_play_using_mitogen_connection | ||
if redirect_connection: | ||
name = 'mitogen_' + name | ||
|
||
return ansible_mitogen.loaders.connection_loader__get(name, *args, **kwargs) | ||
return ansible_mitogen.loaders.connection_loader__get_with_context(name, *args, **kwargs) | ||
|
||
|
||
def wrap_worker__run(self): | ||
|
@@ -173,8 +193,8 @@ def _install_wrappers(self): | |
Install our PluginLoader monkey patches and update global variables | ||
with references to the real functions. | ||
""" | ||
ansible_mitogen.loaders.action_loader.get = wrap_action_loader__get | ||
ansible_mitogen.loaders.connection_loader.get_with_context = wrap_connection_loader__get | ||
ansible_mitogen.loaders.action_loader.get_with_context = wrap_action_loader__get_with_context | ||
ansible_mitogen.loaders.connection_loader.get_with_context = wrap_connection_loader__get_with_context | ||
|
||
global worker__run | ||
worker__run = ansible.executor.process.worker.WorkerProcess.run | ||
|
@@ -184,11 +204,11 @@ def _remove_wrappers(self): | |
""" | ||
Uninstall the PluginLoader monkey patches. | ||
""" | ||
ansible_mitogen.loaders.action_loader.get = ( | ||
ansible_mitogen.loaders.action_loader__get | ||
ansible_mitogen.loaders.action_loader.get_with_context = ( | ||
ansible_mitogen.loaders.action_loader__get_with_context | ||
) | ||
ansible_mitogen.loaders.connection_loader.get_with_context = ( | ||
ansible_mitogen.loaders.connection_loader__get | ||
ansible_mitogen.loaders.connection_loader__get_with_context | ||
) | ||
ansible.executor.process.worker.WorkerProcess.run = worker__run | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# https://github.com/mitogen-hq/mitogen/issues/776 | ||
--- | ||
- name: regression/issue_766__get_with_context.yml | ||
hosts: localhost | ||
gather_facts: true | ||
tasks: | ||
- block: | ||
- name: Start container | ||
delegate_to: localhost | ||
vars: | ||
ansible_port: 8040 | ||
containers.podman.podman_container: | ||
name: sysprep | ||
image: ghcr.io/mitogen-hq/sysrepo-netopeer2:latest | ||
auto_remove: true | ||
detach: true | ||
recreate: true | ||
published_ports: | ||
- "{{ ansible_port }}:830" | ||
|
||
- name: Wait for container | ||
delegate_to: localhost | ||
# TODO robust condition. wait_for + search_regex? wait_for_connection? | ||
wait_for: | ||
timeout: 5 | ||
|
||
- name: Get running configuration and state data | ||
vars: | ||
ansible_port: 8040 | ||
ansible_connection: netconf | ||
ansible_user: netconf | ||
ansible_password: netconf | ||
ansible.netcommon.netconf_get: | ||
|
||
always: | ||
- name: Cleanup container | ||
delegate_to: localhost | ||
containers.podman.podman_container: | ||
name: sysprep | ||
state: absent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 changing the names.
...__get = ...get_with_context
has long bugged me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I saw that and thought like hell I'm leaving it like that hah