From 8eac2d6615b9c6bd775d5fc8e3be1d7def04dffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Fri, 11 Mar 2016 17:36:11 +0200 Subject: [PATCH] implemented execution manager for lider events --- opt/ahenk/base/execution/ExecutionManager.py | 81 ++++++++++++-------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/opt/ahenk/base/execution/ExecutionManager.py b/opt/ahenk/base/execution/ExecutionManager.py index 93d5de8..6608458 100644 --- a/opt/ahenk/base/execution/ExecutionManager.py +++ b/opt/ahenk/base/execution/ExecutionManager.py @@ -1,43 +1,64 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: Volkan Şahin -import sys -import logging,subprocess -import logging.config + +import subprocess from base.Scope import Scope +from base.messaging.MessageSender import MessageSender +import hashlib,json,os,stat,shutil class ExecutionManager(object): - """docstring for FileTransferManager""" + """docstring for FileTransferManager""" - def __init__(self): - super(ExecutionManager, self).__init__() + def __init__(self): + super(ExecutionManager, self).__init__() - scope = Scope.getInstance() - self.config_manager = scope.getConfigurationManager() - self.event_manager = scope.getEventManager() + scope = Scope.getInstance() + self.config_manager = scope.getConfigurationManager() + self.event_manager = scope.getEventManager() - self.event_manager.register_event('EXECUTE_TASK',self.execute_task) - self.event_manager.register_event('EXECUTE_SCRIPT',self.execute_script) - self.event_manager.register_event('SEND_FILE',self.send_file) + self.event_manager.register_event('EXECUTE_TASK',self.execute_task) + self.event_manager.register_event('EXECUTE_SCRIPT',self.execute_script) + self.event_manager.register_event('REQUEST_FILE',self.request_file) + self.event_manager.register_event('MOVE_FILE',self.move_file) - def execute_task(self,arg): - print("execute_task") + def execute_task(self,arg): + #TODO + self.logger.debug('[ExecutionManager] Executing task...') - def execute_script(self,arg): - print("execute_script") - j = json.loads(arg) - msg_id =str(j['id']).lower() - file_name =str(j['filePath']).lower() - time_stamp=str(j['timestamp']).lower() - subprocess.call("/bin/sh "+self.conf_manager.get('CONNECTION', 'receivefileparam')+file_name, shell=True) + def move_file(self,arg): + default_file_path=self.config_manager.get('CONNECTION', 'receiveFileParam') + j = json.loads(arg) + #msg_id =str(j['id']).lower() + target_file_path =str(j['filepath']).lower() + file_name =str(j['filename']).lower() + self.logger.debug('[ExecutionManager] %s will be moved to %s' % file_name,target_file_path) + shutil.move(default_file_path+file_name,target_file_path+file_name) - #need to move somewhere else - def send_file(self,arg): - print("send_file") - j = json.loads(arg) - msg_id =str(j['id']).lower() - file_path =str(j['filePath']).lower() - time_stamp=str(j['timestamp']).lower() + def execute_script(self,arg): + j = json.loads(arg) + #msg_id =str(j['id']).lower() + file_path =str(j['filepath']).lower() + time_stamp=str(j['timestamp']).lower() + self.logger.debug('[ExecutionManager] Making executable file (%s) for execution' % file_path) + st = os.stat(file_path) + os.chmod(file_path, st.st_mode | stat.S_IEXEC) + subprocess.call("/bin/sh "+file_path, shell=True) - message_sender=MessageSender(None,file_path) - message_sender.connect_to_server() + #need to move somewhere else + def request_file(self,arg): + j = json.loads(arg) + #msg_id =str(j['id']).lower() + file_path =str(j['filepath']).lower() + time_stamp=str(j['timestamp']).lower() + self.logger.debug('[ExecutionManager] Requested file is '+file_path) + message_sender=MessageSender(None,file_path) + message_sender.connect_to_server() + + def get_md5_file(self,fname): + self.logger.debug('[ExecutionManager] md5 hashing') + hash_md5 = hashlib.md5() + with open(fname, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_md5.update(chunk) + return str(hash_md5.hexdigest())