2016-02-29 10:48:00 +02:00
|
|
|
#!/usr/bin/python3
|
2016-02-18 16:38:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
2016-03-23 17:15:27 +02:00
|
|
|
import imp
|
|
|
|
import os
|
|
|
|
|
|
|
|
from base.Scope import Scope
|
2016-02-18 16:38:30 +02:00
|
|
|
from base.plugin.Plugin import Plugin
|
2016-03-02 10:27:43 +02:00
|
|
|
from base.plugin.PluginQueue import PluginQueue
|
2016-03-30 17:12:38 +03:00
|
|
|
from base.model.PluginKillSignal import PluginKillSignal
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-02-18 16:38:30 +02:00
|
|
|
|
|
|
|
class PluginManager(object):
|
|
|
|
"""docstring for PluginManager"""
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
# implement logger
|
2016-03-02 10:27:43 +02:00
|
|
|
def __init__(self):
|
2016-02-18 16:38:30 +02:00
|
|
|
super(PluginManager, self).__init__()
|
2016-03-07 17:13:03 +02:00
|
|
|
self.scope = Scope.getInstance()
|
|
|
|
self.configManager = self.scope.getConfigurationManager()
|
2016-03-25 17:56:15 +02:00
|
|
|
self.db_service = self.scope.getDbService()
|
2016-02-18 16:38:30 +02:00
|
|
|
self.plugins = []
|
2016-03-02 10:27:43 +02:00
|
|
|
self.pluginQueueDict = dict()
|
2016-03-07 17:13:03 +02:00
|
|
|
self.logger = self.scope.getLogger()
|
2016-02-18 16:38:30 +02:00
|
|
|
|
2016-03-25 17:56:15 +02:00
|
|
|
#TODO version?
|
2016-03-02 15:45:36 +02:00
|
|
|
def loadPlugins(self):
|
2016-03-30 17:12:38 +03:00
|
|
|
"""
|
|
|
|
This method loads plugins
|
|
|
|
"""
|
|
|
|
self.logger.info('[PluginManager] Loading plugins...')
|
2016-02-18 16:38:30 +02:00
|
|
|
self.plugins = []
|
2016-03-30 17:12:38 +03:00
|
|
|
self.logger.debug('[PluginManager] Lookup for possible plugins...')
|
2016-02-18 16:38:30 +02:00
|
|
|
possibleplugins = os.listdir(self.configManager.get("PLUGIN", "pluginFolderPath"))
|
2016-03-30 17:12:38 +03:00
|
|
|
self.logger.debug('[PluginManager] Possible plugins.. ' + str(possibleplugins))
|
2016-02-18 16:38:30 +02:00
|
|
|
for pname in possibleplugins:
|
|
|
|
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pname)
|
|
|
|
if not os.path.isdir(location) or not self.configManager.get("PLUGIN", "mainModuleName") + ".py" in os.listdir(location):
|
2016-03-30 17:12:38 +03:00
|
|
|
self.logger.debug('It is not a plugin location ! There is no main module - ' + str(location))
|
2016-02-18 16:38:30 +02:00
|
|
|
continue
|
2016-03-02 10:27:43 +02:00
|
|
|
try:
|
|
|
|
self.loadSinglePlugin(pname)
|
|
|
|
except Exception as e:
|
2016-03-30 17:12:38 +03:00
|
|
|
self.logger.error('Exception occured when loading plugin ! Plugin name : ' + str(pname) + ' Exception : ' + str(e))
|
|
|
|
self.logger.info('[PluginManager] Loaded plugins successfully.')
|
2016-03-02 10:27:43 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def loadSinglePlugin(self, pluginName):
|
2016-03-02 10:27:43 +02:00
|
|
|
# TODO check already loaded plugin
|
2016-03-23 17:15:27 +02:00
|
|
|
self.pluginQueueDict[pluginName] = PluginQueue()
|
|
|
|
plugin = Plugin(pluginName, self.pluginQueueDict[pluginName])
|
2016-03-02 10:27:43 +02:00
|
|
|
plugin.setDaemon(True)
|
|
|
|
plugin.start()
|
|
|
|
self.plugins.append(plugin)
|
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def findCommand(self, pluginName, commandId):
|
2016-03-02 11:59:19 +02:00
|
|
|
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pluginName)
|
2016-03-23 10:04:31 +02:00
|
|
|
if os.path.isdir(location) and commandId + ".py" in os.listdir(location):
|
2016-03-02 10:27:43 +02:00
|
|
|
info = imp.find_module(commandId, [location])
|
|
|
|
return imp.load_module(commandId, *info)
|
|
|
|
else:
|
2016-03-23 17:15:27 +02:00
|
|
|
self.logger.warning('Command id -' + commandId + ' - not found')
|
2016-03-02 10:27:43 +02:00
|
|
|
return None
|
2016-03-02 11:59:19 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def processTask(self, task):
|
2016-03-02 11:59:19 +02:00
|
|
|
try:
|
2016-03-23 17:15:27 +02:00
|
|
|
if task.plugin.name.lower() in self.pluginQueueDict:
|
|
|
|
self.pluginQueueDict[task.plugin.name.lower()].put(task, 1)
|
2016-03-02 11:59:19 +02:00
|
|
|
except Exception as e:
|
|
|
|
# TODO update task - status to not found command
|
2016-03-23 10:04:31 +02:00
|
|
|
self.logger.error("[PluginManager] Exception occurred when processing task " + str(e))
|
|
|
|
|
2016-03-02 11:59:19 +02:00
|
|
|
def reloadPlugins(self):
|
2016-03-30 17:12:38 +03:00
|
|
|
#TODO
|
|
|
|
try:
|
|
|
|
self.logger.info('[PluginManager] Reloading plugins... ')
|
|
|
|
kill_sgnl = PluginKillSignal()
|
|
|
|
for p_queue in self.pluginQueueDict:
|
|
|
|
p_queue.put(kill_sgnl)
|
|
|
|
self.plugins = []
|
|
|
|
self.loadPlugins()
|
|
|
|
self.logger.info('[PluginManager] Plugin reloaded successfully.')
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error('[PluginManager] Exception occurred when reloading plugins ' + str(e))
|
2016-03-02 11:59:19 +02:00
|
|
|
|
2016-04-04 18:15:18 +03:00
|
|
|
def findPolicyModule(self, plugin_name):
|
2016-03-29 11:52:18 +03:00
|
|
|
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name)
|
|
|
|
if os.path.isdir(location) and "policy.py" in os.listdir(location):
|
|
|
|
info = imp.find_module("policy", [location])
|
|
|
|
return imp.load_module("policy", *info)
|
|
|
|
else:
|
|
|
|
self.logger.warning('[PluginManager] policy.py not found Plugin Name : ' + str(plugin_name))
|
|
|
|
return None
|
|
|
|
|
|
|
|
def processPolicy(self, policy):
|
2016-04-04 18:15:18 +03:00
|
|
|
#TODO do you need username in profile?
|
|
|
|
|
|
|
|
username = policy.username
|
|
|
|
ahenk_profiles = policy.ahenk_profiles
|
|
|
|
user_profiles = policy.user_profiles
|
|
|
|
|
|
|
|
if ahenk_profiles is not None:
|
|
|
|
for profile in ahenk_profiles:
|
|
|
|
self.process_profile(profile)
|
|
|
|
|
|
|
|
if user_profiles is not None:
|
|
|
|
for profile in user_profiles:
|
|
|
|
profile.set_username(username)
|
|
|
|
self.process_profile(profile)
|
|
|
|
|
|
|
|
def process_profile(self, profile):
|
|
|
|
try:
|
|
|
|
plugin = profile.get_plugin()
|
|
|
|
plugin_name = plugin.name
|
|
|
|
if plugin_name in self.pluginQueueDict:
|
|
|
|
self.pluginQueueDict[plugin_name].put(profile, 1)
|
|
|
|
except Exception as e:
|
|
|
|
print("Exception occured..")
|
|
|
|
self.logger.error("Policy profile not processed " + str(profile.plugin.name))
|
2016-03-29 11:52:18 +03:00
|
|
|
|
2016-03-25 17:56:15 +02:00
|
|
|
def checkPluginExists(self, plugin_name, version=None):
|
|
|
|
|
|
|
|
criteria = ' name=\''+plugin_name+'\''
|
|
|
|
if version is not None:
|
|
|
|
criteria += ' and version=\'' + str(version) + '\''
|
|
|
|
result = self.db_service.select('plugin', 'name', criteria)
|
|
|
|
|
|
|
|
if result is None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2016-03-02 11:59:19 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def reloadSinglePlugin(self, pluginName):
|
2016-03-02 11:59:19 +02:00
|
|
|
# Not implemented yet
|
2016-03-30 17:12:38 +03:00
|
|
|
|
2016-03-02 11:59:19 +02:00
|
|
|
pass
|
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def checkCommandExist(self, pluginName, commandId):
|
2016-03-02 11:59:19 +02:00
|
|
|
# Not implemented yet
|
|
|
|
pass
|
2016-03-02 17:20:42 +02:00
|
|
|
|
|
|
|
def printQueueSize(self):
|
|
|
|
print("size " + str(len(self.pluginQueueDict)))
|
2016-04-04 18:15:18 +03:00
|
|
|
|