2 import os, sys, signal, time
3 from twisted.python import usage
4 from allmydata.scripts.common import BasedirMixin
5 from allmydata.util import fileutil
6 from twisted.python.procutils import which
8 class StartOptions(BasedirMixin, usage.Options):
10 ["basedir", "C", None, "which directory to start the node in"],
13 class StopOptions(BasedirMixin, usage.Options):
15 ["basedir", "C", None, "which directory to stop the node in"],
18 class RestartOptions(BasedirMixin, usage.Options):
20 ["basedir", "C", None, "which directory to restart the node in"],
23 ["force", "f", "if the node is not already running, start it "
24 "instead of complaining that you should have used 'start' instead "
28 def do_start(basedir, config, out=sys.stdout, err=sys.stderr):
29 print >>out, "STARTING", basedir
30 if os.path.exists(os.path.join(basedir, "client.tac")):
33 elif os.path.exists(os.path.join(basedir, "introducer.tac")):
34 tac = "introducer.tac"
37 print >>err, "%s does not look like a node directory" % basedir
38 if not os.path.isdir(basedir):
39 print >>err, " in fact, it doesn't look like a directory at all!"
41 twistds = which("twistd")
43 print "Can't find twistd (it comes with Twisted). Aborting."
46 path, ext = os.path.splitext(twistd)
47 if ext.lower() in [".exe", ".bat",]:
50 cmd = [sys.executable, twistd,]
52 fileutil.make_dirs(os.path.join(basedir, "logs"))
53 cmd.extend(["-y", tac, "--logfile", os.path.join("logs", "twistd.log")])
57 rc = os.system(' '.join(cmd))
61 print >>out, "%s node probably started" % type
64 print >>err, "%s node probably not started" % type
67 def do_stop(basedir, config, out=sys.stdout, err=sys.stderr):
68 print >>out, "STOPPING", basedir
69 pidfile = os.path.join(basedir, "twistd.pid")
70 if not os.path.exists(pidfile):
71 print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
73 pid = open(pidfile, "r").read()
78 os.kill(pid, signal.SIGINT)
79 except OSError, oserr:
87 # poll once per second until twistd.pid goes away, up to 5 seconds
91 print >>out, "process %d is dead" % pid
95 print >>err, "never saw process go away"
98 def start(config, stdout, stderr):
100 for basedir in config['basedirs']:
101 rc = do_start(basedir, config, stdout, stderr) or rc
104 def stop(config, stdout, stderr):
106 for basedir in config['basedirs']:
107 rc = do_stop(basedir, config, stdout, stderr) or rc
110 def restart(config, stdout, stderr):
112 for basedir in config['basedirs']:
113 rc = do_stop(basedir, config, stdout, stderr) or rc
114 if rc == 2 and config['force']:
115 print >>stderr, "ignoring couldn't-stop"
118 print >>stderr, "not restarting"
120 for basedir in config['basedirs']:
121 rc = do_start(basedir, config, stdout, stderr) or rc
126 ["start", None, StartOptions, "Start a node (of any type)."],
127 ["stop", None, StopOptions, "Stop a node."],
128 ["restart", None, RestartOptions, "Restart a node."],