]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/startstop_node.py
startstop_node.py: remove debug printout which breaks unit tests
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / startstop_node.py
1
2 import os, sys, signal, time
3 from twisted.python import usage
4 from allmydata.scripts.common import BasedirMixin
5 from allmydata.util import fileutil
6 from twisted.python.procutils import which
7
8 class StartOptions(BasedirMixin, usage.Options):
9     optParameters = [
10         ["basedir", "C", None, "which directory to start the node in"],
11         ]
12
13 class StopOptions(BasedirMixin, usage.Options):
14     optParameters = [
15         ["basedir", "C", None, "which directory to stop the node in"],
16         ]
17
18 class RestartOptions(BasedirMixin, usage.Options):
19     optParameters = [
20         ["basedir", "C", None, "which directory to restart the node in"],
21         ]
22     optFlags = [
23         ["force", "f", "if the node is not already running, start it "
24          "instead of complaining that you should have used 'start' instead "
25          "of 'restart'"],
26         ]
27
28 def do_start(basedir, config, out=sys.stdout, err=sys.stderr):
29     print >>out, "STARTING", basedir
30     if os.path.exists(os.path.join(basedir, "client.tac")):
31         tac = "client.tac"
32         type = "client"
33     elif os.path.exists(os.path.join(basedir, "introducer.tac")):
34         tac = "introducer.tac"
35         type = "introducer"
36     else:
37         print >>err, "%s does not look like a node directory" % basedir
38         if not os.path.isdir(basedir):
39             print >>err, " in fact, it doesn't look like a directory at all!"
40         return 1
41     twistds = which("twistd")
42     if not twistds:
43         print "Can't find twistd (it comes with Twisted).  Aborting."
44         sys.exit(1)
45     twistd = twistds[0]
46     path, ext = os.path.splitext(twistd)
47     if ext.lower() in [".exe", ".bat",]:
48         cmd = [twistd,]
49     else:
50         cmd = [sys.executable, twistd,]
51     
52     fileutil.make_dirs(os.path.join(basedir, "logs"))
53     cmd.extend(["-y", tac, "--logfile", os.path.join("logs", "twistd.log")])
54     curdir = os.getcwd()
55     try:
56         os.chdir(basedir)
57         rc = os.system(' '.join(cmd))
58     finally:
59         os.chdir(curdir)
60     if rc == 0:
61         print >>out, "%s node probably started" % type
62         return 0
63     else:
64         print >>err, "%s node probably not started" % type
65         return 1
66
67 def do_stop(basedir, config, out=sys.stdout, err=sys.stderr):
68     print >>out, "STOPPING", basedir
69     pidfile = os.path.join(basedir, "twistd.pid")
70     if not os.path.exists(pidfile):
71         print >>err, "%s does not look like a running node directory (no twistd.pid)" % basedir
72         return 2
73     pid = open(pidfile, "r").read()
74     pid = int(pid)
75
76     timer = 0
77     try:
78         os.kill(pid, signal.SIGINT)
79     except OSError, oserr:
80         if oserr.errno == 3:
81             print oserr.strerror
82             return 1
83         else:
84             raise
85     time.sleep(0.1)
86     while timer < 5:
87         # poll once per second until twistd.pid goes away, up to 5 seconds
88         try:
89             os.kill(pid, 0)
90         except OSError:
91             print >>out, "process %d is dead" % pid
92             return
93         timer += 1
94         time.sleep(1)
95     print >>err, "never saw process go away"
96     return 1
97
98 def start(config, stdout, stderr):
99     rc = 0
100     for basedir in config['basedirs']:
101         rc = do_start(basedir, config, stdout, stderr) or rc
102     return rc
103
104 def stop(config, stdout, stderr):
105     rc = 0
106     for basedir in config['basedirs']:
107         rc = do_stop(basedir, config, stdout, stderr) or rc
108     return rc
109
110 def restart(config, stdout, stderr):
111     rc = 0
112     for basedir in config['basedirs']:
113         rc = do_stop(basedir, config, stdout, stderr) or rc
114     if rc == 2 and config['force']:
115         print >>stderr, "ignoring couldn't-stop"
116         rc = 0
117     if rc:
118         print >>stderr, "not restarting"
119         return rc
120     for basedir in config['basedirs']:
121         rc = do_start(basedir, config, stdout, stderr) or rc
122     return rc
123
124
125 subCommands = [
126     ["start", None, StartOptions, "Start a node (of any type)."],
127     ["stop", None, StopOptions, "Stop a node."],
128     ["restart", None, RestartOptions, "Restart a node."],
129 ]
130
131 dispatch = {
132     "start": start,
133     "stop": stop,
134     "restart": restart,
135     }