Mercurial we show you the characteristics and basic steps to start working with it. Now we are going to teach you the necessary steps so that the repositories are accessible through Apache.
Mercurial supports different protocols to access a Repository (http, https, ssh, file and local), but when we want to publish a repository to be remotely accessible, the possibilities are reduced. Of all of them, https is the most used and the one Mercurial recommends in its documentation.
On the official page there is a table that describes all the possible solutions to publish a repository. In our case, for the https protocol, we can use:
- hg server under a Nginx proxy : this solution uses the server that comes integrated with Mercurial. Since this only supports the http protocol, it will be necessary to use a proxy to use https. With this solution we will be able to publish several repositories. By default, the ability to push is enabled, allows authentication and does not use CGI.
- hgweb : is a solution that integrates with an existing web server using python, CGI or WSGI. It is integrated with server authentication, but it will only be able to publish a single repository and by default the use of push will not be enabled.
- hgwebdir : this is the same solution as hgweb but with the only difference that we will be able to publish more than one repository.
Of all the solutions, the most suitable is hgwebdir since it gives us the possibility of publishing several repositories and its execution will be under a web server as Apache will be. The hg server solution should only be used for temporary situations and not as a permanent server.
As the only requirement of this tutorial we will assume that we have an Apache web server with the https protocol configured.
Environment used to write this tutorial:
- Hardware : VirtualBox 3.2.8 running on a Mac Book Pro (Core 2 Duo 2.8 Ghz, 4 GB RAM, 500 GB)
- Operating System : Ubuntu 64 bits 10.04 LTS
- Mercurial : 1.6.3
- Python : 2.6.1
- Wsgi: 2.8.2
3. Mercurial installation
The first thing we have to do is install Mercurial and the python library. For this we will execute the following command:
apt-get install mercurial python python-dev
Then we must create the directory where we will store the repositories and other configuration files:
Now we copy the hgwebdir script that we will later use in Apache. This file is in one of these two paths: / usr / share / doc / mercurial-common / examples or / usr / share / doc / mercurial / examples /. In our case we would execute the following command
At the moment we are not going to make any changes in it, we will do it later. The last thing that we would have left is to give permissions so that these resources can be accessed by the user with whom Apache runs.
4. Installation of modwsgi module for Apache
As we have said in the introduction, we are going to use the hgwebdir solution. This implies that we have to decide which option to use to serve the repositories (python module, wsgi module or CGI). In our case we are going to install the modwsgi module because it is the best solution because of its ease of configuration and its better performance compared to the other alternatives.
There are several ways to install the module in our Apache: manually, compiling the sources of http://code.google.com/p/modwsgi/, or installing the precompiled module of the Debian repositories. For convenience we will use the second option, so we execute the following command:
apt-get install libapache2-mod-wsgi
Once the installation of the module is finished, we have to check that in the directory / etc / apache2 / mods-enabled / there are the following files wsgi.conf and wsgi.load. If they did not exist we should create links to those files from / etc / apache2 / mods-available /.
Remember that all the paths that we are using in this tutorial are the standard ones and therefore they should be modified if your environment uses different ones.
5 Configuring Apache 2.x with modwsgi module
The next step we must follow is to configure our web server. We edit the file default-ssl located in the path / etc / apache2 / sites-available, and just before the line SSLEngine On, we add:
With these lines what we do is that when we access the url https: // ourserver / hg, launch the script /var/hg/cgi-bin/hgwebdir.wsgi and give us access to the repositories that we have configured.
6 Retouching the hgwebdir.wsgi script
Once we have configured Apache we will have to modify the file hgwebdir.wsgi that we copied at the beginning. This file will be responsible for providing access to the repositories that we configure. It defines the route of the Mercurial configuration file. By default the script will try to look for a file called hgweb.config inside the cgi-bin directory. In our case this route is not valid and we will have to replace it with /var/hg/hgweb.config, so we edit the file /var/hg/cgi-bin/hgwebdir.wsgi, we go to the last line and where it puts :
application = hgwebdir(‘hgweb.config’)
we replace it with:
Now we only need to add the necessary configuration to the hgweb.config file.
7 Configuring Mercurial
We have practically completed the basic configuration to be able to publish a repository through Apache. Now we have to add the necessary configuration to the file /var/hg/hgweb.config. It could really be considered as any other hgrc configuration file that we could find in the .hg directory of a repository. The only difference is that in it we will configure everything necessary to access the repositories via https.
As we explained in the previous Mercurial tutorial, this file is divided into sections where each one will have a set of properties that make up a part of Mercurial. At the moment we are going to add to this file the base URL that we use to access the repositories and the style with which the information will be presented via the web. For this we add the following lines:
As you can see we do not see almost anything because we have not yet configured any repository so that it can be accessed through the Apache.
8 Publishing a repository
As it seems evident, before publishing a repository we will have to create it. So hands on the job. To create a Mercurial repository we must use the “hg init” command. In our case we will do the following:
Once created, we must add it to the configuration so that it is accessible. There are two ways to publish a repository or set of repositories. We can do it using the paths or collections section. Collections was the old form that was used in the previous versions of Mercurial 1.1 and is maintained to maintain compatibility, but the correct thing would be to use the paths section. In it we will define all the repositories that we want to be accessible through our web server. Basically it is to indicate the alias that will be used to access it and the physical path where the repository is located in our system. In our case, the paths section would look like this:
With this configuration, when we access https: // myserver / hg / adictos-repo, we will be accessing the repository located in / var / hg / repositories / adictos-repo.
If we save the file and reopen a browser by accessing https: // localhost / hg, we will see our accessible repository:
9 Restricting access to the repository
If we want access to our repository to be restricted, we must configure Apache to request credentials when we want to perform an operation with the repository. In this example we are going to use basic authentication, but we can configure any authentication that Apache allows such as Digest or Ldap.
We edit the default-ssl file and add the following lines to the Directory section created above:
Now we only have to create the user file, and when we finish we will restart the apache to take the changes. To create the users we do the following:
10 Activating the push on the repository.
By default, the push of the repositories is disabled. To enable it, we must edit the hgrc file that is inside the .hg directory of the repository and add the following lines (in the case that it does not exist, we should create it):
With this we allow the push operation of any user on the repository. To restrict which users can push instead of “*”, we will indicate the list of users who can perform this operation separated by commas.
And with this last change we have mounted our repository accessible by https. In a next tutorial we will see how to add fine grain permissions.
Enabling remote access to repositories is commonplace when working with a version control system. The development teams need it to be able to work with them. In the case of Mercurial, it can only be accessed through ssh or http. With this tutorial we have seen the second option for being the most used in this SCM.
The post Publish a Mercurial repository with Apache appeared first on Target Veb.