2007-03-29 21:01:28 +00:00
|
|
|
#! /usr/bin/env python
|
2006-12-05 19:25:23 +00:00
|
|
|
|
|
|
|
import os, sys, signal, time
|
|
|
|
from twisted.python import usage
|
|
|
|
|
|
|
|
class StartOptions(usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["basedir", "C", ".", "which directory to start the node in"],
|
|
|
|
]
|
|
|
|
|
|
|
|
class StopOptions(usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["basedir", "C", ".", "which directory to stop the node in"],
|
|
|
|
]
|
|
|
|
|
|
|
|
class RestartOptions(usage.Options):
|
|
|
|
optParameters = [
|
|
|
|
["basedir", "C", ".", "which directory to restart the node in"],
|
|
|
|
]
|
|
|
|
|
|
|
|
class CreateClientOptions(usage.Options):
|
|
|
|
optParameters = [
|
2006-12-05 19:35:15 +00:00
|
|
|
["basedir", "C", None, "which directory to create the client in"],
|
2006-12-05 19:25:23 +00:00
|
|
|
]
|
2007-04-24 04:17:13 +00:00
|
|
|
optFlags = [
|
|
|
|
["quiet", "q", "operate silently"],
|
|
|
|
]
|
2006-12-05 19:35:15 +00:00
|
|
|
|
|
|
|
def parseArgs(self, *args):
|
|
|
|
if len(args) > 0:
|
|
|
|
self['basedir'] = args[0]
|
|
|
|
if len(args) > 1:
|
|
|
|
raise usage.UsageError("I wasn't expecting so many arguments")
|
|
|
|
|
|
|
|
def postOptions(self):
|
|
|
|
if self['basedir'] is None:
|
|
|
|
raise usage.UsageError("<basedir> parameter is required")
|
|
|
|
self['basedir'] = os.path.abspath(self['basedir'])
|
|
|
|
|
2007-04-20 00:30:21 +00:00
|
|
|
class CreateIntroducerOptions(usage.Options):
|
2006-12-05 19:25:23 +00:00
|
|
|
optParameters = [
|
2007-04-20 00:30:21 +00:00
|
|
|
["basedir", "C", None, "which directory to create the introducer in"],
|
2006-12-05 19:25:23 +00:00
|
|
|
]
|
2007-04-24 04:17:13 +00:00
|
|
|
optFlags = [
|
|
|
|
["quiet", "q", "operate silently"],
|
|
|
|
]
|
2006-12-05 19:25:23 +00:00
|
|
|
|
2006-12-05 19:35:15 +00:00
|
|
|
def parseArgs(self, *args):
|
|
|
|
if len(args) > 0:
|
|
|
|
self['basedir'] = args[0]
|
|
|
|
if len(args) > 1:
|
|
|
|
raise usage.UsageError("I wasn't expecting so many arguments")
|
|
|
|
|
|
|
|
def postOptions(self):
|
|
|
|
if self['basedir'] is None:
|
|
|
|
raise usage.UsageError("<basedir> parameter is required")
|
|
|
|
self['basedir'] = os.path.abspath(self['basedir'])
|
|
|
|
|
2006-12-05 19:25:23 +00:00
|
|
|
client_tac = """
|
|
|
|
# -*- python -*-
|
|
|
|
|
|
|
|
from allmydata import client
|
|
|
|
from twisted.application import service
|
|
|
|
|
|
|
|
c = client.Client()
|
|
|
|
|
|
|
|
application = service.Application("allmydata_client")
|
|
|
|
c.setServiceParent(application)
|
|
|
|
"""
|
|
|
|
|
2007-04-20 00:30:21 +00:00
|
|
|
introducer_tac = """
|
2006-12-05 19:25:23 +00:00
|
|
|
# -*- python -*-
|
|
|
|
|
|
|
|
from allmydata import queen
|
|
|
|
from twisted.application import service
|
|
|
|
|
|
|
|
c = queen.Queen()
|
|
|
|
|
2007-04-20 00:30:21 +00:00
|
|
|
application = service.Application("allmydata_introducer")
|
2006-12-05 19:25:23 +00:00
|
|
|
c.setServiceParent(application)
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Options(usage.Options):
|
|
|
|
synopsis = "Usage: allmydata <command> [command options]"
|
|
|
|
|
2006-12-05 19:35:15 +00:00
|
|
|
subCommands = [
|
|
|
|
["create-client", None, CreateClientOptions, "Create a client node."],
|
2007-04-20 00:30:21 +00:00
|
|
|
["create-introducer", None, CreateIntroducerOptions, "Create a introducer node."],
|
2006-12-05 19:35:15 +00:00
|
|
|
["start", None, StartOptions, "Start a node (of any type)."],
|
|
|
|
["stop", None, StopOptions, "Stop a node."],
|
|
|
|
["restart", None, RestartOptions, "Restart a node."],
|
2006-12-05 19:25:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def postOptions(self):
|
|
|
|
if not hasattr(self, 'subOptions'):
|
|
|
|
raise usage.UsageError("must specify a command")
|
|
|
|
|
2007-04-24 04:28:19 +00:00
|
|
|
def runner(argv, run_by_human=True):
|
2006-12-05 19:25:23 +00:00
|
|
|
config = Options()
|
|
|
|
try:
|
2007-04-20 01:56:45 +00:00
|
|
|
config.parseOptions(argv)
|
2006-12-05 19:25:23 +00:00
|
|
|
except usage.error, e:
|
2007-04-24 04:28:19 +00:00
|
|
|
if not run_by_human:
|
|
|
|
raise
|
2006-12-05 19:25:23 +00:00
|
|
|
print "%s: %s" % (sys.argv[0], e)
|
|
|
|
print
|
|
|
|
c = getattr(config, 'subOptions', config)
|
|
|
|
print str(c)
|
2007-04-20 01:56:45 +00:00
|
|
|
return 1
|
2006-12-05 19:25:23 +00:00
|
|
|
|
|
|
|
command = config.subCommand
|
|
|
|
so = config.subOptions
|
|
|
|
|
|
|
|
if command == "create-client":
|
|
|
|
rc = create_client(so)
|
2007-04-20 00:30:21 +00:00
|
|
|
elif command == "create-introducer":
|
|
|
|
rc = create_introducer(so)
|
2006-12-05 19:25:23 +00:00
|
|
|
elif command == "start":
|
|
|
|
rc = start(so)
|
|
|
|
elif command == "stop":
|
|
|
|
rc = stop(so)
|
|
|
|
elif command == "restart":
|
|
|
|
rc = restart(so)
|
|
|
|
rc = rc or 0
|
2007-04-20 01:56:45 +00:00
|
|
|
return rc
|
|
|
|
|
|
|
|
def run():
|
|
|
|
rc = runner(sys.argv[1:])
|
2006-12-05 19:25:23 +00:00
|
|
|
sys.exit(rc)
|
|
|
|
|
|
|
|
def create_client(config):
|
|
|
|
basedir = config['basedir']
|
2007-03-29 21:32:28 +00:00
|
|
|
if os.path.exists(basedir):
|
|
|
|
if os.listdir(basedir):
|
|
|
|
print "The base directory already exists: %s" % basedir
|
|
|
|
print "To avoid clobbering anything, I am going to quit now"
|
|
|
|
print "Please use a different directory, or delete this one"
|
|
|
|
return -1
|
|
|
|
# we're willing to use an empty directory
|
|
|
|
else:
|
|
|
|
os.mkdir(basedir)
|
2006-12-05 19:25:23 +00:00
|
|
|
f = open(os.path.join(basedir, "client.tac"), "w")
|
|
|
|
f.write(client_tac)
|
|
|
|
f.close()
|
2007-04-24 04:17:13 +00:00
|
|
|
if not config['quiet']:
|
|
|
|
print "client created in %s" % basedir
|
|
|
|
print " please copy introducer.furl and vdrive.furl into the directory"
|
2006-12-05 19:25:23 +00:00
|
|
|
|
2007-04-20 00:30:21 +00:00
|
|
|
def create_introducer(config):
|
2006-12-05 19:25:23 +00:00
|
|
|
basedir = config['basedir']
|
2007-03-29 21:32:28 +00:00
|
|
|
if os.path.exists(basedir):
|
|
|
|
if os.listdir(basedir):
|
|
|
|
print "The base directory already exists: %s" % basedir
|
|
|
|
print "To avoid clobbering anything, I am going to quit now"
|
|
|
|
print "Please use a different directory, or delete this one"
|
|
|
|
return -1
|
|
|
|
# we're willing to use an empty directory
|
|
|
|
else:
|
|
|
|
os.mkdir(basedir)
|
2007-04-20 00:30:21 +00:00
|
|
|
f = open(os.path.join(basedir, "introducer.tac"), "w")
|
|
|
|
f.write(introducer_tac)
|
2006-12-05 19:25:23 +00:00
|
|
|
f.close()
|
2007-04-24 04:17:13 +00:00
|
|
|
if not config['quiet']:
|
|
|
|
print "introducer created in %s" % basedir
|
2006-12-05 19:25:23 +00:00
|
|
|
|
|
|
|
def start(config):
|
|
|
|
basedir = config['basedir']
|
|
|
|
if os.path.exists(os.path.join(basedir, "client.tac")):
|
|
|
|
tac = "client.tac"
|
|
|
|
type = "client"
|
2007-04-20 00:30:21 +00:00
|
|
|
elif os.path.exists(os.path.join(basedir, "introducer.tac")):
|
|
|
|
tac = "introducer.tac"
|
|
|
|
type = "introducer"
|
2006-12-05 19:25:23 +00:00
|
|
|
else:
|
|
|
|
print "%s does not look like a node directory" % basedir
|
|
|
|
sys.exit(1)
|
|
|
|
os.chdir(basedir)
|
|
|
|
rc = os.system("twistd -y %s" % tac)
|
|
|
|
if rc == 0:
|
2007-04-20 00:30:21 +00:00
|
|
|
print "%s node probably started" % type
|
2006-12-06 02:43:52 +00:00
|
|
|
return 0
|
2006-12-05 19:25:23 +00:00
|
|
|
else:
|
2007-04-20 00:30:21 +00:00
|
|
|
print "%s node probably not started" % type
|
2006-12-06 02:43:52 +00:00
|
|
|
return 1
|
2006-12-05 19:25:23 +00:00
|
|
|
|
|
|
|
def stop(config):
|
|
|
|
basedir = config['basedir']
|
|
|
|
pidfile = os.path.join(basedir, "twistd.pid")
|
|
|
|
if not os.path.exists(pidfile):
|
|
|
|
print "%s does not look like a running node directory (no twistd.pid)" % basedir
|
|
|
|
return 1
|
|
|
|
pid = open(pidfile, "r").read()
|
|
|
|
pid = int(pid)
|
|
|
|
|
|
|
|
timer = 0
|
2006-12-05 19:37:58 +00:00
|
|
|
os.kill(pid, signal.SIGTERM)
|
2006-12-05 19:25:23 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
while timer < 5:
|
|
|
|
# poll once per second until twistd.pid goes away, up to 5 seconds
|
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError:
|
|
|
|
print "process %d is dead" % pid
|
|
|
|
return
|
|
|
|
timer += 1
|
|
|
|
time.sleep(1)
|
|
|
|
print "never saw process go away"
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def restart(config):
|
|
|
|
rc = stop(config)
|
|
|
|
if rc:
|
|
|
|
print "not restarting"
|
|
|
|
return rc
|
|
|
|
return start(config)
|