2 import os, sys, signal, time
3 from allmydata.scripts.common import BasedirOptions
4 from allmydata.util import fileutil
5 from allmydata.util.assertutil import precondition
6 from allmydata.util.encodingutil import listdir_unicode, quote_output
9 class StartOptions(BasedirOptions):
11 ["profile", "p", "Run under the Python profiler, putting results in 'profiling_results.prof'."],
12 ["syslog", None, "Tell the node to log to syslog, not a file."],
15 def getSynopsis(self):
16 return "Usage: %s start [options] [NODEDIR]" % (self.command_name,)
19 class StopOptions(BasedirOptions):
20 def getSynopsis(self):
21 return "Usage: %s stop [options] [NODEDIR]" % (self.command_name,)
24 class RestartOptions(BasedirOptions):
26 ["profile", "p", "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 def getSynopsis(self):
31 return "Usage: %s restart [options] [NODEDIR]" % (self.command_name,)
34 class RunOptions(BasedirOptions):
35 default_nodedir = u"."
37 def getSynopsis(self):
38 return "Usage: %s run [options] [NODEDIR]" % (self.command_name,)
41 def start(opts, out=sys.stdout, err=sys.stderr):
42 basedir = opts['basedir']
43 print >>out, "STARTING", quote_output(basedir)
44 if not os.path.isdir(basedir):
45 print >>err, "%s does not look like a directory at all" % quote_output(basedir)
47 for fn in listdir_unicode(basedir):
48 if fn.endswith(u".tac"):
52 print >>err, "%s does not look like a node directory (no .tac file)" % quote_output(basedir)
56 elif "introducer" in tac:
57 nodetype = "introducer"
59 nodetype = "unknown (%s)" % tac
61 args = ["twistd", "-y", tac]
63 args.append("--syslog")
64 elif nodetype in ("client", "introducer"):
65 fileutil.make_dirs(os.path.join(basedir, "logs"))
66 args.extend(["--logfile", os.path.join("logs", "twistd.log")])
68 args.extend(["--profile=profiling_results.prof", "--savestats",])
71 from twisted.scripts import twistd
74 # run() doesn't return: the parent does os._exit(0) in daemonize(), so
75 # we'll never get here. If application setup fails (e.g. ImportError),
76 # run() will raise an exception.
78 def stop(config, out=sys.stdout, err=sys.stderr):
79 basedir = config['basedir']
80 print >>out, "STOPPING", quote_output(basedir)
81 pidfile = os.path.join(basedir, "twistd.pid")
82 if not os.path.exists(pidfile):
83 print >>err, "%s does not look like a running node directory (no twistd.pid)" % quote_output(basedir)
84 # we define rc=2 to mean "nothing is running, but it wasn't me who
87 pid = open(pidfile, "r").read()
90 # kill it hard (SIGKILL), delete the twistd.pid file, then wait for the
91 # process itself to go away. If it hasn't gone away after 20 seconds, warn
92 # the user but keep waiting until they give up.
94 os.kill(pid, signal.SIGKILL)
95 except OSError, oserr:
98 # the process didn't exist, so wipe the pid file
105 except EnvironmentError:
112 # poll once per second until we see the process is no longer running
116 print >>out, "process %d is dead" % pid
121 print >>err, ("It looks like pid %d is still running "
122 "after %d seconds" % (pid,
123 (time.time() - start)))
124 print >>err, "I will keep watching it until you interrupt me."
128 print >>err, "pid %d still running after %d seconds" % \
129 (pid, (time.time() - start))
132 # we define rc=1 to mean "I think something is still running, sorry"
135 def restart(config, stdout, stderr):
136 rc = stop(config, stdout, stderr)
138 print >>stderr, "ignoring couldn't-stop"
141 print >>stderr, "not restarting"
143 return start(config, stdout, stderr)
145 def run(config, stdout, stderr):
146 from twisted.internet import reactor
147 from twisted.python import log, logfile
148 from allmydata import client
150 basedir = config['basedir']
151 precondition(isinstance(basedir, unicode), basedir)
153 if not os.path.isdir(basedir):
154 print >>stderr, "%s does not look like a directory at all" % quote_output(basedir)
156 for fn in listdir_unicode(basedir):
157 if fn.endswith(u".tac"):
161 print >>stderr, "%s does not look like a node directory (no .tac file)" % quote_output(basedir)
163 if "client" not in tac:
164 print >>stderr, ("%s looks like it contains a non-client node (%s).\n"
165 "Use 'tahoe start' instead of 'tahoe run'."
166 % (quote_output(basedir), tac))
171 # set up twisted logging. this will become part of the node rsn.
172 logdir = os.path.join(basedir, 'logs')
173 if not os.path.exists(logdir):
175 lf = logfile.LogFile('tahoesvc.log', logdir)
178 # run the node itself
179 c = client.Client(basedir)
180 reactor.callLater(0, c.startService) # after reactor startup
187 ["start", None, StartOptions, "Start a node (of any type)."],
188 ["stop", None, StopOptions, "Stop a node."],
189 ["restart", None, RestartOptions, "Restart a node."],
190 ["run", None, RunOptions, "Run a node synchronously."],