implemented policy process

This commit is contained in:
İsmail Başaran 2016-03-29 11:52:18 +03:00
parent eba656d8ae
commit 5d905432ad
7 changed files with 70 additions and 8 deletions

View file

@ -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"

View file

@ -57,3 +57,7 @@ class Profile(object):
def to_json(self):
return json.load(self.profile)
@property
def obj_name(self):
return "PROFILE"

View file

@ -46,3 +46,7 @@ class Task(object):
def to_json(self):
return json.load(self.task)
@property
def obj_name(self):
return "TASK"

View file

@ -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))

View file

@ -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+'\''

View file

@ -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()]

View file

@ -0,0 +1,7 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
def handle_policy(profile_data):
print("This is policy file - plugin 1")