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, find_exe
7 class StartOptions(BasedirMixin, usage.Options):
9 ["basedir", "C", None, "which directory to start the node in"],
12 ["profile", "p", "whether to run under the Python profiler, putting results in \"profiling_results.prof\""],
13 ["syslog", None, "tell the node to log to syslog, not a file"],
16 class StopOptions(BasedirMixin, usage.Options):
18 ["basedir", "C", None, "which directory to stop the node in"],
21 class RestartOptions(BasedirMixin, usage.Options):
23 ["basedir", "C", None, "which directory to restart the node in"],
26 ["profile", "p", "whether to run under the Python profiler, putting results in \"profiling_results.prof\""],
27 ["syslog", None, "tell the node to log to syslog, not a file"],
30 class RunOptions(usage.Options):
32 ["basedir", "C", None, "which directory to run the node in, CWD by default"],
35 def do_start(basedir, opts, out=sys.stdout, err=sys.stderr):
36 print >>out, "STARTING", basedir
37 if not os.path.isdir(basedir):
38 print >>err, "%s does not look like a directory at all" % basedir
40 for fn in os.listdir(basedir):
41 if fn.endswith(".tac"):
45 print >>err, "%s does not look like a node directory (no .tac file)" % basedir
49 elif "introducer" in tac:
50 nodetype = "introducer"
52 nodetype = "unknown (%s)" % tac
54 cmd = find_exe.find_exe('twistd')
56 # If 'twistd' wasn't on $PATH, maybe we're running from source and
57 # Twisted was built as one of our dependencies. If so, we're at
58 # BASEDIR/src/allmydata/scripts/startstop_node.py, and it's at
59 # BASEDIR/support/$BINDIR/twistd
61 TAHOEDIR = up(up(up(up(os.path.abspath(__file__)))))
62 if sys.platform == "win32":
66 bindir = os.path.join(TAHOEDIR, "support", bin_dir)
68 maybe = os.path.join(bindir, "twistd")
69 if os.path.exists(maybe):
71 oldpath = os.environ.get("PATH", "").split(os.pathsep)
72 os.environ["PATH"] = os.pathsep.join(oldpath + [bindir])
73 # sys.path and $PYTHONPATH are taken care of by the extra code in
77 if os.path.exists(maybe):
78 cmd = [sys.executable, maybe]
79 oldpath = os.environ.get("PATH", "").split(os.pathsep)
80 os.environ["PATH"] = os.pathsep.join(oldpath + [bindir])
81 # sys.path and $PYTHONPATH are taken care of by the extra code in
85 print "Can't find twistd (it comes with Twisted). Aborting."
88 cmd.extend(["-y", tac])
90 cmd.append("--syslog")
91 elif nodetype in ("client", "introducer"):
92 fileutil.make_dirs(os.path.join(basedir, "logs"))
93 cmd.extend(["--logfile", os.path.join("logs", "twistd.log")])
95 cmd.extend(["--profile=profiling_results.prof", "--savestats",])
99 rc = os.system(' '.join(cmd))
103 print >>out, "%s node probably started" % nodetype
106 print >>err, "%s node probably not started" % nodetype
109 def do_stop(basedir, out=sys.stdout, err=sys.stderr):
110 print >>out, "STOPPING", basedir
111 pidfile = os.path.join(basedir, "twistd.pid")
112 if not os.path.exists(pidfile):
113 print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
114 # we define rc=2 to mean "nothing is running, but it wasn't me who
117 pid = open(pidfile, "r").read()
120 # kill it hard (SIGKILL), delete the twistd.pid file, then wait for the
121 # process itself to go away. If it hasn't gone away after 20 seconds, warn
122 # the user but keep waiting until they give up.
124 os.kill(pid, signal.SIGKILL)
125 except OSError, oserr:
128 # the process didn't exist, so wipe the pid file
135 except EnvironmentError:
142 # poll once per second until we see the process is no longer running
146 print >>out, "process %d is dead" % pid
151 print >>err, ("It looks like pid %d is still running "
152 "after %d seconds" % (pid,
153 (time.time() - start)))
154 print >>err, "I will keep watching it until you interrupt me."
158 print >>err, "pid %d still running after %d seconds" % \
159 (pid, (time.time() - start))
162 # we define rc=1 to mean "I think something is still running, sorry"
165 def start(config, stdout, stderr):
167 for basedir in config['basedirs']:
168 rc = do_start(basedir, config, stdout, stderr) or rc
171 def stop(config, stdout, stderr):
173 for basedir in config['basedirs']:
174 rc = do_stop(basedir, stdout, stderr) or rc
177 def restart(config, stdout, stderr):
179 for basedir in config['basedirs']:
180 rc = do_stop(basedir, stdout, stderr) or rc
182 print >>stderr, "ignoring couldn't-stop"
185 print >>stderr, "not restarting"
187 for basedir in config['basedirs']:
188 rc = do_start(basedir, config, stdout, stderr) or rc
191 def run(config, stdout, stderr):
192 from twisted.internet import reactor
193 from twisted.python import log, logfile
194 from allmydata import client
196 basedir = config['basedir']
202 # set up twisted logging. this will become part of the node rsn.
203 logdir = os.path.join(basedir, 'logs')
204 if not os.path.exists(logdir):
206 lf = logfile.LogFile('tahoesvc.log', logdir)
209 # run the node itself
210 c = client.Client(basedir)
211 reactor.callLater(0, c.startService) # after reactor startup
218 ["start", None, StartOptions, "Start a node (of any type)."],
219 ["stop", None, StopOptions, "Stop a node."],
220 ["restart", None, RestartOptions, "Restart a node."],
221 ["run", None, RunOptions, "Run a node synchronously."],