Skip to content

Commit

Permalink
Moving the new plugin loader code into place
Browse files Browse the repository at this point in the history
  • Loading branch information
jimi-c committed Apr 29, 2020
1 parent 5d8839f commit 12a2223
Show file tree
Hide file tree
Showing 19 changed files with 339 additions and 709 deletions.
1 change: 0 additions & 1 deletion lib/ansible/cli/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from ansible.errors import AnsibleError, AnsibleOptionsError
from ansible.inventory.host import Host
from ansible.module_utils._text import to_bytes, to_native
from ansible.plugins.loader import vars_loader
from ansible.utils.vars import combine_vars
from ansible.utils.display import Display
from ansible.vars.plugins import get_vars_from_inventory_sources, get_vars_from_path
Expand Down
10 changes: 7 additions & 3 deletions lib/ansible/cli/scripts/ansible_connection_cli_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ def start(self, variables):
# find it now that our cwd is /
if self.play_context.private_key_file and self.play_context.private_key_file[0] not in '~/':
self.play_context.private_key_file = os.path.join(self.original_path, self.play_context.private_key_file)
self.connection = connection_loader.get(self.play_context.connection, self.play_context, '/dev/null',
task_uuid=self._task_uuid, ansible_playbook_pid=self._ansible_playbook_pid)
self.connection = connection_loader.get(self.play_context.connection)(
self.play_context,
'/dev/null',
task_uuid=self._task_uuid,
ansible_playbook_pid=self._ansible_playbook_pid
)
self.connection.set_options(var_options=variables)

self.connection._socket_path = self.socket_path
Expand Down Expand Up @@ -256,7 +260,7 @@ def main():
})

