diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index b3a4355..de7e9f1 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -93,13 +93,11 @@ class AhenkDeamon(BaseDaemon): logger.info('[AhenkDeamon] Ahenk is registered') - messager = Messager() messanger_thread = threading.Thread(target=messager.connect_to_server) messanger_thread.start() - - while (messager.is_connected() is False): + while messager.is_connected() is False: time.sleep(1) time.sleep(5) @@ -139,15 +137,6 @@ class AhenkDeamon(BaseDaemon): while True: time.sleep(1) - # request policies - # logger.info('[AhenkDeamon] Requesting policies...') - # messager.send_direct_message(messageManager.policy_request_msg()) - - - # this is must be created after message services - - - def signal_handler(self, num, stack): print("signal handled") # TODO###### @@ -181,7 +170,6 @@ class AhenkDeamon(BaseDaemon): logger.debug('[AhenkDeamon] Processing of handled event is completed') - def get_pid_number(): config = configparser.ConfigParser() config._interpolation = configparser.ExtendedInterpolation() diff --git a/opt/ahenk/base/execution/ExecutionManager.py b/opt/ahenk/base/execution/ExecutionManager.py index 3f27cae..a53f25a 100644 --- a/opt/ahenk/base/execution/ExecutionManager.py +++ b/opt/ahenk/base/execution/ExecutionManager.py @@ -35,13 +35,15 @@ class ExecutionManager(object): self.event_manager.register_event('EXECUTE_TASK', self.execute_task) self.event_manager.register_event('EXECUTE_POLICY', self.execute_policy) + def execute_policy(self, arg): self.logger.debug('[ExecutionManager] Updating policies...') policy = Policy(json.loads(arg)) + # TODO get username and machine uid - username = 'volkan' - machine_uid='616161616161' + username = '_username' + machine_uid='_machine_uid' ahenk_policy_ver = self.db_service.select_one_result('policy', 'version', 'type = \'A\'') user_policy_version = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \'' + username + '\'') @@ -57,7 +59,6 @@ class ExecutionManager(object): else: self.db_service.update('policy', ['type', 'version', 'name'], ['A', str(policy.ahenk_policy_version), machine_uid]) ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'') - for profile in policy.ahenk_profiles: args = [str(ahenk_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label), str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)] @@ -89,6 +90,11 @@ class ExecutionManager(object): # TODO check plugins print("but first need these plugins:" + str(missing_plugins)) + print("Executing policy") + + self.task_manager.addPolicy(policy) + + def get_installed_plugins(self): plugins = self.db_service.select('plugin', ['name', 'version']) p_list = [] diff --git a/opt/ahenk/base/messaging/Messager.py b/opt/ahenk/base/messaging/Messager.py index 9512004..b085611 100644 --- a/opt/ahenk/base/messaging/Messager.py +++ b/opt/ahenk/base/messaging/Messager.py @@ -102,14 +102,17 @@ class Messager(slixmpp.ClientXMPP): def send_direct_message(self, msg): self.logger.debug('[Messager] Sending message: ' + msg) self.send_message(mto=self.receiver, mbody=msg, mtype='normal') + print('<---'+msg) def recv_direct_message(self, msg): if msg['type'] in ('chat', 'normal'): j = json.loads(str(msg['body'])) message_type = j['type'] self.logger.debug('[Messager] Fired event is: ' + message_type) + print('----->'+str(msg['body'])) self.event_manger.fireEvent(message_type, str(msg['body'])) + def connect_to_server(self): # Connect to the XMPP server and start processing XMPP stanzas. try: self.logger.debug('[Messager] Connecting to server as thread') diff --git a/opt/ahenk/base/model/Policy.py b/opt/ahenk/base/model/Policy.py index a758c51..135dd9f 100644 --- a/opt/ahenk/base/model/Policy.py +++ b/opt/ahenk/base/model/Policy.py @@ -35,12 +35,18 @@ class Policy(object): @property def user_profiles(self): profiles = [] - for p in self.policy['userPolicyProfiles']: - profiles.append(Profile(p)) - return profiles + try: + for p in self.policy['userPolicyProfiles']: + profiles.append(Profile(p)) + return profiles + except Exception as e: + return None def to_string(self): return str(self.policy) def to_json(self): return json.load(self.policy) + + def obj_type(self): + return "POLICY" diff --git a/opt/ahenk/base/model/Profile.py b/opt/ahenk/base/model/Profile.py index 68f62cd..f2b4b58 100644 --- a/opt/ahenk/base/model/Profile.py +++ b/opt/ahenk/base/model/Profile.py @@ -57,3 +57,7 @@ class Profile(object): def to_json(self): return json.load(self.profile) + + @property + def obj_name(self): + return "PROFILE" diff --git a/opt/ahenk/base/model/Task.py b/opt/ahenk/base/model/Task.py index fc51ab1..a12295b 100644 --- a/opt/ahenk/base/model/Task.py +++ b/opt/ahenk/base/model/Task.py @@ -46,3 +46,7 @@ class Task(object): def to_json(self): return json.load(self.task) + + @property + def obj_name(self): + return "TASK" diff --git a/opt/ahenk/base/plugin/Plugin.py b/opt/ahenk/base/plugin/Plugin.py index 808c29e..4864adc 100644 --- a/opt/ahenk/base/plugin/Plugin.py +++ b/opt/ahenk/base/plugin/Plugin.py @@ -20,11 +20,21 @@ class Plugin(threading.Thread): def run(self): while True: try: - task = self.InQueue.get(block=True) - command = Scope.getInstance().getPluginManager().findCommand(self.getName(), task.command_cls_id) - command.handle_task(task) - # TODO add result to response queue - + 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) + # 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) + else: + self.logger.warning("Not supported object type " + obj_name) except Exception as e: # TODO error log here self.logger.error("Plugin running exception " + str(e)) diff --git a/opt/ahenk/base/plugin/PluginManager.py b/opt/ahenk/base/plugin/PluginManager.py index 3d6feed..38dddf1 100644 --- a/opt/ahenk/base/plugin/PluginManager.py +++ b/opt/ahenk/base/plugin/PluginManager.py @@ -66,6 +66,28 @@ class PluginManager(object): # Not implemented yet pass + def findPolicyModule(self,plugin_name): + location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name) + if os.path.isdir(location) and "policy.py" in os.listdir(location): + info = imp.find_module("policy", [location]) + return imp.load_module("policy", *info) + else: + self.logger.warning('[PluginManager] policy.py not found Plugin Name : ' + str(plugin_name)) + return None + + def processPolicy(self, policy): + user_profiles = policy.user_profiles + for profile in user_profiles: + try: + plugin = profile.plugin + plugin_name = plugin.name + if plugin_name in self.pluginQueueDict: + self.pluginQueueDict[plugin_name].put(profile, 1) + except Exception as e: + print("Exception occured..") + self.logger.error("Policy profile not processed " + str(profile.plugin.name)) + + def checkPluginExists(self, plugin_name, version=None): criteria = ' name=\''+plugin_name+'\'' diff --git a/opt/ahenk/base/task/TaskManager.py b/opt/ahenk/base/task/TaskManager.py index 9c0d927..b79f457 100644 --- a/opt/ahenk/base/task/TaskManager.py +++ b/opt/ahenk/base/task/TaskManager.py @@ -28,6 +28,14 @@ class TaskManager(object): self.logger.debug('Exception occured when adding task ' + str(e)) pass + def addPolicy(self,policy): + try: + print("adding policy") + self.pluginManager.processPolicy(policy) + except Exception as e: + self.logger.error("Exception occured when adding policy ") + pass + def saveTask(self, task): cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin'] values = [str(task.id), str(task.create_date), str(task.modify_date), str(task.command_cls_id), str(task.parameter_map), str(task.deleted), task.plugin.to_string()] diff --git a/opt/ahenk/plugins/plugin1/policy.py b/opt/ahenk/plugins/plugin1/policy.py new file mode 100644 index 0000000..2e4176f --- /dev/null +++ b/opt/ahenk/plugins/plugin1/policy.py @@ -0,0 +1,7 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN + + +def handle_policy(profile_data): + print("This is policy file - plugin 1")