diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index 3456899..1431524 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -254,16 +254,21 @@ class AhenkDeamon(BaseDaemon): def run_command_from_fifo(self, num, stack): - json_data = json.loads(Commander().get_event()) + scope = Scope().getInstance() + plugin_manager = scope.getPluginManager() + message_manager = scope.getMessageManager() + messenger = scope.getMessenger() + conf_manager = scope.getConfigurationManager() + db_service = scope.getDbService() + execute_manager = scope.getExecutionManager() + + try: + json_data = json.loads(Commander().get_event()) + except Exception as e: + self.logger.error('[AhenkDeamon] A problem occurred while loading json. Check json format! Error Message: {0}'.format(str(e))) + return if json_data is not None: - scope = Scope().getInstance() - plugin_manager = scope.getPluginManager() - message_manager = scope.getMessageManager() - messenger = scope.getMessenger() - conf_manager = scope.getConfigurationManager() - db_service = scope.getDbService() - execute_manager = scope.getExecutionManager() self.logger.debug('[AhenkDeamon] Signal handled') self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event']))) @@ -347,8 +352,8 @@ class AhenkDeamon(BaseDaemon): plugin_manager.process_mode('safe', username) elif 'send' == str(json_data['event']): - self.logger.info('[AhenkDeamon] Sending message over ahenkd command. Response Message: {}'.format(str(json_data['message']))) - message = str(json.dumps(json_data['message'])) + self.logger.info('[AhenkDeamon] Sending message over ahenkd command. Response Message: {}'.format(json.dumps(json_data['message']))) + message = json.dumps(json_data['message']) messenger.send_direct_message(message) elif 'load' == str(json_data['event']): diff --git a/opt/ahenk/base/command/commander.py b/opt/ahenk/base/command/commander.py index ed5f438..eb45b57 100644 --- a/opt/ahenk/base/command/commander.py +++ b/opt/ahenk/base/command/commander.py @@ -4,6 +4,7 @@ import json import os import queue as Queue import threading +import ast from base.command.fifo import Fifo from base.model.enum.ContentType import ContentType @@ -71,7 +72,7 @@ class Commander(object): response['type'] = MessageType.TASK_STATUS.value response['taskId'] = params[3] if params[4] == '-m': - response['contentType'] = ContentType.TEXT_PLAIN.value + response['contentType'] = ContentType.APPLICATION_JSON.value response['responseData'] = params[5] elif params[4] == '-f': if os.path.exists(str(params[5])): @@ -101,7 +102,7 @@ class Commander(object): response['commandExecutionId'] = params[5] if params[6] == '-m': - response['contentType'] = ContentType.TEXT_PLAIN.value + response['contentType'] = ContentType.APPLICATION_JSON.value response['responseData'] = params[7] elif params[6] == '-f': if os.path.exists(str(params[7])): @@ -125,13 +126,12 @@ class Commander(object): else: print('Wrong or missing parameter. Usage: send -p -c -m|-f | -e|-s|-w') return None - print('RESPONSE=' + str(response).replace("'", '"')) - data['message'] = json.loads(str(response).replace("'", '"')) + + data['message'] = ast.literal_eval(str(response)) else: print('Wrong or missing parameter. Usage : %s start|stop|restart|status|clean|send') return None - else: if params[1] == 'clean': @@ -196,7 +196,7 @@ class Commander(object): if os.path.exists(db_path): os.remove(db_path) - #TODO remove pid file + # TODO remove pid file config.set('CONNECTION', 'uid', '')