Friday, May 14, 2010

Serving mercurial repositories with SCGI and Cherokee


I made a quick hack to be able to server mercurial repositories with SCGI served by Cherokee. I guess it would also work with other web servers, but I haven't tested it.

Requirements
  • Cherokee Web Server
  • Mercurial
  • python-flup
If you are using Debian it's as simple as

# aptitude install cherokee
# aptitude install mercurial
# aptitude install python-flup

Installing the Mercurial SCGI server

Fetch the Mercurial SCGI server here and copy it to a directory in your webserver. Change the owner and group if required and add the executable bit using chmod

Configuration options

The Mercurial SCGI server can be configured by using the following options:

  • HGWEBCONFIGPATH - Defines the path where the hgweb.config is located. If not defined, the default will be the directory where the Mercurial SCGI server file (hgwebdir-scgi) file is located
  • SCGIPORT - Defines the port in wich the SCGI server will listen. Defaults to 4000
  • BINDADDRESS - Defines the name/ip in which the SCGI server will listen. Defaults to localhost.
The format and contents of the hgweb.config file are documented here

Configuring Cherokee

Once you have the basics installed it's time to setup cherokee to use the SCGI handler to server the mercurial repositories.

Start your cherokee-admin and login the admin web page.

In this case we will run the SCGI server in the same machine and we will make Cherokee responsible for starting and stopping the server. For that you'll have to create a new information source. Click in Sources (top panel toolbar) and then add a new information source with localhost:4000 in the Connection field.

Change the Type from Remote Server to Local Interpreter. Fill the interpreter field with the path and name of the Mercurial SCGI server (for example /usr/local/bin/hgwebdir-scgi)

Uncheck Inherit Environment and then add a HGWEBCONFIGPATH variable with the directory where hgweb.config is located as value (for example /var/hg)

Now go to vServers to create a new rule to redirect requests to the SCGI server.

Click on the Behavior tab and then in Rule management use the Directory Rule Type and set it to the desired path (example /hgrepos). Then click on the Handler tab and select the SCGI handler.

Select a Balancer Policy and then choose the previously created Information Source
Click on Save and then choose Graceful restart and voila you'll be serving your mercurial repositories using SCGI.

As an extra you can add a new rule to serve the static content using the high performance Static content handler.

Enjoy!

2 comments:

  1. Nice post. Had to fix some bugs in the scgi script, but works fine with python2.7 on ArchLinux 64bit

    ReplyDelete
  2. Thanks for the script. There's a problem with one of the stderr lines which stop the script from starting if the variables have been set. I've made some modifications to change this (and moved the default port to 3001 which was needed for my purposes): http://pastebin.com/REZWL4Vu

    ReplyDelete