2 import os, sys, signal, time, subprocess
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 "
31 return subprocess.call(["python", loc,], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
36 for maybetwistd in which("twistd"):
37 ret = testtwistd(maybetwistd)
41 for maybetwistd in which("twistd.py"):
42 ret = testtwistd(maybetwistd)
46 maybetwistd = os.path.join(sys.prefix, 'Scripts', 'twistd')
47 ret = testtwistd(maybetwistd)
51 maybetwistd = os.path.join(sys.prefix, 'Scripts', 'twistd.py')
52 ret = testtwistd(maybetwistd)
56 print "Can't find twistd (it comes with Twisted). Aborting."
60 def do_start(basedir, config, out=sys.stdout, err=sys.stderr):
61 print >>out, "STARTING", basedir
62 if os.path.exists(os.path.join(basedir, "client.tac")):
65 elif os.path.exists(os.path.join(basedir, "introducer.tac")):
66 tac = "introducer.tac"
69 print >>err, "%s does not look like a node directory" % basedir
70 if not os.path.isdir(basedir):
71 print >>err, " in fact, it doesn't look like a directory at all!"
73 twistd = find_twistd()
74 fileutil.make_dirs(os.path.join(basedir, "logs"))
75 cmd = ["python", twistd, "-y", tac, "--logfile", "logs/twistd.log"]
76 rc = subprocess.call(cmd, cwd=basedir)
78 print >>out, "%s node probably started" % type
81 print >>err, "%s node probably not started" % type
84 def do_stop(basedir, config, out=sys.stdout, err=sys.stderr):
85 print >>out, "STOPPING", basedir
86 pidfile = os.path.join(basedir, "twistd.pid")
87 if not os.path.exists(pidfile):
88 print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
90 pid = open(pidfile, "r").read()
95 os.kill(pid, signal.SIGTERM)
96 except OSError, oserr:
104 # poll once per second until twistd.pid goes away, up to 5 seconds
108 print >>out, "process %d is dead" % pid
112 print >>err, "never saw process go away"
115 def start(config, stdout, stderr):
117 for basedir in config['basedirs']:
118 rc = do_start(basedir, config, stdout, stderr) or rc
121 def stop(config, stdout, stderr):
123 for basedir in config['basedirs']:
124 rc = do_stop(basedir, config, stdout, stderr) or rc
127 def restart(config, stdout, stderr):
129 for basedir in config['basedirs']:
130 rc = do_stop(basedir, config, stdout, stderr) or rc
131 if rc == 2 and config['force']:
132 print >>stderr, "ignoring couldn't-stop"
135 print >>stderr, "not restarting"
137 for basedir in config['basedirs']:
138 rc = do_start(basedir, config, stdout, stderr) or rc
143 ["start", None, StartOptions, "Start a node (of any type)."],
144 ["stop", None, StopOptions, "Stop a node."],
145 ["restart", None, RestartOptions, "Restart a node."],