def identify_plugins(self, enabled_plugins): if not os.path.exists(self.plugin_location): return [] module_name = os.path.basename(self.plugin_location) root_plugin_directory = self.plugin_location plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) plugins = [] found_modules = pynsive.list_modules(module_name) for found_module in found_modules: module_filename, module_name = found_module.split('.') if enabled_plugins is not None and module_name not in enabled_plugins: # Skip this plugin since it's not listed as enabled plugins # as long as we have specified some enabled plugins though # this allows us to specify no specific plugins and get all of them continue module_obj = pynsive.import_module(found_module) reload(module_obj) plugin_class_obj = module_obj.Command() logger.info('Plugin {0} registered to receive command with {1}'.format(module_name, plugin_class_obj.command_name)) plugins.append( { 'plugin_class': plugin_class_obj, 'command_name': plugin_class_obj.command_name, 'help_text': plugin_class_obj.help_text } ) return plugins
def setUpClass(cls): """ This crazy setUp method for the following unit tests creates a temporary plugin directory and then drops a Python module and related testing code into it. The method then spins up a plugin context via a pynsive.PluginManager instance. """ cls.directory = tempfile.mkdtemp() cls.module_path = os.path.join(cls.directory, 'pynsive_test') os.mkdir(cls.module_path) with open(os.path.join(cls.module_path, '__init__.py'), 'w') as f: f.write(INIT_PY) with open(os.path.join(cls.module_path, 'test_classes.py'), 'w') as f: f.write(TEST_CLASSES_PY) embedded_module = os.path.join(cls.module_path, 'embedded') os.mkdir(embedded_module) with open(os.path.join(embedded_module, '__init__.py'), 'w') as f: f.write('\n') with open(os.path.join(embedded_module, 'test.py'), 'w') as f: f.write('\n') cls.plugin_manager = pynsive.PluginManager() cls.plugin_manager.plug_into(cls.directory)
def setUp(self): self.req_message = http.HttpRequest() self.req_message.url = 'http://127.0.0.1' self.req_message.method = 'GET' self.req_message.version = "1.0" plugin_manager = pynsive.PluginManager() plugin_manager.plug_into('examples/filter') simple_filter_plugin = pynsive.import_module('simple_example') self.simple_filter = simple_filter_plugin.SimpleFilter()
def registerPlugins(): '''walk the plugins directory and register modules in pluginList as a tuple: (mfile, mname, mdescription, mreg, mpriority, mclass) ''' plugin_location = os.path.join(os.path.dirname(__file__), "plugins") module_name = os.path.basename(plugin_location) root_plugin_directory = os.path.join(plugin_location, '..') plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) if os.path.exists(plugin_location): modules = pynsive.list_modules(module_name) for mfile in modules: module = pynsive.import_module(mfile) importlib.reload(module) if not module: raise ImportError('Unable to load module {}'.format(mfile)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration mclass.restoptions = options.__dict__ if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if 'name' in dir(mclass): mname = mclass.name else: mname = mfile if 'description' in dir(mclass): mdescription = mclass.description else: mdescription = mfile if isinstance(mreg, list): logger.info( '[*] plugin {0} registered to receive messages from /{1}' .format(mfile, mreg)) pluginList.append((mfile, mname, mdescription, mreg, mpriority, mclass))
def identify_plugins(self, enabled_plugins): if not os.path.exists(self.plugin_location): return [] module_name = os.path.basename(self.plugin_location) root_plugin_directory = os.path.join(self.plugin_location, '..') plugin_manager = pynsive.PluginManager() plugin_manager.plug_into(root_plugin_directory) plugins = [] found_modules = pynsive.list_modules(module_name) for found_module in found_modules: module_filename, module_name = found_module.split('.') if enabled_plugins is not None and module_name not in enabled_plugins: # Skip this plugin since it's not listed as enabled plugins # as long as we have specified some enabled plugins though # this allows us to specify no specific plugins and get all of them continue try: module_obj = pynsive.import_module(found_module) reload(module_obj) plugin_class_obj = module_obj.message() if 'priority' in dir(plugin_class_obj): priority = plugin_class_obj.priority else: priority = 100 logger.info( '[*] plugin {0} registered to receive messages with {1}'. format(module_name, plugin_class_obj.registration)) plugins.append({ 'plugin_class': plugin_class_obj, 'registration': plugin_class_obj.registration, 'priority': priority }) except Exception as e: logger.exception( 'Received exception when loading {0} plugins\n{1}'.format( module_name, e.message)) plugin_manager.destroy() return plugins
def start_proxy(sockets, config): # Take over SIGTERM and SIGINT signal.signal(signal.SIGTERM, stop_child) signal.signal(signal.SIGINT, stop_child) # Create a PluginManager plugin_manager = pynsive.PluginManager() for path in config.core.plugin_paths: plugin_manager.plug_into(path) # Resolve our filter chains try: if config.pipeline.use_singletons: filter_pipeline_factories = _build_singleton_plfactories(config) else: filter_pipeline_factories = _build_plfactories(config) except Exception as ex: _LOG.exception(ex) return -1 # Load any SSL configurations ssl_options = None cert_file = config.ssl.cert_file key_file = config.ssl.key_file if None not in (cert_file, key_file): ssl_options = dict() ssl_options['certfile'] = cert_file ssl_options['keyfile'] = key_file _LOG.debug('SSL enabled: {}'.format(ssl_options)) # Create proxy server ref http_proxy = TornadoHttpProxy(filter_pipeline_factories, config.routing.upstream_hosts, ssl_options) # Add our sockets for watching http_proxy.add_sockets(sockets) # Start tornado IOLoop.current().start()
def registerPlugins(): '''walk the ./plugins directory and register modules in pluginList as a tuple: (mfile, mname, mdescription, mreg, mpriority, mclass) ''' plugin_manager = pynsive.PluginManager() if os.path.exists('plugins'): modules = pynsive.list_modules('plugins') for mfile in modules: module = pynsive.import_module(mfile) reload(module) if not module: raise ImportError('Unable to load module {}'.format(mfile)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration mclass.restoptions = options if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if 'name' in dir(mclass): mname = mclass.name else: mname = mfile if 'description' in dir(mclass): mdescription = mclass.description else: mdescription = mfile if isinstance(mreg, list): logger.info( '[*] plugin {0} registered to receive messages from /{1}' .format(mfile, mreg)) pluginList.append((mfile, mname, mdescription, mreg, mpriority, mclass))
def registerPlugins(): pluginList = list() # tuple of module,registration dict,priority plugin_manager = pynsive.PluginManager() if os.path.exists('plugins'): modules = pynsive.list_modules('plugins') for mname in modules: module = pynsive.import_module(mname) reload(module) if not module: raise ImportError('Unable to load module {}'.format(mname)) else: if 'message' in dir(module): mclass = module.message() mreg = mclass.registration if 'priority' in dir(mclass): mpriority = mclass.priority else: mpriority = 100 if isinstance(mreg, list): print('[*] plugin {0} registered to receive messages with {1}'.format(mname, mreg)) pluginList.append((mclass, mreg, mpriority)) return pluginList
def setUpClass(cls): """ This crazy setUp method for the following unit tests creates a temporary plugin directory and then drops a Python module and related testing code into it. The method then spins up a plugin context via a pynsive.PluginManager instance. """ cls.directory = tempfile.mkdtemp() cls.module_path = os.path.join(cls.directory, 'pynsive_test') os.mkdir(cls.module_path) with open(os.path.join(cls.module_path, '__init__.py'), 'w') as f: f.write(INIT_PY) with open(os.path.join(cls.module_path, 'test_classes.py'), 'w') as f: f.write(TEST_CLASSES_PY) embedded_module = os.path.join(cls.module_path, 'embedded') os.mkdir(embedded_module) with open(os.path.join(embedded_module, '__init__.py'), 'w') as f: f.write('\n') with open(os.path.join(embedded_module, 'test.py'), 'w') as f: f.write('\n') # Adding sub-sub module sub_module = os.path.join(embedded_module, 'sub') os.mkdir(sub_module) with open(os.path.join(sub_module, '__init__.py'), 'w') as f: f.write('\n') with open(os.path.join(sub_module, 'other.py'), 'w') as f: f.write(TEST_CLASSES_PY) # Adding an empty (without __init__.py) into the package # - Pynsive should ignore this folder garbage_folder = os.path.join(cls.module_path, 'garbage') os.mkdir(garbage_folder) cls.plugin_manager = pynsive.PluginManager() cls.plugin_manager.plug_into(cls.directory)
def setUp(self): self.manager = pynsive.PluginManager()