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/bashset -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.
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
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.
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.
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=$DOMAINecho -e "Finished Creating SVN Repo."
Here we are simply telling plesk to accept the configuration and create the include files.
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:
chmod755 /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 ~]#
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: