2007-07-11 02:05:18 +00:00
|
|
|
|
2007-09-20 19:37:50 +00:00
|
|
|
import os, sys, signal, time
|
2007-07-11 02:05:18 +00:00
|
|
|
from twisted.python import usage
|
|
|
|
from allmydata.scripts.common import BasedirMixin
|
2007-10-15 17:25:15 +00:00
|
|
|
from allmydata.util import fileutil, find_exe
|
2007-07-11 02:05:18 +00:00
|
|
|
|
|
|
|
class StartOptions(BasedirMixin, usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["basedir", "C", None, "which directory to start the node in"],
|
|
|
|
]
|
2007-09-21 22:03:47 +00:00
|
|
|
optFlags = [
|
|
|
|
["profile", "p", "whether to run under the Python profiler, putting results in \"profiling_results.prof\""],
|
|
|
|
]
|
2007-07-11 02:05:18 +00:00
|
|
|
|
|
|
|
class StopOptions(BasedirMixin, usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["basedir", "C", None, "which directory to stop the node in"],
|
|
|
|
]
|
|
|
|
|
|
|
|
class RestartOptions(BasedirMixin, usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["basedir", "C", None, "which directory to restart the node in"],
|
|
|
|
]
|
|
|
|
optFlags = [
|
|
|
|
["force", "f", "if the node is not already running, start it "
|
|
|
|
"instead of complaining that you should have used 'start' instead "
|
|
|
|
"of 'restart'"],
|
2007-09-21 22:48:15 +00:00
|
|
|
["profile", "p", "whether to run under the Python profiler, putting results in \"profiling_results.prof\""],
|
|
|
|
]
|
2007-07-11 02:05:18 +00:00
|
|
|
|
2008-01-10 02:54:12 +00:00
|
|
|
class RunOptions(usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["basedir", "C", None, "which directory to run the node in, CWD by default"],
|
|
|
|
]
|
|
|
|
|
2007-09-21 22:03:47 +00:00
|
|
|
def do_start(basedir, profile=False, out=sys.stdout, err=sys.stderr):
|
2007-07-11 02:05:18 +00:00
|
|
|
print >>out, "STARTING", basedir
|
|
|
|
if os.path.exists(os.path.join(basedir, "client.tac")):
|
|
|
|
tac = "client.tac"
|
|
|
|
type = "client"
|
2007-10-11 09:48:06 +00:00
|
|
|
elif os.path.exists(os.path.join(basedir, "tahoe-client.tac")):
|
|
|
|
tac = "tahoe-client.tac"
|
|
|
|
type = "client"
|
2007-07-11 02:05:18 +00:00
|
|
|
elif os.path.exists(os.path.join(basedir, "introducer.tac")):
|
|
|
|
tac = "introducer.tac"
|
|
|
|
type = "introducer"
|
2007-10-11 09:48:06 +00:00
|
|
|
elif os.path.exists(os.path.join(basedir, "tahoe-introducer.tac")):
|
|
|
|
tac = "tahoe-introducer.tac"
|
|
|
|
type = "introducer"
|
2007-07-11 02:05:18 +00:00
|
|
|
else:
|
|
|
|
print >>err, "%s does not look like a node directory" % basedir
|
|
|
|
if not os.path.isdir(basedir):
|
|
|
|
print >>err, " in fact, it doesn't look like a directory at all!"
|
2007-09-19 08:50:27 +00:00
|
|
|
return 1
|
2007-11-01 22:28:50 +00:00
|
|
|
|
2007-10-15 17:25:15 +00:00
|
|
|
cmd = find_exe.find_exe('twistd')
|
|
|
|
if not cmd:
|
2007-09-20 19:37:50 +00:00
|
|
|
print "Can't find twistd (it comes with Twisted). Aborting."
|
|
|
|
sys.exit(1)
|
2007-10-15 17:25:15 +00:00
|
|
|
|
2007-08-09 04:43:48 +00:00
|
|
|
fileutil.make_dirs(os.path.join(basedir, "logs"))
|
2007-09-20 19:37:50 +00:00
|
|
|
cmd.extend(["-y", tac, "--logfile", os.path.join("logs", "twistd.log")])
|
2007-09-21 22:03:47 +00:00
|
|
|
if profile:
|
|
|
|
cmd.extend(["--profile=profiling_results.prof", "--savestats",])
|
2007-09-20 19:37:50 +00:00
|
|
|
curdir = os.getcwd()
|
|
|
|
try:
|
|
|
|
os.chdir(basedir)
|
|
|
|
rc = os.system(' '.join(cmd))
|
|
|
|
finally:
|
|
|
|
os.chdir(curdir)
|
2007-07-11 02:05:18 +00:00
|
|
|
if rc == 0:
|
|
|
|
print >>out, "%s node probably started" % type
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
print >>err, "%s node probably not started" % type
|
|
|
|
return 1
|
|
|
|
|
2007-09-21 22:03:15 +00:00
|
|
|
def do_stop(basedir, out=sys.stdout, err=sys.stderr):
|
2007-07-11 02:05:18 +00:00
|
|
|
print >>out, "STOPPING", basedir
|
|
|
|
pidfile = os.path.join(basedir, "twistd.pid")
|
|
|
|
if not os.path.exists(pidfile):
|
|
|
|
print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
|
|
|
|
return 2
|
|
|
|
pid = open(pidfile, "r").read()
|
|
|
|
pid = int(pid)
|
|
|
|
|
2007-10-29 18:43:35 +00:00
|
|
|
# kill it hard (SIGKILL), delete the twistd.pid file, then wait for the
|
|
|
|
# process itself to go away. If it hasn't gone away after 5 seconds, warn
|
|
|
|
# the user but keep waiting until they give up.
|
2007-08-09 15:57:45 +00:00
|
|
|
try:
|
2007-10-29 18:43:35 +00:00
|
|
|
os.kill(pid, signal.SIGKILL)
|
2007-08-09 15:57:45 +00:00
|
|
|
except OSError, oserr:
|
|
|
|
if oserr.errno == 3:
|
|
|
|
print oserr.strerror
|
2007-10-29 18:43:35 +00:00
|
|
|
# the process didn't exist, so wipe the pid file
|
|
|
|
os.remove(pidfile)
|
2007-08-09 15:57:45 +00:00
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
raise
|
2007-10-29 18:43:35 +00:00
|
|
|
try:
|
|
|
|
os.remove(pidfile)
|
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
start = time.time()
|
2007-07-11 02:05:18 +00:00
|
|
|
time.sleep(0.1)
|
2007-10-29 18:43:35 +00:00
|
|
|
wait = 5
|
|
|
|
first_time = True
|
|
|
|
while True:
|
|
|
|
# poll once per second until we see the process is no longer running
|
2007-07-11 02:05:18 +00:00
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError:
|
|
|
|
print >>out, "process %d is dead" % pid
|
|
|
|
return
|
2007-10-29 18:43:35 +00:00
|
|
|
wait -= 1
|
|
|
|
if wait < 0:
|
|
|
|
if first_time:
|
|
|
|
print >>err, ("It looks like pid %d is still running "
|
|
|
|
"after %d seconds" % (pid,
|
|
|
|
(time.time() - start)))
|
|
|
|
print >>err, "I will keep watching it until you interrupt me."
|
|
|
|
wait = 10
|
|
|
|
first_time = False
|
|
|
|
else:
|
|
|
|
print >>err, "pid %d still running after %d seconds" % \
|
|
|
|
(pid, (time.time() - start))
|
|
|
|
wait = 10
|
2007-07-11 02:05:18 +00:00
|
|
|
time.sleep(1)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def start(config, stdout, stderr):
|
|
|
|
rc = 0
|
|
|
|
for basedir in config['basedirs']:
|
2007-09-21 22:03:47 +00:00
|
|
|
rc = do_start(basedir, config['profile'], stdout, stderr) or rc
|
2007-07-11 02:05:18 +00:00
|
|
|
return rc
|
|
|
|
|
|
|
|
def stop(config, stdout, stderr):
|
|
|
|
rc = 0
|
|
|
|
for basedir in config['basedirs']:
|
2007-09-21 22:03:15 +00:00
|
|
|
rc = do_stop(basedir, stdout, stderr) or rc
|
2007-07-11 02:05:18 +00:00
|
|
|
return rc
|
|
|
|
|
|
|
|
def restart(config, stdout, stderr):
|
|
|
|
rc = 0
|
|
|
|
for basedir in config['basedirs']:
|
2007-09-21 22:03:15 +00:00
|
|
|
rc = do_stop(basedir, stdout, stderr) or rc
|
2007-07-11 02:05:18 +00:00
|
|
|
if rc == 2 and config['force']:
|
|
|
|
print >>stderr, "ignoring couldn't-stop"
|
|
|
|
rc = 0
|
|
|
|
if rc:
|
|
|
|
print >>stderr, "not restarting"
|
|
|
|
return rc
|
|
|
|
for basedir in config['basedirs']:
|
2007-09-21 22:48:15 +00:00
|
|
|
rc = do_start(basedir, config['profile'], stdout, stderr) or rc
|
2007-07-11 02:05:18 +00:00
|
|
|
return rc
|
|
|
|
|
2008-01-10 02:54:12 +00:00
|
|
|
def run(config, stdout, stderr):
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.python import log, logfile
|
|
|
|
from allmydata import client
|
|
|
|
|
|
|
|
basedir = config['basedir']
|
|
|
|
if basedir is None:
|
|
|
|
basedir = '.'
|
|
|
|
else:
|
|
|
|
os.chdir(basedir)
|
|
|
|
|
|
|
|
# set up twisted logging. this will become part of the node rsn.
|
|
|
|
logdir = os.path.join(basedir, 'logs')
|
|
|
|
if not os.path.exists(logdir):
|
|
|
|
os.makedirs(logdir)
|
|
|
|
lf = logfile.LogFile('tahoesvc.log', logdir)
|
|
|
|
log.startLogging(lf)
|
|
|
|
|
|
|
|
# run the node itself
|
|
|
|
c = client.Client(basedir)
|
|
|
|
reactor.callLater(c.startService) # after reactor startup
|
|
|
|
reactor.run()
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2007-07-11 02:05:18 +00:00
|
|
|
|
|
|
|
subCommands = [
|
|
|
|
["start", None, StartOptions, "Start a node (of any type)."],
|
|
|
|
["stop", None, StopOptions, "Stop a node."],
|
|
|
|
["restart", None, RestartOptions, "Restart a node."],
|
2008-01-10 02:54:12 +00:00
|
|
|
["run", None, RunOptions, "Run a node synchronously."],
|
2007-07-11 02:05:18 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
dispatch = {
|
|
|
|
"start": start,
|
|
|
|
"stop": stop,
|
|
|
|
"restart": restart,
|
2008-01-10 02:54:12 +00:00
|
|
|
"run": run,
|
2007-07-11 02:05:18 +00:00
|
|
|
}
|