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, 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")
42 twistd = twistds and twistds[0]
44 twistd = os.path.join(sys.prefix, 'Scripts', 'twistd.py')
45 if not os.path.exists(twistd):
46 print "Can't find twistd (it comes with Twisted). Aborting."
48 path, ext = os.path.splitext(twistd)
49 if ext.lower() in [".exe", ".bat",]:
52 cmd = [sys.executable, twistd,]
54 fileutil.make_dirs(os.path.join(basedir, "logs"))
55 cmd.extend(["-y", tac, "--logfile", os.path.join("logs", "twistd.log")])
59 rc = os.system(' '.join(cmd))
63 print >>out, "%s node probably started" % type
66 print >>err, "%s node probably not started" % type
69 def do_stop(basedir, out=sys.stdout, err=sys.stderr):
70 print >>out, "STOPPING", basedir
71 pidfile = os.path.join(basedir, "twistd.pid")
72 if not os.path.exists(pidfile):
73 print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
75 pid = open(pidfile, "r").read()
80 os.kill(pid, signal.SIGINT)
81 except OSError, oserr:
89 # poll once per second until twistd.pid goes away, up to 5 seconds
93 print >>out, "process %d is dead" % pid
97 print >>err, "never saw process go away"
100 def start(config, stdout, stderr):
102 for basedir in config['basedirs']:
103 rc = do_start(basedir, stdout, stderr) or rc
106 def stop(config, stdout, stderr):
108 for basedir in config['basedirs']:
109 rc = do_stop(basedir, stdout, stderr) or rc
112 def restart(config, stdout, stderr):
114 for basedir in config['basedirs']:
115 rc = do_stop(basedir, stdout, stderr) or rc
116 if rc == 2 and config['force']:
117 print >>stderr, "ignoring couldn't-stop"
120 print >>stderr, "not restarting"
122 for basedir in config['basedirs']:
123 rc = do_start(basedir, stdout, stderr) or rc
128 ["start", None, StartOptions, "Start a node (of any type)."],
129 ["stop", None, StopOptions, "Stop a node."],
130 ["restart", None, RestartOptions, "Restart a node."],