Bash script to create a full software development environment

I have create this (first version) script to automatize the creation of all the services related with a new software development project.

The script creates…

  • A new Subversion repository to host the source code of the project.
  • A new MySQL database to store the data of the Trac instance.
  • A new instance of Trac, linked to the just created Subversion repository, to be used like the project’s portal.
  • A configuration file for Apache2 in order to made accesible the Trac repository.

The structure of the new Subversion repository is as follows:

-> branches
-> tags
-> trunk
   -> docs
   -> src

The environment used was the following

  • Ubuntu Server 11.04 64 bits
  • Apache/2.2.17 (installed from the Ubuntu repositories)
  • MySQL Server 5.1.541ub (installed from the Ubuntu repositories)
  • Subversion 1.6.12 (installed from the Ubuntu repositories)
  • Python 2.7.1+ (installed from the Ubuntu repositories)
  • Trac 0.12

TODO

  • Implement checking to ensure that the different elements do not exists before trying to create them (for example the Subversion repository, or the MySQL database).
  • Implement the installation of the Subversion hooks that ensure that the Trac instance keeps in sync with the Subversion repository
#!/bin/bash

# Author: Jorge Tomé Hernando <jorge@jorgetome.info>
# Date: August 2011
# Version: 1.0
#
# Description
# -----------
# This scripts creates all the environment needed to support
# a new software development project.
#
# It creates a new Subversion repository, a new Trac instance
# (and the associated MySQL database) and a configuration file
# for the Apache2 web server.
#
# It also restart the Apache2 server in order to apply the new
# configuration.
#
# It has been developed and tested in an Ubuntu 11.04 environment.

usage()
{
    cat<<EOF
usage:$0 options

This script creates a new support environment for a software
development project including: Subversion repository and
Trac instance.

Options:
-h Shows this message
-p Name of the project
-u User name of the project's administrator
EOF
}

if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

PROJECT_NAME=
PROJECT_ADMIN=

while getopts ":hp:u:" opt; do
    case $opt in
        h)
            usage
            exit 1
            ;;
        p)
            PROJECT_NAME=$OPTARG
            ;;
        u)
            PROJECT_ADMIN=$OPTARG
            ;;
        ?)
            usage
            exit
        ;;
    esac
done

if [ -z $PROJECT_NAME ] || [ -z $PROJECT_ADMIN ]
then
    usage
    exit 1
fi

# Configuration variables
SVN_HOME=/srv/svn
TRAC_HOME=/srv/trac
DB_PREFIX=trac_
DB_USR=MyUserForTrac
DB_PWD=MyPasswordForTheUserForTrac
DB_HOST=localhost
DB_PORT=3306
APACHE_USR=www-data
APACHE_CONF_DIR=/etc/apache2/projects.d

# Utility variables
PROJECT_DIR=`echo ${PROJECT_NAME,,}`
DB_NAME=${DB_PREFIX}${PROJECT_DIR}
SVN_DIR=${SVN_HOME}/${PROJECT_DIR}
TRAC_DIR=${TRAC_HOME}/${PROJECT_DIR}

# First we create the Subversion repository
svnadmin create --fs-type fsfs ${SVN_DIR}
svn mkdir -m "Initialization of the repository" \
--parents \
file://${SVN_DIR}/trunk/docs \
file://${SVN_DIR}/trunk/src \
file://${SVN_DIR}/branches \
file://${SVN_DIR}/tags

# Second we have to create the MySQL database to support Trac
mysql -u root -p <<QUERY_INPUT
CREATE DATABASE ${DB_NAME};
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO ${DB_USR}@${DB_HOST} IDENTIFIED BY '${DB_PWD}';
QUERY_INPUT

# Third we have to create the Trac instance
trac-admin ${TRAC_DIR} initenv ${PROJECT_NAME} mysql://${DB_USR}:${DB_PWD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
trac-admin ${TRAC_DIR} repository add ${PROJECT_DIR} ${SVN_DIR} svn
trac-admin ${TRAC_DIR} repository resync ${PROJECT_DIR}
trac-admin ${TRAC_DIR} permission add ${PROJECT_ADMIN} TRAC_ADMIN
trac-admin ${TRAC_DIR} deploy ${TRAC_DIR}/deploy

# Fourth we have to create the Apache2 configuration file
cat > ${APACHE_CONF_DIR}/${PROJECT_DIR}.conf <<EOF
WSGIScriptAlias /trac/${PROJECT_DIR} ${TRAC_DIR}/deploy/cgi-bin/trac.wsgi

<Directory ${TRAC_DIR}/deploy/cgi-bin>
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>https://www.jorgetome.info/bash-script-to-create-a-full-software-development-environment.html  

<Location "/trac/${PROJECT_DIR}/login">
    AuthType Basic
    AuthName "Trac"
    AuthUserFile /srv/trac/.htpasswd
    Require valid-user
</Location>
EOF

# Last we have to adjust the permissions on the directories and
# restart the web server
chown -R ${APACHE_USR}:${APACHE_USR} ${SVN_DIR} ${TRAC_DIR}
apache2ctl restart