conflict merging

This commit is contained in:
Volkan Şahin 2016-03-29 16:31:49 +03:00
commit cff628e5df
10 changed files with 82 additions and 24 deletions

View file

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

View file

@ -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 = []

View file

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

View file

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

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