2016-06-21 20:25:47 +02:00
|
|
|
"""
|
|
|
|
control.py - Implements SHUTDOWN and REHASH functionality.
|
|
|
|
"""
|
|
|
|
import signal
|
|
|
|
import os
|
|
|
|
|
|
|
|
from pylinkirc import world, utils, conf, classes
|
2016-08-18 06:39:46 +02:00
|
|
|
from pylinkirc.log import log, makeFileLogger, stopFileLoggers
|
2016-08-25 21:07:55 +02:00
|
|
|
from . import 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]
|
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
def _shutdown(irc=None):
|
|
|
|
"""Shuts down the Pylink daemon."""
|
|
|
|
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:
|
|
|
|
plugin.die(irc)
|
|
|
|
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
|
|
|
|
2016-07-27 01:26:01 +02:00
|
|
|
# Remove our main PyLink bot as well.
|
|
|
|
utils.unregisterService('pylink')
|
|
|
|
|
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.
|
|
|
|
remove_network(ircobj)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2016-10-15 22:50:25 +02:00
|
|
|
# Remove our pid file.
|
|
|
|
log.info("Removing our pid.")
|
2016-10-15 23:30:52 +02:00
|
|
|
try:
|
|
|
|
os.remove("%s.pid" % conf.confname)
|
|
|
|
except OSError:
|
|
|
|
log.exception("Failed to remove PID, ignoring...")
|
2016-10-15 22:50:25 +02:00
|
|
|
|
|
|
|
# Done.
|
|
|
|
|
2016-07-27 01:16:23 +02:00
|
|
|
def sigterm_handler(signo, stack_frame):
|
|
|
|
"""Handles SIGTERM and SIGINT gracefully by shutting down the PyLink daemon."""
|
|
|
|
log.info("Shutting down on signal %s." % signo)
|
2016-06-21 20:25:47 +02:00
|
|
|
_shutdown()
|
|
|
|
|
|
|
|
signal.signal(signal.SIGTERM, sigterm_handler)
|
2016-07-27 01:16:23 +02:00
|
|
|
signal.signal(signal.SIGINT, sigterm_handler)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
|
|
|
def _rehash():
|
|
|
|
"""Rehashes the PyLink daemon."""
|
|
|
|
old_conf = conf.conf.copy()
|
|
|
|
fname = conf.fname
|
|
|
|
new_conf = conf.loadConf(fname, errors_fatal=False)
|
|
|
|
new_conf = conf.validateConf(new_conf)
|
|
|
|
conf.conf = new_conf
|
2016-08-18 06:39:46 +02:00
|
|
|
|
|
|
|
# Reset any file logger options.
|
|
|
|
stopFileLoggers()
|
|
|
|
files = new_conf['logging'].get('files')
|
|
|
|
if files:
|
|
|
|
for filename, config in files.items():
|
|
|
|
makeFileLogger(filename, config.get('loglevel'))
|
|
|
|
|
2016-08-25 21:07:55 +02:00
|
|
|
# Reset permissions.
|
|
|
|
log.debug('rehash: resetting permissions.')
|
|
|
|
permissions.resetPermissions()
|
|
|
|
|
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.
|
|
|
|
log.debug('rehash: checking if %r is in new conf still.', network)
|
|
|
|
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:
|
|
|
|
ircobj.conf = new_conf
|
|
|
|
ircobj.serverdata = new_conf['servers'][network]
|
|
|
|
ircobj.botdata = new_conf['bot']
|
|
|
|
|
|
|
|
# Clear the IRC object's channel loggers and replace them with
|
|
|
|
# new ones by re-running logSetup().
|
|
|
|
while ircobj.loghandlers:
|
|
|
|
log.removeHandler(ircobj.loghandlers.pop())
|
|
|
|
|
|
|
|
ircobj.logSetup()
|
|
|
|
|
|
|
|
# TODO: update file loggers here too.
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (network not in world.networkobjects) or (not world.networkobjects[network].connection_thread.is_alive()):
|
2016-06-21 20:25:47 +02:00
|
|
|
proto = utils.getProtocolModule(sdata['protocol'])
|
|
|
|
world.networkobjects[network] = classes.Irc(network, proto, new_conf)
|
|
|
|
|
|
|
|
if os.name == 'posix':
|
|
|
|
# Only register SIGHUP on *nix.
|
|
|
|
def sighup_handler(_signo, _stack_frame):
|
|
|
|
"""Handles SIGHUP by rehashing the PyLink daemon."""
|
|
|
|
log.info("SIGHUP received, reloading config.")
|
|
|
|
_rehash()
|
|
|
|
|
|
|
|
signal.signal(signal.SIGHUP, sighup_handler)
|