2016-06-21 20:25:47 +02:00
|
|
|
"""
|
|
|
|
control.py - Implements SHUTDOWN and REHASH functionality.
|
|
|
|
"""
|
2019-07-15 00:12:29 +02:00
|
|
|
import atexit
|
2016-06-21 20:25:47 +02:00
|
|
|
import os
|
2019-07-15 00:12:29 +02:00
|
|
|
import signal
|
2017-02-18 21:19:42 +01:00
|
|
|
import sys
|
2019-07-15 00:12:29 +02:00
|
|
|
import threading
|
|
|
|
|
|
|
|
from pylinkirc import conf, utils, world # Do not import classes, it'll import loop
|
|
|
|
from pylinkirc.log import _get_console_log_level, _make_file_logger, _stop_file_loggers, log
|
|
|
|
|
|
|
|
from . import login, permissions
|
2016-06-21 20:25:47 +02:00
|
|
|
|
|
|
|
|
2016-07-02 05:08:38 +02:00
|
|
|
def remove_network(ircobj):
|
|
|
|
"""Removes a network object from the pool."""
|
|
|
|
# Disable autoconnect first by setting the delay negative.
|
|
|
|
ircobj.serverdata['autoconnect'] = -1
|
|
|
|
ircobj.disconnect()
|
|
|
|
del world.networkobjects[ircobj.name]
|
|
|
|
|
2017-05-07 22:39:46 +02:00
|
|
|
def _print_remaining_threads():
|
2017-07-13 06:50:20 +02:00
|
|
|
log.debug('shutdown(): Remaining threads: %s', ['%s/%s' % (t.name, t.ident) for t in threading.enumerate()])
|
2017-05-07 22:39:46 +02:00
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
def _remove_pid():
|
2017-07-13 07:17:00 +02:00
|
|
|
pidfile = "%s.pid" % conf.confname
|
2017-07-20 15:13:01 +02:00
|
|
|
if world._should_remove_pid:
|
|
|
|
# Remove our pid file.
|
|
|
|
log.info("Removing PID file %r.", pidfile)
|
|
|
|
try:
|
|
|
|
os.remove(pidfile)
|
|
|
|
except OSError:
|
|
|
|
log.exception("Failed to remove PID file %r, ignoring..." % pidfile)
|
|
|
|
else:
|
|
|
|
log.debug('Not removing PID file %s as world._should_remove_pid is False.' % pidfile)
|
2017-02-18 21:19:42 +01:00
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
def _kill_plugins(irc=None):
|
2017-08-16 21:17:08 +02:00
|
|
|
if not world.plugins:
|
|
|
|
# No plugins were loaded or we were in a pre-initialized state, ignore.
|
|
|
|
return
|
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
log.info("Shutting down plugins.")
|
2016-06-21 20:25:47 +02:00
|
|
|
for name, plugin in world.plugins.items():
|
|
|
|
# Before closing connections, tell all plugins to shutdown cleanly first.
|
|
|
|
if hasattr(plugin, 'die'):
|
2016-07-02 06:08:50 +02:00
|
|
|
log.debug('coremods.control: Running die() on plugin %s due to shutdown.', name)
|
2016-06-21 20:25:47 +02:00
|
|
|
try:
|
2017-07-13 06:42:50 +02:00
|
|
|
plugin.die(irc=irc)
|
2016-06-21 20:25:47 +02:00
|
|
|
except: # But don't allow it to crash the server.
|
2016-07-02 06:08:50 +02:00
|
|
|
log.exception('coremods.control: Error occurred in die() of plugin %s, skipping...', name)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
# We use atexit to register certain functions so that when PyLink cleans up after itself if it
|
|
|
|
# shuts down because all networks have been disconnected.
|
|
|
|
atexit.register(_remove_pid)
|
|
|
|
atexit.register(_kill_plugins)
|
|
|
|
|
2017-07-13 06:50:20 +02:00
|
|
|
def shutdown(irc=None):
|
2017-05-13 04:02:25 +02:00
|
|
|
"""Shuts down the Pylink daemon."""
|
2017-08-31 22:36:46 +02:00
|
|
|
if world.shutting_down.is_set(): # We froze on shutdown last time, so immediately abort.
|
2017-05-13 04:02:25 +02:00
|
|
|
_print_remaining_threads()
|
|
|
|
raise KeyboardInterrupt("Forcing shutdown.")
|
|
|
|
|
2017-08-31 22:36:46 +02:00
|
|
|
world.shutting_down.set()
|
2017-05-13 04:02:25 +02:00
|
|
|
|
|
|
|
# HACK: run the _kill_plugins trigger with the current IRC object. XXX: We should really consider removing this
|
|
|
|
# argument, since no plugins actually use it to do anything.
|
|
|
|
atexit.unregister(_kill_plugins)
|
2017-07-13 06:42:50 +02:00
|
|
|
_kill_plugins(irc=irc)
|
2017-05-13 04:02:25 +02:00
|
|
|
|
2016-07-27 01:26:01 +02:00
|
|
|
# Remove our main PyLink bot as well.
|
2018-03-03 04:58:10 +01:00
|
|
|
utils.unregister_service('pylink')
|
2016-07-27 01:26:01 +02:00
|
|
|
|
2016-07-02 07:20:24 +02:00
|
|
|
for ircobj in world.networkobjects.copy().values():
|
2016-07-02 05:08:38 +02:00
|
|
|
# Disconnect all our networks.
|
2019-03-29 04:14:28 +01:00
|
|
|
try:
|
|
|
|
remove_network(ircobj)
|
|
|
|
except NotImplementedError:
|
|
|
|
continue
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2016-12-17 04:42:12 +01:00
|
|
|
log.info("Waiting for remaining threads to stop; this may take a few seconds. If PyLink freezes "
|
|
|
|
"at this stage, press Ctrl-C to force a shutdown.")
|
2017-05-07 22:39:46 +02:00
|
|
|
_print_remaining_threads()
|
2016-12-17 04:42:12 +01:00
|
|
|
|
2016-10-15 22:50:25 +02:00
|
|
|
# Done.
|
|
|
|
|
2017-07-13 06:51:28 +02:00
|
|
|
def _sigterm_handler(signo, stack_frame):
|
2016-07-27 01:16:23 +02:00
|
|
|
"""Handles SIGTERM and SIGINT gracefully by shutting down the PyLink daemon."""
|
|
|
|
log.info("Shutting down on signal %s." % signo)
|
2017-07-13 06:50:20 +02:00
|
|
|
shutdown()
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-07-13 06:51:28 +02:00
|
|
|
signal.signal(signal.SIGTERM, _sigterm_handler)
|
|
|
|
signal.signal(signal.SIGINT, _sigterm_handler)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-07-13 06:50:20 +02:00
|
|
|
def rehash():
|
2016-06-21 20:25:47 +02:00
|
|
|
"""Rehashes the PyLink daemon."""
|
2017-06-02 17:46:27 +02:00
|
|
|
log.info('Reloading PyLink configuration...')
|
2016-06-21 20:25:47 +02:00
|
|
|
old_conf = conf.conf.copy()
|
|
|
|
fname = conf.fname
|
2018-03-03 05:22:31 +01:00
|
|
|
new_conf = conf.load_conf(fname, errors_fatal=False, logger=log)
|
2016-06-21 20:25:47 +02:00
|
|
|
conf.conf = new_conf
|
2016-08-18 06:39:46 +02:00
|
|
|
|
|
|
|
# Reset any file logger options.
|
2018-03-03 04:55:17 +01:00
|
|
|
_stop_file_loggers()
|
2016-08-18 06:39:46 +02:00
|
|
|
files = new_conf['logging'].get('files')
|
|
|
|
if files:
|
|
|
|
for filename, config in files.items():
|
2018-03-03 04:55:17 +01:00
|
|
|
_make_file_logger(filename, config.get('loglevel'))
|
2016-08-18 06:39:46 +02:00
|
|
|
|
2017-06-02 17:42:32 +02:00
|
|
|
log.debug('rehash: updating console log level')
|
2018-03-03 04:55:17 +01:00
|
|
|
world.console_handler.setLevel(_get_console_log_level())
|
2019-06-07 23:10:42 +02:00
|
|
|
login._make_cryptcontext() # refresh password hashing settings
|
2017-01-07 09:10:47 +01:00
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
for network, ircobj in world.networkobjects.copy().items():
|
|
|
|
# Server was removed from the config file, disconnect them.
|
2019-06-24 02:39:06 +02:00
|
|
|
log.debug('rehash: checking if %r is still in new conf.', network)
|
|
|
|
if ircobj.has_cap('virtual-server') or hasattr(ircobj, 'virtual_parent'):
|
|
|
|
log.debug('rehash: not removing network %r since it is a virtual server.', network)
|
2019-02-09 00:10:21 +01:00
|
|
|
continue
|
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
if network not in new_conf['servers']:
|
|
|
|
log.debug('rehash: removing connection to %r (removed from config).', network)
|
2016-07-02 05:08:38 +02:00
|
|
|
remove_network(ircobj)
|
2016-06-21 20:25:47 +02:00
|
|
|
else:
|
2017-03-11 09:21:30 +01:00
|
|
|
# XXX: we should really just add abstraction to Irc to update config settings...
|
2016-06-21 20:25:47 +02:00
|
|
|
ircobj.serverdata = new_conf['servers'][network]
|
2018-06-12 08:43:57 +02:00
|
|
|
|
2017-03-11 10:05:07 +01:00
|
|
|
ircobj.autoconnect_active_multiplier = 1
|
2016-06-21 20:25:47 +02:00
|
|
|
|
|
|
|
# Clear the IRC object's channel loggers and replace them with
|
2017-07-12 16:50:34 +02:00
|
|
|
# new ones by re-running log_setup().
|
2016-06-21 20:25:47 +02:00
|
|
|
while ircobj.loghandlers:
|
|
|
|
log.removeHandler(ircobj.loghandlers.pop())
|
|
|
|
|
2017-07-12 16:50:34 +02:00
|
|
|
ircobj.log_setup()
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2018-03-03 05:04:49 +01:00
|
|
|
utils._reset_module_dirs()
|
2017-03-09 07:30:32 +01:00
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
for network, sdata in new_conf['servers'].items():
|
2016-07-02 05:33:00 +02:00
|
|
|
# Connect any new networks or disconnected networks if they aren't already.
|
2018-03-17 20:18:34 +01:00
|
|
|
if network not in world.networkobjects:
|
2018-03-03 04:53:27 +01:00
|
|
|
try:
|
2018-03-03 05:04:49 +01:00
|
|
|
proto = utils._get_protocol_module(sdata['protocol'])
|
2017-06-30 09:41:10 +02:00
|
|
|
|
2018-03-03 04:53:27 +01:00
|
|
|
# API note: 2.0.x style of starting network connections
|
|
|
|
world.networkobjects[network] = newirc = proto.Class(network)
|
|
|
|
newirc.connect()
|
|
|
|
except:
|
|
|
|
log.exception('Failed to initialize network %r, skipping it...', network)
|
2017-06-30 09:41:10 +02:00
|
|
|
|
2017-06-02 17:46:27 +02:00
|
|
|
log.info('Finished reloading PyLink configuration.')
|
2016-06-21 20:25:47 +02:00
|
|
|
|
|
|
|
if os.name == 'posix':
|
2018-01-21 22:35:21 +01:00
|
|
|
# Only register SIGHUP/SIGUSR1 on *nix.
|
|
|
|
def _sighup_handler(signo, _stack_frame):
|
|
|
|
"""Handles SIGHUP/SIGUSR1 by rehashing the PyLink daemon."""
|
|
|
|
log.info("Signal %s received, reloading config." % signo)
|
2017-07-13 06:50:20 +02:00
|
|
|
rehash()
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2018-01-21 22:35:21 +01:00
|
|
|
signal.signal(signal.SIGHUP, _sighup_handler)
|
|
|
|
signal.signal(signal.SIGUSR1, _sighup_handler)
|