2016-02-29 10:48:00 +02:00
|
|
|
#!/usr/bin/python3
|
2016-02-01 17:49:17 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
2016-03-08 18:09:26 +02:00
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
2016-02-01 17:49:17 +02:00
|
|
|
|
2016-07-18 18:21:03 +03:00
|
|
|
import json
|
2016-03-23 17:15:27 +02:00
|
|
|
import os
|
|
|
|
import queue
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
2016-07-18 18:21:03 +03:00
|
|
|
from multiprocessing import Process
|
|
|
|
|
2016-08-24 18:04:06 +03:00
|
|
|
from base.scope import Scope
|
2016-07-18 18:21:03 +03:00
|
|
|
from base.agreement.agreement import Agreement
|
2016-06-06 12:27:46 +03:00
|
|
|
from base.command.commander import Commander
|
2016-08-24 18:04:06 +03:00
|
|
|
from base.config.config_manager import ConfigManager
|
|
|
|
from base.database.ahenk_db_service import AhenkDbService
|
|
|
|
from base.deamon.base_daemon import BaseDaemon
|
|
|
|
from base.event.event_manager import EventManager
|
|
|
|
from base.execution.execution_manager import ExecutionManager
|
|
|
|
from base.logger.ahenk_logger import Logger
|
|
|
|
from base.messaging.message_response_queue import MessageResponseQueue
|
|
|
|
from base.messaging.messaging import Messaging
|
|
|
|
from base.messaging.messenger import Messenger
|
2016-04-13 11:55:45 +03:00
|
|
|
from base.plugin.plugin_manager_factory import PluginManagerFactory
|
2016-08-24 18:04:06 +03:00
|
|
|
from base.registration.registration import Registration
|
2016-04-05 17:55:15 +03:00
|
|
|
from base.scheduler.scheduler_factory import SchedulerFactory
|
2016-06-06 12:27:46 +03:00
|
|
|
from base.system.system import System
|
2016-08-24 18:04:06 +03:00
|
|
|
from base.task.task_manager import TaskManager
|
2016-07-22 10:40:41 +03:00
|
|
|
from base.timer.setup_timer import SetupTimer
|
|
|
|
from base.timer.timer import Timer
|
2016-06-27 17:20:26 +03:00
|
|
|
from base.util.util import Util
|
2016-02-01 17:49:17 +02:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon = None
|
2016-02-08 19:03:38 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
class AhenkDaemon(BaseDaemon):
|
|
|
|
"""Ahenk service base class which initializes services and maintains events/commands"""
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def reload():
|
|
|
|
""" docstring"""
|
2016-03-11 17:51:26 +02:00
|
|
|
# reload service here
|
|
|
|
pass
|
2016-03-04 18:01:47 +02:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_logger():
|
|
|
|
""" docstring"""
|
2016-04-05 17:55:15 +03:00
|
|
|
logger = Logger()
|
2016-08-18 21:04:17 +03:00
|
|
|
logger.info('[AhenkDaemon] Log was set')
|
2016-04-05 17:55:15 +03:00
|
|
|
Scope.getInstance().setLogger(logger)
|
|
|
|
return logger
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_config_manager(config_file_path, configfile_folder_path):
|
|
|
|
""" docstring"""
|
|
|
|
config_manager = ConfigManager(config_file_path, configfile_folder_path)
|
|
|
|
config = config_manager.read()
|
2016-04-05 17:55:15 +03:00
|
|
|
Scope.getInstance().setConfigurationManager(config)
|
|
|
|
return config
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_scheduler():
|
|
|
|
""" docstring"""
|
2016-04-05 17:55:15 +03:00
|
|
|
scheduler_ins = SchedulerFactory.get_intstance()
|
|
|
|
scheduler_ins.initialize()
|
|
|
|
Scope.getInstance().set_scheduler(scheduler_ins)
|
|
|
|
sc_thread = threading.Thread(target=scheduler_ins.run)
|
|
|
|
sc_thread.setDaemon(True)
|
|
|
|
sc_thread.start()
|
2016-04-05 19:12:40 +03:00
|
|
|
return scheduler_ins
|
2016-04-05 17:55:15 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_event_manager():
|
|
|
|
""" docstring"""
|
|
|
|
event_manager = EventManager()
|
|
|
|
Scope.getInstance().setEventManager(event_manager)
|
|
|
|
return event_manager
|
2016-04-05 17:55:15 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_ahenk_db():
|
|
|
|
""" docstring"""
|
2016-04-05 17:55:15 +03:00
|
|
|
db_service = AhenkDbService()
|
|
|
|
db_service.connect()
|
|
|
|
db_service.initialize_table()
|
|
|
|
Scope.getInstance().setDbService(db_service)
|
|
|
|
return db_service
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_messaging():
|
|
|
|
""" docstring"""
|
|
|
|
message_manager = Messaging()
|
|
|
|
Scope.getInstance().setMessageManager(message_manager)
|
|
|
|
return message_manager
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init_plugin_manager():
|
|
|
|
""" docstring"""
|
|
|
|
plugin_manager = PluginManagerFactory.get_instance()
|
|
|
|
Scope.getInstance().setPluginManager(plugin_manager)
|
2016-07-18 18:21:03 +03:00
|
|
|
# order changed, problem?
|
2016-08-18 21:04:17 +03:00
|
|
|
plugin_manager.load_plugins()
|
|
|
|
return plugin_manager
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init_task_manager():
|
|
|
|
""" docstring"""
|
|
|
|
task_manager = TaskManager()
|
|
|
|
Scope.getInstance().setTaskManager(task_manager)
|
|
|
|
return task_manager
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init_registration():
|
|
|
|
""" docstring"""
|
2016-04-05 17:55:15 +03:00
|
|
|
registration = Registration()
|
|
|
|
Scope.getInstance().setRegistration(registration)
|
|
|
|
return registration
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_execution_manager():
|
|
|
|
""" docstring"""
|
2016-04-05 17:55:15 +03:00
|
|
|
execution_manager = ExecutionManager()
|
|
|
|
Scope.getInstance().setExecutionManager(execution_manager)
|
|
|
|
return execution_manager
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_messenger():
|
|
|
|
""" docstring"""
|
|
|
|
messenger_ = Messenger()
|
|
|
|
messenger_.connect_to_server()
|
|
|
|
Scope.getInstance().setMessenger(messenger_)
|
|
|
|
return messenger_
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init_message_response_queue():
|
|
|
|
""" docstring"""
|
|
|
|
response_queue = queue.Queue()
|
|
|
|
message_response_queue = MessageResponseQueue(response_queue)
|
|
|
|
message_response_queue.setDaemon(True)
|
|
|
|
message_response_queue.start()
|
|
|
|
Scope.getInstance().setResponseQueue(response_queue)
|
|
|
|
return response_queue
|
2016-04-05 17:55:15 +03:00
|
|
|
|
|
|
|
def check_registration(self):
|
2016-08-18 21:04:17 +03:00
|
|
|
""" docstring"""
|
|
|
|
max_attempt_number = int(System.Hardware.Network.interface_size()) * 3
|
|
|
|
# self.logger.debug()
|
|
|
|
# logger = Scope.getInstance().getLogger()
|
2016-07-22 10:40:41 +03:00
|
|
|
registration = Scope.getInstance().getRegistration()
|
2016-06-21 17:47:25 +03:00
|
|
|
|
2016-04-08 15:54:26 +03:00
|
|
|
try:
|
2016-07-22 10:40:41 +03:00
|
|
|
while registration.is_registered() is False:
|
2016-08-18 21:04:17 +03:00
|
|
|
max_attempt_number -= 1
|
|
|
|
self.logger.debug('[AhenkDaemon] Ahenk is not registered. Attempting for registration')
|
2016-07-22 10:40:41 +03:00
|
|
|
registration.registration_request()
|
2016-08-18 21:04:17 +03:00
|
|
|
if max_attempt_number < 0:
|
|
|
|
self.logger.warning('[AhenkDaemon] Number of Attempting for registration is over')
|
2016-05-06 16:34:58 +03:00
|
|
|
self.registration_failed()
|
|
|
|
break
|
2016-04-08 15:54:26 +03:00
|
|
|
except Exception as e:
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.error('[AhenkDaemon] Registration failed. Error message: {0}'.format(str(e)))
|
2016-05-06 16:34:58 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def shutdown_mode():
|
|
|
|
""" docstring"""
|
2016-07-13 17:39:44 +03:00
|
|
|
scope = Scope().getInstance()
|
|
|
|
plugin_manager = scope.getPluginManager()
|
|
|
|
plugin_manager.process_mode('shutdown')
|
|
|
|
|
2016-05-06 16:34:58 +03:00
|
|
|
def registration_failed(self):
|
2016-08-18 21:04:17 +03:00
|
|
|
""" docstring"""
|
|
|
|
self.logger.error(
|
|
|
|
'[AhenkDaemon] Registration failed. All registration attempts were failed. Ahenk is stopping...')
|
2016-06-21 17:47:25 +03:00
|
|
|
print('Registration failed. Ahenk is stopping..')
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon.stop()
|
2016-04-08 15:54:26 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def reload_plugins():
|
|
|
|
""" docstring"""
|
2016-04-06 17:40:25 +03:00
|
|
|
Scope.getInstance().getPluginManager().reloadPlugins()
|
|
|
|
|
|
|
|
def reload_configuration(self):
|
|
|
|
# Not implemented yet
|
|
|
|
pass
|
|
|
|
|
|
|
|
def reload_messaging(self):
|
2016-04-11 19:27:26 +03:00
|
|
|
# Not implemented yet
|
2016-04-06 17:40:25 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
def reload_logger(self):
|
2016-04-11 19:27:26 +03:00
|
|
|
# Not implemented yet
|
2016-04-06 17:40:25 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
def update_plugin_manager(self):
|
2016-08-18 21:04:17 +03:00
|
|
|
""" docstring"""
|
2016-04-11 19:27:26 +03:00
|
|
|
# TODO destroy plugin manager here
|
2016-04-06 17:40:25 +03:00
|
|
|
self.init_plugin_manager()
|
|
|
|
|
2016-07-22 10:40:41 +03:00
|
|
|
def init_signal_listener(self):
|
2016-08-18 21:04:17 +03:00
|
|
|
""" docstring"""
|
2016-07-22 10:40:41 +03:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGALRM, self.run_command_from_fifo)
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Signal handler is set up')
|
2016-07-22 10:40:41 +03:00
|
|
|
except Exception as e:
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.error('[AhenkDaemon] Signal handler could not set up. Error Message: {0} '.format(str(e)))
|
2016-07-22 10:40:41 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_pid_file():
|
|
|
|
""" docstring"""
|
2016-07-22 10:40:41 +03:00
|
|
|
with open(System.Ahenk.pid_path(), 'w+') as f:
|
|
|
|
f.write(str(os.getpid()))
|
|
|
|
|
2016-08-24 18:04:06 +03:00
|
|
|
@staticmethod
|
|
|
|
def init_fifo_file():
|
|
|
|
""" docstring"""
|
|
|
|
if Util.is_exist(System.Ahenk.fifo_file()):
|
|
|
|
Util.delete_file(System.Ahenk.fifo_file())
|
|
|
|
Util.create_file(System.Ahenk.fifo_file())
|
|
|
|
Util.set_permission(System.Ahenk.fifo_file(), '600')
|
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
def run(self):
|
2016-08-18 21:04:17 +03:00
|
|
|
""" docstring"""
|
2016-03-22 16:43:47 +02:00
|
|
|
print('Ahenk running...')
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
global_scope = Scope()
|
|
|
|
global_scope.setInstance(global_scope)
|
2016-02-25 01:46:27 +02:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
config_file_folder_path = '/etc/ahenk/config.d/'
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
# configuration manager must be first load
|
2016-08-18 21:04:17 +03:00
|
|
|
self.init_config_manager(System.Ahenk.config_path(), config_file_folder_path)
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
# Logger must be second
|
2016-06-06 12:27:46 +03:00
|
|
|
self.logger = self.init_logger()
|
2016-02-08 19:03:38 +02:00
|
|
|
|
2016-07-22 10:40:41 +03:00
|
|
|
self.init_pid_file()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Pid file was created')
|
2016-07-22 10:40:41 +03:00
|
|
|
|
2016-08-24 18:04:06 +03:00
|
|
|
self.init_fifo_file()
|
|
|
|
self.logger.info('[AhenkDaemon] Pid file was created')
|
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_event_manager()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Event Manager was set')
|
2016-03-04 18:01:47 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_ahenk_db()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] DataBase Service was set')
|
2016-03-18 18:45:06 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_messaging()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Message Manager was set')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_plugin_manager()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Plugin Manager was set')
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-06-22 18:20:25 +03:00
|
|
|
self.init_scheduler()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Scheduler was set')
|
2016-06-22 18:20:25 +03:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_task_manager()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Task Manager was set')
|
2016-03-01 12:41:55 +02:00
|
|
|
|
2016-04-08 15:54:26 +03:00
|
|
|
self.init_registration()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Registration was set')
|
2016-03-04 19:23:21 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_execution_manager()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Execution Manager was set')
|
2016-03-08 18:09:26 +02:00
|
|
|
|
2016-04-08 15:54:26 +03:00
|
|
|
self.check_registration()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Ahenk was registered')
|
2016-07-13 17:39:44 +03:00
|
|
|
|
2016-06-20 17:35:59 +03:00
|
|
|
self.messenger = self.init_messenger()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Messenger was set')
|
2016-07-22 10:40:41 +03:00
|
|
|
|
|
|
|
self.init_signal_listener()
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] Signals listeners was set')
|
2016-03-18 18:45:06 +02:00
|
|
|
|
2016-06-27 17:20:26 +03:00
|
|
|
Agreement().agreement_contract_update()
|
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_message_response_queue()
|
2016-03-18 18:45:06 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
# if registration.is_ldap_registered() is False:
|
2016-08-18 21:04:17 +03:00
|
|
|
# logger.debug('[AhenkDaemon] Attempting to registering ldap')
|
2016-03-23 10:04:31 +02:00
|
|
|
# registration.ldap_registration_request() #TODO work on message
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info('[AhenkDaemon] LDAP registration of Ahenk is completed')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-06-20 17:35:59 +03:00
|
|
|
self.messenger.send_direct_message('test')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
while True:
|
|
|
|
time.sleep(1)
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def running_plugin():
|
|
|
|
""" docstring"""
|
2016-07-13 17:39:44 +03:00
|
|
|
scope = Scope().getInstance()
|
|
|
|
plugin_manager = scope.getPluginManager()
|
|
|
|
|
|
|
|
for plugin in plugin_manager.plugins:
|
|
|
|
if plugin.keep_run is True:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2016-06-06 12:27:46 +03:00
|
|
|
def run_command_from_fifo(self, num, stack):
|
2016-08-18 21:04:17 +03:00
|
|
|
""" docstring"""
|
2016-07-27 16:03:32 +03:00
|
|
|
scope = Scope().getInstance()
|
|
|
|
plugin_manager = scope.getPluginManager()
|
|
|
|
message_manager = scope.getMessageManager()
|
|
|
|
messenger = scope.getMessenger()
|
|
|
|
conf_manager = scope.getConfigurationManager()
|
|
|
|
db_service = scope.getDbService()
|
|
|
|
execute_manager = scope.getExecutionManager()
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
event = Commander().get_event()
|
|
|
|
if event is None:
|
|
|
|
break
|
|
|
|
json_data = json.loads(event)
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(
|
|
|
|
'[AhenkDaemon] A problem occurred while loading json. Check json format! Error Message: {0}.'
|
|
|
|
' Event = {1}'.format(str(e), str(event)))
|
|
|
|
return
|
|
|
|
|
|
|
|
if json_data is not None:
|
|
|
|
|
|
|
|
self.logger.debug('[AhenkDaemon] Signal handled')
|
|
|
|
self.logger.debug('[AhenkDaemon] Signal is :{0}'.format(str(json_data['event'])))
|
|
|
|
|
|
|
|
if str(json_data['event']) == 'login':
|
|
|
|
username = json_data['username']
|
|
|
|
display = json_data['display']
|
|
|
|
desktop = json_data['desktop']
|
|
|
|
self.logger.info('[AhenkDaemon] login event is handled for user: {0}'.format(username))
|
|
|
|
login_message = message_manager.login_msg(username)
|
|
|
|
messenger.send_direct_message(login_message)
|
|
|
|
|
|
|
|
agreement = Agreement()
|
|
|
|
agreement_choice = None
|
|
|
|
|
|
|
|
if agreement.check_agreement(username) is not True:
|
|
|
|
self.logger.debug('[AhenkDaemon] User {0} has not accepted agreement.'.format(username))
|
|
|
|
thread_ask = Process(target=agreement.ask, args=(username, display,))
|
|
|
|
thread_ask.start()
|
|
|
|
|
|
|
|
agreement_timeout = conf_manager.get('SESSION', 'agreement_timeout')
|
|
|
|
|
|
|
|
timeout = int(agreement_timeout) # sec
|
|
|
|
timer = time.time()
|
|
|
|
while 1:
|
|
|
|
if thread_ask.is_alive() is False:
|
|
|
|
self.logger.warning('[AhenkDaemon] {0} was answered the question '.format(username))
|
|
|
|
if Agreement().check_agreement(username) is True:
|
|
|
|
self.logger.warning('[AhenkDaemon] Choice of {0} is YES'.format(username))
|
|
|
|
agreement_choice = True
|
|
|
|
break
|
|
|
|
elif Agreement().check_agreement(username) is False:
|
|
|
|
self.logger.warning('[AhenkDaemon] Choice of {0} is NO'.format(username))
|
|
|
|
agreement_choice = False
|
|
|
|
Util.close_session(username)
|
|
|
|
break
|
|
|
|
|
|
|
|
if (time.time() - timer) > timeout:
|
|
|
|
if thread_ask.is_alive():
|
|
|
|
thread_ask.terminate()
|
|
|
|
Util.close_session(username)
|
|
|
|
self.logger.warning(
|
|
|
|
'[AhenkDaemon] Session of {0} was ended because of timeout of contract agreement'.format(
|
|
|
|
username))
|
2016-06-27 17:20:26 +03:00
|
|
|
break
|
2016-08-18 21:04:17 +03:00
|
|
|
time.sleep(1)
|
2016-06-27 17:20:26 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
if agreement_choice is not None:
|
2016-08-24 18:04:06 +03:00
|
|
|
messenger.send_direct_message(
|
|
|
|
message_manager.agreement_answer_msg(username, agreement_choice))
|
2016-08-18 21:04:17 +03:00
|
|
|
else:
|
|
|
|
agreement_choice = True
|
2016-07-25 12:21:23 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
if agreement_choice is True:
|
|
|
|
db_service.delete('session', 'username=\'{0}\''.format(username))
|
2016-07-22 10:40:41 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info(
|
2016-08-24 18:04:06 +03:00
|
|
|
'[AhenkDaemon] Display is {0}, desktop env is {1} for {2}'.format(display, desktop,
|
|
|
|
username))
|
2016-06-27 17:20:26 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
db_service.update('session', scope.getDbService().get_cols('session'),
|
|
|
|
[username, display, desktop, Util.timestamp()])
|
|
|
|
get_policy_message = message_manager.policy_request_msg(username)
|
2016-07-13 17:39:44 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
plugin_manager.process_mode('safe', username)
|
|
|
|
plugin_manager.process_mode('login', username)
|
2016-07-13 17:39:44 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
kward = dict()
|
|
|
|
kward['timeout_args'] = username
|
|
|
|
kward['checker_args'] = username
|
2016-07-13 17:39:44 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
SetupTimer.start(Timer(timeout=System.Ahenk.get_policy_timeout(),
|
|
|
|
timeout_function=execute_manager.execute_default_policy,
|
|
|
|
checker_func=execute_manager.is_policy_executed, kwargs=kward))
|
2016-07-18 18:21:03 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.info(
|
|
|
|
'[AhenkDaemon] Requesting updated policies from Lider. If Ahenk could not reach updated '
|
|
|
|
'policies in {0} sec, booked policies will be executed'.format(
|
|
|
|
System.Ahenk.get_policy_timeout()))
|
|
|
|
messenger.send_direct_message(get_policy_message)
|
2016-07-18 18:21:03 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
elif str(json_data['event']) == 'logout':
|
|
|
|
username = json_data['username']
|
|
|
|
db_service.delete('session', 'username=\'{0}\''.format(username))
|
|
|
|
execute_manager.remove_user_executed_policy_dict(username)
|
|
|
|
# TODO delete all user records while initializing
|
|
|
|
self.logger.info('[AhenkDaemon] logout event is handled for user: {0}'.format(username))
|
|
|
|
logout_message = message_manager.logout_msg(username)
|
|
|
|
messenger.send_direct_message(logout_message)
|
2016-07-18 18:21:03 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
plugin_manager.process_mode('logout', username)
|
|
|
|
plugin_manager.process_mode('safe', username)
|
2016-07-18 18:21:03 +03:00
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
elif str(json_data['event']) == 'send':
|
|
|
|
self.logger.info('[AhenkDaemon] Sending message over ahenkd command. Response Message: {0}'.format(
|
|
|
|
json.dumps(json_data['message'])))
|
|
|
|
message = json.dumps(json_data['message'])
|
|
|
|
messenger.send_direct_message(message)
|
|
|
|
|
|
|
|
elif str(json_data['event']) == 'load':
|
|
|
|
plugin_name = str(json_data['plugins'])
|
|
|
|
|
|
|
|
if plugin_name == 'all':
|
|
|
|
self.logger.debug('[AhenkDaemon] All plugins are loading to ahenk')
|
|
|
|
plugin_manager.load_plugins()
|
|
|
|
else:
|
|
|
|
for p_name in plugin_name.split(','):
|
|
|
|
self.logger.debug('[AhenkDaemon] {0} plugin is loading to ahenk'.format(p_name))
|
|
|
|
plugin_manager.load_single_plugin(p_name)
|
|
|
|
|
|
|
|
elif str(json_data['event']) == 'reload':
|
|
|
|
plugin_name = str(json_data['plugins'])
|
|
|
|
|
|
|
|
if plugin_name == 'all':
|
|
|
|
self.logger.debug('[AhenkDaemon] All plugins are reloading to ahenk')
|
|
|
|
plugin_manager.reload_plugins()
|
|
|
|
else:
|
|
|
|
for p_name in plugin_name.split(','):
|
|
|
|
self.logger.debug('[AhenkDaemon] {0} plugin is reloading to ahenk'.format(p_name))
|
|
|
|
plugin_manager.reload_single_plugin(p_name)
|
|
|
|
|
|
|
|
elif str(json_data['event']) == 'remove':
|
|
|
|
plugin_name = str(json_data['plugins'])
|
|
|
|
|
|
|
|
if plugin_name == 'all':
|
|
|
|
self.logger.debug('[AhenkDaemon] All plugins are removing from ahenk')
|
|
|
|
plugin_manager.remove_plugins()
|
|
|
|
else:
|
|
|
|
for p_name in plugin_name.split(','):
|
|
|
|
self.logger.debug('[AhenkDaemon] {0} plugin is removing from ahenk'.format(p_name))
|
|
|
|
plugin_manager.remove_single_plugin(p_name)
|
|
|
|
|
|
|
|
elif str(json_data['event']) == 'stop':
|
|
|
|
self.shutdown_mode()
|
|
|
|
self.logger.info('[AhenkDaemon] Shutdown mode activated.')
|
|
|
|
|
|
|
|
# TODO timeout
|
|
|
|
while self.running_plugin() is False:
|
|
|
|
self.logger.debug('[AhenkDaemon] Waiting for progress of plugins...')
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
2016-08-24 18:04:06 +03:00
|
|
|
if Util.is_exist(System.Ahenk.fifo_file()):
|
|
|
|
Util.delete_file(System.Ahenk.fifo_file())
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon.stop()
|
2016-07-18 18:21:03 +03:00
|
|
|
else:
|
2016-08-18 21:04:17 +03:00
|
|
|
self.logger.error('[AhenkDaemon] Unknown command error. Command:' + json_data['event'])
|
|
|
|
self.logger.debug('[AhenkDaemon] Processing of handled event is completed')
|
2016-03-22 16:43:47 +02:00
|
|
|
|
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon = AhenkDaemon(System.Ahenk.pid_path())
|
2016-03-29 16:11:39 +03:00
|
|
|
try:
|
2016-07-13 17:39:44 +03:00
|
|
|
if len(sys.argv) == 2 and (sys.argv[1] in ('start', 'stop', 'restart', 'status')):
|
2016-03-29 16:11:39 +03:00
|
|
|
if sys.argv[1] == 'start':
|
2016-06-06 12:27:46 +03:00
|
|
|
if System.Ahenk.is_running() is True:
|
2016-08-18 21:04:17 +03:00
|
|
|
print('There is already running Ahenk service. It will be killed.[{0}]'.format(
|
|
|
|
str(System.Ahenk.get_pid_number())))
|
2016-06-06 12:27:46 +03:00
|
|
|
System.Process.kill_by_pid(int(System.Ahenk.get_pid_number()))
|
|
|
|
else:
|
|
|
|
print('Ahenk starting...')
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon.run()
|
2016-03-29 16:11:39 +03:00
|
|
|
elif sys.argv[1] == 'stop':
|
2016-06-06 12:27:46 +03:00
|
|
|
if System.Ahenk.is_running() is True:
|
2016-07-13 17:39:44 +03:00
|
|
|
raise SystemExit
|
2016-06-06 12:27:46 +03:00
|
|
|
else:
|
|
|
|
print('Ahenk not working!')
|
2016-03-29 16:11:39 +03:00
|
|
|
elif sys.argv[1] == 'restart':
|
2016-06-06 12:27:46 +03:00
|
|
|
if System.Ahenk.is_running() is True:
|
|
|
|
print('Ahenk restarting...')
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon.restart()
|
2016-06-06 12:27:46 +03:00
|
|
|
else:
|
|
|
|
print('Ahenk starting...')
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon.run()
|
2016-03-29 16:11:39 +03:00
|
|
|
elif sys.argv[1] == 'status':
|
2016-06-06 12:27:46 +03:00
|
|
|
print(Commander().status())
|
2016-03-29 16:11:39 +03:00
|
|
|
else:
|
2016-06-06 12:27:46 +03:00
|
|
|
print('Unknown command. Usage : %s start|stop|restart|status|clean' % sys.argv[0])
|
2016-03-29 16:11:39 +03:00
|
|
|
sys.exit(2)
|
2016-03-22 16:43:47 +02:00
|
|
|
else:
|
2016-06-06 12:27:46 +03:00
|
|
|
result = Commander().set_event(sys.argv)
|
|
|
|
if result is None:
|
|
|
|
print('Usage : {0} start|stop|restart|status|clean'.format(sys.argv[0]))
|
|
|
|
sys.exit(2)
|
|
|
|
elif result is True:
|
|
|
|
if System.Ahenk.is_running() is True:
|
|
|
|
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
|
2016-03-29 16:11:39 +03:00
|
|
|
except(KeyboardInterrupt, SystemExit):
|
2016-06-06 12:27:46 +03:00
|
|
|
if System.Ahenk.is_running() is True:
|
2016-07-13 17:39:44 +03:00
|
|
|
print('Ahenk stopping...')
|
2016-07-18 18:21:03 +03:00
|
|
|
result = Commander().set_event([None, 'stop'])
|
2016-07-13 17:39:44 +03:00
|
|
|
if result is True:
|
|
|
|
if System.Ahenk.is_running() is True:
|
|
|
|
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
|
|
|
|
else:
|
2016-08-18 21:04:17 +03:00
|
|
|
ahenk_daemon.stop()
|