2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
1 |
Serving Bazaar with FastCGI |
2 |
=========================== |
|
3 |
||
2018.4.2
by Andrew Bennetts
Add security warning to http_smart_server.txt. |
4 |
**This feature is EXPERIMENTAL and is NOT SECURE. It will allow access to |
5 |
arbitrary files on your server.** |
|
6 |
||
2293.1.6
by Brad Crittenden
post review changes |
7 |
This document describes one way to set up a Bazaar HTTP smart server, |
8 |
using Apache 2.0 and FastCGI or mod_python. |
|
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
9 |
|
2601.1.2
by James Westby
Add a reference to the main smart server documentation. |
10 |
For more information on the smart server, and other ways to configure it |
11 |
see the main `smart server documentation`_. |
|
12 |
||
3104.2.5
by Ian Clatworthy
fix some broken links and make doc more Windows user friendly |
13 |
.. _smart server documentation: #running-a-smart-server |
2601.1.2
by James Westby
Add a reference to the main smart server documentation. |
14 |
|
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
15 |
Example |
2977.1.1
by Ian Clatworthy
First cut at new look User Guide including chapters 1 and 2 |
16 |
------- |
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
17 |
|
18 |
You have a webserver already publishing `/srv/example.com/www/code` as |
|
19 |
`http://example.com/code/...` with plain HTTP. It contains bzr branches and |
|
20 |
directories like `/srv/example.com/www/code/branch-one` and |
|
2018.4.2
by Andrew Bennetts
Add security warning to http_smart_server.txt. |
21 |
`/srv/example.com/www/code/my-repo/branch-two`. You want to provide read-only |
22 |
smart server access to these directories in addition to the existing HTTP |
|
23 |
access. |
|
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
24 |
|
25 |
Configuring Apache 2.0 |
|
26 |
---------------------- |
|
27 |
||
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
28 |
FastCGI |
29 |
~~~~~~~ |
|
30 |
||
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
31 |
First, configure mod_fastcgi, e.g. by adding lines like these to your |
32 |
httpd.conf:: |
|
33 |
||
34 |
LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so |
|
35 |
FastCgiIpcDir /var/lib/apache2/fastcgi |
|
36 |
||
37 |
In our example, we're already serving `/srv/example.com/www/code` at |
|
38 |
`http://example.com/code`, so our existing Apache configuration would look |
|
39 |
like:: |
|
40 |
||
41 |
Alias /code /srv/example.com/www/code |
|
42 |
<Directory /srv/example.com/www/code> |
|
43 |
Options Indexes |
|
44 |
# ... |
|
45 |
</Directory> |
|
46 |
||
47 |
We need to change it to handle all requests for URLs ending in `.bzr/smart`. It |
|
48 |
will look like:: |
|
49 |
||
50 |
Alias /code /srv/example.com/www/code |
|
51 |
<Directory /srv/example.com/www/code> |
|
2706.1.1
by Martin Albisetti
updated smart server documentation |
52 |
Options Indexes FollowSymLinks |
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
53 |
RewriteEngine On |
54 |
RewriteBase /code |
|
2190.1.2
by John Arbash Meinel
Need the correct rewrite rule to get .bzr at the root of the exposed directory. |
55 |
RewriteRule ^(.*/|)\.bzr/smart$ /srv/example.com/scripts/bzr-smart.fcgi |
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
56 |
</Directory> |
57 |
||
2018.4.5
by Andrew Bennetts
Improvement thanks to John's review. |
58 |
# bzr-smart.fcgi isn't under the DocumentRoot, so Alias it into the URL |
59 |
# namespace so it can be executed. |
|
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
60 |
Alias /srv/example.com/scripts/bzr-smart.fcgi /srv/example.com/scripts/bzr-smart.fcgi |
61 |
<Directory /srv/example.com/scripts> |
|
62 |
Options ExecCGI |
|
63 |
<Files bzr-smart.fcgi> |
|
64 |
SetHandler fastcgi-script |
|
65 |
</Files> |
|
66 |
</Directory> |
|
67 |
||
68 |
This instructs Apache to hand requests for any URL ending with `/.bzr/smart` |
|
69 |
inside `/code` to a Bazaar smart server via FastCGI. |
|
70 |
||
71 |
Refer to the mod_rewrite_ and mod_fastcgi_ documentation for further |
|
72 |
information. |
|
73 |
||
74 |
.. _mod_rewrite: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html |
|
75 |
.. _mod_fastcgi: http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html |
|
76 |
||
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
77 |
mod_python |
78 |
~~~~~~~~~~ |
|
79 |
||
80 |
First, configure mod_python, e.g. by adding lines like these to your |
|
81 |
httpd.conf:: |
|
82 |
||
83 |
LoadModule python_module /usr/lib/apache2/modules/mod_python.so |
|
84 |
||
85 |
Define the rewrite rules with mod_rewrite the same way as for FastCGI, except |
|
86 |
change:: |
|
87 |
||
2190.1.2
by John Arbash Meinel
Need the correct rewrite rule to get .bzr at the root of the exposed directory. |
88 |
RewriteRule ^(.*/|)\.bzr/smart$ /srv/example.com/scripts/bzr-smart.fcgi |
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
89 |
|
90 |
to:: |
|
91 |
||
2190.1.2
by John Arbash Meinel
Need the correct rewrite rule to get .bzr at the root of the exposed directory. |
92 |
RewriteRule ^(.*/|)\.bzr/smart$ /srv/example.com/scripts/bzr-smart.py |
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
93 |
|
94 |
Like with mod_fastcgi, we also define how our script is to be handled:: |
|
95 |
||
96 |
Alias /srv/example.com/scripts/bzr-smart.py /srv/example.com/scripts/bzr-smart.py |
|
97 |
<Directory /srv/example.com/scripts> |
|
98 |
<Files bzr-smart.py> |
|
99 |
PythonPath "sys.path+['/srv/example.com/scripts']" |
|
100 |
AddHandler python-program .py |
|
2190.1.1
by John Arbash Meinel
Update the documentation so the smart server actually works with mod python. |
101 |
PythonHandler bzr-smart::handler |
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
102 |
</Files> |
103 |
</Directory> |
|
104 |
||
105 |
This instructs Apache to hand requests for any URL ending with `/.bzr/smart` |
|
106 |
inside `/code` to a Bazaar smart server via mod_python. |
|
107 |
||
2706.1.3
by Aaron Bentley
Style tweakage and NEWS |
108 |
NOTE: If you don't have bzrlib in your PATH, you will be need to change the |
109 |
following line:: |
|
2706.1.1
by Martin Albisetti
updated smart server documentation |
110 |
|
111 |
PythonPath "sys.path+['/srv/example.com/scripts']" |
|
112 |
||
2706.1.3
by Aaron Bentley
Style tweakage and NEWS |
113 |
To:: |
2706.1.1
by Martin Albisetti
updated smart server documentation |
114 |
|
115 |
PythonPath "['/path/to/bzr']+sys.path+['/srv/example.com/scripts']" |
|
116 |
||
117 |
||
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
118 |
Refer to the mod_python_ documentation for further information. |
119 |
||
120 |
.. _mod_python: http://www.modpython.org/ |
|
121 |
||
122 |
||
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
123 |
Configuring Bazaar |
124 |
------------------ |
|
125 |
||
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
126 |
FastCGI |
127 |
~~~~~~~ |
|
128 |
||
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
129 |
We've configured Apache to run the smart server at |
130 |
`/srv/example.com/scripts/bzr-smart.fcgi`. This is just a simple script we need |
|
131 |
to write to configure a smart server, and glue it to the FastCGI gateway. |
|
132 |
Here's what it looks like:: |
|
133 |
||
134 |
import fcgi |
|
135 |
from bzrlib.transport.http import wsgi |
|
136 |
||
137 |
smart_server_app = wsgi.make_app( |
|
138 |
root='/srv/example.com/code', |
|
139 |
prefix='/code/', |
|
2190.1.4
by John Arbash Meinel
Add ability to enable writeable bzr+http access. |
140 |
path_var='REQUEST_URI', |
141 |
readonly=True) |
|
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
142 |
|
143 |
fcgi.WSGIServer(smart_server_app).run() |
|
144 |
||
145 |
The `fcgi` module can be found at http://svn.saddi.com/py-lib/trunk/fcgi.py. It |
|
146 |
is part of flup_. |
|
147 |
||
148 |
.. _flup: http://www.saddi.com/software/flup/ |
|
149 |
||
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
150 |
mod_python |
151 |
~~~~~~~~~~ |
|
152 |
||
153 |
We've configured Apache to run the smart server at |
|
154 |
`/srv/example.com/scripts/bzr-smart.py`. This is just a simple script we need |
|
155 |
to write to configure a smart server, and glue it to the mod_python gateway. |
|
156 |
Here's what it looks like:: |
|
157 |
||
158 |
import modpywsgi |
|
159 |
from bzrlib.transport.http import wsgi |
|
160 |
||
161 |
smart_server_app = wsgi.make_app( |
|
162 |
root='/srv/example.com/code', |
|
163 |
prefix='/code/', |
|
2190.1.4
by John Arbash Meinel
Add ability to enable writeable bzr+http access. |
164 |
path_var='REQUEST_URI', |
165 |
readonly=True) |
|
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
166 |
|
2190.1.1
by John Arbash Meinel
Update the documentation so the smart server actually works with mod python. |
167 |
def handler(request): |
168 |
"""Handle a single request.""" |
|
169 |
wsgi_server = modpywsgi.WSGIServer(smart_server_app) |
|
170 |
return wsgi_server.run(request) |
|
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
171 |
|
172 |
The `modpywsgi` module can be found at http://trac.pocoo.org/wiki/ModPyWsgi. It |
|
2706.1.3
by Aaron Bentley
Style tweakage and NEWS |
173 |
is part of pocoo_. |
174 |
You sould make sure you place modpywsgi.py in the same directory as |
|
2706.1.1
by Martin Albisetti
updated smart server documentation |
175 |
bzr-smart.py (ie. /srv/example.com/scripts/). |
2180.2.1
by Andrew Bennetts
Describe smart server configuration with mod_python. |
176 |
|
177 |
.. _pocoo: http://trac.pocoo.org/wiki/ |
|
178 |
||
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
179 |
Clients |
180 |
------- |
|
181 |
||
182 |
Now you can use `bzr+http://` URLs, e.g.:: |
|
183 |
||
184 |
bzr log bzr+http://example.com/code/my-branch |
|
185 |
||
186 |
Plain HTTP access should continue to work:: |
|
187 |
||
188 |
bzr log http://example.com/code/my-branch |
|
189 |
||
190 |
||
191 |
Advanced configuration |
|
2977.1.1
by Ian Clatworthy
First cut at new look User Guide including chapters 1 and 2 |
192 |
---------------------- |
2018.4.1
by Andrew Bennetts
Add WSGI smart server. |
193 |
|
194 |
Because the Bazaar HTTP smart server is a WSGI application, it can be used with |
|
195 |
any 3rd-party WSGI middleware or server that conforms the WSGI standard. The |
|
196 |
only requirements are: |
|
197 |
||
198 |
* to construct a `SmartWSGIApp`, you need to specify a **root transport** that it |
|
199 |
will serve. |
|
200 |
* each request's `environ` dict must have a **'bzrlib.relpath'** variable set. |
|
201 |
||
202 |
The `make_app` helper used in the example constructs a `SmartWSGIApp` with a |
|
203 |
transport based on the `root` path given to it, and calculates the |
|
204 |
'bzrlib.relpath` for each request based on the `prefix` and `path_var` |
|
205 |
arguments. In the example above, it will take the 'REQUEST_URI' (which is set |
|
206 |
by Apache), strip the '/code/' prefix and the '/.bzr/smart' suffix, and set that |
|
207 |
as the 'bzrlib.relpath', so that a request for '/code/foo/bar/.bzr/smart' will |
|
208 |
result in a 'bzrlib.relpath' of 'foo/bzr'. |
|
209 |
||
210 |
It's possible to configure a smart server for a non-local transport, or that |
|
211 |
does arbitrary path translations, etc, by constructing a `SmartWSGIApp` |
|
212 |
directly. Refer to the docstrings of `bzrlib.transport.http.wsgi` and the `WSGI |
|
213 |
standard`_ for further information. |
|
214 |
||
215 |
.. _WSGI standard: http://www.python.org/dev/peps/pep-0333/ |
|
216 |
||
2190.1.4
by John Arbash Meinel
Add ability to enable writeable bzr+http access. |
217 |
|
218 |
Pushing over ``bzr+http://`` |
|
2977.1.1
by Ian Clatworthy
First cut at new look User Guide including chapters 1 and 2 |
219 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2190.1.4
by John Arbash Meinel
Add ability to enable writeable bzr+http access. |
220 |
|
221 |
It is possible to allow pushing data over the http smart server. The |
|
222 |
easiest way to do this, is to just supply ``readonly=False`` to the |
|
223 |
``wsgi.make_app()`` call. But be careful, because the smart protocol does |
|
224 |
not contain any Authentication. So if you enable write support, you will |
|
225 |
want to restrict access to ``.bzr/smart`` URLs to restrict who can |
|
226 |
actually write data on your system. At this time, it is not possible to |
|
227 |
allow some people to have read-only access and others to have read-write |
|
228 |
access to the same urls. Because at the HTTP layer (which is doing the |
|
2190.1.7
by John Arbash Meinel
Comment that https:// is a good way to have a writable URL next to a read-only http:// url. |
229 |
Authenticating), everything is just a POST request. However, it would |
230 |
certainly be possible to have HTTPS require authentication and use a |
|
231 |
writable server, and plain HTTP allow read-only access. |
|
232 |
||
2190.1.4
by John Arbash Meinel
Add ability to enable writeable bzr+http access. |
233 |
|
2190.1.1
by John Arbash Meinel
Update the documentation so the smart server actually works with mod python. |
234 |
.. |
235 |
vim: ft=rst tw=74 et |