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 ["profile", "p", "whether to run under the Python profiler, putting results in \"profiling_results.prof\""],
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 ["force", "f", "if the node is not already running, start it "
27 "instead of complaining that you should have used 'start' instead "
29 ["profile", "p", "whether to run under the Python profiler, putting results in \"profiling_results.prof\""],
32 def do_start(basedir, profile=False, out=sys.stdout, err=sys.stderr):
33 print >>out, "STARTING", basedir
34 if os.path.exists(os.path.join(basedir, "client.tac")):
37 elif os.path.exists(os.path.join(basedir, "introducer.tac")):
38 tac = "introducer.tac"
41 print >>err, "%s does not look like a node directory" % basedir
42 if not os.path.isdir(basedir):
43 print >>err, " in fact, it doesn't look like a directory at all!"
45 twistds = which("twistd")
46 twistd = twistds and twistds[0]
48 twistd = os.path.join(sys.prefix, 'Scripts', 'twistd.py')
49 if not os.path.exists(twistd):
50 print "Can't find twistd (it comes with Twisted). Aborting."
52 path, ext = os.path.splitext(twistd)
53 if ext.lower() in [".exe", ".bat",]:
56 cmd = [sys.executable, twistd,]
58 fileutil.make_dirs(os.path.join(basedir, "logs"))
59 cmd.extend(["-y", tac, "--logfile", os.path.join("logs", "twistd.log")])
61 cmd.extend(["--profile=profiling_results.prof", "--savestats",])
65 rc = os.system(' '.join(cmd))
69 print >>out, "%s node probably started" % type
72 print >>err, "%s node probably not started" % type
75 def do_stop(basedir, out=sys.stdout, err=sys.stderr):
76 print >>out, "STOPPING", basedir
77 pidfile = os.path.join(basedir, "twistd.pid")
78 if not os.path.exists(pidfile):
79 print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
81 pid = open(pidfile, "r").read()
86 os.kill(pid, signal.SIGTERM)
87 except OSError, oserr:
95 # poll once per second until twistd.pid goes away, up to 5 seconds
99 print >>out, "process %d is dead" % pid
103 print >>err, "never saw process go away"
106 def start(config, stdout, stderr):
108 for basedir in config['basedirs']:
109 rc = do_start(basedir, config['profile'], stdout, stderr) or rc
112 def stop(config, stdout, stderr):
114 for basedir in config['basedirs']:
115 rc = do_stop(basedir, stdout, stderr) or rc
118 def restart(config, stdout, stderr):
120 for basedir in config['basedirs']:
121 rc = do_stop(basedir, stdout, stderr) or rc
122 if rc == 2 and config['force']:
123 print >>stderr, "ignoring couldn't-stop"
126 print >>stderr, "not restarting"
128 for basedir in config['basedirs']:
129 rc = do_start(basedir, config['profile'], stdout, stderr) or rc
134 ["start", None, StartOptions, "Start a node (of any type)."],
135 ["stop", None, StopOptions, "Stop a node."],
136 ["restart", None, RestartOptions, "Restart a node."],