[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[minion-cvs] Add stop-server, reload-server commands.



Update of /home/minion/cvsroot/src/minion/lib/mixminion/server
In directory moria.mit.edu:/tmp/cvs-serv15992/lib/mixminion/server

Modified Files:
	ServerMain.py 
Log Message:
Add stop-server, reload-server commands.

Yeah, I know that you can just run 'kill -TERM `cat pid`', but this is
a frequently-requested feature.


Index: ServerMain.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/ServerMain.py,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -d -r1.40 -r1.41
--- ServerMain.py	13 Feb 2003 10:56:40 -0000	1.40
+++ ServerMain.py	14 Feb 2003 17:22:21 -0000	1.41
@@ -14,6 +14,7 @@
 import os
 import sys
 import signal
+import string
 import time
 import threading
 # We pull this from mixminion.Common, just in case somebody still has
@@ -752,6 +753,66 @@
     LOG.info("Server is shut down")
 
     sys.exit(0)
+
+#----------------------------------------------------------------------
+_SIGNAL_SERVER_USAGE = """\
+Usage: %s [options]
+Options:
+  -h, --help:                Print this usage message and exit.
+  -f <file>, --config=<file> Use a configuration file other than the default.
+""".strip()
+
+def signalServer(cmd, args):
+    options, args = getopt.getopt(args, "hf:", ["help", "config="])
+    usage = 0
+    # XXXX Refactor this and configFromServerArgs to raise UsageError
+    if args:
+        usage = 1
+    configFile = None
+    for o,v in options:
+        if o in ('-h', '--help'):
+            usageAndExit(cmd)
+        elif o in ('-f', '--config'):
+            configFile = v
+
+    LOG.setMinSeverity("ERROR")
+    config = readConfigFile(configFile)
+    if cmd.endswith("stop-server"):
+        reload = 0
+    else:
+        assert cmd.endswith("reload-server")
+        reload = 1
+
+    if usage:
+        print _SIGNAL_SERVER_USAGE % { 'cmd' : cmd }
+        return
+
+    homeDir = config['Server']['Homedir']
+    pidFile = os.path.join(homeDir, "pid")
+    if not os.path.exists(pidFile):
+        raise UIError("No server seems to be running.")
+
+    try:
+        f = open(pidFile, 'r')
+        s = f.read()
+        f.close()
+        pid = string.atoi(s)
+    except (IOError, ValueError), e:
+        raise UIError("Couldn't read pid file: %s"%e)
+
+    if reload:
+        signal_num = signal.SIGHUP
+        signal_name = "SIGHUP"
+    else:
+        signal_num = signal.SIGTERM
+        signal_name = "SIGTERM"
+
+    try:
+        print "Sending %s to server (pid=%s)"%(signal_name, pid)
+        os.kill(pid, signal_num)
+        print "Done."
+    except OSError, e:
+        print UIError("Couldn't send signal: %s"%e)
 
 #----------------------------------------------------------------------
 _KEYGEN_USAGE = """\