]>
Commit | Line | Data |
---|---|---|
8b8ebb78 | 1 | #!/usr/bin/env python |
1d924d97 | 2 | # this entire thing is a hack and badly needs reimplementing |
4942c2a0 | 3 | import bin.configcheck |
a409b906 | 4 | import bin.compile |
4c407b6a | 5 | import sys |
a409b906 CP |
6 | bin.compile.vcheck() |
7 | ||
8b8ebb78 CP |
8 | DEFAULT_PORT = 9090 |
9 | ||
8b8ebb78 | 10 | from optparse import OptionParser |
42fdc4c6 | 11 | import sys, os, config |
8b8ebb78 CP |
12 | |
13 | def run_twistd(args1=None, args2=None): | |
724f3003 | 14 | from twisted.scripts.twistd import run |
8b8ebb78 CP |
15 | args = [sys.argv[0]] |
16 | if args1 is not None: | |
17 | args.extend(args1) | |
18 | args.append("qwebirc") | |
19 | if args2 is not None: | |
20 | args.extend(args2) | |
21 | sys.argv = args | |
22 | run() | |
23 | ||
24 | def help_reactors(*args): | |
25 | run_twistd(["--help-reactors"]) | |
26 | sys.exit(1) | |
27 | ||
7637ceb0 CP |
28 | try: |
29 | from select import epoll | |
30 | DEFAULT_REACTOR = "epoll" | |
31 | except ImportError: | |
32 | try: | |
33 | from select import kqueue | |
34 | DEFAULT_REACTOR = "kqueue" | |
35 | except ImportError: | |
36 | try: | |
37 | from select import poll | |
38 | DEFAULT_REACTOR = "poll" | |
39 | except ImportError: | |
40 | DEFAULT_REACTOR = "select" | |
8b8ebb78 CP |
41 | |
42 | parser = OptionParser() | |
43 | parser.add_option("-n", "--no-daemon", help="Don't run in the background.", action="store_false", dest="daemonise", default=True) | |
44 | parser.add_option("--help-reactors", help="Display a list of reactor names.", action="callback", callback=help_reactors) | |
45 | parser.add_option("-b", "--debug", help="Run in the Python Debugger.", action="store_true", dest="debug", default=False) | |
0f3c92dc | 46 | parser.add_option("-t", "--tracebacks", help="Display tracebacks in error pages (this reveals a LOT of information, do NOT use in production!)", action="store_true", dest="tracebacks", default=False) |
8b8ebb78 CP |
47 | parser.add_option("-r", "--reactor", help="Which reactor to use (see --help-reactors for a list).", dest="reactor", default=DEFAULT_REACTOR) |
48 | parser.add_option("-p", "--port", help="Port to start the server on.", type="int", dest="port", default=DEFAULT_PORT) | |
ef74f2c1 | 49 | parser.add_option("-i", "--ip", help="IP address to listen on.", dest="ip", default="0.0.0.0") |
8b8ebb78 CP |
50 | parser.add_option("-l", "--logfile", help="Path to twisted log file.", dest="logfile") |
51 | parser.add_option("-c", "--clf", help="Path to web CLF (Combined Log Format) log file.", dest="clogfile") | |
64429ed7 CP |
52 | parser.add_option("-C", "--certificate", help="Path to SSL certificate.", dest="sslcertificate") |
53 | parser.add_option("-k", "--key", help="Path to SSL key.", dest="sslkey") | |
9e93b6fe | 54 | parser.add_option("-H", "--certificate-chain", help="Path to SSL certificate chain file.", dest="sslchain") |
4cfa1a92 | 55 | parser.add_option("-P", "--pidfile", help="Path to store PID file", dest="pidfile") |
39cf79cc | 56 | parser.add_option("-s", "--syslog", help="Log to syslog", action="store_true", dest="syslog", default=False) |
b1e3cd67 CP |
57 | parser.add_option("--profile", help="Run in profile mode, dumping results to this file", dest="profile") |
58 | parser.add_option("--profiler", help="Name of profiler to use", dest="profiler") | |
39cf79cc | 59 | parser.add_option("--syslog-prefix", help="Syslog prefix", dest="syslog_prefix", default="qwebirc") |
42fdc4c6 CP |
60 | |
61 | sargs = sys.argv[1:] | |
62 | if "ARGS" in dir(config): | |
63 | import shlex | |
64 | sargs = shlex.split(config.ARGS) + sargs | |
65 | ||
66 | (options, args) = parser.parse_args(args=sargs) | |
8b8ebb78 CP |
67 | |
68 | args1, args2 = [], [] | |
69 | ||
70 | if not options.daemonise: | |
71 | args1.append("-n") | |
72 | if options.debug: | |
73 | args1.append("-b") | |
4bc06f30 CP |
74 | |
75 | if options.reactor != DEFAULT_REACTOR: | |
724f3003 CP |
76 | rn = options.reactor + "reactor" |
77 | getattr(__import__("twisted.internet", fromlist=[rn]), rn).install() | |
8b8ebb78 | 78 | if options.logfile: |
4cfa1a92 CP |
79 | args1+=["--logfile", options.logfile] |
80 | if options.pidfile: | |
81 | args1+=["--pidfile", options.pidfile] | |
39cf79cc CP |
82 | if options.syslog: |
83 | args1+=["--syslog"] | |
b1e3cd67 CP |
84 | if options.profile: |
85 | args1+=["--profile", options.profile] | |
86 | if options.profiler: | |
87 | args1+=["--profiler", options.profiler] | |
88 | ||
94ba3643 | 89 | if options.syslog and options.syslog_prefix: |
39cf79cc CP |
90 | import syslog |
91 | syslog.openlog(options.syslog_prefix) | |
8b8ebb78 | 92 | |
0f3c92dc CP |
93 | if not options.tracebacks: |
94 | args2.append("-n") | |
8b8ebb78 CP |
95 | if options.clogfile: |
96 | args2+=["--logfile", options.clogfile] | |
0f3c92dc | 97 | |
64429ed7 CP |
98 | if options.sslcertificate and options.sslkey: |
99 | args2+=["--certificate", options.sslcertificate, "--privkey", options.sslkey, "--https", options.port] | |
9e93b6fe CP |
100 | if options.sslchain: |
101 | args2+=["--certificate-chain", options.sslchain] | |
64429ed7 CP |
102 | else: |
103 | args2+=["--port", options.port] | |
104 | ||
ef74f2c1 | 105 | args2+=["--ip", options.ip] |
106 | ||
df5f9be6 CP |
107 | if os.name == "posix" and os.getuid() == 0: |
108 | print >>sys.stderr, "refusing to run as root" | |
4c407b6a | 109 | sys.exit(1) |
df5f9be6 | 110 | |
8b8ebb78 | 111 | run_twistd(args1, args2) |