def test_print_paths(self, mock_method): mock_method.return_value = ['/path/one', '/path/two', '/path/three'] pl = PluginLoader('foo', 'foo', '', 'test_plugins') paths = pl.print_paths() expected_paths = os.pathsep.join( ['/path/one', '/path/two', '/path/three']) self.assertEqual(paths, expected_paths)
def test_plugins__get_package_paths_with_package(self): # the _get_package_paths() call uses __import__ to load a # python library, and then uses the __file__ attribute of # the result for that to get the library path, so we mock # that here and patch the builtin to use our mocked result m = MagicMock() m.return_value.__file__ = '/path/to/my/test.py' pl = PluginLoader('test', 'foo.bar.bam', 'test', 'test_plugin') with patch('{0}.__import__'.format(BUILTINS), m): self.assertEqual(pl._get_package_paths(), ['/path/to/my/bar/bam'])
def _setup_inventory_plugins(self): ''' sets up loaded inventory plugins for usage ''' inventory_loader = PluginLoader('InventoryModule', 'ansible.plugins.inventory', 'inventory_plugins', 'inventory_plugins') display.vvvv('setting up inventory plugins') for name in C.INVENTORY_ENABLED: plugin = inventory_loader.get(name) if plugin: self._inventory_plugins.append(plugin) else: display.warning('Failed to load inventory plugin, skipping %s' % name) if not self._inventory_plugins: raise AnsibleError("No inventory plugins available to generate inventory, make sure you have at least one whitelisted.")
class Template(object): def __init__(self, root_dir, ops_config): loader = ChoiceLoader([ FileSystemLoader(root_dir), FileSystemLoader("/") ]) mode = ops_config.get('jinja2.undefined') undefined = Undefined if mode == 'StrictUndefined': undefined = StrictUndefined elif mode == 'DebugUndefined': undefined = DebugUndefined self.env = Environment(loader=loader, undefined=undefined) self.filter_plugin_loader = PluginLoader( 'FilterModule', 'ansible.plugins.filter', ops_config.ansible_filter_plugins.split(':'), 'filter_plugins' ) for filter in self.filter_plugin_loader.all(): self.env.filters.update(filter.filters()) def render(self, source, vars): jinja_template = self.env.get_template(source) return jinja_template.render(**vars)
def _setup_inventory_plugins(self): ''' sets up loaded inventory plugins for usage ''' inventory_loader = PluginLoader('InventoryModule', 'ansible.plugins.inventory', 'inventory_plugins', 'inventory_plugins') display.vvvv('setting up inventory plugins') for name in C.INVENTORY_ENABLED: plugin = inventory_loader.get(name) name = os.path.splitext(os.path.basename(plugin._original_path))[0] self._inventory_plugins[name] = plugin if not self._inventory_plugins: raise AnsibleError( "No inventory plugins available to generate inventory, make sure you have at least one whitelisted." )
def test_plugins__get_paths(self): pl = PluginLoader('test', '', 'test', 'test_plugin') pl._paths = ['/path/one', '/path/two'] self.assertEqual(pl._get_paths(), ['/path/one', '/path/two'])
def test_plugins__get_package_paths_no_package(self): pl = PluginLoader('test', '', 'test', 'test_plugin') self.assertEqual(pl._get_package_paths(), [])
def test_print_paths(self, mock_method): mock_method.return_value = ['/path/one', '/path/two', '/path/three'] pl = PluginLoader('foo', 'foo', '', 'test_plugins') paths = pl.print_paths() expected_paths = os.pathsep.join(['/path/one', '/path/two', '/path/three']) self.assertEqual(paths, expected_paths)
def run(self): super(DocCLI, self).run() plugin_type = self.options.type # choose plugin type if plugin_type == 'cache': loader = cache_loader elif plugin_type == 'callback': loader = callback_loader elif plugin_type == 'connection': loader = connection_loader elif plugin_type == 'lookup': loader = lookup_loader elif plugin_type == 'strategy': loader = strategy_loader elif plugin_type == 'inventory': loader = PluginLoader('InventoryModule', 'ansible.plugins.inventory', 'inventory_plugins', 'inventory_plugins') else: loader = module_loader # add to plugin path from command line if self.options.module_path is not None: for i in self.options.module_path.split(os.pathsep): loader.add_directory(i) # list plugins for type if self.options.list_dir: paths = loader._get_paths() for path in paths: self.find_plugins(path, plugin_type) self.pager(self.get_plugin_list_text(loader)) return 0 # process all plugins of type if self.options.all_plugins: paths = loader._get_paths() for path in paths: self.find_plugins(path, plugin_type) if len(self.args) == 0: raise AnsibleOptionsError("Incorrect options passed") # process command line list text = '' for plugin in self.args: try: # if the plugin lives in a non-python file (eg, win_X.ps1), require the corresponding python file for docs filename = loader.find_plugin(plugin, mod_type='.py', ignore_deprecated=True) if filename is None: display.warning( "%s %s not found in %s\n" % (plugin_type, plugin, DocCLI.print_paths(loader))) continue if any(filename.endswith(x) for x in C.BLACKLIST_EXTS): continue try: doc, plainexamples, returndocs, metadata = plugin_docs.get_docstring( filename, verbose=(self.options.verbosity > 0)) except: display.vvv(traceback.format_exc()) display.error( "%s %s has a documentation error formatting or is missing documentation." % (plugin_type, plugin)) continue if doc is not None: # assign from other sections doc['plainexamples'] = plainexamples doc['returndocs'] = returndocs doc['metadata'] = metadata # generate extra data if plugin_type == 'module': # is there corresponding action plugin? if plugin in action_loader: doc['action'] = True else: doc['action'] = False doc['filename'] = filename doc['now_date'] = datetime.date.today().strftime( '%Y-%m-%d') doc['docuri'] = doc[plugin_type].replace('_', '-') if self.options.show_snippet and plugin_type == 'module': text += self.get_snippet_text(doc) else: text += self.get_man_text(doc) else: # this typically means we couldn't even parse the docstring, not just that the YAML is busted, # probably a quoting issue. raise AnsibleError("Parsing produced an empty object.") except Exception as e: display.vvv(traceback.format_exc()) raise AnsibleError( "%s %s missing documentation (or could not parse documentation): %s\n" % (plugin_type, plugin, str(e))) if text: self.pager(text) return 0
def assertPluginLoaderConfigBecomes(self, arg, expected): pl = PluginLoader('test', '', arg, 'test_plugin') self.assertEqual(pl.config, expected)
import getpass from ansible import constants as C from ansible.errors import AnsibleError from ansible.module_utils._text import to_bytes from ansible.playbook.play_context import PlayContext from ansible.plugins import PluginLoader from updatewrapper.command import exec_command connection_loader = PluginLoader( 'Connection', 'updatewrapper.connection.ssh', C.DEFAULT_CONNECTION_PLUGIN_PATH, 'connection_plugins', aliases={'paramiko': 'paramiko_ssh'}, required_base_class='ConnectionBase', ) class Host: def __init__(self, name='localhost', addr='localhost', port='22', user='******', sudo=False, sudo_pass=False, flavor='debian'): self.name = addr if name is 'localhost' else name self.addr = name if addr is 'localhost' else addr