mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-29 13:12:20 +03:00
handling events with parameters while running main ahenk process.Sending login-logout messages according to handled events
This commit is contained in:
parent
91b752d97e
commit
104530c62b
2 changed files with 98 additions and 43 deletions
131
opt/ahenk/ahenkd.py
Normal file → Executable file
131
opt/ahenk/ahenkd.py
Normal file → Executable file
|
@ -16,18 +16,20 @@ from base.event.EventManager import EventManager
|
||||||
from base.plugin.PluginManager import PluginManager
|
from base.plugin.PluginManager import PluginManager
|
||||||
from base.task.TaskManager import TaskManager
|
from base.task.TaskManager import TaskManager
|
||||||
from base.database.AhenkDbService import AhenkDbService
|
from base.database.AhenkDbService import AhenkDbService
|
||||||
import threading,time,sys
|
import threading, time, sys, os, signal, configparser
|
||||||
|
|
||||||
|
pidfilePath = '/var/run/ahenk.pid'
|
||||||
|
|
||||||
class AhenkDeamon(BaseDaemon):
|
class AhenkDeamon(BaseDaemon):
|
||||||
"""docstring for AhenkDeamon"""
|
"""docstring for AhenkDeamon"""
|
||||||
|
|
||||||
|
|
||||||
def reload(self,msg):
|
def reload(self,msg):
|
||||||
# reload service here
|
# reload service here
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
print ("Ahenk running...")
|
print('Ahenk running...')
|
||||||
|
|
||||||
globalscope = Scope()
|
globalscope = Scope()
|
||||||
globalscope.setInstance(globalscope)
|
globalscope.setInstance(globalscope)
|
||||||
|
@ -42,47 +44,47 @@ class AhenkDeamon(BaseDaemon):
|
||||||
|
|
||||||
# Logger must be second
|
# Logger must be second
|
||||||
logger = Logger()
|
logger = Logger()
|
||||||
logger.info("[AhenkDeamon] Log was set")
|
logger.info('[AhenkDeamon] Log was set')
|
||||||
globalscope.setLogger(logger)
|
globalscope.setLogger(logger)
|
||||||
|
|
||||||
eventManager = EventManager()
|
eventManager = EventManager()
|
||||||
globalscope.setEventManager(eventManager)
|
globalscope.setEventManager(eventManager)
|
||||||
logger.info("[AhenkDeamon] Event Manager was set")
|
logger.info('[AhenkDeamon] Event Manager was set')
|
||||||
|
|
||||||
db_service=AhenkDbService()
|
db_service=AhenkDbService()
|
||||||
db_service.connect()
|
db_service.connect()
|
||||||
db_service.initialize_table()
|
db_service.initialize_table()
|
||||||
globalscope.setDbService(db_service)
|
globalscope.setDbService(db_service)
|
||||||
logger.info("[AhenkDeamon] Data Base Service was set")
|
logger.info('[AhenkDeamon] Data Base Service was set')
|
||||||
|
|
||||||
messageManager = Messaging()
|
messageManager = Messaging()
|
||||||
globalscope.setMessageManager(messageManager)
|
globalscope.setMessageManager(messageManager)
|
||||||
logger.info("[AhenkDeamon] Message Manager was set")
|
logger.info('[AhenkDeamon] Message Manager was set')
|
||||||
|
|
||||||
pluginManager = PluginManager()
|
pluginManager = PluginManager()
|
||||||
pluginManager.loadPlugins()
|
pluginManager.loadPlugins()
|
||||||
globalscope.setPluginManager(pluginManager)
|
globalscope.setPluginManager(pluginManager)
|
||||||
logger.info("[AhenkDeamon] Plugin Manager was set")
|
logger.info('[AhenkDeamon] Plugin Manager was set')
|
||||||
|
|
||||||
taskManger = TaskManager()
|
taskManager = TaskManager()
|
||||||
globalscope.setTaskManager(taskManger)
|
globalscope.setTaskManager(taskManager)
|
||||||
logger.info("[AhenkDeamon] Task Manager was set")
|
logger.info('[AhenkDeamon] Task Manager was set')
|
||||||
|
|
||||||
registration=Registration()
|
registration=Registration()
|
||||||
globalscope.setRegistration(registration)
|
globalscope.setRegistration(registration)
|
||||||
logger.info("[AhenkDeamon] Registration was set")
|
logger.info('[AhenkDeamon] Registration was set')
|
||||||
|
|
||||||
execution_manager=ExecutionManager()
|
execution_manager=ExecutionManager()
|
||||||
globalscope.setExecutionManager(execution_manager)
|
globalscope.setExecutionManager(execution_manager)
|
||||||
logger.info("[AhenkDeamon] Execution Manager was set")
|
logger.info('[AhenkDeamon] Execution Manager was set')
|
||||||
|
|
||||||
|
|
||||||
#TODO restrict number of attemption
|
#TODO restrict number of attemption
|
||||||
while registration.is_registered() is False:
|
while registration.is_registered() is False:
|
||||||
logger.debug("[AhenkDeamon] Attempting to register")
|
logger.debug('[AhenkDeamon] Attempting to register')
|
||||||
registration.registration_request()
|
registration.registration_request()
|
||||||
|
|
||||||
logger.info("[AhenkDeamon] Ahenk is registered")
|
logger.info('[AhenkDeamon] Ahenk is registered')
|
||||||
|
|
||||||
messager = Messager()
|
messager = Messager()
|
||||||
messanger_thread = threading.Thread(target=messager.connect_to_server)
|
messanger_thread = threading.Thread(target=messager.connect_to_server)
|
||||||
|
@ -92,27 +94,37 @@ class AhenkDeamon(BaseDaemon):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
globalscope.setMessager(messager)
|
globalscope.setMessager(messager)
|
||||||
logger.info("[AhenkDeamon] Messager was set")
|
logger.info('[AhenkDeamon] Messager was set')
|
||||||
|
|
||||||
if registration.is_ldap_registered() is False:
|
if registration.is_ldap_registered() is False:
|
||||||
logger.debug("[AhenkDeamon] Attempting to registering ldap")
|
logger.debug('[AhenkDeamon] Attempting to registering ldap')
|
||||||
registration.ldap_registration_request() #TODO work on message
|
registration.ldap_registration_request() #TODO work on message
|
||||||
|
|
||||||
logger.info("[AhenkDeamon] LDAP registration of Ahenk is completed")
|
logger.info('[AhenkDeamon] LDAP registration of Ahenk is completed')
|
||||||
|
|
||||||
|
#TODO###############
|
||||||
|
cnfg = configparser.ConfigParser()
|
||||||
|
cnfg.add_section('PID')
|
||||||
|
cnfg.set('PID','pid_number',str(os.getpid()))
|
||||||
|
|
||||||
#login
|
with open(pidfilePath, 'w') as config_file:
|
||||||
logger.info("[AhenkDeamon] Logining...")
|
cnfg.write(config_file)
|
||||||
messager.send_direct_message(messageManager.login_msg())
|
#TODO##############
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
#messager.send_direct_message('test')
|
||||||
|
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
#request policies
|
#request policies
|
||||||
logger.info("[AhenkDeamon] Requesting policies...")
|
#logger.info('[AhenkDeamon] Requesting policies...')
|
||||||
messager.send_direct_message(messageManager.policy_request_msg())
|
#messager.send_direct_message(messageManager.policy_request_msg())
|
||||||
|
|
||||||
#logout
|
|
||||||
#logger.info("[AhenkDeamon] Logouting...")
|
|
||||||
#messager.send_direct_message(messageManager.logout_msg())
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
this is must be created after message services
|
this is must be created after message services
|
||||||
|
@ -123,31 +135,74 @@ class AhenkDeamon(BaseDaemon):
|
||||||
globalscope.setResponseQueue(responseQueue)
|
globalscope.setResponseQueue(responseQueue)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
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])
|
||||||
|
|
||||||
|
logger.debug('[AhenkDeamon] Processing of handled event is completed')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
pidfilePath='/var/run/ahenk.pid'
|
|
||||||
|
|
||||||
ahenkdaemon = AhenkDeamon(pidfilePath)
|
ahenkdaemon = AhenkDeamon(pidfilePath)
|
||||||
|
|
||||||
print (sys.argv)
|
|
||||||
|
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
if sys.argv[1] == "start":
|
if sys.argv[1] == 'start':
|
||||||
print ("starting")
|
print('starting')
|
||||||
ahenkdaemon.run()
|
ahenkdaemon.run()
|
||||||
#print (ahenkdaemon.get_pid())
|
|
||||||
elif sys.argv[1] == 'stop':
|
elif sys.argv[1] == 'stop':
|
||||||
ahenkdaemon.stop()
|
ahenkdaemon.stop()
|
||||||
elif sys.argv[1] == 'restart':
|
elif sys.argv[1] == 'restart':
|
||||||
ahenkdaemon.restart()
|
ahenkdaemon.restart()
|
||||||
elif sys.argv[1] == 'status':
|
elif sys.argv[1] == 'status':
|
||||||
# print (status)
|
print('status')
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
print ('Unknown command. Usage : %s start|stop|restart|status' % sys.argv[0])
|
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])
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
print ('Usage : %s start|stop|restart|status' % sys.argv[0])
|
print('Usage : %s start|stop|restart|status' % sys.argv[0])
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ sys.path.append('../..')
|
||||||
from base.Scope import Scope
|
from base.Scope import Scope
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
|
#TODO Message Factory
|
||||||
class Messaging(object):
|
class Messaging(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
scope = Scope().getInstance()
|
scope = Scope().getInstance()
|
||||||
|
@ -45,19 +45,19 @@ class Messaging(object):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def login_msg(self):
|
def login_msg(self,username):
|
||||||
data = {}
|
data = {}
|
||||||
data['type'] = 'LOGIN'
|
data['type'] = 'LOGIN'
|
||||||
data['username'] = str(pwd.getpwuid(os.getuid())[0])
|
data['username'] = username
|
||||||
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
||||||
json_data = json.dumps(data)
|
json_data = json.dumps(data)
|
||||||
self.logger.debug('[Messaging] Login message was created')
|
self.logger.debug('[Messaging] Login message was created')
|
||||||
return json_data
|
return json_data
|
||||||
|
|
||||||
def logout_msg(self):
|
def logout_msg(self,username):
|
||||||
data = {}
|
data = {}
|
||||||
data['type'] = 'LOGOUT'
|
data['type'] = 'LOGOUT'
|
||||||
data['username'] = str(pwd.getpwuid(os.getuid())[0])
|
data['username'] = str(username)
|
||||||
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
||||||
json_data = json.dumps(data)
|
json_data = json.dumps(data)
|
||||||
self.logger.debug('[Messaging] Logout message was created')
|
self.logger.debug('[Messaging] Logout message was created')
|
||||||
|
|
Loading…
Reference in a new issue