created initial context obj for handling task and policy

This commit is contained in:
İsmail Başaran 2016-03-30 17:12:38 +03:00
parent fdb095cf7c
commit 9b63aff8ab
5 changed files with 62 additions and 16 deletions

View file

@ -48,5 +48,5 @@ class Policy(object):
def to_json(self):
return json.load(self.policy)
def obj_type(self):
return "POLICY"
def obj_name(self):
return "PROFILE"

View file

@ -5,39 +5,67 @@ import threading
from base.Scope import Scope
class Context(object):
def __init__(self):
self.data = {}
def put(self,var_name,data):
self.data[var_name] = data
def empty_data(self):
self.data = {}
class Plugin(threading.Thread):
"""docstring for Plugin"""
"""
This is a thread inherit class and have a queue.
Plugin class responsible for processing TASK or USER PLUGIN PROFILE.
"""
def __init__(self, name, InQueue):
threading.Thread.__init__(self)
self.name = name
self.InQueue = InQueue
scope = Scope.getInstance()
self.pluginManager = scope.getPluginManager()
self.logger = scope.getLogger()
self.keep_run = True
self.context = Context()
def run(self):
while True:
while self.keep_run:
try:
item_obj = self.InQueue.get(block=True)
obj_name = item_obj.obj_name
print(obj_name)
if obj_name == "TASK":
command = Scope.getInstance().getPluginManager().findCommand(self.getName(), item_obj.command_cls_id)
command.handle_task(item_obj)
command.handle_task(item_obj,self.context)
# TODO create response message from context and item_obj. item_obj is task
# Empty context for next use
self.context.empty_data()
# TODO add result to response queue
elif obj_name == "PROFILE":
plugin = item_obj.plugin
plugin_name = plugin.name
profile_data = item_obj.profile_data
policy_module = Scope.getInstance().getPluginManager().findPolicyModule(plugin_name)
policy_module.handle_policy(profile_data)
policy_module.handle_policy(profile_data,self.context)
# TODO create response message from context and item_obj. item_obj is profile
# Empty context for next use
self.context.empty_data()
elif obj_name == "KILL_SIGNAL":
self.keep_run = False
self.logger.debug('[Plugin] Killing queue ! Plugin Name : ' + str(self.name))
else:
self.logger.warning("Not supported object type " + obj_name)
self.logger.warning("[Plugin] Not supported object type " + obj_name)
except Exception as e:
# TODO error log here
self.logger.error("Plugin running exception " + str(e))
self.logger.error("[Plugin] Plugin running exception " + str(e))
def getName(self):
return self.name

View file

@ -7,6 +7,7 @@ import os
from base.Scope import Scope
from base.plugin.Plugin import Plugin
from base.plugin.PluginQueue import PluginQueue
from base.model.PluginKillSignal import PluginKillSignal
class PluginManager(object):
@ -24,18 +25,24 @@ class PluginManager(object):
#TODO version?
def loadPlugins(self):
"""
This method loads plugins
"""
self.logger.info('[PluginManager] Loading plugins...')
self.plugins = []
self.logger.debug('[PluginManager] Lookup for possible plugins...')
possibleplugins = os.listdir(self.configManager.get("PLUGIN", "pluginFolderPath"))
self.logger.debug('[PluginManager] Possible plugins.. ' + str(possibleplugins))
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):
self.logger.debug('It is not a plugin location - - ')
self.logger.debug('It is not a plugin location ! There is no main module - ' + str(location))
continue
try:
self.loadSinglePlugin(pname)
except Exception as e:
# TODO error log
pass
self.logger.error('Exception occured when loading plugin ! Plugin name : ' + str(pname) + ' Exception : ' + str(e))
self.logger.info('[PluginManager] Loaded plugins successfully.')
def loadSinglePlugin(self, pluginName):
# TODO check already loaded plugin
@ -63,8 +70,17 @@ class PluginManager(object):
self.logger.error("[PluginManager] Exception occurred when processing task " + str(e))
def reloadPlugins(self):
# Not implemented yet
pass
#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))
def findPolicyModule(self,plugin_name):
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name)
@ -102,6 +118,7 @@ class PluginManager(object):
def reloadSinglePlugin(self, pluginName):
# Not implemented yet
pass
def checkCommandExist(self, pluginName, commandId):

View file

@ -15,9 +15,10 @@ class MySamplePlugin(AbstractCommand):
print("parameter map="+self.task.parameter_map)
def handle_task(task):
def handle_task(task,context):
# Do what ever you want here
# You can create command class but it is not necessary
# You can use directly this method.
context.put('my_data_name','my data')
myPlugin = MySamplePlugin(task)
myPlugin.handle_task()

View file

@ -3,5 +3,5 @@
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
def handle_policy(profile_data):
def handle_policy(profile_data,context):
print("This is policy file - plugin 1")