From b5735702f7fb25ddc60bb73605e5dbc522c5e530 Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 30 Mar 2018 23:01:23 -0700 Subject: [PATCH] Backport the launcher as of commit 8321485315c9e83c1a45a053ec51ce81cd7977cc to 1.3 This adds support for stale PID file checking (#182), as well as the --rehash/--stop/--restart options --- launcher.py | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pylink | 98 +++----------------------------- setup.py | 7 ++- 3 files changed, 171 insertions(+), 91 deletions(-) create mode 100644 launcher.py diff --git a/launcher.py b/launcher.py new file mode 100644 index 0000000..b592f75 --- /dev/null +++ b/launcher.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python3 +""" +PyLink IRC Services launcher. +""" + +import os +import sys +import signal +import time +from pylinkirc import world, conf, __version__, real_version + +try: + import psutil +except ImportError: + psutil = None + +def main(): + import argparse + + parser = argparse.ArgumentParser(description='Starts an instance of PyLink IRC Services.') + parser.add_argument('config', help='specifies the path to the config file (defaults to pylink.yml)', nargs='?', default='pylink.yml') + parser.add_argument("-v", "--version", help="displays the program version and exits", action='store_true') + parser.add_argument("-c", "--check-pid", help="no-op; kept for compatiblity with PyLink 1.x", action='store_true') + parser.add_argument("-n", "--no-pid", help="skips generating and checking PID files", action='store_true') + parser.add_argument("-r", "--restart", help="restarts the PyLink instance with the given config file", action='store_true') + parser.add_argument("-s", "--stop", help="stops the PyLink instance with the given config file", action='store_true') + parser.add_argument("-R", "--rehash", help="rehashes the PyLink instance with the given config file", action='store_true') + args = parser.parse_args() + + if args.version: # Display version and exit + print('PyLink %s (in VCS: %s)' % (__version__, real_version)) + sys.exit() + + # XXX: repetitive + elif args.no_pid and (args.restart or args.stop or args.rehash): + print('ERROR: --no-pid cannot be combined with --restart or --stop') + sys.exit(1) + elif args.rehash and os.name != 'posix': + print('ERROR: Rehashing via the command line is not supported outside Unix.') + sys.exit(1) + + # FIXME: we can't pass logging on to conf until we set up the config... + conf.loadConf(args.config) + + from pylinkirc.log import log + from pylinkirc import classes, utils, coremods + + # Write and check for an existing PID file unless specifically told not to. + if not args.no_pid: + pidfile = '%s.pid' % conf.confname + has_pid = False + pid = None + if os.path.exists(pidfile): + + has_pid = True + if psutil is not None and os.name == 'posix': + # FIXME: Haven't tested this on other platforms, so not turning it on by default. + with open(pidfile) as f: + try: + pid = int(f.read()) + proc = psutil.Process(pid) + except psutil.NoSuchProcess: # Process doesn't exist! + has_pid = False + log.info("Ignoring stale PID %s from PID file %r: no such process exists.", pid, pidfile) + else: + # This PID got reused for something that isn't us? + if not any('pylink' in arg.lower() for arg in proc.cmdline()): + log.info("Ignoring stale PID %s from PID file %r: process command line %r is not us", pid, pidfile, proc.cmdline()) + has_pid = False + + if has_pid: + if args.rehash: + os.kill(pid, signal.SIGUSR1) + log.info('OK, rehashed PyLink instance %s (config %r)', pid, args.config) + sys.exit() + elif args.stop or args.restart: # Handle --stop and --restart options + os.kill(pid, signal.SIGTERM) + + log.info("Waiting for PyLink instance %s (config %r) to stop...", pid, args.config) + while os.path.exists(pidfile): + # XXX: this is ugly, but os.waitpid() only works on non-child processes on Windows + time.sleep(0.2) + log.info("Successfully killed PID %s for config %r.", pid, args.config) + + if args.stop: + sys.exit() + else: + log.error("PID file %r exists; aborting!", pidfile) + + if psutil is None: + log.error("If PyLink didn't shut down cleanly last time it ran, or you're upgrading " + "from PyLink < 1.1-dev, delete %r and start the server again.", pidfile) + if os.name == 'posix': + log.error("Alternatively, you can install psutil for Python 3 (pip3 install psutil), " + "which will allow this launcher to detect stale PID files and ignore them.") + sys.exit(1) + elif args.stop or args.restart or args.rehash: # XXX: also repetitive + # --stop and --restart should take care of stale PIDs. + if pid: + world._should_remove_pid = True + log.error('Cannot stop/rehash PyLink: no process with PID %s exists.', pid) + else: + log.error('Cannot stop/rehash PyLink: PID file %r does not exist.', pidfile) + sys.exit(1) + + with open(pidfile, 'w') as f: + f.write(str(os.getpid())) + world._should_remove_pid = True + + log.info('PyLink %s starting...', __version__) + + # Set terminal window title. See https://bbs.archlinux.org/viewtopic.php?id=85567 + # and https://stackoverflow.com/questions/7387276/ + if os.name == 'nt': + import ctypes + ctypes.windll.kernel32.SetConsoleTitleW("PyLink %s" % __version__) + elif os.name == 'posix': + sys.stdout.write("\x1b]2;PyLink %s\x07" % __version__) + + # Load configured plugins + to_load = conf.conf['plugins'] + utils.resetModuleDirs() + + for plugin in to_load: + try: + world.plugins[plugin] = pl = utils.loadPlugin(plugin) + except Exception as e: + log.exception('Failed to load plugin %r: %s: %s', plugin, type(e).__name__, str(e)) + else: + if hasattr(pl, 'main'): + log.debug('Calling main() function of plugin %r', pl) + pl.main() + + # Initialize all the networks one by one + for network, sdata in conf.conf['servers'].items(): + try: + protoname = sdata['protocol'] + except (KeyError, TypeError): + log.error("(%s) Configuration error: No protocol module specified, aborting.", network) + else: + # Fetch the correct protocol module. + try: + proto = utils.getProtocolModule(protoname) + + # Create and connect the network. + log.debug('Connecting to network %r', network) + world.networkobjects[network] = classes.Irc(network, proto, conf.conf) + except: + log.exception('(%s) Failed to connect to network %r, skipping it...', + network, network) + continue + + world.started.set() + log.info("Loaded plugins: %s", ', '.join(sorted(world.plugins.keys()))) + + from pylinkirc import coremods + coremods.permissions.resetPermissions() # Future note: this is moved to run on import in 2.0 diff --git a/pylink b/pylink index 08166fb..f6e3b88 100755 --- a/pylink +++ b/pylink @@ -1,95 +1,13 @@ #!/usr/bin/env python3 -""" -PyLink IRC Services launcher. -""" - -import os import sys try: - from pylinkirc import world + from pylinkirc import launcher except ImportError: - sys.stderr.write("ERROR: Failed to import PyLink main module (pylinkirc.world).\n\nIf you are " - "running PyLink from source, please install PyLink first using 'python3 " - "setup.py install' (global install) or 'python3 setup.py install --user'" - " (local install)\n") + print("ERROR: Failed to import PyLink launcher module (pylinkirc.launcher).\n\nIf you are " + "running PyLink from source, please install PyLink first using 'python3 " + "setup.py install' (global install) or 'python3 setup.py install --user'" + " (local install)\n") sys.exit(1) -from pylinkirc import conf, __version__, real_version - -if __name__ == '__main__': - import argparse - - parser = argparse.ArgumentParser(description='Starts an instance of PyLink IRC Services.') - parser.add_argument('config', help='specifies the path to the config file (defaults to pylink.yml)', nargs='?', default='pylink.yml') - parser.add_argument("-v", "--version", help="displays the program version and exits", action='store_true') - parser.add_argument("-c", "--check-pid", help="enables PID file checking", action='store_true') - parser.add_argument("-n", "--no-pid", help="skips generating PID files", action='store_true') - args = parser.parse_args() - - if args.version: # Display version and exit - print('PyLink %s (in VCS: %s)' % (__version__, real_version)) - sys.exit() - - # FIXME: we can't pass logging on to conf until we set up the config... - conf.loadConf(args.config) - - from pylinkirc.log import log - from pylinkirc import classes, utils, coremods - log.info('PyLink %s starting...', __version__) - - # Set terminal window title. See https://bbs.archlinux.org/viewtopic.php?id=85567 - # and https://stackoverflow.com/questions/7387276/ - if os.name == 'nt': - import ctypes - ctypes.windll.kernel32.SetConsoleTitleW("PyLink %s" % __version__) - elif os.name == 'posix': - sys.stdout.write("\x1b]2;PyLink %s\x07" % __version__) - - # Check for a pid file. This stops instance overlap and user mistakes, which are especially an - # issue for clientbot... - if args.check_pid: - config = conf.confname - if os.path.exists("%s.pid" % config): - log.error("PID file exists; aborting! If PyLink didn't shut down cleanly last time it " - "was run, or you're upgrading from PyLink < 1.1-dev, delete %s.pid and try " - "again." % config) - sys.exit(1) - - # Write a PID file unless specifically told not to. - if not args.no_pid: - with open('%s.pid' % conf.confname, 'w') as f: - f.write(str(os.getpid())) - world._should_remove_pid = True - - # Import plugins first globally, because they can listen for events - # that happen before the connection phase. - to_load = conf.conf['plugins'] - utils.resetModuleDirs() - # Here, we override the module lookup and import the plugins - # dynamically depending on which were configured. - for plugin in to_load: - try: - world.plugins[plugin] = pl = utils.loadPlugin(plugin) - except Exception as e: - log.exception('Failed to load plugin %r: %s: %s', plugin, type(e).__name__, str(e)) - else: - if hasattr(pl, 'main'): - log.debug('Calling main() function of plugin %r', pl) - pl.main() - - # Initialize all the networks one by one - for network, sdata in conf.conf['servers'].items(): - - try: - protoname = sdata['protocol'] - except (KeyError, TypeError): - log.error("(%s) Configuration error: No protocol module specified, aborting.", network) - else: - # Fetch the correct protocol module - proto = utils.getProtocolModule(protoname) - world.networkobjects[network] = irc = classes.Irc(network, proto, conf.conf) - - world.started.set() - log.info("Loaded plugins: %s", ', '.join(sorted(world.plugins.keys()))) - - from pylinkirc import coremods - coremods.permissions.resetPermissions() # XXX we should probably move this to run on import +else: + if __name__ == '__main__': + launcher.main() diff --git a/setup.py b/setup.py index 9ef7a33..a3a9545 100644 --- a/setup.py +++ b/setup.py @@ -80,6 +80,7 @@ setup( extras_require={ 'password-hashing': ['passlib'], + 'cron-support': ['psutil'], 'servprotect': ['expiringdict>=1.1.4'], }, @@ -98,5 +99,9 @@ setup( package_dir = {'pylinkirc': '.'}, # Executable scripts - scripts=["pylink", "pylink-mkpasswd"], + scripts=["pylink-mkpasswd"], + + entry_points = { + 'console_scripts': ['pylink=pylinkirc.launcher:main'], + } )