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-02-08 19:03:38 +02:00
|
|
|
from base.config.ConfigManager import ConfigManager
|
2016-02-25 17:24:07 +02:00
|
|
|
from base.deamon.BaseDeamon import BaseDaemon
|
|
|
|
from base.logger.AhenkLogger import Logger
|
2016-02-21 03:56:08 +02:00
|
|
|
from base.Scope import Scope
|
2016-03-07 17:59:02 +02:00
|
|
|
from base.messaging.Messaging import Messaging
|
2016-03-18 18:45:06 +02:00
|
|
|
from base.messaging.Messager import Messager
|
2016-03-08 18:09:26 +02:00
|
|
|
from base.execution.ExecutionManager import ExecutionManager
|
2016-03-02 18:31:29 +02:00
|
|
|
from base.registration.Registration import Registration
|
2016-03-02 17:20:42 +02:00
|
|
|
from base.messaging.MessageResponseQueue import MessageResponseQueue
|
2016-03-04 18:01:47 +02:00
|
|
|
from base.event.EventManager import EventManager
|
2016-03-02 17:20:42 +02:00
|
|
|
from base.plugin.PluginManager import PluginManager
|
|
|
|
from base.task.TaskManager import TaskManager
|
2016-03-18 18:45:06 +02:00
|
|
|
from base.database.AhenkDbService import AhenkDbService
|
2016-03-22 16:43:47 +02:00
|
|
|
import threading, time, sys, os, signal, configparser
|
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-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-03-22 16:43:47 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
def reload(self,msg):
|
|
|
|
# reload service here
|
|
|
|
pass
|
2016-03-04 18:01:47 +02:00
|
|
|
|
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-11 17:51:26 +02:00
|
|
|
configFilePath='/etc/ahenk/ahenk.conf'
|
|
|
|
configfileFolderPath='/etc/ahenk/config.d/'
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
#configuration manager must be first load
|
|
|
|
configManager = ConfigManager(configFilePath,configfileFolderPath)
|
|
|
|
config = configManager.read()
|
|
|
|
globalscope.setConfigurationManager(config)
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
# Logger must be second
|
|
|
|
logger = Logger()
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Log was set')
|
2016-03-11 17:51:26 +02:00
|
|
|
globalscope.setLogger(logger)
|
2016-02-08 19:03:38 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
eventManager = EventManager()
|
|
|
|
globalscope.setEventManager(eventManager)
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Event Manager was set')
|
2016-03-04 18:01:47 +02:00
|
|
|
|
2016-03-18 18:45:06 +02:00
|
|
|
db_service=AhenkDbService()
|
|
|
|
db_service.connect()
|
|
|
|
db_service.initialize_table()
|
|
|
|
globalscope.setDbService(db_service)
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Data Base Service was set')
|
2016-03-18 18:45:06 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
messageManager = Messaging()
|
|
|
|
globalscope.setMessageManager(messageManager)
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Message Manager was set')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
pluginManager = PluginManager()
|
|
|
|
pluginManager.loadPlugins()
|
|
|
|
globalscope.setPluginManager(pluginManager)
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Plugin Manager was set')
|
2016-02-21 03:56:08 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
taskManager = TaskManager()
|
|
|
|
globalscope.setTaskManager(taskManager)
|
|
|
|
logger.info('[AhenkDeamon] Task Manager was set')
|
2016-03-01 12:41:55 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
registration=Registration()
|
|
|
|
globalscope.setRegistration(registration)
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Registration was set')
|
2016-03-04 19:23:21 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
execution_manager=ExecutionManager()
|
|
|
|
globalscope.setExecutionManager(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-03-04 19:23:21 +02:00
|
|
|
|
2016-03-18 18:45:06 +02:00
|
|
|
#TODO restrict number of attemption
|
2016-03-11 17:51:26 +02:00
|
|
|
while registration.is_registered() is False:
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.debug('[AhenkDeamon] Attempting to register')
|
2016-03-11 17:51:26 +02:00
|
|
|
registration.registration_request()
|
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Ahenk is registered')
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-03-18 18:45:06 +02:00
|
|
|
messager = Messager()
|
|
|
|
messanger_thread = threading.Thread(target=messager.connect_to_server)
|
|
|
|
messanger_thread.start()
|
|
|
|
|
|
|
|
while(messager.is_connected() is False):
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
globalscope.setMessager(messager)
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.info('[AhenkDeamon] Messager was set')
|
2016-03-11 17:51:26 +02:00
|
|
|
|
|
|
|
if registration.is_ldap_registered() is False:
|
2016-03-22 16:43:47 +02:00
|
|
|
logger.debug('[AhenkDeamon] Attempting to registering ldap')
|
2016-03-18 18:45:06 +02:00
|
|
|
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-22 16:43:47 +02:00
|
|
|
#TODO###############
|
|
|
|
cnfg = configparser.ConfigParser()
|
|
|
|
cnfg.add_section('PID')
|
|
|
|
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)
|
|
|
|
#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:
|
|
|
|
logger.error('[AhenkDeamon] Signal handler could not set up :'+e.errno+'-'+e.strerror)
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-22 16:43:47 +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:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
#request policies
|
|
|
|
#logger.info('[AhenkDeamon] Requesting policies...')
|
|
|
|
#messager.send_direct_message(messageManager.policy_request_msg())
|
2016-03-07 12:55:14 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
"""
|
|
|
|
this is must be created after message services
|
|
|
|
responseQueue = queue.Queue()
|
|
|
|
messageResponseQueue = MessageResponseQueue(responseQueue)
|
|
|
|
messageResponseQueue.setDaemon(True)
|
|
|
|
messageResponseQueue.start()
|
|
|
|
globalscope.setResponseQueue(responseQueue)
|
|
|
|
"""
|
2016-03-01 12:41:55 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
def signal_handler (self, num, stack):
|
|
|
|
|
|
|
|
#TODO######
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config._interpolation = configparser.ExtendedInterpolation()
|
|
|
|
config.read(pidfilePath)
|
|
|
|
event=config.get('PID', 'event')
|
|
|
|
#TODO######
|
|
|
|
|
|
|
|
params = event.split()
|
|
|
|
scope = Scope().getInstance()
|
|
|
|
logger = scope.getLogger()
|
|
|
|
|
|
|
|
if 'login' == str(params[0]):
|
|
|
|
message = scope.getMessageManager().login_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])
|
|
|
|
else:
|
|
|
|
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-22 16:43:47 +02:00
|
|
|
if __name__ == '__main__':
|
2016-03-11 17:51:26 +02:00
|
|
|
|
|
|
|
ahenkdaemon = AhenkDeamon(pidfilePath)
|
|
|
|
|
|
|
|
if len(sys.argv) == 2:
|
2016-03-22 16:43:47 +02:00
|
|
|
if sys.argv[1] == 'start':
|
|
|
|
print('starting')
|
2016-03-11 17:51:26 +02:00
|
|
|
ahenkdaemon.run()
|
|
|
|
elif sys.argv[1] == 'stop':
|
|
|
|
ahenkdaemon.stop()
|
|
|
|
elif sys.argv[1] == 'restart':
|
|
|
|
ahenkdaemon.restart()
|
|
|
|
elif sys.argv[1] == 'status':
|
2016-03-22 16:43:47 +02:00
|
|
|
print('status')
|
2016-03-11 17:51:26 +02:00
|
|
|
else:
|
2016-03-22 16:43:47 +02:00
|
|
|
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]))
|
|
|
|
|
|
|
|
#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])
|
2016-03-11 17:51:26 +02:00
|
|
|
sys.exit(2)
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
2016-03-22 16:43:47 +02:00
|
|
|
print('Usage : %s start|stop|restart|status' % sys.argv[0])
|
2016-03-11 17:51:26 +02:00
|
|
|
sys.exit(2)
|
2016-03-22 16:43:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|