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-03-23 17:15:27 +02:00
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
import queue
|
|
|
|
import signal
|
2016-05-06 16:34:58 +03:00
|
|
|
import subprocess
|
2016-03-23 17:15:27 +02:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
from base.Scope import Scope
|
2016-02-08 19:03:38 +02:00
|
|
|
from base.config.ConfigManager import ConfigManager
|
2016-03-23 17:15:27 +02:00
|
|
|
from base.database.AhenkDbService import AhenkDbService
|
2016-02-25 17:24:07 +02:00
|
|
|
from base.deamon.BaseDeamon import BaseDaemon
|
2016-03-23 17:15:27 +02:00
|
|
|
from base.event.EventManager import EventManager
|
2016-03-08 18:09:26 +02:00
|
|
|
from base.execution.ExecutionManager import ExecutionManager
|
2016-03-23 17:15:27 +02:00
|
|
|
from base.logger.AhenkLogger import Logger
|
2016-03-02 17:20:42 +02:00
|
|
|
from base.messaging.MessageResponseQueue import MessageResponseQueue
|
2016-03-23 17:15:27 +02:00
|
|
|
from base.messaging.Messager import Messager
|
|
|
|
from base.messaging.Messaging import Messaging
|
2016-04-13 11:55:45 +03:00
|
|
|
from base.plugin.plugin_manager_factory import PluginManagerFactory
|
2016-03-23 17:15:27 +02:00
|
|
|
from base.registration.Registration import Registration
|
2016-04-05 17:55:15 +03:00
|
|
|
from base.scheduler.scheduler_factory import SchedulerFactory
|
2016-04-13 11:55:45 +03:00
|
|
|
from base.task.TaskManager import TaskManager
|
2016-02-01 17:49:17 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
pidfilePath = '/var/run/ahenk.pid'
|
2016-02-08 19:03:38 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-02-25 17:24:07 +02:00
|
|
|
class AhenkDeamon(BaseDaemon):
|
2016-03-11 17:51:26 +02:00
|
|
|
"""docstring for AhenkDeamon"""
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
def reload(self):
|
2016-03-11 17:51:26 +02:00
|
|
|
# reload service here
|
|
|
|
pass
|
2016-03-04 18:01:47 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
def init_logger(self):
|
|
|
|
logger = Logger()
|
|
|
|
logger.info('[AhenkDeamon] Log was set')
|
|
|
|
Scope.getInstance().setLogger(logger)
|
|
|
|
return logger
|
|
|
|
|
|
|
|
def init_config_manager(self, configFilePath, configfileFolderPath):
|
|
|
|
configManager = ConfigManager(configFilePath, configfileFolderPath)
|
|
|
|
config = configManager.read()
|
|
|
|
Scope.getInstance().setConfigurationManager(config)
|
|
|
|
return config
|
|
|
|
|
|
|
|
def init_scheduler(self):
|
|
|
|
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
|
|
|
|
|
|
|
def init_event_manager(self):
|
|
|
|
eventManager = EventManager()
|
|
|
|
Scope.getInstance().setEventManager(eventManager)
|
|
|
|
return eventManager
|
|
|
|
|
|
|
|
def init_ahenk_db(self):
|
|
|
|
db_service = AhenkDbService()
|
|
|
|
db_service.connect()
|
|
|
|
db_service.initialize_table()
|
|
|
|
Scope.getInstance().setDbService(db_service)
|
|
|
|
return db_service
|
|
|
|
|
|
|
|
def init_messaging(self):
|
|
|
|
messageManager = Messaging()
|
|
|
|
Scope.getInstance().setMessageManager(messageManager)
|
|
|
|
return messageManager
|
|
|
|
|
|
|
|
def init_plugin_manager(self):
|
2016-04-06 17:40:25 +03:00
|
|
|
pluginManager = PluginManagerFactory.get_instance()
|
2016-04-05 17:55:15 +03:00
|
|
|
pluginManager.loadPlugins()
|
|
|
|
Scope.getInstance().setPluginManager(pluginManager)
|
|
|
|
return pluginManager
|
|
|
|
|
|
|
|
def init_task_manager(self):
|
|
|
|
taskManager = TaskManager()
|
|
|
|
Scope.getInstance().setTaskManager(taskManager)
|
|
|
|
return taskManager
|
|
|
|
|
|
|
|
def init_registration(self):
|
|
|
|
registration = Registration()
|
|
|
|
Scope.getInstance().setRegistration(registration)
|
|
|
|
return registration
|
|
|
|
|
|
|
|
def init_execution_manager(self):
|
|
|
|
execution_manager = ExecutionManager()
|
|
|
|
Scope.getInstance().setExecutionManager(execution_manager)
|
|
|
|
return execution_manager
|
|
|
|
|
|
|
|
def init_messager(self):
|
2016-05-06 16:34:58 +03:00
|
|
|
messenger = Messager()
|
|
|
|
messanger_thread = threading.Thread(target=messenger.connect_to_server)
|
2016-04-05 17:55:15 +03:00
|
|
|
messanger_thread.start()
|
|
|
|
|
2016-05-06 16:34:58 +03:00
|
|
|
while messenger.is_connected() is False:
|
2016-04-05 17:55:15 +03:00
|
|
|
time.sleep(1)
|
|
|
|
time.sleep(5)
|
|
|
|
|
2016-05-06 16:34:58 +03:00
|
|
|
Scope.getInstance().setMessager(messenger)
|
|
|
|
return messenger
|
2016-04-05 17:55:15 +03:00
|
|
|
|
|
|
|
def init_message_response_queue(self):
|
|
|
|
responseQueue = queue.Queue()
|
|
|
|
messageResponseQueue = MessageResponseQueue(responseQueue)
|
|
|
|
messageResponseQueue.setDaemon(True)
|
|
|
|
messageResponseQueue.start()
|
|
|
|
Scope.getInstance().setResponseQueue(responseQueue)
|
|
|
|
return responseQueue
|
|
|
|
|
|
|
|
def check_registration(self):
|
2016-05-06 16:34:58 +03:00
|
|
|
# TODO get number of attemption
|
|
|
|
max_attemp_number = 50
|
|
|
|
logger = Scope.getInstance().getLogger()
|
2016-04-08 15:54:26 +03:00
|
|
|
try:
|
|
|
|
while Scope.getInstance().getRegistration().is_registered() is False:
|
2016-05-06 16:34:58 +03:00
|
|
|
max_attemp_number -= 1
|
|
|
|
logger.debug('[AhenkDeamon] Ahenk is not registered. Attempting for registration')
|
2016-04-08 15:54:26 +03:00
|
|
|
Scope.getInstance().getRegistration().registration_request()
|
2016-05-06 16:34:58 +03:00
|
|
|
|
|
|
|
if max_attemp_number < 0:
|
|
|
|
logger.warning('[AhenkDeamon] Number of Attempting for registration is over')
|
|
|
|
self.registration_failed()
|
|
|
|
break
|
2016-04-08 15:54:26 +03:00
|
|
|
except Exception as e:
|
2016-05-06 16:34:58 +03:00
|
|
|
logger.error('[AhenkDeamon] Registration failed. Error message: {}'.format(str(e)))
|
|
|
|
|
|
|
|
def registration_failed(self):
|
|
|
|
# TODO registration fail protocol implement
|
|
|
|
pass
|
2016-04-08 15:54:26 +03:00
|
|
|
|
2016-04-06 17:40:25 +03:00
|
|
|
def reload_plugins(self):
|
|
|
|
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-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-03-11 17:51:26 +02:00
|
|
|
def run(self):
|
2016-03-22 16:43:47 +02:00
|
|
|
print('Ahenk running...')
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
globalscope = Scope()
|
|
|
|
globalscope.setInstance(globalscope)
|
2016-02-25 01:46:27 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
configFilePath = '/etc/ahenk/ahenk.conf'
|
|
|
|
configfileFolderPath = '/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-04-05 17:55:15 +03:00
|
|
|
self.init_config_manager(configFilePath, configfileFolderPath)
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
# Logger must be second
|
2016-04-05 17:55:15 +03:00
|
|
|
logger = self.init_logger()
|
2016-02-08 19:03:38 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_event_manager()
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Event Manager was set')
|
2016-03-04 18:01:47 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_ahenk_db()
|
|
|
|
logger.info('[AhenkDeamon] DataBase Service was set')
|
2016-03-18 18:45:06 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_messaging()
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] 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-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Plugin Manager was set')
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_task_manager()
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Task Manager was set')
|
2016-03-01 12:41:55 +02:00
|
|
|
|
2016-04-08 15:54:26 +03:00
|
|
|
self.init_registration()
|
2016-03-23 17:15:27 +02:00
|
|
|
logger.info('[AhenkDeamon] Registration was set')
|
2016-03-04 19:23:21 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_execution_manager()
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Execution Manager was set')
|
2016-03-08 18:09:26 +02:00
|
|
|
|
2016-04-08 15:54:26 +03:00
|
|
|
self.check_registration()
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Ahenk is registered')
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
messager = self.init_messager()
|
|
|
|
logger.info('[AhenkDeamon] Messager was set')
|
2016-03-18 18:45:06 +02:00
|
|
|
|
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-03-23 10:04:31 +02:00
|
|
|
# logger.debug('[AhenkDeamon] Attempting to registering ldap')
|
|
|
|
# registration.ldap_registration_request() #TODO work on message
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] LDAP registration of Ahenk is completed')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
# TODO###############
|
2016-03-22 16:43:47 +02:00
|
|
|
cnfg = configparser.ConfigParser()
|
|
|
|
cnfg.add_section('PID')
|
2016-03-23 17:15:27 +02:00
|
|
|
cnfg.set('PID', 'pid_number', str(os.getpid()))
|
2016-03-18 18:45:06 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
with open(pidfilePath, 'w') as config_file:
|
|
|
|
cnfg.write(config_file)
|
2016-03-23 17:15:27 +02:00
|
|
|
# TODO##############
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGALRM, self.signal_handler)
|
|
|
|
logger.info('[AhenkDeamon] Signal handler is set up')
|
|
|
|
except Exception as e:
|
2016-03-23 17:15:27 +02:00
|
|
|
logger.error('[AhenkDeamon] Signal handler could not set up :' + e.errno + '-' + e.strerror)
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
messager.send_direct_message('test')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
while True:
|
2016-05-06 16:34:58 +03:00
|
|
|
if messager.ping_lider() is False:
|
|
|
|
Scope.getInstance().getLogger().warning('[AhenkDeamon] Connection is lost. Ahenk is trying for reconnection')
|
2016-05-12 17:35:47 +03:00
|
|
|
messager = self.init_messager()
|
2016-03-22 16:43:47 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
def signal_handler(self, num, stack):
|
2016-05-06 16:34:58 +03:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
# TODO######
|
2016-03-22 16:43:47 +02:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config._interpolation = configparser.ExtendedInterpolation()
|
|
|
|
config.read(pidfilePath)
|
2016-03-23 17:15:27 +02:00
|
|
|
event = config.get('PID', 'event')
|
|
|
|
# TODO######
|
2016-03-22 16:43:47 +02:00
|
|
|
|
|
|
|
params = event.split()
|
|
|
|
scope = Scope().getInstance()
|
|
|
|
logger = scope.getLogger()
|
2016-05-09 17:50:12 +03:00
|
|
|
plugin_manager = scope.getPluginManager()
|
2016-03-22 16:43:47 +02:00
|
|
|
|
2016-05-06 16:34:58 +03:00
|
|
|
message_manager = scope.getMessageManager()
|
|
|
|
messenger = scope.getMessager()
|
|
|
|
|
|
|
|
logger.debug('[AhenkDeamon] Signal handled')
|
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
if 'login' == str(params[0]):
|
2016-05-06 16:34:58 +03:00
|
|
|
logger.debug('[AhenkDeamon] Signal is :{}'.format(str(params[0])))
|
|
|
|
login_message = message_manager.login_msg(params[1])
|
2016-05-12 17:35:47 +03:00
|
|
|
# messenger.send_direct_message(login_message)
|
2016-05-06 16:34:58 +03:00
|
|
|
get_policy_message = message_manager.policy_request_msg(params[1])
|
|
|
|
messenger.send_direct_message(get_policy_message)
|
2016-03-23 17:15:27 +02:00
|
|
|
logger.debug('[AhenkDeamon] login event is handled for user:' + params[1])
|
2016-03-22 16:43:47 +02:00
|
|
|
elif 'logout' == str(params[0]):
|
2016-05-06 16:34:58 +03:00
|
|
|
logger.debug('[AhenkDeamon] Signal is {}'.format(str(params[0])))
|
|
|
|
message = message_manager.logout_msg(params[1])
|
|
|
|
messenger.send_direct_message(message)
|
2016-05-09 17:50:12 +03:00
|
|
|
plugin_manager.process_safe_mode(str(params[1]))
|
2016-03-23 17:15:27 +02:00
|
|
|
logger.debug('[AhenkDeamon] logout event is handled for user:' + params[1])
|
2016-03-29 16:11:39 +03:00
|
|
|
elif 'exit' == str(params[0]):
|
2016-05-06 16:34:58 +03:00
|
|
|
logger.debug('[AhenkDeamon] Signal is {}'.format(str(params[0])))
|
|
|
|
messenger.disconnect()
|
2016-04-11 19:27:26 +03:00
|
|
|
# TODO kill thread
|
2016-05-06 16:34:58 +03:00
|
|
|
subprocess.Popen('kill -9 ' + get_pid_number(), shell=True)
|
2016-03-29 16:11:39 +03:00
|
|
|
print('stopping ahenk')
|
2016-03-22 16:43:47 +02:00
|
|
|
else:
|
2016-03-23 17:15:27 +02:00
|
|
|
logger.error('[AhenkDeamon] Unknown command error. Command:' + params[0])
|
2016-03-04 19:23:21 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.debug('[AhenkDeamon] Processing of handled event is completed')
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
def get_pid_number():
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config._interpolation = configparser.ExtendedInterpolation()
|
|
|
|
config.read(pidfilePath)
|
|
|
|
return config.get('PID', 'pid_number')
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
def set_event(event_param):
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config._interpolation = configparser.ExtendedInterpolation()
|
|
|
|
config.read(pidfilePath)
|
|
|
|
config.set('PID', 'event', event_param)
|
2016-03-22 16:43:47 +02:00
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
with open(pidfilePath, 'w') as config_file:
|
|
|
|
config.write(config_file)
|
2016-03-22 16:43:47 +02:00
|
|
|
|
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
2016-03-22 16:43:47 +02:00
|
|
|
else:
|
2016-03-29 16:11:39 +03:00
|
|
|
print('Usage : %s start|stop|restart|status' % sys.argv[0])
|
2016-03-11 17:51:26 +02:00
|
|
|
sys.exit(2)
|
2016-03-29 16:11:39 +03:00
|
|
|
except(KeyboardInterrupt, SystemExit):
|
|
|
|
if str(os.getpid()) == get_pid_number():
|
|
|
|
set_event('exit true')
|
|
|
|
os.kill(int(get_pid_number()), signal.SIGALRM)
|