Ahenk/opt/ahenk/base/plugin/Plugin.py

100 lines
3.9 KiB
Python
Raw Normal View History

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>
import threading
import subprocess
from base.Scope import Scope
2016-03-30 18:51:50 +03:00
from base.model.Response import Response
from base.model.MessageType import MessageType
from base.model.MessageCode import MessageCode
from base.messaging.Messaging import Messaging
2016-02-18 16:38:30 +02:00
class Context(object):
def __init__(self):
self.data = {}
2016-03-30 18:51:50 +03:00
def put(self, var_name, data):
self.data[var_name] = data
2016-03-30 18:51:50 +03:00
def get(self, var_name):
return self.data[var_name]
def empty_data(self):
self.data = {}
def execute(self, command):
return subprocess.call(command, shell=True)
2016-03-30 18:51:50 +03:00
class Plugin(threading.Thread):
"""
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):
2016-03-23 10:04:31 +02:00
threading.Thread.__init__(self)
2016-02-18 16:38:30 +02:00
self.name = name
self.InQueue = InQueue
2016-03-30 18:51:50 +03:00
2016-03-23 10:04:31 +02:00
scope = Scope.getInstance()
self.logger = scope.getLogger()
2016-03-30 18:51:50 +03:00
self.response_queue = scope.getResponseQueue()
self.messaging = scope.getMessageManager()
self.messager =None
self.keep_run = True
self.context = Context()
2016-03-23 10:04:31 +02:00
def run(self):
2016-03-30 18:51:50 +03:00
while self.keep_run:
2016-03-23 10:04:31 +02:00
try:
2016-03-29 11:52:18 +03:00
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)
2016-03-30 18:51:50 +03:00
command.handle_task(item_obj, self.context)
# TODO create response message from context and item_obj. item_obj is task
2016-03-30 18:51:50 +03:00
#TODO Message Code keep
2016-03-31 18:21:24 +03:00
response = Response(type=MessageType.TASK_STATUS, id=item_obj.id, code=MessageCode.TASK_PROCESSED, message='__message__', data=self.context.get('data'), content_type=self.context.get('content_type'))
2016-03-30 18:51:50 +03:00
#self.response_queue.put(self.messaging.response_msg(response)) #TODO DEBUG
Scope.getInstance().getMessager().send_direct_message(self.messaging.response_msg(response)) #TODO REMOVE
# Empty context for next use
self.context.empty_data()
2016-03-29 11:52:18 +03:00
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)
self.context.put('username', item_obj.get_username())
2016-03-30 18:51:50 +03:00
policy_module.handle_policy(profile_data, self.context)
#TODO Message Code keep
response = Response(type=self.context.get('message_type'), id=item_obj.id, code=self.context.get('message_code'), message=self.context.get('message'), data=self.context.get('data'), content_type=self.context.get('content_type'), execution_id='get_execution_id')
2016-03-30 18:51:50 +03:00
#self.response_queue.put(self.messaging.response_msg(response)) #TODO DEBUG
Scope.getInstance().getMessager().send_direct_message(self.messaging.response_msg(response))#TODO REMOVE
# Empty context for next use
self.context.empty_data()
2016-03-30 18:51:50 +03:00
elif obj_name == "KILL_SIGNAL":
self.keep_run = False
self.logger.debug('[Plugin] Killing queue ! Plugin Name : ' + str(self.name))
2016-03-29 11:52:18 +03:00
else:
self.logger.warning("[Plugin] Not supported object type " + obj_name)
2016-03-23 10:04:31 +02:00
except Exception as e:
# TODO error log here
self.logger.error("[Plugin] Plugin running exception " + str(e))
2016-02-18 16:38:30 +02:00
def getName(self):
return self.name