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-03-23 17:15:27 +02: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-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.Messaging import Messaging
|
2016-07-18 18:21:03 +03:00
|
|
|
from base.messaging.Messenger import Messenger
|
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-06-06 12:27:46 +03:00
|
|
|
from base.system.system import System
|
2016-04-13 11:55:45 +03:00
|
|
|
from base.task.TaskManager import TaskManager
|
2016-06-27 17:20:26 +03:00
|
|
|
from base.util.util import Util
|
2016-02-01 17:49:17 +02:00
|
|
|
|
2016-06-06 12:27:46 +03:00
|
|
|
ahenkdaemon = None
|
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
|
|
|
Scope.getInstance().setPluginManager(pluginManager)
|
2016-07-18 18:21:03 +03:00
|
|
|
# order changed, problem?
|
|
|
|
pluginManager.load_plugins()
|
2016-04-05 17:55:15 +03:00
|
|
|
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
|
|
|
|
|
2016-06-20 17:35:59 +03:00
|
|
|
def init_messenger(self):
|
|
|
|
messenger = Messenger()
|
|
|
|
messenger.connect_to_server()
|
|
|
|
Scope.getInstance().setMessenger(messenger)
|
2016-05-06 16:34:58 +03:00
|
|
|
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-06-06 12:27:46 +03:00
|
|
|
max_attemp_number = int(System.Hardware.Network.interface_size()) * 3
|
2016-05-06 16:34:58 +03:00
|
|
|
logger = Scope.getInstance().getLogger()
|
2016-06-21 17:47:25 +03:00
|
|
|
|
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-06-21 17:47:25 +03:00
|
|
|
# TODO 'Could not reach Registration response from Lider. Be sure Lider is running and it is connected to XMPP server!'
|
2016-06-06 12:27:46 +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)))
|
|
|
|
|
2016-07-13 17:39:44 +03:00
|
|
|
def shutdown_mode(self):
|
|
|
|
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-06-21 17:47:25 +03:00
|
|
|
self.logger.error('[AhenkDeamon] Registration failed. All registration attemps were failed. Ahenk is stopping...')
|
|
|
|
print('Registration failed. Ahenk is stopping..')
|
|
|
|
ahenkdaemon.stop()
|
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
|
|
|
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-06-06 12:27:46 +03:00
|
|
|
self.init_config_manager(System.Ahenk.config_path(), configfileFolderPath)
|
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-04-05 17:55:15 +03:00
|
|
|
self.init_event_manager()
|
2016-06-06 12:27:46 +03:00
|
|
|
self.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()
|
2016-06-06 12:27:46 +03:00
|
|
|
self.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-06-06 12:27:46 +03:00
|
|
|
self.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-06-06 12:27:46 +03:00
|
|
|
self.logger.info('[AhenkDeamon] 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('[AhenkDeamon] Scheduler was set')
|
|
|
|
|
2016-04-05 17:55:15 +03:00
|
|
|
self.init_task_manager()
|
2016-06-06 12:27:46 +03:00
|
|
|
self.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-06-06 12:27:46 +03:00
|
|
|
self.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-06-06 12:27:46 +03:00
|
|
|
self.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-07-13 17:39:44 +03:00
|
|
|
self.logger.info('[AhenkDeamon] Ahenk was registered')
|
|
|
|
|
2016-06-20 17:35:59 +03:00
|
|
|
self.messenger = self.init_messenger()
|
2016-06-06 12:27:46 +03:00
|
|
|
self.logger.info('[AhenkDeamon] Messager 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-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-06-06 12:27:46 +03:00
|
|
|
self.logger.info('[AhenkDeamon] LDAP registration of Ahenk is completed')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-06-06 12:27:46 +03:00
|
|
|
with open(System.Ahenk.pid_path(), 'w+') as config_file:
|
|
|
|
config_file.write(str(os.getpid()))
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-03-22 16:43:47 +02:00
|
|
|
try:
|
2016-06-06 12:27:46 +03:00
|
|
|
signal.signal(signal.SIGALRM, self.run_command_from_fifo)
|
|
|
|
self.logger.info('[AhenkDeamon] Signal handler is set up')
|
2016-03-22 16:43:47 +02:00
|
|
|
except Exception as e:
|
2016-06-06 12:27:46 +03:00
|
|
|
self.logger.error('[AhenkDeamon] Signal handler could not set up. Error Message: {} '.format(str(e)))
|
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:
|
2016-06-20 17:35:59 +03:00
|
|
|
# if messager.is_connected() is False:
|
|
|
|
# self.logger.debug('reconnecting')
|
|
|
|
# Scope.getInstance().getLogger().warning('[AhenkDeamon] Connection is lost. Ahenk is trying for reconnection')
|
|
|
|
# messager = self.init_messager()
|
2016-03-22 16:43:47 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
2016-07-13 17:39:44 +03:00
|
|
|
def running_plugin(self):
|
|
|
|
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):
|
|
|
|
|
|
|
|
json_data = json.loads(Commander().get_event())
|
|
|
|
|
|
|
|
if json_data is not None:
|
|
|
|
scope = Scope().getInstance()
|
|
|
|
plugin_manager = scope.getPluginManager()
|
|
|
|
message_manager = scope.getMessageManager()
|
2016-06-20 17:35:59 +03:00
|
|
|
messenger = scope.getMessenger()
|
2016-06-27 18:13:43 +03:00
|
|
|
conf_manager = scope.getConfigurationManager()
|
|
|
|
db_service = scope.getDbService()
|
2016-06-06 12:27:46 +03:00
|
|
|
|
|
|
|
self.logger.debug('[AhenkDeamon] Signal handled')
|
|
|
|
self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event'])))
|
|
|
|
|
|
|
|
if 'login' == str(json_data['event']):
|
2016-06-27 17:20:26 +03:00
|
|
|
username = json_data['username']
|
|
|
|
display = json_data['display']
|
|
|
|
desktop = json_data['desktop']
|
|
|
|
self.logger.info('[AhenkDeamon] login event is handled for user: {}'.format(username))
|
|
|
|
login_message = message_manager.login_msg(username)
|
2016-06-06 12:27:46 +03:00
|
|
|
messenger.send_direct_message(login_message)
|
2016-06-27 17:20:26 +03:00
|
|
|
|
|
|
|
agreement = Agreement()
|
2016-06-28 12:24:02 +03:00
|
|
|
agreement_choice = None
|
2016-07-13 17:39:44 +03:00
|
|
|
|
2016-06-28 12:24:02 +03:00
|
|
|
if agreement.check_agreement(username) is not True:
|
2016-06-27 17:20:26 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] User {} has not accepted agreement.'.format(username))
|
|
|
|
thread_ask = Process(target=agreement.ask, args=(username, display,))
|
|
|
|
thread_ask.start()
|
|
|
|
|
2016-06-27 18:13:43 +03:00
|
|
|
agreement_timeout = conf_manager.get('SESSION', 'agreement_timeout')
|
2016-06-27 17:20:26 +03:00
|
|
|
|
|
|
|
timeout = int(agreement_timeout) # sec
|
|
|
|
timer = time.time()
|
|
|
|
while 1:
|
|
|
|
if thread_ask.is_alive() is False:
|
|
|
|
self.logger.warning('[AhenkDeamon] {} was answered the question '.format(username))
|
2016-06-28 12:24:02 +03:00
|
|
|
if Agreement().check_agreement(username) is True:
|
2016-06-27 17:20:26 +03:00
|
|
|
self.logger.warning('[AhenkDeamon] Choice of {} is YES'.format(username))
|
|
|
|
agreement_choice = True
|
|
|
|
break
|
2016-06-28 12:24:02 +03:00
|
|
|
elif Agreement().check_agreement(username) is False:
|
2016-06-27 17:20:26 +03:00
|
|
|
self.logger.warning('[AhenkDeamon] Choice of {} is NO'.format(username))
|
|
|
|
agreement_choice = False
|
2016-06-28 12:24:02 +03:00
|
|
|
Util.execute('pkill -9 -u {}'.format(username))
|
2016-06-27 17:20:26 +03:00
|
|
|
break
|
|
|
|
|
|
|
|
if (time.time() - timer) > timeout:
|
|
|
|
if thread_ask.is_alive():
|
|
|
|
thread_ask.terminate()
|
|
|
|
Util.execute('pkill -9 -u {}'.format(username))
|
|
|
|
self.logger.warning('[AhenkDeamon] Session of {} was ended because of timeout of contract agreement'.format(username))
|
|
|
|
break
|
2016-06-28 12:24:02 +03:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
if agreement_choice is not None:
|
2016-06-30 17:05:46 +03:00
|
|
|
messenger.send_direct_message(message_manager.agreement_answer_msg(username, agreement_choice))
|
2016-06-27 17:20:26 +03:00
|
|
|
else:
|
|
|
|
agreement_choice = True
|
|
|
|
|
|
|
|
if agreement_choice is True:
|
2016-06-27 18:13:43 +03:00
|
|
|
db_service.delete('session', 'username=\'{}\''.format(username))
|
2016-07-18 18:21:03 +03:00
|
|
|
|
|
|
|
self.logger.info('[AhenkDeamon] Display is {0}, desktop env is {1} for {2}'.format(display, desktop, username))
|
|
|
|
|
2016-06-27 18:13:43 +03:00
|
|
|
db_service.update('session', scope.getDbService().get_cols('session'), [username, display, desktop, Util.timestamp()])
|
2016-06-27 17:20:26 +03:00
|
|
|
get_policy_message = message_manager.policy_request_msg(username)
|
2016-06-29 12:17:19 +03:00
|
|
|
|
2016-07-13 17:39:44 +03:00
|
|
|
plugin_manager.process_mode('safe', username)
|
|
|
|
plugin_manager.process_mode('login', username)
|
|
|
|
|
2016-06-27 17:20:26 +03:00
|
|
|
messenger.send_direct_message(get_policy_message)
|
|
|
|
|
2016-06-06 12:27:46 +03:00
|
|
|
elif 'logout' == str(json_data['event']):
|
2016-06-27 17:20:26 +03:00
|
|
|
username = json_data['username']
|
2016-06-27 18:13:43 +03:00
|
|
|
db_service.delete('session', 'username=\'{}\''.format(username))
|
2016-06-27 17:20:26 +03:00
|
|
|
self.logger.info('[AhenkDeamon] logout event is handled for user: {}'.format(username))
|
|
|
|
logout_message = message_manager.logout_msg(username)
|
2016-06-06 12:27:46 +03:00
|
|
|
messenger.send_direct_message(logout_message)
|
2016-07-13 17:39:44 +03:00
|
|
|
|
|
|
|
plugin_manager.process_mode('logout', username)
|
|
|
|
plugin_manager.process_mode('safe', username)
|
|
|
|
|
2016-06-06 12:27:46 +03:00
|
|
|
elif 'send' == str(json_data['event']):
|
|
|
|
self.logger.info('[AhenkDeamon] Sending message over ahenkd command. Response Message: {}'.format(str(json_data['message'])))
|
|
|
|
message = str(json.dumps(json_data['message']))
|
|
|
|
messenger.send_direct_message(message)
|
2016-07-13 17:39:44 +03:00
|
|
|
|
2016-07-18 18:21:03 +03:00
|
|
|
elif 'load' == str(json_data['event']):
|
|
|
|
plugin_name = str(json_data['plugins'])
|
|
|
|
|
|
|
|
if plugin_name == 'all':
|
2016-07-19 16:05:46 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] All plugins are loading to ahenk')
|
2016-07-18 18:21:03 +03:00
|
|
|
plugin_manager.load_plugins()
|
|
|
|
else:
|
|
|
|
for p_name in plugin_name.split(','):
|
2016-07-19 16:05:46 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] {} plugin is loading to ahenk'.format(p_name))
|
2016-07-18 18:21:03 +03:00
|
|
|
plugin_manager.load_single_plugin(p_name)
|
|
|
|
|
|
|
|
elif 'reload' == str(json_data['event']):
|
|
|
|
plugin_name = str(json_data['plugins'])
|
|
|
|
|
|
|
|
if plugin_name == 'all':
|
2016-07-19 16:05:46 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] All plugins are reloading to ahenk')
|
2016-07-18 18:21:03 +03:00
|
|
|
plugin_manager.reload_plugins()
|
|
|
|
else:
|
|
|
|
for p_name in plugin_name.split(','):
|
2016-07-19 16:05:46 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] {} plugin is reloading to ahenk'.format(p_name))
|
2016-07-18 18:21:03 +03:00
|
|
|
plugin_manager.reload_single_plugin(p_name)
|
|
|
|
|
|
|
|
elif 'remove' == str(json_data['event']):
|
|
|
|
plugin_name = str(json_data['plugins'])
|
|
|
|
|
|
|
|
if plugin_name == 'all':
|
2016-07-19 16:05:46 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] All plugins are removing from ahenk')
|
2016-07-18 18:21:03 +03:00
|
|
|
plugin_manager.remove_plugins()
|
|
|
|
else:
|
|
|
|
for p_name in plugin_name.split(','):
|
2016-07-19 16:05:46 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] {} plugin is removing from ahenk'.format(p_name))
|
2016-07-18 18:21:03 +03:00
|
|
|
plugin_manager.remove_single_plugin(p_name)
|
|
|
|
|
2016-07-13 17:39:44 +03:00
|
|
|
elif 'stop' == str(json_data['event']):
|
|
|
|
self.shutdown_mode()
|
|
|
|
self.logger.info('[AhenkDeamon] Shutdown mode activated.')
|
|
|
|
|
|
|
|
# TODO timeout
|
|
|
|
while self.running_plugin() is False:
|
|
|
|
self.logger.debug('[AhenkDeamon] Waiting for progress of plugins...')
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
ahenkdaemon.stop()
|
2016-06-06 12:27:46 +03:00
|
|
|
else:
|
|
|
|
self.logger.error('[AhenkDeamon] Unknown command error. Command:' + json_data['event'])
|
2016-05-25 17:53:52 +03:00
|
|
|
|
2016-06-06 12:27:46 +03:00
|
|
|
self.logger.debug('[AhenkDeamon] Processing of handled event is completed')
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2016-03-22 16:43:47 +02:00
|
|
|
|
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2016-06-06 12:27:46 +03:00
|
|
|
ahenkdaemon = AhenkDeamon(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-07-13 17:39:44 +03:00
|
|
|
print('There is already running Ahenk service. It will be killed.[{}]'.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-03-29 16:11:39 +03:00
|
|
|
ahenkdaemon.run()
|
|
|
|
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...')
|
|
|
|
ahenkdaemon.restart()
|
|
|
|
else:
|
|
|
|
print('Ahenk starting...')
|
|
|
|
ahenkdaemon.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:
|
|
|
|
ahenkdaemon.stop()
|