Bash Scripting a new SVN Repository with Plesk API

Bookmark and Share

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:

1
#!/bin/bash
2
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

1
# Check user input
2
SUBDOMAIN=$1
3
DOMAIN=$2
4
SUBDOMAINPATH=/var/www/vhosts/$DOMAIN/subdomains/$SUBDOMAIN
5
if [ -z $SUBDOMAIN ] || [ -z $DOMAIN ]; then
6
    echo invalid input
7
    exit
8
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

01
# Report to the user
02
echo
03
echo -e "    subdomain:   $SUBDOMAIN"
04
echo -e "       domain:   $DOMAIN"
05
echo -e " repolocation:   http://$1.$2"
06
echo -e "disk location:   $SUBDOMAINPATH/svn/"
07
echo
08
echo -e "Are you sure? (y/n) \c "
09
read confirm
10
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
11
    exit
12
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

1
# Create subdomain in plesk
2
echo -e "Creating subdomain."
3
cd '/usr/local/psa/bin/'
4
./subdomain --create $SUBDOMAIN -domain $DOMAIN
5
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

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

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

01
# Create Apache config file
02
cd $SUBDOMAINPATH/conf
03
cat > vhost.conf << EOF
04
<Location />
05
    DAV svn
06
    SVNPath $SUBDOMAINPATH/svn
07
    AuthType Basic
08
    AuthName "Subversion Repository"
09
    AuthUserFile /etc/svn-auth-file
10
    Require valid-user
11
</Location>
12
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

1
# Reconfigure the host to accept new settings
2
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=$DOMAIN
3
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:

01
#!/bin/bash
02
set -e
03
 
04
# Check user input
05
SUBDOMAIN=$1
06
DOMAIN=$2
07
SUBDOMAINPATH=/var/www/vhosts/$DOMAIN/subdomains/$SUBDOMAIN
08
if [ -z $SUBDOMAIN ] || [ -z $DOMAIN ]; then
09
    echo invalid input
10
    exit
11
fi
12
 
13
# Report to the user
14
echo
15
echo -e "    subdomain:   $SUBDOMAIN"
16
echo -e "       domain:   $DOMAIN"
17
echo -e " repolocation:   http://$1.$2"
18
echo -e "disk location:   $SUBDOMAINPATH/svn/"
19
echo
20
echo -e "Are you sure? (y/n) \c "
21
read confirm
22
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
23
    exit
24
fi
25
 
26
# Create subdomain in plesk
27
echo -e "Creating subdomain."
28
cd '/usr/local/psa/bin/'
29
./subdomain --create $SUBDOMAIN -domain $DOMAIN
30
echo -e "Finished Creating Subdomain."
31
 
32
# Create SVN Repo
33
echo -e "Creating SVN Repo."
34
svnadmin create $SUBDOMAINPATH/svn
35
chown -R apache:apache $SUBDOMAINPATH/svn
36
 
37
# Create Apache config file
38
cd $SUBDOMAINPATH/conf
39
cat > vhost.conf << EOF
40
<Location />
41
    DAV svn
42
    SVNPath $SUBDOMAINPATH/svn
43
    AuthType Basic
44
    AuthName "Subversion Repository"
45
    AuthUserFile /etc/svn-auth-file
46
    Require valid-user
47
</Location>
48
EOF
49
 
50
# Reconfigure the host to accept new settings
51
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=$DOMAIN
52
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:
1
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:

01
[root@server ~]# svncreate svn1 mydomain.net
02
 
03
subdomain:   svn1
04
domain:   mydomain.net
05
repolocation:   http://svn1.mydomain.net
06
disk location:   /var/www/vhosts/mydomain.net/subdomains/svn1/svn/
07
 
08
Are you sure? (y/n) y
09
Creating subdomain.
10
SUCCESS: Creation of subdomain 'svn1' complete.
11
Finished Creating Subdomain.
12
Creating SVN Repo.
13
Finished Creating SVN Repo.
14
[root@server ~]#

This entry was posted in Server Admin and tagged , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.