Ahenk/src/ahenkd.py

411 lines
14 KiB
Python
Raw Normal View History

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
import os
import queue
import signal
import sys
import threading
import time
2018-11-14 11:03:32 +03:00
from glob import glob
from base.agreement.agreement import Agreement
from base.command.command_manager import Commander
from base.command.command_runner import CommandRunner
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
from base.scope import Scope
from base.system.system import System
2016-08-24 18:04:06 +03:00
from base.task.task_manager import TaskManager
from base.util.util import Util
from easygui import msgbox
2016-02-01 17:49:17 +02:00
sys.path.append('../..')
ahenk_daemon = None
2016-02-08 19:03:38 +02:00
class AhenkDaemon(BaseDaemon):
"""Ahenk service base class which initializes services and maintains events/commands"""
2016-02-21 03:56:08 +02:00
@staticmethod
def reload():
""" docstring"""
# reload service here
pass
2016-03-04 18:01:47 +02:00
@staticmethod
def init_logger():
""" docstring"""
2016-04-05 17:55:15 +03:00
logger = Logger()
logger.info('Log was set')
Scope.get_instance().set_logger(logger)
2016-04-05 17:55:15 +03:00
return logger
@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()
Scope.get_instance().set_configuration_manager(config)
2016-04-05 17:55:15 +03:00
return config
@staticmethod
def init_scheduler():
""" docstring"""
2016-04-05 17:55:15 +03:00
scheduler_ins = SchedulerFactory.get_intstance()
scheduler_ins.initialize()
Scope.get_instance().set_scheduler(scheduler_ins)
2016-04-05 17:55:15 +03:00
sc_thread = threading.Thread(target=scheduler_ins.run)
sc_thread.setDaemon(True)
sc_thread.start()
return scheduler_ins
2016-04-05 17:55:15 +03:00
@staticmethod
def init_event_manager():
""" docstring"""
event_manager = EventManager()
Scope.get_instance().set_event_manager(event_manager)
return event_manager
2016-04-05 17:55:15 +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.get_instance().set_sb_service(db_service)
2016-04-05 17:55:15 +03:00
return db_service
@staticmethod
def init_messaging():
""" docstring"""
message_manager = Messaging()
Scope.get_instance().set_message_manager(message_manager)
return message_manager
@staticmethod
def init_plugin_manager():
""" docstring"""
plugin_manager = PluginManagerFactory.get_instance()
Scope.get_instance().set_plugin_manager(plugin_manager)
# order changed, problem?
plugin_manager.load_plugins()
return plugin_manager
@staticmethod
def init_task_manager():
""" docstring"""
task_manager = TaskManager()
Scope.get_instance().set_task_manager(task_manager)
return task_manager
@staticmethod
def init_registration():
""" docstring"""
2016-04-05 17:55:15 +03:00
registration = Registration()
Scope.get_instance().set_registration(registration)
2016-04-05 17:55:15 +03:00
return registration
@staticmethod
def init_execution_manager():
""" docstring"""
2016-04-05 17:55:15 +03:00
execution_manager = ExecutionManager()
Scope.get_instance().set_execution_manager(execution_manager)
2016-04-05 17:55:15 +03:00
return execution_manager
@staticmethod
def init_messenger():
""" docstring"""
messenger_ = Messenger()
messenger_.connect_to_server()
Scope.get_instance().set_messenger(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.get_instance().set_response_queue(response_queue)
return response_queue
2016-04-05 17:55:15 +03:00
def check_registration(self):
""" docstring"""
# max_attempt_number = int(System.Hardware.Network.interface_size()) * 3
2020-07-23 16:38:40 +03:00
max_attempt_number = 2
# self.logger.debug()
# logger = Scope.getInstance().getLogger()
registration = Scope.get_instance().get_registration()
2016-06-21 17:47:25 +03:00
2016-04-08 15:54:26 +03:00
try:
#if registration.is_registered() is False:
# self.logger.debug('Ahenk is not registered. Attempting for registration')
# if registration.registration_request() == False:
# self.registration_failed()
while registration.is_registered() is False:
2020-04-06 18:10:41 +03:00
print("Registration attemp")
max_attempt_number -= 1
self.logger.debug('Ahenk is not registered. Attempting for registration')
2020-07-23 14:35:27 +03:00
# registration.registration_request(self.register_hostname,self.register_user_name,self.register_user_password,self.register_directory_server)
registration.registration_request(self.register_hostname,self.register_user_name,self.register_user_password)
if max_attempt_number < 0:
self.logger.warning('Number of Attempting for registration is over')
Util.execute("/etc/init.d/ahenk stop")
break
#if max_attempt_number < 0:
# self.logger.warning('Number of Attempting for registration is over')
# self.registration_failed()
# break
2016-04-08 15:54:26 +03:00
except Exception as e:
self.registration_failed()
self.logger.error('Registration failed. Error message: {0}'.format(str(e)))
2020-06-25 10:37:36 +03:00
#Util.execute("/etc/init.d/ahenk stop")
def is_registered(self):
try:
registration = Scope.get_instance().get_registration()
if registration.is_registered() is False:
self.registration_failed()
except Exception as e:
self.registration_failed()
self.logger.error('Registration failed. Error message: {0}'.format(str(e)))
@staticmethod
def shutdown_mode():
""" docstring"""
scope = Scope().get_instance()
plugin_manager = scope.get_plugin_manager()
plugin_manager.process_mode('shutdown')
def registration_failed(self):
""" docstring"""
self.logger.error('Registration failed. All registration attempts were failed. Ahenk is stopping...')
2016-06-21 17:47:25 +03:00
print('Registration failed. Ahenk is stopping..')
ahenk_daemon.stop()
2016-04-08 15:54:26 +03:00
@staticmethod
def reload_plugins():
""" docstring"""
Scope.get_instance().get_plugin_manager().reloadPlugins()
2016-04-06 17:40:25 +03:00
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):
""" 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()
def init_signal_listener(self):
""" docstring"""
try:
signal.signal(signal.SIGALRM, CommandRunner().run_command_from_fifo)
self.logger.info('Signal handler is set up')
except Exception as e:
self.logger.error('Signal handler could not set up. Error Message: {0} '.format(str(e)))
@staticmethod
def init_pid_file():
""" docstring"""
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')
def set_register_user(self, hostName, username, password):
2020-04-06 18:10:41 +03:00
self.register_hostname=hostName
self.register_user_name=username
self.register_user_password=password
2020-07-23 14:35:27 +03:00
# self.register_directory_server = directoryServer
2018-11-19 13:57:35 +03:00
2020-04-06 18:10:41 +03:00
# if user_disabled is when ahenk service restarted TRUE disabled local users
def disable_local_users(self):
2018-11-19 13:57:35 +03:00
self.logger.info('Local users disable action start..')
conf_manager = Scope.get_instance().get_configuration_manager()
if conf_manager.has_section('MACHINE'):
user_disabled = conf_manager.get("MACHINE", "user_disabled")
self.logger.info('User disabled value=' + str(user_disabled))
2020-04-06 18:10:41 +03:00
if user_disabled == 'true':
2018-11-19 13:57:35 +03:00
self.logger.info('local user disabling')
Scope.get_instance().get_registration().disable_local_users()
2020-04-06 18:10:41 +03:00
conf_manager.set('MACHINE', 'user_disabled', 'disabled')
2018-11-19 13:57:35 +03:00
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
2020-04-06 18:10:41 +03:00
self.logger.info('opening config file ')
2018-11-19 13:57:35 +03:00
conf_manager.write(configfile)
user_disabled = conf_manager.get("MACHINE", "user_disabled")
self.logger.info('User succesfully disabled value=' + str(user_disabled))
2020-04-06 18:10:41 +03:00
2018-11-19 13:57:35 +03:00
else:
2020-04-06 18:10:41 +03:00
self.logger.info('local users will not be disabled because local_user_paramater is FALSE')
2018-11-19 13:57:35 +03:00
def run(self):
""" docstring"""
print('Ahenk running...')
global_scope = Scope()
global_scope.set_instance(global_scope)
2016-02-25 01:46:27 +02:00
config_file_folder_path = '/etc/ahenk/config.d/'
2016-02-21 03:56:08 +02:00
# configuration manager must be first load
self.init_config_manager(System.Ahenk.config_path(), config_file_folder_path)
2016-02-21 03:56:08 +02:00
# Logger must be second
self.logger = self.init_logger()
2016-02-08 19:03:38 +02:00
self.init_pid_file()
self.logger.info('Pid file was created')
2016-08-24 18:04:06 +03:00
self.init_fifo_file()
self.logger.info('Fifo file was created')
2016-08-24 18:04:06 +03:00
2016-04-05 17:55:15 +03:00
self.init_event_manager()
self.logger.info('Event Manager was set')
2016-03-04 18:01:47 +02:00
2016-04-05 17:55:15 +03:00
self.init_ahenk_db()
self.logger.info('DataBase Service was set')
2016-04-05 17:55:15 +03:00
self.init_messaging()
self.logger.info('Message Manager was set')
2016-04-05 17:55:15 +03:00
self.init_plugin_manager()
self.logger.info('Plugin Manager was set')
2016-02-21 03:56:08 +02:00
2016-06-22 18:20:25 +03:00
self.init_scheduler()
self.logger.info('Scheduler was set')
2016-06-22 18:20:25 +03:00
2016-04-05 17:55:15 +03:00
self.init_task_manager()
self.logger.info('Task Manager was set')
2016-03-01 12:41:55 +02:00
2016-04-08 15:54:26 +03:00
self.init_registration()
self.logger.info('Registration was set')
2016-04-05 17:55:15 +03:00
self.init_execution_manager()
self.logger.info('Execution Manager was set')
2016-03-08 18:09:26 +02:00
2016-04-08 15:54:26 +03:00
self.check_registration()
2020-04-06 18:10:41 +03:00
self.is_registered()
2018-11-15 12:49:42 +03:00
2018-11-19 13:57:35 +03:00
self.disable_local_users()
2018-11-14 11:03:32 +03:00
2018-11-10 22:28:32 +03:00
#self.logger.info('Ahenk was registered')
self.messenger = self.init_messenger()
self.logger.info('Messenger was set')
self.init_signal_listener()
self.logger.info('Signals listeners was set')
2020-04-06 18:10:41 +03:00
# Agreement().agreement_contract_update()
global_scope.put_custom_map('ahenk_daemon', ahenk_daemon)
2016-04-05 17:55:15 +03:00
self.init_message_response_queue()
# if registration.is_ldap_registered() is False:
# logger.debug('Attempting to registering ldap')
2016-03-23 10:04:31 +02:00
# registration.ldap_registration_request() #TODO work on message
self.logger.info('LDAP registration of Ahenk is completed')
self.messenger.send_direct_message('test')
while True:
time.sleep(1)
if __name__ == '__main__':
ahenk_daemon = AhenkDaemon(System.Ahenk.pid_path())
try:
if len(sys.argv) == 2 and (sys.argv[1] in ('start', 'stop', 'restart', 'status')):
2020-10-05 13:43:30 +03:00
ahenk_daemon.set_register_user(None, None, None)
if sys.argv[1] == 'start':
if System.Ahenk.is_running() is True:
print('There is already running Ahenk service. It will be killed.[{0}]'.format(
str(System.Ahenk.get_pid_number())))
System.Process.kill_by_pid(int(System.Ahenk.get_pid_number()))
else:
print('Ahenk starting...')
ahenk_daemon.run()
elif sys.argv[1] == 'stop':
if System.Ahenk.is_running() is True:
raise SystemExit
else:
print('Ahenk not working!')
elif sys.argv[1] == 'restart':
if System.Ahenk.is_running() is True:
print('Ahenk restarting...')
ahenk_daemon.restart()
else:
print('Ahenk starting...')
ahenk_daemon.run()
elif sys.argv[1] == 'status':
print(Commander().status())
else:
print('Unknown command. Usage : %s start|stop|restart|status|clean' % sys.argv[0])
sys.exit(2)
elif len(sys.argv) > 2 and (sys.argv[1] in ('start')):
2020-04-06 18:10:41 +03:00
params = sys.argv[1]
hostName = sys.argv[2]
userName = sys.argv[3]
password = sys.argv[4]
2020-07-23 14:35:27 +03:00
# directoryServer = sys.argv[5]
# ahenk_daemon.set_register_user(hostName,userName,password,directoryServer)
ahenk_daemon.set_register_user(hostName,userName,password)
2020-04-06 18:10:41 +03:00
ahenk_daemon.run()
else:
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)
except(KeyboardInterrupt, SystemExit):
if System.Ahenk.is_running() is True:
print('Ahenk stopping...')
result = Commander().set_event([None, 'stop'])
if result is True:
if System.Ahenk.is_running() is True:
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
else:
ahenk_daemon.stop()