From 5d905432ad243705d0949c8479c43f98ac15c7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Ba=C5=9Faran?= Date: Tue, 29 Mar 2016 11:52:18 +0300 Subject: [PATCH] implemented policy process --- opt/ahenk/base/model/Policy.py | 13 ++++++++++--- opt/ahenk/base/model/Profile.py | 4 ++++ opt/ahenk/base/model/Task.py | 4 ++++ opt/ahenk/base/plugin/Plugin.py | 20 +++++++++++++++----- opt/ahenk/base/plugin/PluginManager.py | 22 ++++++++++++++++++++++ opt/ahenk/base/task/TaskManager.py | 8 ++++++++ opt/ahenk/plugins/plugin1/policy.py | 7 +++++++ 7 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 opt/ahenk/plugins/plugin1/policy.py diff --git a/opt/ahenk/base/model/Policy.py b/opt/ahenk/base/model/Policy.py index e028761..bd6a702 100644 --- a/opt/ahenk/base/model/Policy.py +++ b/opt/ahenk/base/model/Policy.py @@ -35,12 +35,19 @@ class Policy(object): @property def user_profiles(self): profiles = [] - for p in self.policy['userProfiles']: - 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 fbbf8d6..42b5256 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")