3 import os, sys, signal, time
4 from twisted.python import usage
6 class StartOptions(usage.Options):
8 ["basedir", "C", ".", "which directory to start the node in"],
11 class StopOptions(usage.Options):
13 ["basedir", "C", ".", "which directory to stop the node in"],
16 class RestartOptions(usage.Options):
18 ["basedir", "C", ".", "which directory to restart the node in"],
21 class CreateClientOptions(usage.Options):
23 ["basedir", "C", None, "which directory to create the client in"],
26 def parseArgs(self, *args):
28 self['basedir'] = args[0]
30 raise usage.UsageError("I wasn't expecting so many arguments")
32 def postOptions(self):
33 if self['basedir'] is None:
34 raise usage.UsageError("<basedir> parameter is required")
35 self['basedir'] = os.path.abspath(self['basedir'])
37 class CreateIntroducerOptions(usage.Options):
39 ["basedir", "C", None, "which directory to create the introducer in"],
42 def parseArgs(self, *args):
44 self['basedir'] = args[0]
46 raise usage.UsageError("I wasn't expecting so many arguments")
48 def postOptions(self):
49 if self['basedir'] is None:
50 raise usage.UsageError("<basedir> parameter is required")
51 self['basedir'] = os.path.abspath(self['basedir'])
56 from allmydata import client
57 from twisted.application import service
61 application = service.Application("allmydata_client")
62 c.setServiceParent(application)
68 from allmydata import queen
69 from twisted.application import service
73 application = service.Application("allmydata_introducer")
74 c.setServiceParent(application)
77 class Options(usage.Options):
78 synopsis = "Usage: allmydata <command> [command options]"
81 ["create-client", None, CreateClientOptions, "Create a client node."],
82 ["create-introducer", None, CreateIntroducerOptions, "Create a introducer node."],
83 ["start", None, StartOptions, "Start a node (of any type)."],
84 ["stop", None, StopOptions, "Stop a node."],
85 ["restart", None, RestartOptions, "Restart a node."],
88 def postOptions(self):
89 if not hasattr(self, 'subOptions'):
90 raise usage.UsageError("must specify a command")
96 except usage.error, e:
97 print "%s: %s" % (sys.argv[0], e)
99 c = getattr(config, 'subOptions', config)
103 command = config.subCommand
104 so = config.subOptions
106 if command == "create-client":
107 rc = create_client(so)
108 elif command == "create-introducer":
109 rc = create_introducer(so)
110 elif command == "start":
112 elif command == "stop":
114 elif command == "restart":
119 def create_client(config):
120 basedir = config['basedir']
121 if os.path.exists(basedir):
122 if os.listdir(basedir):
123 print "The base directory already exists: %s" % basedir
124 print "To avoid clobbering anything, I am going to quit now"
125 print "Please use a different directory, or delete this one"
127 # we're willing to use an empty directory
130 f = open(os.path.join(basedir, "client.tac"), "w")
133 print "client created in %s" % basedir
134 print " please copy introducer.furl and vdrive.furl into the directory"
136 def create_introducer(config):
137 basedir = config['basedir']
138 if os.path.exists(basedir):
139 if os.listdir(basedir):
140 print "The base directory already exists: %s" % basedir
141 print "To avoid clobbering anything, I am going to quit now"
142 print "Please use a different directory, or delete this one"
144 # we're willing to use an empty directory
147 f = open(os.path.join(basedir, "introducer.tac"), "w")
148 f.write(introducer_tac)
150 print "introducer created in %s" % basedir
153 basedir = config['basedir']
154 if os.path.exists(os.path.join(basedir, "client.tac")):
157 elif os.path.exists(os.path.join(basedir, "introducer.tac")):
158 tac = "introducer.tac"
161 print "%s does not look like a node directory" % basedir
164 rc = os.system("twistd -y %s" % tac)
166 print "%s node probably started" % type
169 print "%s node probably not started" % type
173 basedir = config['basedir']
174 pidfile = os.path.join(basedir, "twistd.pid")
175 if not os.path.exists(pidfile):
176 print "%s does not look like a running node directory (no twistd.pid)" % basedir
178 pid = open(pidfile, "r").read()
182 os.kill(pid, signal.SIGTERM)
185 # poll once per second until twistd.pid goes away, up to 5 seconds
189 print "process %d is dead" % pid
193 print "never saw process go away"
199 print "not restarting"