Bash Scripting a new SVN Repository with Plesk API

I have recently found myself trying to automate setting up SVN repositories for my projects via shell scripts and thought I would share how I did it. I needed a script that would create a new SVN repository linked to a global user and password file. Since I was already using plesk I needed it to be compatible. Here's how you do it:

Create your shell script

Open up your favorite editor and lets create the following script I called mine svncreate:

#!/bin/bash
set -e

This bit is just for telling our shell to use bash as opposed to any other scripting languages. The set -e bit is making our script exit if any command returns an error.

Check your script input

# Check user input
SUBDOMAIN=$1
DOMAIN=$2
SUBDOMAINPATH=/var/www/vhosts/$DOMAIN/subdomains/$SUBDOMAIN
if [ -z $SUBDOMAIN ] || [ -z $DOMAIN ]; then
    echo invalid input
    exit
fi

We then accept the command line input parameters ($1,$2 etc.) and turn them into variables with meaningfull names. This is not really necessary although it is something I like to do as it makes the code easier to read later on which is important. The if statement is there to check if the input strings are both not null.

Report to the user

# Report to the user
echo
echo -e "    subdomain:   $SUBDOMAIN"
echo -e "       domain:   $DOMAIN"
echo -e " repolocation:   http://$1.$2"
echo -e "disk location:   $SUBDOMAINPATH/svn/"
echo
echo -e "Are you sure? (y/n) \c "
read confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
    exit
fi

This section just presents a little report stating what the script is going to do and asks for a y or n answer. if the user doesnt enter y or Y the script will exit

Create a subdomain using the Plesk CLI

# Create subdomain in plesk
echo -e "Creating subdomain."
cd '/usr/local/psa/bin/'
./subdomain --create $SUBDOMAIN -domain $DOMAIN
echo -e "Finished Creating Subdomain."

This part of the script creates the subdomain in plesk. First we change our working directory to the CLI folder in Plesk. Then we run the subdomain command which creates the subdomain passing in the relavent user input from the commandline script call. The echo statements let us know where we are in the code execution.

Create the SVN Repo

# Create SVN Repo
echo -e "Creating SVN Repo."
svnadmin create $SUBDOMAINPATH/svn
chown -R apache:apache $SUBDOMAINPATH/sv

Here we create an svn repo with the svn admin tool within the subdomain path folder under a folder called svn. We then set the folders permissions so that apache can read and write to it.

Link the Apache domain to the SVN Repo

# Create Apache config file
cd $SUBDOMAINPATH/conf
cat > vhost.conf << EOF
<Location />
    DAV svn
    SVNPath $SUBDOMAINPATH/svn
    AuthType Basic
    AuthName "Subversion Repository"
    AuthUserFile /etc/svn-auth-file
    Require valid-user
</Location>
EOF

We then create the apache config file within the conf folder of the plesk domain by using the cat unix tool here we are piping the output of cat into a file called vhost.conf. Plesk uses this file to create Apache include files within the domain settings.

The Location directive here is telling Apache to connect to the SVN repository located at $SUBDOMAINPATH/svn as well as the auth file I had set up before at /etc/svn-auth-file which simply contains the login credentials of my SVN user.

Reconfigure apache to accept the new settings

# Reconfigure the host to accept new settings
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=$DOMAIN
echo -e "Finished Creating SVN Repo."

Here we are simply telling plesk to accept the configuration and create the include files.

Uploading the script

The whole script should look something like this:

#!/bin/bash
set -e
 
# Check user input
SUBDOMAIN=$1
DOMAIN=$2
SUBDOMAINPATH=/var/www/vhosts/$DOMAIN/subdomains/$SUBDOMAIN
if [ -z $SUBDOMAIN ] || [ -z $DOMAIN ]; then
    echo invalid input
    exit
fi
 
# Report to the user
echo
echo -e "    subdomain:   $SUBDOMAIN"
echo -e "       domain:   $DOMAIN"
echo -e " repolocation:   http://$1.$2"
echo -e "disk location:   $SUBDOMAINPATH/svn/"
echo
echo -e "Are you sure? (y/n) \c "
read confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
    exit
fi
 
# Create subdomain in plesk
echo -e "Creating subdomain."
cd '/usr/local/psa/bin/'
./subdomain --create $SUBDOMAIN -domain $DOMAIN
echo -e "Finished Creating Subdomain."
 
# Create SVN Repo
echo -e "Creating SVN Repo."
svnadmin create $SUBDOMAINPATH/svn
chown -R apache:apache $SUBDOMAINPATH/svn
 
# Create Apache config file
cd $SUBDOMAINPATH/conf
cat > vhost.conf << EOF
<Location />
    DAV svn
    SVNPath $SUBDOMAINPATH/svn
    AuthType Basic
    AuthName "Subversion Repository"
    AuthUserFile /etc/svn-auth-file
    Require valid-user
</Location>
EOF
 
# Reconfigure the host to accept new settings
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=$DOMAIN
echo -e "Finished Creating SVN Repo."

Make sure you have /root/bin in your $PATH variable and then upload the script to your /root/bin folder under the filename svncreate and then run the following:

chmod 755 /root/bin/svncreate

Running the script

Now we have the script created we can run it if all is set up correctly we should see the following when we run it:

[root@server ~]# svncreate svn1 mydomain.net
 
subdomain:   svn1
domain:   mydomain.net
repolocation:   http://svn1.mydomain.net
disk location:   /var/www/vhosts/mydomain.net/subdomains/svn1/svn/
 
Are you sure? (y/n) y
Creating subdomain.
SUCCESS: Creation of subdomain 'svn1' complete.
Finished Creating Subdomain.
Creating SVN Repo.
Finished Creating SVN Repo.
[root@server ~]#
This entry was posted in Server Admin and tagged , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.