mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-10 06:32:17 +03:00
Merge branch 'master' of https://github.com/Pardus-Kurumsal/ahenk
This commit is contained in:
commit
bf5ddb7aff
15 changed files with 157 additions and 119 deletions
2
debian/control
vendored
2
debian/control
vendored
|
@ -8,5 +8,5 @@ Homepage: http://www.liderahenk.org.tr
|
|||
|
||||
Package: ahenk
|
||||
Architecture: any
|
||||
Depends:python3 (>= 3), cython, libidn11, libidn11-dev, python3-dev, python3-pip, libffi-dev, libssl-dev, python3-paramiko, python3-cpuinfo, python3-psutil (>= 4), libpam-script, python-sleekxmpp
|
||||
Depends:python3 (>= 3), cython, libidn11, libidn11-dev, python3-dev, python3-pip, libffi-dev, libssl-dev, python3-paramiko, python3-cpuinfo, python3-psutil (>= 4), libpam-script, python3-sleekxmpp
|
||||
Description: Lider Ahenk is an open source project which provides solutions to manage, monitor and audit unlimited number of different systems and users on a network.
|
||||
|
|
|
@ -114,12 +114,12 @@ class AhenkDeamon(BaseDaemon):
|
|||
def check_registration(self):
|
||||
max_attemp_number = int(System.Hardware.Network.interface_size()) * 3
|
||||
logger = Scope.getInstance().getLogger()
|
||||
|
||||
try:
|
||||
while Scope.getInstance().getRegistration().is_registered() is False:
|
||||
max_attemp_number -= 1
|
||||
logger.debug('[AhenkDeamon] Ahenk is not registered. Attempting for registration')
|
||||
# TODO 'Could not reach Registration response from Lider. Be sure Lider is awake and it is connected to XMPP server!'
|
||||
|
||||
# TODO 'Could not reach Registration response from Lider. Be sure Lider is running and it is connected to XMPP server!'
|
||||
Scope.getInstance().getRegistration().registration_request()
|
||||
if max_attemp_number < 0:
|
||||
logger.warning('[AhenkDeamon] Number of Attempting for registration is over')
|
||||
|
@ -129,8 +129,9 @@ class AhenkDeamon(BaseDaemon):
|
|||
logger.error('[AhenkDeamon] Registration failed. Error message: {}'.format(str(e)))
|
||||
|
||||
def registration_failed(self):
|
||||
# TODO registration fail protocol implement
|
||||
pass
|
||||
self.logger.error('[AhenkDeamon] Registration failed. All registration attemps were failed. Ahenk is stopping...')
|
||||
print('Registration failed. Ahenk is stopping..')
|
||||
ahenkdaemon.stop()
|
||||
|
||||
def reload_plugins(self):
|
||||
Scope.getInstance().getPluginManager().reloadPlugins()
|
||||
|
@ -178,6 +179,9 @@ class AhenkDeamon(BaseDaemon):
|
|||
self.init_plugin_manager()
|
||||
self.logger.info('[AhenkDeamon] Plugin Manager was set')
|
||||
|
||||
self.init_scheduler()
|
||||
self.logger.info('[AhenkDeamon] Scheduler was set')
|
||||
|
||||
self.init_task_manager()
|
||||
self.logger.info('[AhenkDeamon] Task Manager was set')
|
||||
|
||||
|
@ -232,7 +236,6 @@ class AhenkDeamon(BaseDaemon):
|
|||
|
||||
self.logger.debug('[AhenkDeamon] Signal handled')
|
||||
self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event'])))
|
||||
print('event:{}'.format(str(json_data['event'])))
|
||||
|
||||
if 'login' == str(json_data['event']):
|
||||
self.logger.info('[AhenkDeamon] login event is handled for user: {}'.format(json_data['username']))
|
||||
|
|
|
@ -22,10 +22,10 @@ class AhenkDbService(object):
|
|||
|
||||
def initialize_table(self):
|
||||
|
||||
self.check_and_create_table('task', ['id INTEGER', 'create_date TEXT', 'modify_date TEXT', 'command_cls_id TEXT', 'parameter_map BLOB', 'deleted INTEGER', 'plugin TEXT'])
|
||||
self.check_and_create_table('task', ['id INTEGER', 'create_date TEXT', 'modify_date TEXT', 'command_cls_id TEXT', 'parameter_map BLOB', 'deleted INTEGER', 'plugin TEXT','cron_expr TEXT'])
|
||||
self.check_and_create_table('policy', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'type TEXT', 'version TEXT', 'name TEXT', 'execution_id TEXT'])
|
||||
self.check_and_create_table('profile', ['id INTEGER', 'create_date TEXT', 'label TEXT', 'description TEXT', 'overridable INTEGER', 'active TEXT', 'deleted TEXT', 'profile_data TEXT', 'modify_date TEXT', 'plugin TEXT'])
|
||||
self.check_and_create_table('plugin', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'active TEXT', 'create_date TEXT', 'deleted TEXT', 'description TEXT', 'machine_oriented TEXT', 'modify_date TEXT', 'name TEXT', 'policy_plugin TEXT', 'user_oriented TEXT', 'version TEXT'])
|
||||
self.check_and_create_table('plugin', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'active TEXT', 'create_date TEXT', 'deleted TEXT', 'description TEXT', 'machine_oriented TEXT', 'modify_date TEXT', 'name TEXT', 'policy_plugin TEXT', 'user_oriented TEXT', 'version TEXT', 'task_plugin TEXT', 'x_based TEXT'])
|
||||
self.check_and_create_table('registration', ['jid TEXT', 'password TEXT', 'registered INTEGER', 'dn TEXT', 'params TEXT', 'timestamp TEXT'])
|
||||
|
||||
def connect(self):
|
||||
|
|
|
@ -8,9 +8,8 @@ import os
|
|||
import shutil
|
||||
import stat
|
||||
import subprocess
|
||||
import uuid
|
||||
import urllib.request
|
||||
import errno
|
||||
import uuid
|
||||
|
||||
from base.Scope import Scope
|
||||
from base.messaging.ssh_file_transfer import FileTransfer
|
||||
|
@ -108,7 +107,7 @@ class ExecutionManager(object):
|
|||
user_policy_version = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \'' + policy.get_username() + '\'')
|
||||
|
||||
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin']
|
||||
plugin_columns = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version']
|
||||
plugin_columns = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version', 'task_plugin', 'x_based']
|
||||
|
||||
if policy.get_ahenk_policy_version() != ahenk_policy_ver:
|
||||
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'')
|
||||
|
@ -123,7 +122,7 @@ class ExecutionManager(object):
|
|||
for profile in policy.get_ahenk_profiles():
|
||||
plugin = profile.get_plugin()
|
||||
|
||||
plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version())]
|
||||
plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version()), str(plugin.get_task_plugin()), str(plugin.get_x_based())]
|
||||
plugin_id = self.db_service.update('plugin', plugin_columns, plugin_args)
|
||||
|
||||
profile_args = [str(ahenk_policy_id), str(profile.get_create_date()), str(profile.get_modify_date()), str(profile.get_label()), str(profile.get_description()), str(profile.get_overridable()), str(profile.get_active()), str(profile.get_deleted()), str(profile.get_profile_data()), plugin_id]
|
||||
|
@ -131,7 +130,7 @@ class ExecutionManager(object):
|
|||
|
||||
else:
|
||||
self.logger.debug('[ExecutionManager] Already there is ahenk policy. Command Execution Id is updating')
|
||||
self.db_service.update('policy', ['execution_id'], [policy.get_agent_execution_id()],'type = \'A\'')
|
||||
self.db_service.update('policy', ['execution_id'], [policy.get_agent_execution_id()], 'type = \'A\'')
|
||||
|
||||
if policy.get_user_policy_version() != user_policy_version:
|
||||
user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\'' + policy.get_username() + '\'')
|
||||
|
@ -147,7 +146,7 @@ class ExecutionManager(object):
|
|||
for profile in policy.get_user_profiles():
|
||||
plugin = profile.get_plugin()
|
||||
|
||||
plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version())]
|
||||
plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version()), str(plugin.get_task_plugin()), str(plugin.get_x_based())]
|
||||
plugin_id = self.db_service.update('plugin', plugin_columns, plugin_args)
|
||||
|
||||
profile_args = [str(user_policy_id), str(profile.get_create_date()), str(profile.get_modify_date()), str(profile.get_label()), str(profile.get_description()), str(profile.get_overridable()), str(profile.get_active()), str(profile.get_deleted()), str(profile.get_profile_data()), plugin_id]
|
||||
|
@ -155,7 +154,7 @@ class ExecutionManager(object):
|
|||
|
||||
else:
|
||||
self.logger.debug('[ExecutionManager] Already there is user policy. . Command Execution Id is updating')
|
||||
self.db_service.update('policy', ['execution_id'], [policy.get_user_execution_id()],'type = \'U\'')
|
||||
self.db_service.update('policy', ['execution_id'], [policy.get_user_execution_id()], 'type = \'U\'')
|
||||
|
||||
policy = self.get_active_policies(policy.get_username())
|
||||
self.task_manager.addPolicy(policy)
|
||||
|
@ -165,7 +164,7 @@ class ExecutionManager(object):
|
|||
user_policy = self.db_service.select('policy', ['id', 'version', 'name'], ' type=\'U\' and name=\'' + username + '\'')
|
||||
ahenk_policy = self.db_service.select('policy', ['id', 'version'], ' type=\'A\' ')
|
||||
|
||||
plugin_columns = ['id', 'active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version']
|
||||
plugin_columns = ['id', 'active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version', 'task_plugin', 'x_based']
|
||||
profile_columns = ['id', 'create_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'modify_date', 'plugin']
|
||||
|
||||
policy = PolicyBean(username=username)
|
||||
|
@ -198,7 +197,7 @@ class ExecutionManager(object):
|
|||
|
||||
return policy
|
||||
|
||||
#from db
|
||||
# from db
|
||||
def get_installed_plugins(self):
|
||||
plugins = self.db_service.select('plugin', ['name', 'version'])
|
||||
p_list = []
|
||||
|
@ -210,18 +209,16 @@ class ExecutionManager(object):
|
|||
|
||||
str_task = json.loads(arg)['task']
|
||||
json_task = json.loads(str_task)
|
||||
task = self.json_to_TaskBean(json_task)
|
||||
task = self.json_to_task_bean(json_task)
|
||||
|
||||
self.logger.debug('[ExecutionManager] Adding new task...Task is:{}'.format(task.get_command_cls_id()))
|
||||
|
||||
self.task_manager.addTask(task)
|
||||
self.logger.debug('[ExecutionManager] Task added')
|
||||
|
||||
def json_to_TaskBean(self, json_data):
|
||||
|
||||
def json_to_task_bean(self, json_data):
|
||||
plu = json_data['plugin']
|
||||
plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'])
|
||||
|
||||
plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'], task_plugin=plu['taskPlugin'], x_based=plu['xBased'])
|
||||
return TaskBean(_id=json_data['id'], create_date=json_data['createDate'], modify_date=json_data['modifyDate'], command_cls_id=json_data['commandClsId'], parameter_map=json_data['parameterMap'], deleted=json_data['deleted'], plugin=plugin, cron_str=json_data['cronExpression'])
|
||||
|
||||
def move_file(self, arg):
|
||||
|
@ -296,13 +293,13 @@ class ExecutionManager(object):
|
|||
if ahenk_prof_json_arr is not None:
|
||||
for prof in ahenk_prof_json_arr:
|
||||
plu = json.loads(json.dumps(prof['plugin']))
|
||||
plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'])
|
||||
plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'], task_plugin=plu['taskPlugin'], x_based=plu['xBased'])
|
||||
ahenk_prof_arr.append(ProfileBean(prof['id'], prof['createDate'], prof['label'], prof['description'], prof['overridable'], prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'], plugin, username))
|
||||
|
||||
if user_prof_json_arr is not None:
|
||||
for prof in user_prof_json_arr:
|
||||
plu = json.loads(json.dumps(prof['plugin']))
|
||||
plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'])
|
||||
plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'], task_plugin=plu['taskPlugin'], x_based=plu['xBased'])
|
||||
user_prof_arr.append(ProfileBean(prof['id'], prof['createDate'], prof['label'], prof['description'], prof['overridable'], prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'], plugin, username))
|
||||
|
||||
return PolicyBean(ahenk_policy_version=json_data['agentPolicyVersion'], user_policy_version=json_data['userPolicyVersion'], ahenk_profiles=ahenk_prof_arr, user_profiles=user_prof_arr, timestamp=json_data['timestamp'], username=json_data['username'], agent_execution_id=json_data['agentCommandExecutionId'], user_execution_id=json_data['userCommandExecutionId'])
|
||||
|
|
|
@ -65,7 +65,7 @@ class Messenger(ClientXMPP):
|
|||
return False
|
||||
|
||||
def session_end(self):
|
||||
print("disconnect")
|
||||
self.logger.warning('[Messenger] DISCONNECTED')
|
||||
|
||||
def session_start(self, event):
|
||||
self.logger.debug('[Messenger] Session was started')
|
||||
|
@ -77,7 +77,7 @@ class Messenger(ClientXMPP):
|
|||
self.logger.debug('[Messenger] <<--------Sending message: {}'.format(msg))
|
||||
self.send_message(mto=self.receiver, mbody=msg, mtype='normal')
|
||||
except Exception as e:
|
||||
self.logger.debug('[Messenger] A problem occurred while sending direct message. Error Message: {}'.format(str(e)))
|
||||
self.logger.error('[Messenger] A problem occurred while sending direct message. Error Message: {}'.format(str(e)))
|
||||
|
||||
def recv_direct_message(self, msg):
|
||||
if msg['type'] in ('normal'):
|
||||
|
@ -87,6 +87,5 @@ class Messenger(ClientXMPP):
|
|||
message_type = j['type']
|
||||
self.event_manger.fireEvent(message_type, str(msg['body']))
|
||||
self.logger.debug('[Messenger] Fired event is: {}'.format(message_type))
|
||||
|
||||
except Exception as e:
|
||||
self.logger.debug('[Messenger] A problem occurred while keeping message. Error Message: {}'.format(str(e)))
|
||||
self.logger.error('[Messenger] A problem occurred while keeping message. Error Message: {}'.format(str(e)))
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
||||
import json
|
||||
|
||||
|
||||
class PluginBean(object):
|
||||
"""docstring for PluginBean"""
|
||||
|
||||
def __init__(self, p_id=None, active=None, create_date=None, deleted=None, description=None, machine_oriented=None, modify_date=None, name=None, policy_plugin=None, user_oriented=None, version=None):
|
||||
def __init__(self, p_id=None, active=None, create_date=None, deleted=None, description=None, machine_oriented=None, modify_date=None, name=None, policy_plugin=None, task_plugin=None, user_oriented=None, version=None, x_based=None):
|
||||
self.id = p_id
|
||||
self.active = active
|
||||
self.create_date = create_date
|
||||
|
@ -19,6 +18,8 @@ class PluginBean(object):
|
|||
self.policy_plugin = policy_plugin
|
||||
self.user_oriented = user_oriented
|
||||
self.version = version
|
||||
self.task_plugin = task_plugin
|
||||
self.x_based = x_based
|
||||
|
||||
def get_user_oriented(self):
|
||||
return self.user_oriented
|
||||
|
@ -85,3 +86,15 @@ class PluginBean(object):
|
|||
|
||||
def set_version(self, version):
|
||||
self.version = version
|
||||
|
||||
def get_x_based(self):
|
||||
return self.x_based
|
||||
|
||||
def set_x_based(self, x_based):
|
||||
self.x_based = x_based
|
||||
|
||||
def get_task_plugin(self):
|
||||
return self.task_plugin
|
||||
|
||||
def set_task_plugin(self, task_plugin):
|
||||
self.task_plugin = task_plugin
|
||||
|
|
|
@ -65,6 +65,33 @@ class TaskBean(object):
|
|||
def set_cron_str(self, cron_str):
|
||||
self.cron_str = cron_str
|
||||
|
||||
def to_json(self):
|
||||
plugin_data = {}
|
||||
plugin_data['id'] = self.plugin.get_id()
|
||||
plugin_data['name'] = self.plugin.get_name()
|
||||
plugin_data['version'] = self.plugin.get_version()
|
||||
plugin_data['description'] = self.plugin.get_description()
|
||||
plugin_data['active'] = self.plugin.get_active()
|
||||
plugin_data['deleted'] = self.plugin.get_deleted()
|
||||
plugin_data['machineOriented'] = self.plugin.get_machine_oriented()
|
||||
plugin_data['userOriented'] = self.plugin.get_user_oriented()
|
||||
plugin_data['policyPlugin'] = self.plugin.get_policy_plugin()
|
||||
plugin_data['taskPlugin'] = self.plugin.get_task_plugin()
|
||||
plugin_data['xBased'] = self.plugin.get_x_based()
|
||||
plugin_data['createDate'] = self.plugin.get_create_date()
|
||||
plugin_data['modifyDate'] = self.plugin.get_modify_date()
|
||||
|
||||
task_data = {}
|
||||
task_data['id'] = self._id
|
||||
task_data['plugin'] = plugin_data
|
||||
task_data['commandClsId'] = self.command_cls_id
|
||||
task_data['parameterMap'] = self.parameter_map
|
||||
task_data['deleted'] = self.deleted
|
||||
task_data['cronExpression'] = self.cron_str
|
||||
task_data['createDate'] = self.create_date
|
||||
task_data['modifyDate'] = self.modify_date
|
||||
return task_data
|
||||
|
||||
@property
|
||||
def obj_name(self):
|
||||
return "TASK"
|
||||
|
|
|
@ -63,7 +63,7 @@ class PluginManager(object):
|
|||
plugin.start()
|
||||
self.plugins.append(plugin)
|
||||
|
||||
self.logger.debug('[PluginManager] New plugin was loaded.')
|
||||
self.logger.debug('[PluginManager] New plugin was loaded. Plugin Name: {}'.format(plugin_name))
|
||||
|
||||
if len(self.delayed_profiles) > 0:
|
||||
self.pluginQueueDict[plugin_name].put(self.delayed_profiles[plugin_name], 1)
|
||||
|
|
|
@ -21,25 +21,33 @@ class CustomScheduler(BaseScheduler):
|
|||
def initialize(self):
|
||||
self.scheduledb.initialize()
|
||||
tasks = self.scheduledb.load()
|
||||
for task in tasks:
|
||||
self.add_job(ScheduleTaskJob(task))
|
||||
if tasks:
|
||||
for task in tasks:
|
||||
self.add_job(ScheduleTaskJob(task))
|
||||
|
||||
def add_job(self, job):
|
||||
self.events.append(job)
|
||||
|
||||
def save_and_add_job(self, task):
|
||||
self.scheduledb.save(task)
|
||||
self.events.append(ScheduleTaskJob(task))
|
||||
try:
|
||||
self.logger.debug('[CustomScheduler] Saving scheduled task to database...')
|
||||
self.scheduledb.save(task)
|
||||
self.logger.debug('[CustomScheduler] Adding scheduled task to events...')
|
||||
self.events.append(ScheduleTaskJob(task))
|
||||
except Exception as e:
|
||||
self.logger.error('[CustomScheduler] A problem occurred while saving and adding job. Error Message: {}'.format(str(e)))
|
||||
|
||||
def remove_job(self, task):
|
||||
def remove_job(self, task_id):
|
||||
for event in self.events:
|
||||
if event.task.id == task.id:
|
||||
self.scheduledb.delete(task)
|
||||
if event.task.get_id() == task_id:
|
||||
self.scheduledb.delete(task_id)
|
||||
self.logger.debug('[CustomScheduler] Task was deleted from scheduled tasks table')
|
||||
self.events.remove(event)
|
||||
self.logger.debug('[CustomScheduler] Task was removed from events')
|
||||
|
||||
def remove_job_via_task_id(self,task_id):
|
||||
for event in self.events:
|
||||
if event.task.id == task_id:
|
||||
if event.task.get_id() == task_id:
|
||||
self.scheduledb.delete(event.task)
|
||||
self.events.remove(event)
|
||||
|
||||
|
|
|
@ -5,30 +5,40 @@
|
|||
from base.scheduler.custom.all_match import AllMatch
|
||||
from base.Scope import Scope
|
||||
|
||||
|
||||
# TODO Need logs
|
||||
class ScheduleTaskJob(object):
|
||||
def __init__(self, task):
|
||||
self.task = task
|
||||
cron_sj = self.parse_cron_str(task.cron_str)
|
||||
if cron_sj:
|
||||
self.mins = self.conv_to_set(cron_sj[0])
|
||||
self.hours= self.conv_to_set(cron_sj[1])
|
||||
self.days = self.conv_to_set(cron_sj[2])
|
||||
self.months = self.conv_to_set(cron_sj[3])
|
||||
self.dow = self.conv_to_set(cron_sj[4])
|
||||
self.action = self.processTask
|
||||
scope = Scope.getInstance()
|
||||
self.logger = scope.getLogger()
|
||||
self.task_manager = scope.getTaskManager()
|
||||
|
||||
def processTask(self):
|
||||
self.plugin_manager = scope.getPluginManager()
|
||||
self.task = task
|
||||
cron_sj = self.parse_cron_str(task.get_cron_str())
|
||||
try:
|
||||
self.task_manager.addTask(self.task)
|
||||
if self.is_single_shot():
|
||||
Scope.getInstance().get_scheduler().remove_job(self.task)
|
||||
if cron_sj:
|
||||
self.mins = self.conv_to_set(cron_sj[0])
|
||||
self.hours = self.conv_to_set(cron_sj[1])
|
||||
self.days = self.conv_to_set(cron_sj[2])
|
||||
self.months = self.conv_to_set(cron_sj[3])
|
||||
self.dow = self.conv_to_set(cron_sj[4])
|
||||
self.action = self.process_task
|
||||
self.logger.debug('[ScheduleTaskJob] Instance created.')
|
||||
except Exception as e:
|
||||
self.logger.error(e)
|
||||
self.logger.error('[ScheduleTaskJob] A problem occurred while creating instance of ScheduleTaskJob. Error Message : {}'.format(str(e)))
|
||||
|
||||
def process_task(self):
|
||||
try:
|
||||
self.logger.debug('[ScheduleTaskJob] Running scheduled task now...')
|
||||
self.plugin_manager.processTask(self.task)
|
||||
self.logger.debug('[ScheduleTaskJob] Scheduled Task was executed.')
|
||||
if self.is_single_shot():
|
||||
Scope.getInstance().get_scheduler().remove_job(self.task.get_id())
|
||||
except Exception as e:
|
||||
self.logger.error('[ScheduleTaskJob] A problem occurred while running scheduled task. Error Message: {}'.format(str(e)))
|
||||
|
||||
def parse_cron_str(self, cron_str):
|
||||
self.logger.debug('[ScheduleTaskJob] Parsing cron string...')
|
||||
try:
|
||||
cron_exp_arr = cron_str.split(" ")
|
||||
cron_sj = []
|
||||
|
@ -51,21 +61,24 @@ class ScheduleTaskJob(object):
|
|||
elif count == 3:
|
||||
cron_sj.append(range(0, 7, range_val))
|
||||
else:
|
||||
print("it is not supported")
|
||||
self.logger.warning('[ScheduleTaskJob] Unsupported expression.')
|
||||
elif ',' in exp:
|
||||
cron_sj.append("(" + str(exp) + ")")
|
||||
else:
|
||||
print("it is not supported")
|
||||
self.logger.warning('[ScheduleTaskJob] Unsupported expression.')
|
||||
count = count + 1
|
||||
return cron_sj
|
||||
except Exception as e:
|
||||
self.logger.error(str(e))
|
||||
self.logger.error('[ScheduleTaskJob] A problem occurred while parsing cron expression. Error Message: {}'.format(str(e)))
|
||||
|
||||
def conv_to_set(obj):
|
||||
if isinstance(obj, (int)):
|
||||
return set([obj])
|
||||
def conv_to_set(self, obj):
|
||||
self.logger.debug('[ScheduleTaskJob] Converting {} to set'.format(str(obj)))
|
||||
|
||||
if str(obj).isdigit():
|
||||
return set([int(obj)])
|
||||
if not isinstance(obj, set):
|
||||
obj = set(obj)
|
||||
|
||||
return obj
|
||||
|
||||
def is_single_shot(self):
|
||||
|
@ -76,12 +89,12 @@ class ScheduleTaskJob(object):
|
|||
|
||||
def matchtime(self, t):
|
||||
"""Return True if this event should trigger at the specified datetime"""
|
||||
return ((t.minute in self.mins) and
|
||||
(t.hour in self.hours) and
|
||||
(t.day in self.days) and
|
||||
(t.month in self.months) and
|
||||
(t.weekday() in self.dow))
|
||||
return ((t.minute in self.mins) and
|
||||
(t.hour in self.hours) and
|
||||
(t.day in self.days) and
|
||||
(t.month in self.months) and
|
||||
(t.weekday() in self.dow))
|
||||
|
||||
def check(self, t):
|
||||
if self.matchtime(t):
|
||||
self.action(*self.args, **self.kwargs)
|
||||
if self.matchtime(t) is True:
|
||||
self.action()
|
||||
|
|
|
@ -14,22 +14,22 @@ class ScheduleTaskDB(object):
|
|||
|
||||
def initialize(self):
|
||||
self.logger.debug('[ScheduleTaskDB] Initializing scheduler database...')
|
||||
self.db_service.check_and_create_table('schedule_task', ['id INTEGER', 'task_json TEXT'])
|
||||
self.db_service.check_and_create_table('schedule_task', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'task_id TEXT'])
|
||||
self.logger.debug('[ScheduleTaskDB] Scheduler database is ok.')
|
||||
|
||||
def save(self, task):
|
||||
self.logger.debug('[ScheduleTaskDB] Preparing schedule task for save operation... creating columns and values...')
|
||||
cols = ['id', 'task_json']
|
||||
values = [str(task.id), str(task.to_json())]
|
||||
cols = ['task_id']
|
||||
values = [task.get_id()]
|
||||
self.logger.debug('[ScheduleTaskDB] Saving scheduler task to db... ')
|
||||
self.db_service.update('schedule_task', cols, values, None)
|
||||
self.logger.debug('[ScheduleTaskDB] Scheduler task saved.')
|
||||
|
||||
def delete(self, task):
|
||||
def delete(self, task_id):
|
||||
try:
|
||||
self.logger.debug('[ScheduleTaskDB] Deleting schedule task. Task id=' + str(task.id))
|
||||
self.db_service.delete('schedule_task', 'id=' + str(task.id))
|
||||
self.logger.debug('[ScheduleTaskDB] Deleting schedule task deleted successfully. task id=' + str(task.id))
|
||||
self.logger.debug('[ScheduleTaskDB] Deleting schedule task. Task id=' + str(task_id))
|
||||
self.db_service.delete('schedule_task', 'task_id=' + str(task_id))
|
||||
self.logger.debug('[ScheduleTaskDB] Deleting schedule task deleted successfully. task id=' + str(task_id))
|
||||
except Exception as e:
|
||||
self.logger.error('[ScheduleTaskDB] Exception occur when deleting schedule task ' + str(e))
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ from base.scheduler.custom.custom_scheduler import CustomScheduler
|
|||
|
||||
class SchedulerFactory():
|
||||
|
||||
def get_intstance(self):
|
||||
def get_intstance():
|
||||
return CustomScheduler()
|
||||
|
||||
get_intstance = staticmethod(get_intstance)
|
|
@ -13,10 +13,6 @@ import fcntl
|
|||
import struct
|
||||
from uuid import getnode as get_mac
|
||||
|
||||
"""
|
||||
some functions closed because of dependency management
|
||||
"""
|
||||
|
||||
|
||||
class System:
|
||||
class Ahenk(object):
|
||||
|
@ -191,21 +187,13 @@ class System:
|
|||
def user_details():
|
||||
return psutil.users()
|
||||
|
||||
|
||||
|
||||
@staticmethod
|
||||
def last_login_username():
|
||||
# TODO
|
||||
pass
|
||||
|
||||
"""
|
||||
@staticmethod
|
||||
def user_name():
|
||||
arr = []
|
||||
for user in psutil.get_users():
|
||||
if str(user[0]) is not 'None' and user[0] not in arr:
|
||||
arr.append(user[0])
|
||||
return arr
|
||||
"""
|
||||
|
||||
class Os(object):
|
||||
|
||||
@staticmethod
|
||||
|
@ -349,17 +337,6 @@ class System:
|
|||
|
||||
return arr
|
||||
|
||||
"""
|
||||
@staticmethod
|
||||
def mac_addresses():
|
||||
arr=[]
|
||||
for iface in psutil.net_io_counters(pernic=True):
|
||||
mac = open('/sys/class/net/' + iface + '/address').readline()
|
||||
if str(mac[0:17]) != "00:00:00:00:00:00":
|
||||
arr.append(mac[0:17])
|
||||
return arr
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def interfaces_details():
|
||||
return psutil.net_if_addrs()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
||||
|
||||
from base.Scope import Scope
|
||||
from base.model.MessageFactory import MessageFactory
|
||||
from base.model.enum.MessageType import MessageType
|
||||
|
@ -19,33 +20,32 @@ class TaskManager(object):
|
|||
|
||||
def addTask(self, task):
|
||||
try:
|
||||
self.saveTask(task)
|
||||
if task.get_cron_str() == None or task.get_cron_str() == '':
|
||||
self.logger.debug('Adding task ... ')
|
||||
#self.saveTask(task)
|
||||
self.logger.info('Task saved ')
|
||||
# TODO send task received message
|
||||
self.logger.debug('[TaskManager] Adding task ... ')
|
||||
self.pluginManager.processTask(task)
|
||||
else:
|
||||
self.scheduler.save_and_add_job(task)
|
||||
|
||||
except Exception as e:
|
||||
# TODO error log here
|
||||
self.logger.debug('Exception occured when adding task ' + str(e))
|
||||
pass
|
||||
self.logger.debug('[TaskManager] Exception occurred when adding task. Error Message: {}'.format(str(e)))
|
||||
|
||||
def addPolicy(self, policy):
|
||||
try:
|
||||
self.pluginManager.processPolicy(policy)
|
||||
except Exception as e:
|
||||
self.logger.error("Exception occured when adding policy. Error Message: {}".format(str(e)))
|
||||
pass
|
||||
self.logger.error("[TaskManager] Exception occurred when adding policy. Error Message: {}".format(str(e)))
|
||||
|
||||
def saveTask(self, task):
|
||||
|
||||
cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin']
|
||||
values = [str(task.get_id()), str(task.get_create_date()), str(task.get_modify_date()), str(task.get_command_cls_id()), str(task.get_parameter_map()), str(task.get_deleted()), task.plugin.to_string()]
|
||||
self.db_service.update('task', cols, values, None)
|
||||
self.logger.debug('[TaskManager] Task has been saved to database (Task id:' + task.id + ')')
|
||||
try:
|
||||
task_cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin','cron_expr']
|
||||
plu_cols = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version','task_plugin','x_based']
|
||||
plugin_args = [str(task.get_plugin().get_active()), str(task.get_plugin().get_create_date()), str(task.get_plugin().get_deleted()), str(task.get_plugin().get_description()), str(task.get_plugin().get_machine_oriented()), str(task.get_plugin().get_modify_date()), str(task.get_plugin().get_name()), str(task.get_plugin().get_policy_plugin()), str(task.get_plugin().get_user_oriented()), str(task.get_plugin().get_version()), str(task.get_plugin().get_task_plugin()), str(task.get_plugin().get_x_based())]
|
||||
plugin_id = self.db_service.update('plugin', plu_cols, plugin_args)
|
||||
values = [str(task.get_id()), str(task.get_create_date()), str(task.get_modify_date()), str(task.get_command_cls_id()), str(task.get_parameter_map()), str(task.get_deleted()), str(plugin_id),str(task.get_cron_str())]
|
||||
self.db_service.update('task', task_cols, values, None)
|
||||
except Exception as e:
|
||||
self.logger.error("[TaskManager] Exception occurred while saving task. Error Message: {}".format(str(e)))
|
||||
|
||||
def updateTask(self, task):
|
||||
# TODO not implemented yet
|
||||
|
|
|
@ -209,20 +209,20 @@ class Util:
|
|||
|
||||
result_code, p_out, p_err = Util.execute('dpkg -s {}'.format(package_name))
|
||||
try:
|
||||
lines=str(p_out).split('\n')
|
||||
lines = str(p_out).split('\n')
|
||||
for line in lines:
|
||||
if len(line)>1:
|
||||
if line.split(None, 1)[0].lower() =='status:':
|
||||
if len(line) > 1:
|
||||
if line.split(None, 1)[0].lower() == 'status:':
|
||||
if 'installed' in line.split(None, 1)[1].lower():
|
||||
return True
|
||||
return False
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_md5_file(fname):
|
||||
hash_md5 = hashlib.md5()
|
||||
with open(fname, 'rb') as f:
|
||||
for chunk in iter(lambda: f.read(4096), b""):
|
||||
hash_md5.update(chunk)
|
||||
return str(hash_md5.hexdigest())
|
||||
return str(hash_md5.hexdigest())
|
Loading…
Reference in a new issue