2016-10-05 10:47:16 +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
|
2016-10-04 01:36:38 +02:00
|
|
|
import pwd
|
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
|
|
|
|
|
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',
|
2016-10-04 01:36:38 +02:00
|
|
|
default=None, 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()
|
|
|
|
|
2016-10-04 01:36:38 +02:00
|
|
|
if options.interface is None:
|
|
|
|
user_id = os.getuid()
|
|
|
|
user_name = pwd.getpwuid(user_id).pw_name
|
|
|
|
if user_name == "vagrant":
|
|
|
|
# In the Vagrant development environment, we need to listen on
|
|
|
|
# all ports, and it's safe to do so, because Vagrant is only
|
|
|
|
# exposing certain guest ports (by default just 9991) to the host.
|
|
|
|
options.interface = ""
|
|
|
|
else:
|
|
|
|
# Otherwise, only listen to requests on localhost for security.
|
|
|
|
options.interface = "127.0.0.1"
|
|
|
|
|
2012-11-08 23:23:25 +01:00
|
|
|
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
|
|
|
|
2016-10-06 07:07:20 +02:00
|
|
|
from scripts.lib.zulip_tools import WARNING, ENDC
|
|
|
|
|
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-10-05 10:47:16 +02:00
|
|
|
# type: (bytes, server.Request) -> resource.Resource
|
2016-07-30 00:51:14 +02:00
|
|
|
|
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)
|
2016-10-05 10:47:16 +02:00
|
|
|
if (request.uri in [b'/json/get_events'] or
|
|
|
|
request.uri.startswith(b'/json/events') or
|
|
|
|
request.uri.startswith(b'/api/v1/events') or
|
|
|
|
request.uri.startswith(b'/sockjs')):
|
|
|
|
return proxy.ReverseProxyResource('127.0.0.1', tornado_port, b'/' + name)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2016-10-05 10:47:16 +02:00
|
|
|
elif (request.uri.startswith(b'/webpack') or
|
|
|
|
request.uri.startswith(b'/socket.io')):
|
|
|
|
return proxy.ReverseProxyResource('127.0.0.1', webpack_port, b'/' + name)
|
2015-10-26 17:11:44 +01:00
|
|
|
|
2016-10-05 10:47:16 +02:00
|
|
|
return proxy.ReverseProxyResource('127.0.0.1', django_port, b'/'+name)
|
2012-10-10 00:16:25 +02:00
|
|
|
|
2016-10-02 18:20:06 +02:00
|
|
|
|
|
|
|
# log which services/ports will be started
|
2016-10-06 07:07:20 +02:00
|
|
|
print("Starting Zulip services on ports: web proxy: {},".format(proxy_port),
|
2016-10-02 18:20:06 +02:00
|
|
|
"Django: {}, Tornado: {}".format(django_port, tornado_port), end='')
|
|
|
|
if options.test:
|
|
|
|
print("") # no webpack for --test
|
|
|
|
else:
|
|
|
|
print(", webpack: {}".format(webpack_port))
|
|
|
|
|
2016-10-06 07:07:20 +02:00
|
|
|
print(WARNING + "Note: only port {} is exposed to the host in a Vagrant environment.".format(proxy_port) + ENDC)
|
2016-10-02 18:20:06 +02:00
|
|
|
|
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)
|