<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RudiYardley.com &#187; Unix</title>
	<atom:link href="http://blog.rudiyardley.com/tag/unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rudiyardley.com</link>
	<description>Technical tidbits on Flash, Flex, Application Development, MODx, Silverstripe and Freelancing</description>
	<lastBuildDate>Tue, 31 Jan 2012 04:31:03 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bash Scripting a new SVN Repository with Plesk API</title>
		<link>http://blog.rudiyardley.com/2009/10/bash-scripting-svn-repository-plesk-api/</link>
		<comments>http://blog.rudiyardley.com/2009/10/bash-scripting-svn-repository-plesk-api/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 20:25:37 +0000</pubDate>
		<dc:creator>rudi</dc:creator>
				<category><![CDATA[Server Admin]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[plesk]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://blog.rudestar.net/?p=39</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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&#8217;s how you do it:</p>
<h3>Create your shell script</h3>
<p>Open up your favorite editor and lets create the following script I called mine <code>svncreate</code>:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>#!/bin/bash</li><li>set -e</li></ol></div></pre><!--END_DEVFMTCODE--><br />
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.</p>
<h3>Check your script input</h3>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li># Check user input</li><li>SUBDOMAIN=$1</li><li>DOMAIN=$2</li><li>SUBDOMAINPATH=/var/www/vhosts/$DOMAIN/subdomains/$SUBDOMAIN</li><li>if <span class="br0">&#91;</span> -z $SUBDOMAIN <span class="br0">&#93;</span> || <span class="br0">&#91;</span> -z $DOMAIN <span class="br0">&#93;</span>; then</li><li>&nbsp;&nbsp;&nbsp;&nbsp;echo invalid input</li><li>&nbsp;&nbsp;&nbsp;&nbsp;exit</li><li>fi</li></ol></div></pre><!--END_DEVFMTCODE--><br />
We then accept the command line input parameters (<code>$1</code>,<code>$2</code> 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 <code>if</code> statement is there to check if the input strings are both not null.</p>
<h3>Report to the user</h3>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li># Report to the user</li><li>echo</li><li>echo -e &quot;&nbsp;&nbsp;&nbsp;&nbsp;subdomain:&nbsp;&nbsp; $SUBDOMAIN&quot;</li><li>echo -e &quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; domain:&nbsp;&nbsp; $DOMAIN&quot;</li><li>echo -e &quot; repolocation:&nbsp;&nbsp; http://$1.$2&quot;</li><li>echo -e &quot;disk location:&nbsp;&nbsp; $SUBDOMAINPATH/svn/&quot;</li><li>echo</li><li>echo -e &quot;Are you sure? <span class="br0">&#40;</span>y/n<span class="br0">&#41;</span> \c &quot;</li><li>read confirm</li><li>if <span class="br0">&#91;</span> &quot;$confirm&quot; != &quot;y&quot; <span class="br0">&#93;</span> &amp;amp;&amp;amp; <span class="br0">&#91;</span> &quot;$confirm&quot; != &quot;Y&quot; <span class="br0">&#93;</span>; then</li><li>&nbsp;&nbsp;&nbsp;&nbsp;exit</li><li>fi</li></ol></div></pre><!--END_DEVFMTCODE--><br />
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</p>
<h3>Create a subdomain using the Plesk CLI</h3>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li># Create subdomain in plesk</li><li>echo -e &quot;Creating subdomain.&quot;</li><li>cd '/usr/local/psa/bin/'</li><li>./subdomain --create $SUBDOMAIN -domain $DOMAIN</li><li>echo -e &quot;Finished Creating Subdomain.&quot;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
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.</p>
<h3>Create the SVN Repo</h3>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li># Create SVN Repo</li><li>echo -e &quot;Creating SVN Repo.&quot;</li><li>svnadmin create $SUBDOMAINPATH/svn</li><li>chown -R apache:apache $SUBDOMAINPATH/svn</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Here we create an svn repo with the svn admin tool within the subdomain path folder under a folder called <code>svn</code>. We then set the folders permissions so that apache can read and write to it.</p>
<h3>Link the Apache domain to the SVN Repo</h3>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li># Create Apache config file</li><li>cd $SUBDOMAINPATH/conf</li><li>cat &amp;gt; vhost.conf &amp;lt;&amp;lt; EOF</li><li>&amp;lt;Location /&amp;gt;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;DAV svn</li><li>&nbsp;&nbsp;&nbsp;&nbsp;SVNPath $SUBDOMAINPATH/svn</li><li>&nbsp;&nbsp;&nbsp;&nbsp;AuthType Basic</li><li>&nbsp;&nbsp;&nbsp;&nbsp;AuthName &quot;Subversion Repository&quot;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;AuthUserFile /etc/svn-auth-file</li><li>&nbsp;&nbsp;&nbsp;&nbsp;Require valid-user</li><li>&amp;lt;/Location&amp;gt;</li><li>EOF</li></ol></div></pre><!--END_DEVFMTCODE--><br />
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 <code>vhost.conf</code>. Plesk uses this file to create Apache include files within the domain settings.</p>
<p>The Location directive here is telling Apache to connect to the SVN repository located at <code>$SUBDOMAINPATH/svn</code> as well as the auth file I had set up before at <code>/etc/svn-auth-file</code> which simply contains the login credentials of my SVN user.</p>
<h3>Reconfigure apache to accept the new settings</h3>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li># Reconfigure the host to accept new settings</li><li>/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=$DOMAIN</li><li>echo -e &quot;Finished Creating SVN Repo.&quot;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Here we are simply telling plesk to accept the configuration and create the include files.</p>
<h3>Uploading the script</h3>
<p>The whole script should look something like this:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>#!/bin/bash</li><li>set -e</li><li>&nbsp;</li><li># Check user input</li><li>SUBDOMAIN=$1</li><li>DOMAIN=$2</li><li>SUBDOMAINPATH=/var/www/vhosts/$DOMAIN/subdomains/$SUBDOMAIN</li><li>if <span class="br0">&#91;</span> -z $SUBDOMAIN <span class="br0">&#93;</span> || <span class="br0">&#91;</span> -z $DOMAIN <span class="br0">&#93;</span>; then</li><li>&nbsp;&nbsp;&nbsp;&nbsp;echo invalid input</li><li>&nbsp;&nbsp;&nbsp;&nbsp;exit</li><li>fi</li><li>&nbsp;</li><li># Report to the user</li><li>echo</li><li>echo -e &quot;&nbsp;&nbsp;&nbsp;&nbsp;subdomain:&nbsp;&nbsp; $SUBDOMAIN&quot;</li><li>echo -e &quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; domain:&nbsp;&nbsp; $DOMAIN&quot;</li><li>echo -e &quot; repolocation:&nbsp;&nbsp; http://$1.$2&quot;</li><li>echo -e &quot;disk location:&nbsp;&nbsp; $SUBDOMAINPATH/svn/&quot;</li><li>echo</li><li>echo -e &quot;Are you sure? <span class="br0">&#40;</span>y/n<span class="br0">&#41;</span> \c &quot;</li><li>read confirm</li><li>if <span class="br0">&#91;</span> &quot;$confirm&quot; != &quot;y&quot; <span class="br0">&#93;</span> &amp;amp;&amp;amp; <span class="br0">&#91;</span> &quot;$confirm&quot; != &quot;Y&quot; <span class="br0">&#93;</span>; then</li><li>&nbsp;&nbsp;&nbsp;&nbsp;exit</li><li>fi</li><li>&nbsp;</li><li># Create subdomain in plesk</li><li>echo -e &quot;Creating subdomain.&quot;</li><li>cd '/usr/local/psa/bin/'</li><li>./subdomain --create $SUBDOMAIN -domain $DOMAIN</li><li>echo -e &quot;Finished Creating Subdomain.&quot;</li><li>&nbsp;</li><li># Create SVN Repo</li><li>echo -e &quot;Creating SVN Repo.&quot;</li><li>svnadmin create $SUBDOMAINPATH/svn</li><li>chown -R apache:apache $SUBDOMAINPATH/svn</li><li>&nbsp;</li><li># Create Apache config file</li><li>cd $SUBDOMAINPATH/conf</li><li>cat &amp;gt; vhost.conf &amp;lt;&amp;lt; EOF</li><li>&amp;lt;Location /&amp;gt;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;DAV svn</li><li>&nbsp;&nbsp;&nbsp;&nbsp;SVNPath $SUBDOMAINPATH/svn</li><li>&nbsp;&nbsp;&nbsp;&nbsp;AuthType Basic</li><li>&nbsp;&nbsp;&nbsp;&nbsp;AuthName &quot;Subversion Repository&quot;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;AuthUserFile /etc/svn-auth-file</li><li>&nbsp;&nbsp;&nbsp;&nbsp;Require valid-user</li><li>&amp;lt;/Location&amp;gt;</li><li>EOF</li><li>&nbsp;</li><li># Reconfigure the host to accept new settings</li><li>/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=$DOMAIN</li><li>echo -e &quot;Finished Creating SVN Repo.&quot;</li></ol></div></pre><!--END_DEVFMTCODE--><br />
Make sure you have /root/bin in your $PATH variable and then upload the script to your <code>/root/bin</code> folder under the filename <code>svncreate</code> and then run the following:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li>chmod <span style="">755</span> /root/bin/svncreate</li></ol></div></pre><!--END_DEVFMTCODE--></p>
<h3>Running the script</h3>
<p>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:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title=""><div class="devcodeoverflow"><ol><li><span class="br0">&#91;</span>root@server ~<span class="br0">&#93;</span># svncreate svn1 mydomain.net</li><li>&nbsp;</li><li>subdomain:&nbsp;&nbsp; svn1</li><li>domain:&nbsp;&nbsp; mydomain.net</li><li>repolocation:&nbsp;&nbsp; http://svn1.mydomain.net</li><li>disk location:&nbsp;&nbsp; /var/www/vhosts/mydomain.net/subdomains/svn1/svn/</li><li>&nbsp;</li><li>Are you sure? <span class="br0">&#40;</span>y/n<span class="br0">&#41;</span> y</li><li>Creating subdomain.</li><li>SUCCESS: Creation of subdomain 'svn1' complete.</li><li>Finished Creating Subdomain.</li><li>Creating SVN Repo.</li><li>Finished Creating SVN Repo.</li><li><span class="br0">&#91;</span>root@server ~<span class="br0">&#93;</span>#</li></ol></div></pre><!--END_DEVFMTCODE--></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rudiyardley.com/2009/10/bash-scripting-svn-repository-plesk-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

