2016-07-19 09:44:00 +02:00
|
|
|
#!/usr/bin/env python2
|
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
|
|
|
|
|
2016-07-30 00:51:14 +02:00
|
|
|
if False: from typing import Any
|
|
|
|
|
2016-07-19 09:44:00 +02:00
|
|
|
# find out python version
|
|
|
|
major_version = int(subprocess.check_output(['python', '-c', 'import sys; print(sys.version_info[0])']))
|
|
|
|
if major_version != 2:
|
|
|
|
# use twisted from its python2 venv but use django, tornado, etc. from the python3 venv.
|
|
|
|
PATH = os.environ["PATH"]
|
|
|
|
activate_this = "/srv/zulip-venv/bin/activate_this.py"
|
|
|
|
if not os.path.exists(activate_this):
|
|
|
|
activate_this = "/srv/zulip-py2-twisted-venv/bin/activate_this.py"
|
|
|
|
exec(open(activate_this).read(), {}, dict(__file__=activate_this)) # type: ignore # https://github.com/python/mypy/issues/1577
|
|
|
|
os.environ["PATH"] = PATH
|
|
|
|
|
2012-10-10 00:16:25 +02:00
|
|
|
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-07-30 00:51:14 +02:00
|
|
|
# type: (Any) -> None
|
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-09-08 04:35:25 +02:00
|
|
|
# HACK to fix up node_modules/.bin/handlebars deletion issue
|
|
|
|
if not os.path.exists("node_modules/.bin/handlebars") and os.path.exists("node_modules/handlebars"):
|
|
|
|
print("Handlebars binary missing due to rebase past .gitignore fixup; fixing...")
|
|
|
|
subprocess.check_call(["rm", "-rf", "node_modules/handlebars"])
|
|
|
|
subprocess.check_call(["npm", "install"])
|
|
|
|
|
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'] +
|
2016-07-27 05:19:03 +02:00
|
|
|
manage_args + ['127.0.0.1:%d' % (django_port,)],
|
2016-07-13 14:34:31 +02:00
|
|
|
['python', '-u', 'manage.py', 'runtornado'] +
|
2016-07-27 05:19:03 +02:00
|
|
|
manage_args + ['127.0.0.1:%d' % (tornado_port,)],
|
2015-08-21 00:30:50 +02:00
|
|
|
['./tools/run-dev-queue-processors'] + manage_args,
|
2016-07-27 05:19:03 +02:00
|
|
|
['env', 'PGHOST=127.0.0.1', # Force password authentication using .pgpass
|
2015-08-21 23:10:52 +02:00
|
|
|
'./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):
|
2016-07-30 00:51:14 +02:00
|
|
|
# type: (str, server.Request) -> resource.Resource
|
|
|
|
|
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')):
|
2016-07-27 05:19:03 +02:00
|
|
|
return proxy.ReverseProxyResource('127.0.0.1', 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')):
|
2016-07-27 05:19:03 +02:00
|
|
|
return proxy.ReverseProxyResource('127.0.0.1', webpack_port, '/'+name)
|
2015-10-26 17:11:44 +01:00
|
|
|
|
2016-07-27 05:19:03 +02:00
|
|
|
return proxy.ReverseProxyResource('127.0.0.1', 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)
|