mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Create a plugin loader system
This commit is contained in:
@@ -45,14 +45,6 @@ try:
|
||||
except ImportError:
|
||||
HAS_ATFORK=False
|
||||
|
||||
dirname = os.path.dirname(__file__)
|
||||
action_plugin_list = utils.import_plugins(os.path.join(dirname, 'action_plugins'))
|
||||
for i in reversed(C.DEFAULT_ACTION_PLUGIN_PATH.split(os.pathsep)):
|
||||
action_plugin_list.update(utils.import_plugins(i))
|
||||
lookup_plugin_list = utils.import_plugins(os.path.join(dirname, 'lookup_plugins'))
|
||||
for i in reversed(C.DEFAULT_LOOKUP_PLUGIN_PATH.split(os.pathsep)):
|
||||
lookup_plugin_list.update(utils.import_plugins(i))
|
||||
|
||||
multiprocessing_runner = None
|
||||
|
||||
################################################
|
||||
@@ -164,19 +156,6 @@ class Runner(object):
|
||||
# ensure we are using unique tmp paths
|
||||
random.seed()
|
||||
|
||||
# instantiate plugin classes
|
||||
self.action_plugins = {}
|
||||
self.lookup_plugins = {}
|
||||
for (k,v) in action_plugin_list.iteritems():
|
||||
self.action_plugins[k] = v.ActionModule(self)
|
||||
for (k,v) in lookup_plugin_list.iteritems():
|
||||
self.lookup_plugins[k] = v.LookupModule(runner=self, basedir=self.basedir)
|
||||
|
||||
for (k,v) in utils.import_plugins(os.path.join(self.basedir, 'action_plugins')).iteritems():
|
||||
self.action_plugins[k] = v.ActionModule(self)
|
||||
for (k,v) in utils.import_plugins(os.path.join(self.basedir, 'lookup_plugins')).iteritems():
|
||||
self.lookup_plugins[k] = v.LookupModule(runner=self, basedir=self.basedir)
|
||||
|
||||
# *****************************************************
|
||||
|
||||
def _transfer_str(self, conn, tmp, name, data):
|
||||
@@ -292,10 +271,10 @@ class Runner(object):
|
||||
# allow with_foo to work in playbooks...
|
||||
items = None
|
||||
items_plugin = self.module_vars.get('items_lookup_plugin', None)
|
||||
if items_plugin is not None and items_plugin in self.lookup_plugins:
|
||||
if items_plugin is not None and items_plugin in utils.plugins.lookup_loader:
|
||||
items_terms = self.module_vars.get('items_lookup_terms', '')
|
||||
items_terms = utils.varReplaceWithItems(self.basedir, items_terms, inject)
|
||||
items = self.lookup_plugins[items_plugin].run(items_terms, inject=inject)
|
||||
items = utils.plugins.lookup_loader.get(items_plugin, runner=self, basedir=self.basedir).run(items_terms, inject=inject)
|
||||
if type(items) != list:
|
||||
raise errors.AnsibleError("lookup plugins have to return a list: %r" % items)
|
||||
|
||||
@@ -417,16 +396,16 @@ class Runner(object):
|
||||
tmp = self._make_tmp_path(conn)
|
||||
result = None
|
||||
|
||||
handler = self.action_plugins.get(module_name, None)
|
||||
if handler:
|
||||
if module_name in utils.plugins.action_loader:
|
||||
if self.background != 0:
|
||||
raise errors.AnsibleError("async mode is not supported with the %s module" % module_name)
|
||||
handler = utils.plugins.action_loader.get(module_name, self)
|
||||
result = handler.run(conn, tmp, module_name, module_args, inject)
|
||||
else:
|
||||
if self.background == 0:
|
||||
result = self.action_plugins['normal'].run(conn, tmp, module_name, module_args, inject)
|
||||
result = utils.plugins.action_loader.get('normal', self).run(conn, tmp, module_name, module_args, inject)
|
||||
else:
|
||||
result = self.action_plugins['async'].run(conn, tmp, module_name, module_args, inject)
|
||||
result = utils.plugins.action_loader.get('async', self).run(conn, tmp, module_name, module_args, inject)
|
||||
|
||||
conn.close()
|
||||
|
||||
@@ -648,7 +627,7 @@ class Runner(object):
|
||||
# to be ran once per group of hosts. Example module: pause,
|
||||
# run once per hostgroup, rather than pausing once per each
|
||||
# host.
|
||||
p = self.action_plugins.get(self.module_name, None)
|
||||
p = utils.plugins.action_loader.get(self.module_name, self)
|
||||
if p and getattr(p, 'BYPASS_HOST_LOOP', None):
|
||||
# Expose the current hostgroup to the bypassing plugins
|
||||
self.host_set = hosts
|
||||
|
||||
@@ -24,32 +24,19 @@ import ansible.constants as C
|
||||
|
||||
import os
|
||||
import os.path
|
||||
dirname = os.path.dirname(__file__)
|
||||
modules = utils.import_plugins(os.path.join(dirname, 'connection_plugins'))
|
||||
for i in reversed(C.DEFAULT_CONNECTION_PLUGIN_PATH.split(os.pathsep)):
|
||||
modules.update(utils.import_plugins(i))
|
||||
|
||||
# rename this module
|
||||
modules['paramiko'] = modules['paramiko_ssh']
|
||||
del modules['paramiko_ssh']
|
||||
|
||||
class Connection(object):
|
||||
''' Handles abstract connections to remote hosts '''
|
||||
|
||||
def __init__(self, runner):
|
||||
self.runner = runner
|
||||
self.modules = None
|
||||
|
||||
def connect(self, host, port):
|
||||
if self.modules is None:
|
||||
self.modules = modules.copy()
|
||||
self.modules.update(utils.import_plugins(os.path.join(self.runner.basedir, 'connection_plugins')))
|
||||
conn = None
|
||||
transport = self.runner.transport
|
||||
module = self.modules.get(transport, None)
|
||||
if module is None:
|
||||
conn = utils.plugins.connection_loader.get(transport, self.runner, host, port)
|
||||
if conn is None:
|
||||
raise AnsibleError("unsupported connection type: %s" % transport)
|
||||
conn = module.Connection(self.runner, host, port)
|
||||
self.active = conn.connect()
|
||||
return self.active
|
||||
|
||||
|
||||
Reference in New Issue
Block a user