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.
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.
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/
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: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
We then accept the command line input parameters (
$1,$2etc.) 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. Theifstatement is there to check if the input strings are both not null.Report to the user
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
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
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
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/svnas well as the auth file I had set up before at/etc/svn-auth-filewhich simply contains the login credentials of my SVN user.Reconfigure apache to accept the new settings
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:
Make sure you have /root/bin in your $PATH variable and then upload the script to your
/root/binfolder under the filenamesvncreateand then run the following: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: