Agile Development Tools Server Part 2 - Trac

Saturday, 8th January 2011 - Michael Halls-Moore - 2 Comments

In Part 1 of this series I explained the benefits of building a separate agile development tools server to host your version control, project management, continuous integration, monitoring and backup tools. I outlined how to install the open-source version control system Subversion (SVN). Now I wish to show you how to tie SVN into a project management tool created by Edgewall, known as Trac. Trac provides bug tracking, repository integration with timelines, source code browsing, project wikis and a host of additional features with plugins. This tutorial will describe how to install Trac, configure it and connect it to SVN.

Let's begin by connecting to the server you created in Part 1 and installing Trac. I didn't explicitly specify whether I was using a VM or a hosted server instance for our Ubuntu Server 10.04 LTS setup, so you can decide how to perform the connection. We will use Python Setuptools to install Trac, rather than the Ubuntu binaries, as this will give us the most up to date stable version. At the time of writing this post the current stable version was 0.12:

sudo apt-get install python-setuptools
sudo easy_install Trac==0.12

Now we create the directory that Trac will use to store the configuration and database for our project. As with the Subversion directory, I prefer to keep these project roots out of any user home directory. On a multi-developer machine this ensures that access privileges are more straightforward to administer. We will also allow the Apache2 user (www-data) to take ownership of the directory:

sudo mkdir /var/lib/trac
sudo chown -R www-data:www-data /var/lib/trac

The next stage is to tell Apache how we would like to host the site. Firstly though, a word of warning on potential security threats. The Trac Wiki is a common place to store username/password combinations for the myriad services that crop up when running a startup. Serving Trac on the default HTTP port of 80 will send any information back and forth to the server via plaintext, including passwords intended for Wiki files. Thus, as with Subversion, we will serve Trac on a Secure Socket Layer (SSL) over port 443 and use https://***/ when we wish to interact with it. This stops potential snoopers stealing our passwords. Let's configure Apache with a new virtualhost with this information in mind:

sudo emacs /etc/apache2/sites-available/trac

My personal preference is to use subdomains for all of these services. For instance if your project is called My Project, then Subversion would live at https://svn.myproject.com/ while Trac would live at https://trac.myproject.com/ Add the following code to the file, replacing your ServerAdmin email and ServerName as appropriate.

<VirtualHost *:443>
  ServerAdmin youremail@myproject.com
  ServerName trac.myproject.com
  DocumentRoot /var/www/myproject
  
  <Location />
    SetHandler mod_python
    PythonInterpreter main_interpreter
    PythonHandler trac.web.modpython_frontend
    PythonOption TracEnvParentDir /var/lib/trac
    PythonOption TracUriRoot /
    AuthType Basic
    Authname "Trac"
    AuthUserFile /etc/apache2/svn.passwd
    Require valid-user
  </Location>
</VirtualHost>

Let's discuss what is happening in this file. The first line tells Apache that you want to listen on port 443 (HTTPS) for a new virtualhost directive. The name to listen for is provided by the ServerName attribute. This means that when your DNS is correctly configured, any traffic sent to this server on port 443 for "trac.myproject.com" will be redirected to this configuration file.

We're telling Apache to use the mod_python Python module to handle all of the Trac code for serving. The SetHandler and PythonInterpreter tell Apache to use the default Python installation (v2.6 on Ubuntu Server 10.04 LTS). Apache also needs to know where the Trac environment parent directory containing our configuration is located. In addition, we need to inform Apache which URL postfix to look for to serve Trac. We're telling Apache to serve it directly from the webroot. As such, http://trac.mydomain.com/ will point to the Trac page itself.

The remaining four lines indicate that we wish to use Basic Authentication to stop clients accessing our project who do not provide access credentials. We authorise against the password file that we created in Part 1.

Let's continue the tutorial by creating the DocumentRoot directory that we specified in the Apache configuration. Then we will enable the Trac site and restart Apache:

sudo mkdir /var/www/trac
sudo a2ensite trac
sudo /etc/init.d/apache2 restart

The next step is to initalise the Trac environment for myproject with some default configuration, using the trac-admin tool. This is where trac keeps the database for wiki pages, tickets and reports. You will be prompted to enter some information, but I suggest just using the defaults if you are unsure:

sudo trac-admin /var/lib/trac/myproject initenv

We are now ready to integrate Trac with Subversion. This will provide us with the ability to view the codebase across all revisions (very handy!) and keep track of multiple branches/tags for release. Let's begin by backing up our Trac initialisation file (in case we make a mistake), allowing Apache to read it and then editing it:

sudo cp /var/lib/trac/myproject/conf/trac.ini \
/var/lib/trac/myproject/conf/trac.ini.bak
sudo chown www-data:www-data \
/var/lib/trac/myproject/conf/trac.ini.bak
sudo emacs /var/lib/trac/myproject/conf/trac.ini

Find the [header_logo] heading and edit the file so that it points to a logo of your choice, if you so desire. You will need to change the Alt text, the height/width, the link to point the logo to (perhaps your startup homepage) and the location of the image. In addition locate the [trac] heading and modify the repository_dir to point to your Subversion repository:

[header_logo]
alt = MyProjectLogo
height = -1
link =
src = /logo.gif
width = -1
 
..
..
 
[trac]
..
..
repository_dir = /var/lib/svn/repo

We need to provide administrative privilege for at least one user. This will allow you to update task categories, milestones and other meta information about your project. When you login to Trac with the following username, you will see an additional Admin tab:

sudo trac-admin /var/lib/trac/myproject \
permission add myuser TRAC_ADMIN

The final task before fully integrating with Subversion is to modify a set of Subversion hooks. These hooks are analagous to callback functions which are to be performed after certain events have been triggered. We are going to adjust the post-commit and post-revision-property-change hooks so that Trac is aware of the changes and is always up to date with our SVN repo. Let's begin with post-commit.tmpl:

cd /var/lib/svn/repo/hooks
sudo emacs post-commit.tmpl

Add the following lines to the post-commit.tmpl file:

TRAC-ENV="/var/lib/trac/myproject"
/usr/bin/trac-admin "$TRAC-ENV" changeset added "$REPOS" "$REV"

Save the changes, exit and open up post-revprop-change.tmpl:

sudo emacs post-revprop-change.tmpl

Add the following lines to that file:

TRAC-ENV="/var/lib/trac/myproject"
/usr/bin/trac-admin "$TRAC-ENV" changeset modified "$REPOS" "$REV"

You should now find that interacting with your repository will push updates to your Trac environment.

That concludes the tutorial on installing and configuring Trac. In my opinion, this is the minimum you need in order to have a functional tools server. A robust production grade system would not be complete without continuous integration or server monitoring. In addition, I have not discussed backup strategies or network access restriction via a firewall. I will also provide a workflow tutorial which will outline "best practice" for using the tools. All of these topics will be fleshed out in the remaining tutorials.

Let me know how you get on with your Trac environment, in the comments - I'm always keen to hear about other setups as well so do get in touch!

2 comments ... read them below or add one
  • odeh
    14th February 2011
    11:28 am

    very nice article. looking forward to Part 3 :)

  • Cata
    11th April 2011
    3:03 pm

    Hello. Super article :)
    If I don't have a domain set up, only an elastic ip, how I can acces trac ? (ServerName trac.myproject.com)

    Thanks!