if rc == 0:
ssh = connection_loader.get('ssh', class_only=True)
ssh = connection_loader.get('ssh')
ansible_playbook_pid = sys.argv[1]
task_uuid = sys.argv[2]
cp = ssh._create_control_path(play_context.remote_addr, play_context.port, play_context.remote_user, play_context.connection, ansible_playbook_pid)
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/executor/module_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def recursive_finder(name, module_fqn, data, py_module_names, py_module_cache, z
# Determine what imports that we've found are modules (vs class, function.
# variable names) for packages
#
module_utils_paths = [p for p in module_utils_loader._get_paths(subdirs=False) if os.path.isdir(p)]
module_utils_paths = [p for p in module_utils_loader._scanned_paths.keys() if os.path.isdir(p)]
# FIXME: Do we still need this? It feels like module-utils_loader should include
# _MODULE_UTILS_PATH
module_utils_paths.append(_MODULE_UTILS_PATH)
Expand Down
11 changes: 7 additions & 4 deletions lib/ansible/executor/playbook_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.module_utils._text import to_text
from ansible.module_utils.parsing.convert_bool import boolean
from ansible.plugins.loader import become_loader, connection_loader, shell_loader
# from ansible.plugins.loader import become_loader, connection_loader, shell_loader
from ansible.playbook import Playbook
from ansible.template import Templar
from ansible.utils.helpers import pct_to_int
Expand Down Expand Up @@ -83,9 +83,12 @@ def run(self):
entry = {}
try:
# preload become/connection/shell to set config defs cached
list(connection_loader.all(class_only=True))
list(shell_loader.all(class_only=True))
list(become_loader.all(class_only=True))
# FIXME: this shouldn't be required anymore so commenting these lines
# out for now, they should be removed later if there are no
# problems with this.
# list(connection_loader.all(class_only=True))
# list(shell_loader.all(class_only=True))
# list(become_loader.all(class_only=True))

for playbook_path in self._playbooks:
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
Expand Down
14 changes: 5 additions & 9 deletions lib/ansible/executor/task_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _get_loop_items(self):
loop_terms = [t for t in loop_terms if not templar.is_template(t)]

# get lookup
mylookup = self._shared_loader_obj.lookup_loader.get(self._task.loop_with, loader=self._loader, templar=templar)
mylookup = self._shared_loader_obj.lookup_loader.get(self._task.loop_with)(loader=self._loader, templar=templar)

# give lookup task 'context' for subdir (mostly needed for first_found)
for subdir in ['template', 'var', 'file']: # TODO: move this to constants?
Expand Down Expand Up @@ -806,8 +806,7 @@ def _poll_async_result(self, result, templar, task_vars=None):
# Because this is an async task, the action handler is async. However,
# we need the 'normal' action handler for the status check, so get it
# now via the action_loader
async_handler = self._shared_loader_obj.action_loader.get(
'async_status',
async_handler = self._shared_loader_obj.action_loader.get('async_status')(
task=async_task,
connection=self._connection,
play_context=self._play_context,
Expand Down Expand Up @@ -859,7 +858,7 @@ def _poll_async_result(self, result, templar, task_vars=None):
return async_result

def _get_become(self, name):
become = become_loader.get(name)
become = become_loader.get(name)()
if not become:
raise AnsibleError("Invalid become method specified, could not find matching plugin: '%s'. "
"Use `ansible-doc -t become -l` to list available plugins." % name)
Expand Down Expand Up @@ -887,8 +886,7 @@ def _get_connection(self, variables, templar):

# load connection
conn_type = self._play_context.connection
connection = self._shared_loader_obj.connection_loader.get(
conn_type,
connection = self._shared_loader_obj.connection_loader.get(conn_type)(
self._play_context,
self._new_stdin,
task_uuid=self._task._uuid,
Expand Down Expand Up @@ -1039,15 +1037,13 @@ def _get_action_handler(self, connection, templar):
handler_name = 'normal'
collections = None # until then, we don't want the task's collection list to be consulted; use the builtin

handler = self._shared_loader_obj.action_loader.get(
handler_name,
handler = self._shared_loader_obj.action_loader.get(handler_name, collection_list=collections)(
task=self._task,
connection=connection,
play_context=self._play_context,
loader=self._loader,
templar=templar,
shared_loader_obj=self._shared_loader_obj,
collection_list=collections
)

if not handler:
Expand Down
8 changes: 4 additions & 4 deletions lib/ansible/executor/task_queue_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ def load_callbacks(self):
if self._stdout_callback not in callback_loader:
raise AnsibleError("Invalid callback for stdout specified: %s" % self._stdout_callback)
else:
self._stdout_callback = callback_loader.get(self._stdout_callback)
self._stdout_callback = callback_loader.get(self._stdout_callback)()
self._stdout_callback.set_options()
stdout_callback_loaded = True
else:
raise AnsibleError("callback must be an instance of CallbackBase or the name of a callback plugin")

for callback_plugin in callback_loader.all(class_only=True):
for callback_plugin in callback_loader.all():
callback_type = getattr(callback_plugin, 'CALLBACK_TYPE', '')
callback_needs_whitelist = getattr(callback_plugin, 'CALLBACK_NEEDS_WHITELIST', False)
(callback_name, _) = os.path.splitext(os.path.basename(callback_plugin._original_path))
Expand All @@ -161,7 +161,7 @@ def load_callbacks(self):

for callback_plugin_name in (c for c in C.DEFAULT_CALLBACK_WHITELIST if AnsibleCollectionRef.is_valid_fqcr(c)):
# TODO: need to extend/duplicate the stdout callback check here (and possible move this ahead of the old way
callback_obj = callback_loader.get(callback_plugin_name)
callback_obj = callback_loader.get(callback_plugin_name)()
callback_obj.set_options()
self._callback_plugins.append(callback_obj)

Expand Down Expand Up @@ -218,7 +218,7 @@ def run(self, play):
self._initialize_processes(min(self._forks, iterator.batch_size))

# load the specified strategy (or the default linear one)
strategy = strategy_loader.get(new_play.strategy, self)
strategy = strategy_loader.get(new_play.strategy)(self)
if strategy is None:
raise AnsibleError("Invalid play strategy specified: %s" % new_play.strategy, obj=play._ds)

Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/inventory/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _fetch_inventory_plugins(self):

plugins = []
for name in C.INVENTORY_ENABLED:
plugin = inventory_loader.get(name)
plugin = inventory_loader.get(name)()
if plugin:
plugins.append(plugin)
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def _configure_module(self, module_name, module_args, task_vars=None):
if key in module_args:
module_args[key] = self._connection._shell._unquote(module_args[key])

module_path = self._shared_loader_obj.module_loader.find_plugin(module_name, mod_type, collection_list=self._task.collections)
module_path = self._shared_loader_obj.module_loader.find_plugin(module_name, mod_type, collection_list=self._task.collections)[1]
if module_path:
break
else: # This is a for-else: http://bit.ly/1ElPkyg
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def __init__(self, plugin_name='memory', **kwargs):
self._cache = {}
self._retrieved = {}

self._plugin = cache_loader.get(plugin_name, **kwargs)
self._plugin = cache_loader.get(plugin_name)(**kwargs)
if not self._plugin:
raise AnsibleError('Unable to load the cache plugin (%s).' % plugin_name)

Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/plugins/connection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):

self._network_os = self._play_context.network_os

self._local = connection_loader.get('local', play_context, '/dev/null')
self._local = connection_loader.get('local')(play_context, '/dev/null')
self._local.set_options()

self._sub_plugin = {}
Expand Down Expand Up @@ -364,7 +364,7 @@ def _update_connection_state(self):
to True. If the socket path doesn't exist, leave the socket path
value to None and the _connected value to False
'''
ssh = connection_loader.get('ssh', class_only=True)
ssh = connection_loader.get('ssh')
control_path = ssh._create_control_path(
self._play_context.remote_addr, self._play_context.port,
self._play_context.remote_user, self._play_context.connection,
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/inventory/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def parse(self, inventory, loader, path, cache=True):
if not plugin_name:
raise AnsibleParserError("no root 'plugin' key found, '{0}' is not a valid YAML inventory plugin config file".format(path))

plugin = inventory_loader.get(plugin_name)
plugin = inventory_loader.get(plugin_name)()

if not plugin:
raise AnsibleParserError("inventory config '{0}' specifies unknown plugin '{1}'".format(path, plugin_name))
Expand Down
Loading

0 comments on commit 12a2223

Please sign in to comment.