]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/runner.py
c4fe8edb6aede9099572bf22106433cc3611e053
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / runner.py
1 #! /usr/bin/python
2
3 import os, sys, signal, time
4 from twisted.python import usage
5
6 class StartOptions(usage.Options):
7     optParameters = [
8         ["basedir", "C", ".", "which directory to start the node in"],
9         ]
10
11 class StopOptions(usage.Options):
12     optParameters = [
13         ["basedir", "C", ".", "which directory to stop the node in"],
14         ]
15
16 class RestartOptions(usage.Options):
17     optParameters = [
18         ["basedir", "C", ".", "which directory to restart the node in"],
19         ]
20
21 class CreateClientOptions(usage.Options):
22     optParameters = [
23         ["basedir", "C", None, "which directory to create the client in"],
24         ]
25
26     def parseArgs(self, *args):
27         if len(args) > 0:
28             self['basedir'] = args[0]
29         if len(args) > 1:
30             raise usage.UsageError("I wasn't expecting so many arguments")
31
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'])
36
37 class CreateQueenOptions(usage.Options):
38     optParameters = [
39         ["basedir", "C", None, "which directory to create the queen in"],
40         ]
41
42     def parseArgs(self, *args):
43         if len(args) > 0:
44             self['basedir'] = args[0]
45         if len(args) > 1:
46             raise usage.UsageError("I wasn't expecting so many arguments")
47
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'])
52
53 client_tac = """
54 # -*- python -*-
55
56 from allmydata import client
57 from twisted.application import service
58
59 c = client.Client()
60
61 application = service.Application("allmydata_client")
62 c.setServiceParent(application)
63 """
64
65 queen_tac = """
66 # -*- python -*-
67
68 from allmydata import queen
69 from twisted.application import service
70
71 c = queen.Queen()
72
73 application = service.Application("allmydata_queen")
74 c.setServiceParent(application)
75 """
76
77 class Options(usage.Options):
78     synopsis = "Usage:  allmydata <command> [command options]"
79
80     subCommands = [
81         ["create-client", None, CreateClientOptions, "Create a client node."],
82         ["create-queen", None, CreateQueenOptions, "Create a queen 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."],
86         ]
87
88     def postOptions(self):
89         if not hasattr(self, 'subOptions'):
90             raise usage.UsageError("must specify a command")
91
92 def run():
93     config = Options()
94     try:
95         config.parseOptions()
96     except usage.error, e:
97         print "%s:  %s" % (sys.argv[0], e)
98         print
99         c = getattr(config, 'subOptions', config)
100         print str(c)
101         sys.exit(1)
102
103     command = config.subCommand
104     so = config.subOptions
105
106     if command == "create-client":
107         rc = create_client(so)
108     elif command == "create-queen":
109         rc = create_queen(so)
110     elif command == "start":
111         rc = start(so)
112     elif command == "stop":
113         rc = stop(so)
114     elif command == "restart":
115         rc = restart(so)
116     rc = rc or 0
117     sys.exit(rc)
118
119 def create_client(config):
120     basedir = config['basedir']
121     os.mkdir(basedir)
122     f = open(os.path.join(basedir, "client.tac"), "w")
123     f.write(client_tac)
124     f.close()
125     print "client created in %s" % basedir
126     print " please copy introducer.furl and vdrive.furl into the directory"
127
128 def create_queen(config):
129     basedir = config['basedir']
130     os.mkdir(basedir)
131     f = open(os.path.join(basedir, "queen.tac"), "w")
132     f.write(queen_tac)
133     f.close()
134     print "queen created in %s" % basedir
135
136 def start(config):
137     basedir = config['basedir']
138     if os.path.exists(os.path.join(basedir, "client.tac")):
139         tac = "client.tac"
140         type = "client"
141     elif os.path.exists(os.path.join(basedir, "queen.tac")):
142         tac = "queen.tac"
143         type = "queen"
144     else:
145         print "%s does not look like a node directory" % basedir
146         sys.exit(1)
147     os.chdir(basedir)
148     rc = os.system("twistd -y %s" % tac)
149     if rc == 0:
150         print "node probably started"
151         return 0
152     else:
153         print "node probably not started"
154         return 1
155
156 def stop(config):
157     basedir = config['basedir']
158     pidfile = os.path.join(basedir, "twistd.pid")
159     if not os.path.exists(pidfile):
160         print "%s does not look like a running node directory (no twistd.pid)" % basedir
161         return 1
162     pid = open(pidfile, "r").read()
163     pid = int(pid)
164
165     timer = 0
166     os.kill(pid, signal.SIGTERM)
167     time.sleep(0.1)
168     while timer < 5:
169         # poll once per second until twistd.pid goes away, up to 5 seconds
170         try:
171             os.kill(pid, 0)
172         except OSError:
173             print "process %d is dead" % pid
174             return
175         timer += 1
176         time.sleep(1)
177     print "never saw process go away"
178     return 1
179
180 def restart(config):
181     rc = stop(config)
182     if rc:
183         print "not restarting"
184         return rc
185     return start(config)