Pages

Thursday, October 14, 2010

Setting your own private Mercurial server

update : the steps in this post has been updated for using mercurial 1.7.2

First of all I want to say that this wiki link is amazing, it's complete and robust. But even with the best guide sometime you've made mistakes. Never hurt to have many reference :)

So to simply put it,  this is my steps in implementing that guide. As a background i have an ubuntu server that i will be using as my own private mercurial server. I already have some code that works with google mercurial server. So its a matter switching repositories. And I also already have an apache server running on my server.

Now, Lucid lynx comes with mercurial 1.4. If you want to install Mercurial 1.5, 1.6, or 1.7 you need a new repo :

sudo add-apt-repository ppa:mercurial-ppa/releases
sudo apt-get update
sudo apt-get install mercurial

Don't forget to check the version using
hg -v

The steps

Pre-install

Installing mercurial in ubuntu is as easy as running
$>sudo apt-get install mercurial

Finding the hgweb.cgi file

First we need to find the hgweb.cgi file. Usually it resides in /usr/share/doc/mercurial-common/examples/hgweb.cgi . If you can't find it using locate or executing this command should do it.
$>find / -iname hgweb.cgi

go to the /usr/lib/cgi-bin/ and create hg directory. Copy the hgweb.cgi to /usr/lib/cgi-bin/hg/. Then make it executbale using this command
chmod  755

Creating the user and directories

Create user hguser with www-data as its group. it should have the /home/hguser as its default home. Now login as the hguser, change the .bashrc for hguser. You can use
vi ~/.bashrc

Add this line at the end of the file and save it:

umask 002

With the umask set, the file and directories created by the hguser will have group write permission. So the www-data (default apache user) can create and write any file in the repository. Now create hg directory for our repositories, so now we have /home/hguser/hg for our repositories yay !

for my setup I'm using 2 repositories, one for development and one for release. So under the hg directory, I created 2 other directory,
/home/hguser/hg/dev and
/home/hguser/hg/stable

Now you can logout from the hguser and back to your default user.

Setting the apache server

As I mention already have an apache2 server running. My server 80 is already used by something else, so i'm using another port for apache. Lets say port 9000. To do this you must edit the ports.conf file and make the changes accordingly. You can use this command :

sudo vi /etc/apache2/ports.conf


You also need to change the port on the default apache file to 9000. You can use this command :

sudo vi /etc/apach2/sites-enabled/000-default


still in the 000-default files, activate the cgi script. The easiest way is by using Script Alias, add the lines bellow to the 000-default files:
ScriptAlias /hg /usr/lib/cgi-bin/hg/hgweb.cgi

Setting Mercurial Repositories

To enable mercurial for multiple repository, it is easier to use the hgweb.config file.
First login as the hguser.
Create the hgweb.config under the hg directory

The hgweb.config file should contain :
[collections]
/home/hguser/hg/ = /home/hguser/hg/

edit the hgweb.cgi and update the config line to the line bellow :
config = "/home/hguser/hg/hgweb.config"

Restart the apache server, than you should be able to access http://yourserver/hg directory

Allowing Push

I'm allowing all authenticated user to push to the repository, and since the most important thing is make the repo up and running, I'm making SSL implementation none mandatory. So to allow push you need to edit the /etc/mercurial/hgrc and add the following line below

[web]
allow_push = *
push_ssl = false

Adding Authentication


To add the authentication to your mercurial server you need to add the line bellow to the 000-default file, just under the script alias should suffice.
<location /hg>
                AuthType Basic
                AuthName "Mercurial repositories"
                AuthUserFile /home/hguser/hg/hgusers
                Require valid-user
        </Location>

Now login as the hguser and go to the hg directory. To create the hgusers authentication file and create the first user you can use the comand bellow :
htpasswd -c /home/user/hg/hgusers frodo

to add another user you can remove the -c flag

htpasswd /home/user/hg/hgusers sam

restart apache and you should be able to to access the repo at :

http://www.yourserver.com:9000/hg/


Trouble Shooting

The trouble shooting section in the wiki is very good. I just highlight some here since I've encountered them in my installation. 

If you find the error message bellow in your apache2 error log. 

/var/lib/python-support/python2.6/mercurial/hgweb/common.py:24: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6

you're using and older mercurial that work quite right with the latest python implementation. To suppress it add the following lines to the hgweb.cgi immediately after any sys.path adjustment :

import warnings
warnings.simplefilter("ignore", DeprecationWarning)

And if you're able to create a new repo and synch it to your local komputer but  failing when pushing or cloning your existing repo to the server. Try  changing the hguser/hg directory permission recursively to 777. If that did work than you didn't set your permission correctly.

Good luck

No comments: