From cbaf7d5cee5d198cb771710a25b727c75e8ead32 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Mon, 6 Apr 2015 01:24:53 +0200 Subject: [PATCH] daemonisable MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit shamelessly pillé sur basile --- helixbot.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/helixbot.py b/helixbot.py index f9b14c6..6ea8572 100755 --- a/helixbot.py +++ b/helixbot.py @@ -6,13 +6,19 @@ Un bot IRC qui donne les réponses de Helix the fossil. """ -import irclib +import sys +import os import ircbot import random import time import re +import signal from commands import getstatusoutput as ex +# Oui, j'ai recodé ma version d'irclib pour pouvoir rattrapper les SIGHUP +sys.path.insert(0, "/home/vincent/scripts/python-myirclib") +import irclib + # on récupère la config import config @@ -23,6 +29,9 @@ def get_config_logfile(serveur): return config.logfile_template % (serveurs[serveur]) def log(serveur, channel, auteur=None, message=None): + import os + print "\n", serveur, channel, auteur, message, get_config_logfile(serveur), os.getenv("USER") + print os.getcwd() f = open(get_config_logfile(serveur), "a") if auteur == message == None: # alors c'est que c'est pas un channel mais juste une ligne de log @@ -391,26 +400,83 @@ class HelixBot(ircbot.SingleServerIRCBot): def _getnick(self): return self.serv.get_nickname() nick = property(_getnick) + + ### .fork trick + def start_as_daemon(self, outfile): + sys.stderr = Logger(outfile) + self.start() +class Logger(object): + """Pour écrire ailleurs que sur stdout""" + def __init__(self, filename="basile.full.log"): + self.filename = filename -if __name__ == "__main__": - import sys + def write(self, message): + f = open(self.filename, "a") + f.write(message) + f.close() + +def main(): + """Exécution principale : lecture des paramètres et lancement du bot.""" if len(sys.argv) == 1: - print "Usage : helixbot.py [--debug]" + print "Usage : helixbot.py [--debug] [--no-output] [--daemon [--pidfile]] [--outfile]" + print " --outfile sans --no-output ni --daemon n'a aucun effet" exit(1) serveur = sys.argv[1] + if "--daemon" in sys.argv: + thisfile = os.path.realpath(__file__) + thisdirectory = thisfile.rsplit("/", 1)[0] + os.chdir(thisdirectory) + daemon = True + else: + daemon = False if "debug" in sys.argv or "--debug" in sys.argv: debug = True else: debug = False if "--quiet" in sys.argv: config.debug_stdout = False - serveurs = {"a♡":"acoeur.crans.org", "acoeur":"acoeur.crans.org", "acoeur.crans.org":"acoeur.crans.org", - "irc":"irc.crans.org", "crans":"irc.crans.org", "irc.crans.org":"irc.crans.org"} + serveurs = {"a♡" : "acoeur.crans.org", + "acoeur" : "acoeur.crans.org", + "acoeur.crans.org" : "acoeur.crans.org", + "irc" : "irc.crans.org", + "crans" : "irc.crans.org", + "irc.crans.org" : "irc.crans.org"} + if "--no-output" in sys.argv or "--daemon" in sys.argv: + outfile = "/var/log/bots/helixbot.full.log" + for arg in sys.argv: + arg = arg.split("=") + if arg[0].strip('-') in ["out", "outfile", "logfile"]: + outfile = arg[1] + sys.stdout = Logger(outfile) try: serveur = serveurs[serveur] except KeyError: print "Server Unknown : %s" % (serveur) exit(404) - bot = HelixBot(serveur, debug) - bot.start() + helix = HelixBot(serveur,debug) + # Si on reçoit un SIGHUP, on reload la config + def sighup_handler(signum, frame): + helix.execute_reload(auteur="SIGHUP") + signal.signal(signal.SIGHUP, sighup_handler) + # Daemonization + if daemon: + child_pid = os.fork() + if child_pid == 0: + os.setsid() + helix.start_as_daemon(outfile) + else: + # on enregistre le pid de basile + pidfile = "/var/run/bots/helix.pid" + for arg in sys.argv: + arg = arg.split("=") + if arg[0].strip('-') in ["pidfile"]: + pidfile = arg[1] + f = open(pidfile, "w") + f.write("%s\n" % child_pid) + f.close() + else: + helix.start() + +if __name__ == "__main__": + main() -- 2.39.2