Skip to content

Commit

Permalink
Merge pull request #1427 from tardyp/apiproxy
Browse files Browse the repository at this point in the history
fix api_proxy
  • Loading branch information
Mikhail Sobolev committed Dec 10, 2014
2 parents 8b7fec7 + a047fc8 commit 069eb56
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
60 changes: 43 additions & 17 deletions master/contrib/api_proxy.py
@@ -1,39 +1,45 @@
import argparse
import flask
import json
import os
import requests

from buildbot.www.service import WWWService
from flask import Response
from flask import redirect
from flask import render_template
from flask import request
from flask import stream_with_context

master_service = WWWService(None)

main_dir = master_service.apps['base'].static_dir
main_dir = master_service.apps.base.static_dir
dest_buildbot = None
port = None
local_url = None

application = flask.Flask(__name__, static_url_path='', static_folder=main_dir)

for k, v in master_service.apps.items():
if k != "base":
application = flask.Flask(__name__, static_url_path='', static_folder=main_dir, template_folder=main_dir)
for name in master_service.apps.names:
if name != "base":
plugin = master_service.apps.get(name)
try:
os.symlink(v.static_dir, os.path.join(main_dir, k))
os.symlink(plugin.static_dir, os.path.join(main_dir, name))
except OSError:
pass


def get_url():
global args
url = request.url
url = url.replace("http://localhost:8010", args.dest_buildbot)
url = url.replace(local_url, dest_buildbot)
return url


def reroute_stream(url):
def reroute_stream(url=None):
req = requests.get(get_url(), stream=True)
return Response(stream_with_context(req.iter_content()), content_type=req.headers['content-type'])

application.route('/sse/<path:url>')(reroute_stream)
application.route('/avatar')(reroute_stream)
# application.route('/auth/<path:url>')(reroute_stream)


Expand All @@ -50,20 +56,40 @@ def reroute_cache(url):
return Response(res[0], content_type=res[1])


@application.route('/config.js')
plugins = dict((k, {}) for k in master_service.apps.names if k != "base")


def config():
plugins = dict((k, {}) for k in master_service.apps if k != "base")
return ('this.config = {"avatar_methods": {"name": "gravatar"}, "user": {"anonymous": true}, ' +
'"plugins": %r, "url": "http://localhost:8010/", "port": 8010, "auth": {"name": "NoAuth"}}' % (plugins, ))
return {
"avatar_methods": {"name": "gravatar"},
"user": {"anonymous": True},
"plugins": plugins,
"port": port,
"auth": {"name": "NoAuth"}
}


@application.route('/')
def root():
return application.send_static_file('index.html')
if local_url not in request.url:
return redirect(local_url)
return render_template('index.html', configjson=json.dumps(config()), plugins=plugins)

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='api proxy for web ui development.')
parser.add_argument('--dest_buildbot', dest='dest_buildbot', help='url to the destination buildbot',
default="http://nine.buildbot.buildbot.net")
parser.add_argument('--dest_buildbot',
dest='dest_buildbot',
help='url to the destination buildbot',
default="http://nine.buildbot.net")
parser.add_argument('--bind_port',
dest='bind_port',
type=int,
default=8010,
help='port to bind locally')

args = parser.parse_args()
application.run(host='0.0.0.0', port=8010, processes=10)
dest_buildbot = args.dest_buildbot
port = args.bind_port

local_url = "http://127.0.0.1:{port}".format(port=port)
application.run(host='127.0.0.1', port=port, processes=10)
29 changes: 27 additions & 2 deletions master/docs/developer/www.rst
Expand Up @@ -616,9 +616,8 @@ Linking with Buildbot
A running buildmaster needs to be able to find the JavaScript source code it needs to serve the UI.
This needs to work in a variety of contexts - Python development, JavaScript development, and end-user installations.
To accomplish this, the grunt build process finishes by bundling all of the static data into a Python distribution tarball, along with a little bit of Python glue.
To accomplish this, the gulp build process finishes by bundling all of the static data into a Python distribution tarball, along with a little bit of Python glue.
The Python glue implements the interface described below, with some care taken to handle multiple contexts.
The :src:`www/grunt.js`, :src:`www/setup.py`, and :src:`www/buildbot_www.py` scripts are carefully coordinated.
Hacking Quick-Start
-------------------
Expand Down Expand Up @@ -700,6 +699,32 @@ When doing web development, you usually run:
This will compile the base webapp in development mode, and automatically rebuild when files change.
Testing with real data
~~~~~~~~~~~~~~~~~~~~~~
Front-end only hackers might want to just skip the master and slave setup, and just focus on the UI.
It can also be very useful to just try the UI with real data from your production.
For those use-cases, api_proxy.py can be used.
This tools is a small flask app that can proxy the data and sse api from a production server to your development environment.
Having a proxy is slightly slower, but this can be very useful for testing with real complex data.
You still need to have python virtualenv configured with master package installed, like we described in previous paragraph.
Provided you run it in a buildbot master virtualenv, the following command will start the UI and redirect the api calls to the nine demo server:
.. code-block:: none
python buildbot/master/contrib/api_proxy.py --dest_buildbot http://nine.buildbot.net
You can then just point your browser to localhost:8010, and you will access nine.buildbot.net, with your own version of the UI.
Please use following command for a list of options:
.. code-block:: none
python buildbot/master/contrib/api_proxy.py --help
Guanlecoja
----------
Expand Down
2 changes: 1 addition & 1 deletion www/base/src/app/layout.jade
Expand Up @@ -21,4 +21,4 @@ html.no-js(xmlns:ng='http://angularjs.org', xmlns:app='ignored')
| angular.module('app').requires.push('{{app}}')
| {%- endfor -%}
script
| angular.module("app").constant("config", {{configjson}})
| angular.module("app").constant("config", {{configjson|safe}})

0 comments on commit 069eb56

Please sign in to comment.