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-07-22 10:36:56 +03:00
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
2016-05-26 11:19:34 +03:00
2016-07-01 17:04:53 +03:00
import json
2016-07-22 10:36:56 +03:00
import threading
2016-03-01 18:43:26 +02:00
from base . Scope import Scope
2016-07-01 17:04:53 +03:00
from base . file . file_transfer_manager import FileTransferManager
2016-07-22 10:36:56 +03:00
from base . model . Response import Response
2016-07-01 17:04:53 +03:00
from base . model . enum . ContentType import ContentType
2016-07-22 10:36:56 +03:00
from base . model . enum . MessageCode import MessageCode
from base . model . enum . MessageType import MessageType
2016-07-01 17:04:53 +03:00
from base . system . system import System
2016-03-30 18:51:50 +03:00
2016-02-18 16:38:30 +02:00
2016-03-30 17:12:38 +03:00
class Context ( object ) :
def __init__ ( self ) :
self . data = { }
2016-04-25 17:12:45 +03:00
self . scope = Scope ( ) . getInstance ( )
2016-03-30 17:12:38 +03:00
2016-03-30 18:51:50 +03:00
def put ( self , var_name , data ) :
2016-03-30 17:12:38 +03:00
self . data [ var_name ] = data
2016-03-30 18:51:50 +03:00
def get ( self , var_name ) :
return self . data [ var_name ]
2016-07-13 17:37:03 +03:00
def get_username ( self ) :
return self . data [ ' username ' ]
2016-03-30 17:12:38 +03:00
def empty_data ( self ) :
self . data = { }
2016-03-23 17:15:27 +02:00
2016-06-06 12:25:08 +03:00
def create_response ( self , code , message = None , data = None , content_type = None ) :
2016-05-09 17:50:12 +03:00
self . data [ ' responseCode ' ] = code
self . data [ ' responseMessage ' ] = message
self . data [ ' responseData ' ] = data
self . data [ ' contentType ' ] = content_type
2016-03-30 18:51:50 +03:00
2016-03-01 18:43:26 +02:00
class Plugin ( threading . Thread ) :
2016-03-30 17:12:38 +03:00
"""
This is a thread inherit class and have a queue .
Plugin class responsible for processing TASK or USER PLUGIN PROFILE .
"""
2016-03-23 17:15:27 +02:00
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
2016-03-01 18:43:26 +02:00
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 ( )
2016-04-12 17:30:53 +03:00
self . db_service = scope . getDbService ( )
2016-03-30 18:51:50 +03:00
2016-03-30 17:12:38 +03:00
self . keep_run = True
self . context = Context ( )
2016-03-01 18:43:26 +02:00
2016-03-23 10:04:31 +02:00
def run ( self ) :
2016-05-09 14:13:41 +03:00
2016-03-30 17:12:38 +03:00
while self . keep_run :
2016-03-23 10:04:31 +02:00
try :
2016-05-06 16:40:06 +03:00
try :
item_obj = self . InQueue . get ( block = True )
obj_name = item_obj . obj_name
except Exception as e :
2016-05-26 11:19:34 +03:00
self . logger . error ( ' [Plugin] A problem occurred while executing process. Error Message: {} ' . format ( str ( e ) ) )
2016-05-06 16:40:06 +03:00
2016-03-29 11:52:18 +03:00
if obj_name == " TASK " :
2016-05-06 16:40:06 +03:00
self . logger . debug ( ' [Plugin] Executing task ' )
2016-07-18 18:16:37 +03:00
command = Scope . getInstance ( ) . getPluginManager ( ) . find_command ( self . getName ( ) , item_obj . get_command_cls_id ( ) . lower ( ) )
2016-07-27 15:12:32 +03:00
self . context . put ( ' task_id ' , item_obj . get_id ( ) )
2016-06-06 12:25:08 +03:00
task_data = item_obj . get_parameter_map ( )
2016-05-26 11:19:34 +03:00
self . logger . debug ( ' [Plugin] Handling task ' )
2016-06-06 12:25:08 +03:00
command . handle_task ( task_data , self . context )
2016-07-01 17:04:53 +03:00
if self . context . data is not None and self . context . get ( ' responseCode ' ) is not None :
2016-07-11 17:58:00 +03:00
self . logger . debug ( ' [Plugin] Creating response ' )
2016-06-06 12:25:08 +03:00
response = Response ( type = MessageType . TASK_STATUS . value , id = item_obj . get_id ( ) , code = self . context . get ( ' responseCode ' ) , message = self . context . get ( ' responseMessage ' ) , data = self . context . get ( ' responseData ' ) , content_type = self . context . get ( ' contentType ' ) )
2016-07-01 17:04:53 +03:00
2016-07-11 17:58:00 +03:00
if response . get_data ( ) and response . get_content_type ( ) != ContentType . APPLICATION_JSON . value :
success = False
try :
2016-07-01 17:04:53 +03:00
file_manager = FileTransferManager ( json . loads ( item_obj . get_file_server ( ) ) [ ' protocol ' ] , json . loads ( item_obj . get_file_server ( ) ) [ ' parameterMap ' ] )
file_manager . transporter . connect ( )
md5 = str ( json . loads ( response . get_data ( ) ) [ ' md5 ' ] )
success = file_manager . transporter . send_file ( System . Ahenk . received_dir_path ( ) + md5 , md5 )
file_manager . transporter . disconnect ( )
2016-07-11 17:58:00 +03:00
except Exception as e :
self . logger . error ( ' [Plugin] A problem occurred while file transferring. Error Message : {} ' . format ( str ( e ) ) )
2016-07-01 17:04:53 +03:00
2016-07-11 17:58:00 +03:00
self . logger . debug ( ' [Plugin] Sending response ' )
2016-07-01 17:04:53 +03:00
2016-07-11 17:58:00 +03:00
message = self . messaging . task_status_msg ( response )
if success is False :
response = Response ( type = MessageType . TASK_STATUS . value , id = item_obj . get_id ( ) , code = MessageCode . TASK_ERROR . value , message = ' [Ahenk Core] Task processed successfully but file transfer not completed. Check defined server conf ' )
2016-07-01 17:04:53 +03:00
message = self . messaging . task_status_msg ( response )
2016-07-11 17:58:00 +03:00
Scope . getInstance ( ) . getMessenger ( ) . send_direct_message ( message )
2016-07-01 17:04:53 +03:00
else :
2016-07-11 17:58:00 +03:00
self . logger . debug ( ' [Plugin] Sending task response ' )
2016-07-01 17:04:53 +03:00
Scope . getInstance ( ) . getMessenger ( ) . send_direct_message ( self . messaging . task_status_msg ( response ) )
2016-06-06 12:25:08 +03:00
else :
self . logger . error ( ' [Plugin] There is no Response. Plugin must create response after run a task! ' )
2016-03-30 17:12:38 +03:00
2016-03-29 11:52:18 +03:00
elif obj_name == " PROFILE " :
2016-07-11 17:58:00 +03:00
2016-05-06 16:40:06 +03:00
self . logger . debug ( ' [Plugin] Executing profile ' )
2016-04-06 14:28:29 +03:00
profile_data = item_obj . get_profile_data ( )
2016-07-18 18:16:37 +03:00
policy_module = Scope . getInstance ( ) . getPluginManager ( ) . find_policy_module ( item_obj . get_plugin ( ) . get_name ( ) )
2016-05-09 17:50:12 +03:00
self . context . put ( ' username ' , item_obj . get_username ( ) )
2016-03-30 17:12:38 +03:00
2016-05-09 17:50:12 +03:00
execution_id = self . get_execution_id ( item_obj . get_id ( ) )
policy_ver = self . get_policy_version ( item_obj . get_id ( ) )
2016-05-06 16:40:06 +03:00
2016-06-06 12:25:08 +03:00
self . context . put ( ' policy_version ' , policy_ver )
self . context . put ( ' execution_id ' , execution_id )
self . logger . debug ( ' [Plugin] Handling profile ' )
policy_module . handle_policy ( profile_data , self . context )
2016-07-01 17:04:53 +03:00
if self . context . data is not None and self . context . get ( ' responseCode ' ) is not None :
2016-06-06 12:25:08 +03:00
self . logger . debug ( ' [Plugin] Creating response ' )
response = Response ( type = MessageType . POLICY_STATUS . value , id = item_obj . get_id ( ) , code = self . context . get ( ' responseCode ' ) , message = self . context . get ( ' responseMessage ' ) , data = self . context . get ( ' responseData ' ) , content_type = self . context . get ( ' contentType ' ) , execution_id = execution_id , policy_version = policy_ver )
2016-07-11 17:58:00 +03:00
if response . get_data ( ) and response . get_content_type ( ) != ContentType . APPLICATION_JSON . value :
success = False
try :
file_manager = FileTransferManager ( json . loads ( item_obj . get_file_server ( ) ) [ ' protocol ' ] , json . loads ( item_obj . get_file_server ( ) ) [ ' parameterMap ' ] )
file_manager . transporter . connect ( )
md5 = str ( json . loads ( response . get_data ( ) ) [ ' md5 ' ] )
success = file_manager . transporter . send_file ( System . Ahenk . received_dir_path ( ) + md5 , md5 )
file_manager . transporter . disconnect ( )
except Exception as e :
self . logger . error ( ' [Plugin] A problem occurred while file transferring. Error Message : {} ' . format ( str ( e ) ) )
self . logger . debug ( ' [Plugin] Sending response ' )
message = self . messaging . task_status_msg ( response )
if success is False :
response = Response ( type = MessageType . POLICY_STATUS . value , id = item_obj . get_id ( ) , code = MessageCode . POLICY_ERROR . value , message = ' [Ahenk Core] Policy processed successfully but file transfer not completed. Check defined server conf ' )
message = self . messaging . task_status_msg ( response )
Scope . getInstance ( ) . getMessenger ( ) . send_direct_message ( message )
else :
self . logger . debug ( ' [Plugin] Sending profile response ' )
Scope . getInstance ( ) . getMessenger ( ) . send_direct_message ( self . messaging . policy_status_msg ( response ) )
2016-06-06 12:25:08 +03:00
else :
self . logger . error ( ' [Plugin] There is no Response. Plugin must create response after run a policy! ' )
2016-07-13 17:37:03 +03:00
elif ' MODE ' in obj_name :
module = Scope . getInstance ( ) . getPluginManager ( ) . find_module ( obj_name , self . name )
if module is not None :
if item_obj . obj_name in ( ' LOGIN_MODE ' , ' LOGOUT_MODE ' , ' SAFE_MODE ' ) :
self . context . put ( ' username ' , item_obj . username )
try :
self . logger . debug ( ' [Plugin] {0} is running on {1} plugin ' . format ( str ( item_obj . obj_name ) , str ( self . name ) ) )
module . handle_mode ( self . context )
except Exception as e :
self . logger . error ( ' [Plugin] A problem occurred while running {0} on {1} plugin. Error Message: {2} ' . format ( str ( obj_name ) , str ( self . name ) , str ( e ) ) )
if item_obj . obj_name is ' SHUTDOWN_MODE ' :
self . logger . debug ( ' [Plugin] {0} plugin is stopping... ' . format ( str ( self . name ) ) )
self . keep_run = False
2016-03-29 11:52:18 +03:00
else :
2016-05-26 11:19:34 +03:00
self . logger . warning ( " [Plugin] Not supported object type: {} " . format ( str ( obj_name ) ) )
2016-05-09 17:50:12 +03:00
self . context . empty_data ( )
2016-03-23 10:04:31 +02:00
except Exception as e :
2016-07-25 12:21:23 +03:00
self . logger . error ( " [Plugin] Plugin running exception. Exception Message: {0} " . format ( str ( e ) ) )
2016-02-18 16:38:30 +02:00
2016-04-12 17:30:53 +03:00
def get_execution_id ( self , profile_id ) :
try :
return self . db_service . select_one_result ( ' policy ' , ' execution_id ' , ' id= {} ' . format ( profile_id ) )
except Exception as e :
2016-04-25 17:12:45 +03:00
self . logger . error ( " [Plugin] A problem occurred while getting execution id. Exception Message: {} " . format ( str ( e ) ) )
2016-04-12 17:30:53 +03:00
return None
def get_policy_version ( self , profile_id ) :
try :
return self . db_service . select_one_result ( ' policy ' , ' version ' , ' id= {} ' . format ( profile_id ) )
except Exception as e :
2016-04-25 17:12:45 +03:00
self . logger . error ( " [Plugin] A problem occurred while getting policy version . Exception Message: {} " . format ( str ( e ) ) )
2016-04-12 17:30:53 +03:00
return None
2016-02-18 16:38:30 +02:00
def getName ( self ) :
return self . name