plugin version validation feature added

This commit is contained in:
Volkan Şahin 2016-10-19 12:16:37 +03:00
parent d4a8b30a4f
commit 08c0a3b028
2 changed files with 51 additions and 51 deletions

View file

@ -60,10 +60,10 @@ class Plugin(threading.Thread):
Plugin class responsible for processing TASK or USER PLUGIN PROFILE. Plugin class responsible for processing TASK or USER PLUGIN PROFILE.
""" """
def __init__(self, name, InQueue): def __init__(self, name, in_ueue):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.name = name self.name = name
self.InQueue = InQueue self.in_queue = in_ueue
scope = Scope.get_instance() scope = Scope.get_instance()
self.logger = scope.get_logger() self.logger = scope.get_logger()
@ -79,7 +79,7 @@ class Plugin(threading.Thread):
while self.keep_run: while self.keep_run:
try: try:
try: try:
item_obj = self.InQueue.get(block=True) item_obj = self.in_queue.get(block=True)
obj_name = item_obj.obj_name obj_name = item_obj.obj_name
except Exception as e: except Exception as e:
self.logger.error( self.logger.error(
@ -88,6 +88,7 @@ class Plugin(threading.Thread):
if obj_name == "TASK": if obj_name == "TASK":
self.logger.debug('[Plugin] Executing task') self.logger.debug('[Plugin] Executing task')
command = Scope.get_instance().get_plugin_manager().find_command(self.getName(), command = Scope.get_instance().get_plugin_manager().find_command(self.getName(),
item_obj.get_plugin().get_version(),
item_obj.get_command_cls_id().lower()) item_obj.get_command_cls_id().lower())
self.context.put('task_id', item_obj.get_id()) self.context.put('task_id', item_obj.get_id())
@ -158,7 +159,7 @@ class Plugin(threading.Thread):
self.logger.debug('[Plugin] Executing profile') self.logger.debug('[Plugin] Executing profile')
profile_data = item_obj.get_profile_data() profile_data = item_obj.get_profile_data()
policy_module = Scope.get_instance().get_plugin_manager().find_policy_module( policy_module = Scope.get_instance().get_plugin_manager().find_policy_module(
item_obj.get_plugin().get_name()) item_obj.get_plugin().get_name(), item_obj.get_plugin().get_version())
self.context.put('username', item_obj.get_username()) self.context.put('username', item_obj.get_username())
execution_id = self.get_execution_id(item_obj.get_id()) execution_id = self.get_execution_id(item_obj.get_id())

View file

@ -25,13 +25,13 @@ class PluginManager(object):
def __init__(self): def __init__(self):
super(PluginManager, self).__init__() super(PluginManager, self).__init__()
self.scope = Scope.get_instance() self.scope = Scope.get_instance()
self.configManager = self.scope.get_configuration_manager() self.config_manager = self.scope.get_configuration_manager()
self.db_service = self.scope.get_db_service() self.db_service = self.scope.get_db_service()
self.message_manager = self.scope.get_message_manager() self.message_manager = self.scope.get_message_manager()
self.logger = self.scope.get_logger() self.logger = self.scope.get_logger()
self.plugins = [] self.plugins = []
self.pluginQueueDict = dict() self.plugin_queue_dict = dict()
# self.listener = \ # self.listener = \
self.install_listener() self.install_listener()
@ -44,7 +44,7 @@ class PluginManager(object):
self.plugins = [] self.plugins = []
self.logger.debug('Lookup for possible plugins...') self.logger.debug('Lookup for possible plugins...')
try: try:
possible_plugins = os.listdir(self.configManager.get("PLUGIN", "pluginFolderPath")) possible_plugins = os.listdir(self.config_manager.get("PLUGIN", "pluginFolderPath"))
self.logger.debug('Possible plugins: {0} '.format(str(possible_plugins))) self.logger.debug('Possible plugins: {0} '.format(str(possible_plugins)))
for plugin_name in possible_plugins: for plugin_name in possible_plugins:
try: try:
@ -59,8 +59,8 @@ class PluginManager(object):
def load_single_plugin(self, plugin_name): def load_single_plugin(self, plugin_name):
# TODO check already loaded plugin # TODO check already loaded plugin
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name) location = os.path.join(self.config_manager.get("PLUGIN", "pluginFolderPath"), plugin_name)
if not os.path.isdir(location) or not self.configManager.get("PLUGIN", "mainModuleName") + ".py" in os.listdir( if not os.path.isdir(location) or not self.config_manager.get("PLUGIN", "mainModuleName") + ".py" in os.listdir(
location): location):
self.logger.debug( self.logger.debug(
'It is not a plugin location ! There is no main module - {0}'.format(str(location))) 'It is not a plugin location ! There is no main module - {0}'.format(str(location)))
@ -70,8 +70,8 @@ class PluginManager(object):
'{0} plugin was already loaded. Reloading {0} plugin'.format(plugin_name)) '{0} plugin was already loaded. Reloading {0} plugin'.format(plugin_name))
# self.reload_single_plugin(plugin_name) # self.reload_single_plugin(plugin_name)
else: else:
self.pluginQueueDict[plugin_name] = PluginQueue() self.plugin_queue_dict[plugin_name] = PluginQueue()
plugin = Plugin(plugin_name, self.pluginQueueDict[plugin_name]) plugin = Plugin(plugin_name, self.plugin_queue_dict[plugin_name])
plugin.setDaemon(True) plugin.setDaemon(True)
plugin.start() plugin.start()
self.plugins.append(plugin) self.plugins.append(plugin)
@ -79,22 +79,22 @@ class PluginManager(object):
# active init mode # active init mode
mode = InitMode() mode = InitMode()
self.pluginQueueDict[plugin_name].put(mode, 1) self.plugin_queue_dict[plugin_name].put(mode, 1)
if plugin_name in self.delayed_profiles: if plugin_name in self.delayed_profiles:
self.pluginQueueDict[plugin_name].put(self.delayed_profiles[plugin_name], 1) self.plugin_queue_dict[plugin_name].put(self.delayed_profiles[plugin_name], 1)
del self.delayed_profiles[plugin_name] del self.delayed_profiles[plugin_name]
self.logger.debug('Delayed profile was found for this plugin. It will be run.') self.logger.debug('Delayed profile was found for this plugin. It will be run.')
if plugin_name in self.delayed_tasks: if plugin_name in self.delayed_tasks:
self.pluginQueueDict[plugin_name].put(self.delayed_tasks[plugin_name], 1) self.plugin_queue_dict[plugin_name].put(self.delayed_tasks[plugin_name], 1)
del self.delayed_tasks[plugin_name] del self.delayed_tasks[plugin_name]
self.logger.debug('Delayed task was found for this plugin. It will be run.') self.logger.debug('Delayed task was found for this plugin. It will be run.')
def reload_plugins(self): def reload_plugins(self):
try: try:
self.logger.info('Reloading plugins...') self.logger.info('Reloading plugins...')
for p_queue in self.pluginQueueDict: for p_queue in self.plugin_queue_dict:
self.pluginQueueDict[p_queue].put(ShutdownMode(), 1) self.plugin_queue_dict[p_queue].put(ShutdownMode(), 1)
self.plugins = [] self.plugins = []
self.load_plugins() self.load_plugins()
self.logger.info('Plugin reloaded successfully.') self.logger.info('Plugin reloaded successfully.')
@ -116,10 +116,10 @@ class PluginManager(object):
def remove_plugins(self): def remove_plugins(self):
try: try:
self.logger.debug('Removing all plugins...') self.logger.debug('Removing all plugins...')
for p_queue in self.pluginQueueDict: for p_queue in self.plugin_queue_dict:
self.pluginQueueDict[p_queue].put(ShutdownMode(), 1) self.plugin_queue_dict[p_queue].put(ShutdownMode(), 1)
self.plugins = [] self.plugins = []
self.pluginQueueDict = dict() self.plugin_queue_dict = dict()
self.logger.debug('All plugins were removed successfully.') self.logger.debug('All plugins were removed successfully.')
except Exception as e: except Exception as e:
self.logger.error( self.logger.error(
@ -130,8 +130,8 @@ class PluginManager(object):
self.logger.debug('Trying to remove {0} plugin...'.format(plugin_name)) self.logger.debug('Trying to remove {0} plugin...'.format(plugin_name))
if self.is_plugin_loaded(plugin_name): if self.is_plugin_loaded(plugin_name):
self.logger.debug('{0} plugin is killing...'.format(plugin_name)) self.logger.debug('{0} plugin is killing...'.format(plugin_name))
self.pluginQueueDict[plugin_name].put(ShutdownMode(), 1) self.plugin_queue_dict[plugin_name].put(ShutdownMode(), 1)
del self.pluginQueueDict[plugin_name] del self.plugin_queue_dict[plugin_name]
for plugin in self.plugins: for plugin in self.plugins:
if plugin.name == plugin_name: if plugin.name == plugin_name:
@ -144,13 +144,13 @@ class PluginManager(object):
'A problem occurred while removing {0} plugin. Error Message :{1}.'.format(plugin_name, 'A problem occurred while removing {0} plugin. Error Message :{1}.'.format(plugin_name,
str(e))) str(e)))
def find_command(self, pluginName, commandId): def find_command(self, plugin_name, version, command_id):
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pluginName) location = os.path.join(self.config_manager.get("PLUGIN", "pluginFolderPath"), plugin_name)
if os.path.isdir(location) and commandId + ".py" in os.listdir(location): if os.path.isdir(location) and command_id + ".py" in os.listdir(location):
info = imp.find_module(commandId, [location]) info = imp.find_module(command_id, [location])
return imp.load_module(commandId, *info) return imp.load_module(command_id, *info)
else: else:
self.logger.warning('Command id -' + commandId + ' - not found') self.logger.warning('Command id -' + command_id + ' - not found')
return None return None
def process_task(self, task): def process_task(self, task):
@ -163,12 +163,13 @@ class PluginManager(object):
try: try:
plugin_name = task.get_plugin().get_name().lower() plugin_name = task.get_plugin().get_name().lower()
plugin_ver = task.get_plugin().get_version() plugin_ver = task.get_plugin().get_version()
if plugin_name in self.pluginQueueDict:
self.pluginQueueDict[plugin_name].put(task, 1) if self.does_plugin_exist(plugin_name, plugin_ver) and plugin_name in self.plugin_queue_dict:
self.plugin_queue_dict[plugin_name].put(task, 1)
else: else:
self.logger.warning( self.logger.warning(
'{0} plugin not found. Task was delayed. Ahenk will request plugin from Lider if distribution available'.format( '{0} plugin not found. Task was delayed. Ahenk will request plugin from Lider if distribution available'.format(
plugin_name)) plugin_name, plugin_ver))
self.delayed_tasks[plugin_name] = task self.delayed_tasks[plugin_name] = task
msg = self.message_manager.missing_plugin_message(PluginBean(name=plugin_name, version=plugin_ver)) msg = self.message_manager.missing_plugin_message(PluginBean(name=plugin_name, version=plugin_ver))
self.messenger.send_direct_message(msg) self.messenger.send_direct_message(msg)
@ -177,7 +178,7 @@ class PluginManager(object):
'Exception occurred while processing task. Error Message: {0}'.format(str(e))) 'Exception occurred while processing task. Error Message: {0}'.format(str(e)))
def find_policy_module(self, plugin_name): def find_policy_module(self, plugin_name):
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name) location = os.path.join(self.config_manager.get("PLUGIN", "pluginFolderPath"), plugin_name)
if os.path.isdir(location) and "policy.py" in os.listdir(location): if os.path.isdir(location) and "policy.py" in os.listdir(location):
info = imp.find_module("policy", [location]) info = imp.find_module("policy", [location])
return imp.load_module("policy", *info) return imp.load_module("policy", *info)
@ -229,12 +230,12 @@ class PluginManager(object):
plugin = profile.get_plugin() plugin = profile.get_plugin()
plugin_name = plugin.get_name() plugin_name = plugin.get_name()
plugin_ver = plugin.get_version() plugin_ver = plugin.get_version()
if plugin_name in self.pluginQueueDict: if self.does_plugin_exist(plugin_name, plugin_ver) and plugin_name in self.plugin_queue_dict:
self.pluginQueueDict[plugin_name].put(profile, 1) self.plugin_queue_dict[plugin_name].put(profile, 1)
else: else:
self.logger.warning( self.logger.warning(
'{0} plugin not found. Profile was delayed. Ahenk will request plugin from Lider if distribution available'.format( '{0} plugin {1} version not found. Profile was delayed. Ahenk will request plugin from Lider if distribution available'.format(
plugin_name)) plugin_name, plugin_ver))
self.delayed_profiles[plugin_name] = profile self.delayed_profiles[plugin_name] = profile
msg = self.message_manager.missing_plugin_message(PluginBean(name=plugin_name, version=plugin_ver)) msg = self.message_manager.missing_plugin_message(PluginBean(name=plugin_name, version=plugin_ver))
self.scope.get_messenger().send_direct_message(msg) self.scope.get_messenger().send_direct_message(msg)
@ -242,17 +243,15 @@ class PluginManager(object):
self.logger.error( self.logger.error(
'Exception occurred while processing profile. Error Message: {0}'.format(str(e))) 'Exception occurred while processing profile. Error Message: {0}'.format(str(e)))
def check_plugin_exists(self, plugin_name, version=None): def does_plugin_exist(self, name, version):
location = os.path.join(self.config_manager.get("PLUGIN", "pluginFolderPath"), name)
criteria = ' name=\'' + plugin_name + '\'' main = self.config_manager.get("PLUGIN", "mainModuleName")
if version is not None: if os.path.isdir(location) and main + ".py" in os.listdir(location):
criteria += ' and version=\'' + str(version) + '\'' info = imp.find_module(main, [location])
result = self.db_service.select('plugin', 'name', criteria) main_py = imp.load_module(main, *info)
if main_py.info() is None or main_py.info()['version'] == version:
if result is None: return True
return False return False
else:
return True
def process_mode(self, mode_type, username=None): def process_mode(self, mode_type, username=None):
mode = None mode = None
@ -271,9 +270,9 @@ class PluginManager(object):
if mode is not None: if mode is not None:
self.logger.info('{0} mode is running'.format(mode_type)) self.logger.info('{0} mode is running'.format(mode_type))
for plugin_name in self.pluginQueueDict: for plugin_name in self.plugin_queue_dict:
try: try:
self.pluginQueueDict[plugin_name].put(mode, 1) self.plugin_queue_dict[plugin_name].put(mode, 1)
except Exception as e: except Exception as e:
self.logger.error( self.logger.error(
'Exception occurred while switching safe mode. Error Message : {0}'.format( 'Exception occurred while switching safe mode. Error Message : {0}'.format(
@ -281,7 +280,7 @@ class PluginManager(object):
def find_module(self, mode, plugin_name): def find_module(self, mode, plugin_name):
mode = mode.lower().replace('_mode', '') mode = mode.lower().replace('_mode', '')
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name) location = os.path.join(self.config_manager.get("PLUGIN", "pluginFolderPath"), plugin_name)
if os.path.isdir(location) and (mode + ".py") in os.listdir(location): if os.path.isdir(location) and (mode + ".py") in os.listdir(location):
info = imp.find_module(mode, [location]) info = imp.find_module(mode, [location])
@ -297,7 +296,7 @@ class PluginManager(object):
def is_plugin_loaded(self, plugin_name): def is_plugin_loaded(self, plugin_name):
try: try:
if self.pluginQueueDict[plugin_name] is not None: if self.plugin_queue_dict[plugin_name] is not None:
return True return True
else: else:
return False return False
@ -309,4 +308,4 @@ class PluginManager(object):
pass pass
def printQueueSize(self): def printQueueSize(self):
print("size " + str(len(self.pluginQueueDict))) print("size " + str(len(self.plugin_queue_dict)))