2016-04-07 15:03:22 +02:00
|
|
|
#!/usr/bin/env python
|
2016-07-18 15:24:41 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
import optparse
|
2012-10-10 00:16:25 +02:00
|
|
|
import subprocess
|
2012-11-09 20:59:43 +01:00
|
|
|
import signal
|
2012-11-14 19:46:12 +01:00
|
|
|
import traceback
|
2013-10-23 19:12:03 +02:00
|
|
|
import sys
|
2012-10-10 00:16:25 +02:00
|
|
|
import os
|
|
|
|
|
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.web import proxy, server, resource
|
|
|
|
|
2013-08-21 18:01:40 +02:00
|
|
|
# Monkey-patch twisted.web.http to avoid request.finish exceptions
|
2013-09-05 21:24:18 +02:00
|
|
|
# https://trac.zulip.net/ticket/1728
|
2013-08-21 18:01:40 +02:00
|
|
|
from twisted.web.http import Request
|
|
|
|
orig_finish = Request.finish
|
|
|
|
def patched_finish(self):
|
2016-01-26 02:24:09 +01:00
|
|
|
if not self._disconnected:
|
|
|
|
orig_finish(self)
|
2013-08-21 18:01:40 +02:00
|
|
|
Request.finish = patched_finish
|
|
|
|
|
2015-10-15 18:44:48 +02:00
|
|
|
if 'posix' in os.name and os.geteuid() == 0:
|
|
|
|
raise RuntimeError("run-dev.py should not be run as root.")
|
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
parser = optparse.OptionParser(r"""
|
|
|
|
|
2012-10-10 00:16:25 +02:00
|
|
|
Starts the app listening on localhost, for local development.
|
|
|
|
|
|
|
|
This script launches the Django and Tornado servers, then runs a reverse proxy
|
|
|
|
which serves to both of them. After it's all up and running, browse to
|
|
|
|
|
|
|
|
http://localhost:9991/
|
|
|
|
|
|
|
|
Note that, while runserver and runtornado have the usual auto-restarting
|
|
|
|
behavior, the reverse proxy itself does *not* automatically restart on changes
|
|
|
|
to this file.
|
2012-11-08 23:23:25 +01:00
|
|
|
""")
|
|
|
|
|
|
|
|
parser.add_option('--test',
|
|
|
|
action='store_true', dest='test',
|
|
|
|
help='Use the testing database and ports')
|
|
|
|
|
2014-02-12 20:03:58 +01:00
|
|
|
parser.add_option('--interface',
|
|
|
|
action='store', dest='interface',
|
2015-08-19 04:21:39 +02:00
|
|
|
default='127.0.0.1', help='Set the IP or hostname for the proxy to listen on')
|
2014-02-12 20:03:58 +01:00
|
|
|
|
2016-07-18 15:24:41 +02:00
|
|
|
parser.add_option('--no-clear-memcached',
|
|
|
|
action='store_false', dest='clear_memcached',
|
|
|
|
default=True, help='Do not clear memcached')
|
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
base_port = 9991
|
|
|
|
if options.test:
|
|
|
|
base_port = 9981
|
2013-10-23 19:12:03 +02:00
|
|
|
settings_module = "zproject.test_settings"
|
|
|
|
else:
|
|
|
|
settings_module = "zproject.settings"
|
|
|
|
|
2015-08-21 00:30:50 +02:00
|
|
|
manage_args = ['--settings=%s' % (settings_module,)]
|
2013-10-23 19:12:03 +02:00
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
|
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
proxy_port = base_port
|
|
|
|
django_port = base_port+1
|
|
|
|
tornado_port = base_port+2
|
2015-10-26 17:11:44 +01:00
|
|
|
webpack_port = base_port+3
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2013-10-28 15:54:32 +01:00
|
|
|
os.chdir(os.path.join(os.path.dirname(__file__), '..'))
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2013-03-25 23:21:39 +01:00
|
|
|
# Clean up stale .pyc files etc.
|
|
|
|
subprocess.check_call('./tools/clean-repo')
|
|
|
|
|
2016-07-18 15:24:41 +02:00
|
|
|
if options.clear_memcached:
|
|
|
|
print("Clearing memcached ...")
|
|
|
|
subprocess.check_call('./scripts/setup/flush-memcached')
|
|
|
|
|
2012-11-09 20:59:43 +01:00
|
|
|
# Set up a new process group, so that we can later kill run{server,tornado}
|
|
|
|
# and all of the processes they spawn.
|
|
|
|
os.setpgrp()
|
|
|
|
|
2013-01-30 23:35:24 +01:00
|
|
|
# Pass --nostatic because we configure static serving ourselves in
|
2013-10-04 19:27:01 +02:00
|
|
|
# zulip/urls.py.
|
2015-08-21 00:32:15 +02:00
|
|
|
cmds = [['./tools/compile-handlebars-templates', 'forever'],
|
2015-12-06 19:53:17 +01:00
|
|
|
['python', 'manage.py', 'rundjango'] +
|
2015-08-21 00:30:50 +02:00
|
|
|
manage_args + ['localhost:%d' % (django_port,)],
|
2016-07-13 14:34:31 +02:00
|
|
|
['python', '-u', 'manage.py', 'runtornado'] +
|
2015-08-21 00:30:50 +02:00
|
|
|
manage_args + ['localhost:%d' % (tornado_port,)],
|
|
|
|
['./tools/run-dev-queue-processors'] + manage_args,
|
2015-08-21 23:10:52 +02:00
|
|
|
['env', 'PGHOST=localhost', # Force password authentication using .pgpass
|
|
|
|
'./puppet/zulip/files/postgresql/process_fts_updates']]
|
2016-06-30 23:07:30 +02:00
|
|
|
if options.test:
|
|
|
|
# Webpack doesn't support 2 copies running on the same system, so
|
|
|
|
# in order to support running the Casper tests while a Zulip
|
|
|
|
# development server is running, we use webpack in production mode
|
|
|
|
# for the Casper tests.
|
|
|
|
subprocess.check_call('./tools/webpack')
|
|
|
|
else:
|
|
|
|
cmds += [['./tools/webpack', '--watch', '--port', str(webpack_port)]]
|
2013-10-23 19:12:03 +02:00
|
|
|
for cmd in cmds:
|
2015-08-21 00:30:50 +02:00
|
|
|
subprocess.Popen(cmd)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
|
|
|
class Resource(resource.Resource):
|
|
|
|
def getChild(self, name, request):
|
2014-02-12 17:36:21 +01:00
|
|
|
# Assume an HTTP 1.1 request
|
|
|
|
proxy_host = request.requestHeaders.getRawHeaders('Host')
|
|
|
|
request.requestHeaders.setRawHeaders('X-Forwarded-Host', proxy_host)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2013-11-05 17:02:34 +01:00
|
|
|
if (request.uri in ['/json/get_events'] or
|
2013-11-19 23:11:30 +01:00
|
|
|
request.uri.startswith('/json/events') or
|
2013-09-07 00:27:10 +02:00
|
|
|
request.uri.startswith('/api/v1/events') or
|
|
|
|
request.uri.startswith('/sockjs')):
|
2012-11-08 23:23:25 +01:00
|
|
|
return proxy.ReverseProxyResource('localhost', tornado_port, '/'+name)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2015-10-26 17:11:44 +01:00
|
|
|
elif (request.uri.startswith('/webpack') or
|
|
|
|
request.uri.startswith('/socket.io')):
|
|
|
|
return proxy.ReverseProxyResource('localhost', webpack_port, '/'+name)
|
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
return proxy.ReverseProxyResource('localhost', django_port, '/'+name)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
|
|
|
try:
|
2014-02-12 20:03:58 +01:00
|
|
|
reactor.listenTCP(proxy_port, server.Site(Resource()), interface=options.interface)
|
2012-10-10 00:16:25 +02:00
|
|
|
reactor.run()
|
2012-11-14 19:46:12 +01:00
|
|
|
except:
|
|
|
|
# Print the traceback before we get SIGTERM and die.
|
|
|
|
traceback.print_exc()
|
|
|
|
raise
|
2012-10-10 00:16:25 +02:00
|
|
|
finally:
|
2012-11-09 20:59:43 +01:00
|
|
|
# Kill everything in our process group.
|
|
|
|
os.killpg(0, signal.SIGTERM)
|