2 import os, sys, signal, time, subprocess
3 from twisted.python import usage
4 from allmydata.scripts.common import BasedirMixin
5 from twisted.python.procutils import which
7 class StartOptions(BasedirMixin, usage.Options):
9 ["basedir", "C", None, "which directory to start the node in"],
12 class StopOptions(BasedirMixin, usage.Options):
14 ["basedir", "C", None, "which directory to stop the node in"],
17 class RestartOptions(BasedirMixin, usage.Options):
19 ["basedir", "C", None, "which directory to restart the node in"],
22 ["force", "f", "if the node is not already running, start it "
23 "instead of complaining that you should have used 'start' instead "
30 return subprocess.call(["python", loc,], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
35 for maybetwistd in which("twistd"):
36 ret = testtwistd(maybetwistd)
40 for maybetwistd in which("twistd.py"):
41 ret = testtwistd(maybetwistd)
45 maybetwistd = os.path.join(sys.prefix, 'Scripts', 'twistd')
46 ret = testtwistd(maybetwistd)
50 maybetwistd = os.path.join(sys.prefix, 'Scripts', 'twistd.py')
51 ret = testtwistd(maybetwistd)
55 print "Can't find twistd (it comes with Twisted). Aborting."
59 def do_start(basedir, config, out=sys.stdout, err=sys.stderr):
60 print >>out, "STARTING", basedir
61 if os.path.exists(os.path.join(basedir, "client.tac")):
64 elif os.path.exists(os.path.join(basedir, "introducer.tac")):
65 tac = "introducer.tac"
68 print >>err, "%s does not look like a node directory" % basedir
69 if not os.path.isdir(basedir):
70 print >>err, " in fact, it doesn't look like a directory at all!"
72 twistd = find_twistd()
73 os.mkdir(os.path.join(basedir, "logs"))
74 cmd = ["python", twistd, "-y", tac, "--logfile", "logs/twistd.log"]
75 rc = subprocess.call(cmd, cwd=basedir)
77 print >>out, "%s node probably started" % type
80 print >>err, "%s node probably not started" % type
83 def do_stop(basedir, config, out=sys.stdout, err=sys.stderr):
84 print >>out, "STOPPING", basedir
85 pidfile = os.path.join(basedir, "twistd.pid")
86 if not os.path.exists(pidfile):
87 print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
89 pid = open(pidfile, "r").read()
93 os.kill(pid, signal.SIGTERM)
96 # poll once per second until twistd.pid goes away, up to 5 seconds
100 print >>out, "process %d is dead" % pid
104 print >>err, "never saw process go away"
107 def start(config, stdout, stderr):
109 for basedir in config['basedirs']:
110 rc = do_start(basedir, config, stdout, stderr) or rc
113 def stop(config, stdout, stderr):
115 for basedir in config['basedirs']:
116 rc = do_stop(basedir, config, stdout, stderr) or rc
119 def restart(config, stdout, stderr):
121 for basedir in config['basedirs']:
122 rc = do_stop(basedir, config, stdout, stderr) or rc
123 if rc == 2 and config['force']:
124 print >>stderr, "ignoring couldn't-stop"
127 print >>stderr, "not restarting"
129 for basedir in config['basedirs']:
130 rc = do_start(basedir, config, stdout, stderr) or rc
135 ["start", None, StartOptions, "Start a node (of any type)."],
136 ["stop", None, StopOptions, "Stop a node."],
137 ["restart", None, RestartOptions, "Restart a node."],