mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-22 09:42:15 +03:00
requesting policies on logining - interpreting policy response and handle exiting ahenk
This commit is contained in:
parent
7a236423dd
commit
3add386a49
7 changed files with 95 additions and 56 deletions
|
@ -87,17 +87,21 @@ class AhenkDeamon(BaseDaemon):
|
|||
|
||||
# TODO restrict number of attemption
|
||||
while registration.is_registered() is False:
|
||||
print('registration need')
|
||||
logger.debug('[AhenkDeamon] Attempting to register')
|
||||
registration.registration_request()
|
||||
|
||||
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):
|
||||
time.sleep(1)
|
||||
time.sleep(5)
|
||||
|
||||
globalscope.setMessager(messager)
|
||||
logger.info('[AhenkDeamon] Messager was set')
|
||||
|
@ -125,6 +129,7 @@ class AhenkDeamon(BaseDaemon):
|
|||
|
||||
messager.send_direct_message('test')
|
||||
|
||||
|
||||
responseQueue = queue.Queue()
|
||||
messageResponseQueue = MessageResponseQueue(responseQueue)
|
||||
messageResponseQueue.setDaemon(True)
|
||||
|
@ -141,8 +146,10 @@ class AhenkDeamon(BaseDaemon):
|
|||
|
||||
# this is must be created after message services
|
||||
|
||||
def signal_handler(self, num, stack):
|
||||
|
||||
|
||||
def signal_handler(self, num, stack):
|
||||
print("signal handled")
|
||||
# TODO######
|
||||
config = configparser.ConfigParser()
|
||||
config._interpolation = configparser.ExtendedInterpolation()
|
||||
|
@ -155,57 +162,74 @@ class AhenkDeamon(BaseDaemon):
|
|||
logger = scope.getLogger()
|
||||
|
||||
if 'login' == str(params[0]):
|
||||
message = scope.getMessageManager().login_msg(params[1])
|
||||
message = scope.getMessageManager().policies_msg(params[1])
|
||||
scope.getMessager().send_direct_message(message)
|
||||
logger.debug('[AhenkDeamon] login event is handled for user:' + params[1])
|
||||
elif 'logout' == str(params[0]):
|
||||
message = scope.getMessageManager().logout_msg(params[1])
|
||||
scope.getMessager().send_direct_message(message)
|
||||
logger.debug('[AhenkDeamon] logout event is handled for user:' + params[1])
|
||||
elif 'exit' == str(params[0]):
|
||||
print("exit:"+str(params[0]))
|
||||
scope = Scope.getInstance()
|
||||
scope.getMessager().disconnect()
|
||||
#TODO kill thread
|
||||
print('stopping ahenk')
|
||||
else:
|
||||
logger.error('[AhenkDeamon] Unknown command error. Command:' + params[0])
|
||||
|
||||
logger.debug('[AhenkDeamon] Processing of handled event is completed')
|
||||
|
||||
|
||||
|
||||
def get_pid_number():
|
||||
config = configparser.ConfigParser()
|
||||
config._interpolation = configparser.ExtendedInterpolation()
|
||||
config.read(pidfilePath)
|
||||
return config.get('PID', 'pid_number')
|
||||
|
||||
|
||||
def set_event(event_param):
|
||||
config = configparser.ConfigParser()
|
||||
config._interpolation = configparser.ExtendedInterpolation()
|
||||
config.read(pidfilePath)
|
||||
config.set('PID', 'event', event_param)
|
||||
|
||||
with open(pidfilePath, 'w') as config_file:
|
||||
config.write(config_file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
ahenkdaemon = AhenkDeamon(pidfilePath)
|
||||
try:
|
||||
if len(sys.argv) == 2:
|
||||
if sys.argv[1] == 'start':
|
||||
print('starting')
|
||||
ahenkdaemon.run()
|
||||
elif sys.argv[1] == 'stop':
|
||||
ahenkdaemon.stop()
|
||||
elif sys.argv[1] == 'restart':
|
||||
ahenkdaemon.restart()
|
||||
elif sys.argv[1] == 'status':
|
||||
print('status')
|
||||
else:
|
||||
print('Unknown command. Usage : %s start|stop|restart|status' % sys.argv[0])
|
||||
sys.exit(2)
|
||||
|
||||
if len(sys.argv) == 2:
|
||||
if sys.argv[1] == 'start':
|
||||
print('starting')
|
||||
ahenkdaemon.run()
|
||||
elif sys.argv[1] == 'stop':
|
||||
ahenkdaemon.stop()
|
||||
elif sys.argv[1] == 'restart':
|
||||
ahenkdaemon.restart()
|
||||
elif sys.argv[1] == 'status':
|
||||
print('status')
|
||||
elif len(sys.argv) == 3:
|
||||
if sys.argv[1] == 'login' or sys.argv[1] == 'logout':
|
||||
print('event:' + str(sys.argv[1]))
|
||||
set_event(str(sys.argv[1]) + ' ' + sys.argv[2])
|
||||
os.kill(int(get_pid_number()), signal.SIGALRM)
|
||||
else:
|
||||
print('Unknown command. Usage : %s start|stop|restart|status' % sys.argv[0])
|
||||
sys.exit(2)
|
||||
sys.exit(0)
|
||||
else:
|
||||
print('Unknown command. Usage : %s start|stop|restart|status' % sys.argv[0])
|
||||
print('Usage : %s start|stop|restart|status' % sys.argv[0])
|
||||
sys.exit(2)
|
||||
|
||||
elif len(sys.argv) == 3:
|
||||
if sys.argv[1] == 'login' or sys.argv[1] == 'logout':
|
||||
print('event:' + str(sys.argv[1]))
|
||||
|
||||
# TODO##############
|
||||
config = configparser.ConfigParser()
|
||||
config._interpolation = configparser.ExtendedInterpolation()
|
||||
config.read(pidfilePath)
|
||||
pid_number = config.get('PID', 'pid_number')
|
||||
config.set('PID', 'event', str(sys.argv[1]) + ' ' + sys.argv[2])
|
||||
# TODO##############
|
||||
|
||||
with open(pidfilePath, 'w') as config_file:
|
||||
config.write(config_file)
|
||||
|
||||
os.kill(int(pid_number), signal.SIGALRM)
|
||||
else:
|
||||
print('Unknown command. Usage : %s start|stop|restart|status' % sys.argv[0])
|
||||
sys.exit(2)
|
||||
sys.exit(0)
|
||||
else:
|
||||
print('Usage : %s start|stop|restart|status' % sys.argv[0])
|
||||
sys.exit(2)
|
||||
except(KeyboardInterrupt, SystemExit):
|
||||
if str(os.getpid()) == get_pid_number():
|
||||
set_event('exit true')
|
||||
os.kill(int(get_pid_number()), signal.SIGALRM)
|
||||
|
|
|
@ -33,27 +33,32 @@ class ExecutionManager(object):
|
|||
self.event_manager.register_event('REQUEST_FILE', self.request_file)
|
||||
self.event_manager.register_event('MOVE_FILE', self.move_file)
|
||||
self.event_manager.register_event('EXECUTE_TASK', self.execute_task)
|
||||
self.event_manager.register_event('POLICY', self.update_policies)
|
||||
self.event_manager.register_event('EXECUTE_POLICY', self.execute_policy)
|
||||
|
||||
def update_policies(self, arg):
|
||||
def execute_policy(self, arg):
|
||||
self.logger.debug('[ExecutionManager] Updating policies...')
|
||||
|
||||
policy = Policy(json.loads(arg))
|
||||
# TODO get username
|
||||
# TODO get username and machine uid
|
||||
username = 'volkan'
|
||||
machine_uid='616161616161'
|
||||
|
||||
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 + '\'')
|
||||
installed_plugins = self.get_installed_plugins()
|
||||
missing_plugins = []
|
||||
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin']
|
||||
|
||||
if policy.ahenk_policy_version != ahenk_policy_ver:
|
||||
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'')
|
||||
self.db_service.delete('profile', 'id=' + str(ahenk_policy_id))
|
||||
self.db_service.update('policy', ['version'], [str(policy.ahenk_policy_version)], 'type=\'A\'')
|
||||
if ahenk_policy_id is not None:
|
||||
self.db_service.delete('profile', 'id=' + str(ahenk_policy_id))
|
||||
self.db_service.update('policy', ['version'], [str(policy.ahenk_policy_version)], 'type=\'A\'')
|
||||
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:
|
||||
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin']
|
||||
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)]
|
||||
self.db_service.update('profile', profile_columns, args)
|
||||
|
@ -65,12 +70,16 @@ class ExecutionManager(object):
|
|||
|
||||
if policy.user_policy_version != user_policy_version:
|
||||
user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\'' + username + '\'')
|
||||
self.db_service.delete('profile', 'id=' + str(user_policy_id))
|
||||
self.db_service.update('policy', ['version'], [str(policy.user_policy_version)], 'type=\'U\' and name=\'' + username + '\'')
|
||||
if user_policy_id is not None:
|
||||
self.db_service.delete('profile', 'id=' + str(user_policy_id))
|
||||
self.db_service.update('policy', ['version'], [str(policy.user_policy_version)], 'type=\'U\' and name=\'' + username + '\'')
|
||||
else:
|
||||
self.db_service.update('policy', ['type', 'version', 'name'], ['U', str(policy.user_policy_version), username])
|
||||
user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\'' + username + '\'')
|
||||
|
||||
for profile in policy.user_profiles:
|
||||
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin']
|
||||
args = [str(user_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)]
|
||||
args = [int(user_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label),
|
||||
str(profile.description), int(profile.overridable), int(profile.active), int(profile.deleted), str(profile.profile_data), str(profile.plugin.to_string())]
|
||||
self.db_service.update('profile', profile_columns, args)
|
||||
if profile.plugin.name not in installed_plugins and profile.plugin.name not in missing_plugins:
|
||||
missing_plugins.append(profile.plugin.name)
|
||||
|
|
|
@ -75,6 +75,8 @@ class Messager(slixmpp.ClientXMPP):
|
|||
# TODO need check
|
||||
def send_file(self, file_path):
|
||||
self.file = open(file_path, 'rb')
|
||||
|
||||
#TODO read conf file check file size if file size is bigger than max size, divide and send parts.after all send message about them
|
||||
self.logger.debug('[Messager] Sending file: ' + self.file.name)
|
||||
self.logger.debug('[MessageSender] Sending file: ' + self.file.name)
|
||||
try:
|
||||
|
@ -99,7 +101,6 @@ class Messager(slixmpp.ClientXMPP):
|
|||
|
||||
def send_direct_message(self, msg):
|
||||
self.logger.debug('[Messager] Sending message: ' + msg)
|
||||
print("mesaj gidiyoo:"+str(msg))
|
||||
self.send_message(mto=self.receiver, mbody=msg, mtype='normal')
|
||||
|
||||
def recv_direct_message(self, msg):
|
||||
|
@ -115,13 +116,14 @@ class Messager(slixmpp.ClientXMPP):
|
|||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
self.connect((self.hostname, 5222))
|
||||
self.process()
|
||||
self.process(forever=True)
|
||||
self.logger.debug('[Messager] Connection were established successfully')
|
||||
return True
|
||||
except Exception as e:
|
||||
self.logger.error('[Messager] Connection to server is failed! ' + e)
|
||||
return False
|
||||
|
||||
|
||||
def set_file_name_md5(self):
|
||||
self.logger.debug('[Messager] Renaming file as md5 hash')
|
||||
md5_hash = self.execution_manager.get_md5_file(self.file.name)
|
||||
|
|
|
@ -65,10 +65,13 @@ class Messaging(object):
|
|||
self.logger.debug('[Messaging] Logout message was created')
|
||||
return json_data
|
||||
|
||||
def policies_msg(self):
|
||||
def policies_msg(self, username):
|
||||
data = {}
|
||||
data['type'] = 'GET_POLICIES'
|
||||
data['username'] = str(pwd.getpwuid(os.getuid())[0])
|
||||
#TODO fetch db values
|
||||
data['userPolicyVersion'] = '1'
|
||||
data['machinePolicyVersion'] = '1'
|
||||
data['username'] = str(username)
|
||||
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
||||
json_data = json.dumps(data)
|
||||
self.logger.debug('[Messaging] Get Policies message was created')
|
||||
|
|
|
@ -15,12 +15,12 @@ class Policy(object):
|
|||
|
||||
@property
|
||||
def ahenk_policy_version(self):
|
||||
return self.policy['ahenkPolicyVersion']
|
||||
return self.policy['machinePolicyVersion']
|
||||
|
||||
@property
|
||||
def ahenk_profiles(self):
|
||||
profiles = []
|
||||
for p in self.policy['ahenkProfiles']:
|
||||
for p in self.policy['machinePolicyProfiles']:
|
||||
profiles.append(Profile(p))
|
||||
return profiles
|
||||
|
||||
|
@ -35,7 +35,7 @@ class Policy(object):
|
|||
@property
|
||||
def user_profiles(self):
|
||||
profiles = []
|
||||
for p in self.policy['userProfiles']:
|
||||
for p in self.policy['userPolicyProfiles']:
|
||||
profiles.append(Profile(p))
|
||||
return profiles
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ class TaskManager(object):
|
|||
def addTask(self, task):
|
||||
try:
|
||||
self.logger.debug('Adding task ... ')
|
||||
# self.saveTask(task)
|
||||
self.saveTask(task)
|
||||
self.logger.info('Task saved ')
|
||||
# TODO send task received message
|
||||
self.pluginManager.processTask(task)
|
||||
|
|
|
@ -12,6 +12,7 @@ class MySamplePlugin(AbstractCommand):
|
|||
|
||||
def handle_task(self):
|
||||
print("This is command 1 ")
|
||||
print("parameter map="+self.task.parameter_map)
|
||||
|
||||
|
||||
def handle_task(task):
|
||||
|
|
Loading…
Reference in a new issue