SVN Authentication and Auto Update: Media Temple, Linux

SVN Authentication and Auto Update

Tags: Media Temple, Linux | Written on 24/11/07

Media Temple Dedicated-Virtual dv hosting

During the first two parts of my series on setting up a Media Temple dedicated server, I setup subversion (SVN) on apache. Now I want to show you how I setup basic authentication and a SVN hook to automatically deploy files to the server (called continuous integration). This is great because you can check in your files to SVN and see them on the server immediately.

Adding Basic Apache Authentication to your Subversion (SVN) Repository

First lets give apache access to the SVN database (the $ symbol just means it is a shell command).

Bash:
  1. chmod g+s /svnroot/db

Now create a password file with htpasswd, name the file whatever you would like. I chose to make my file hidden by adding the period before the name.

Bash:
  1. htpasswd -cm /etc/.htaccess yourusername

It will prompt you for your password. Now go back to your httpd.conf and than add the last four lines below to your Location code block  - we are adding to what we did in part 2 of the media temple setup series.

Apache:
  1. <Location /svn>
  2.   DAV svn
  3.   SVNParentPath /usr/local/svn
  4.   AuthType Basic
  5.   AuthName "Subversion repository"
  6.   AuthUserFile /etc/.htaccess
  7.   require valid-user
  8. </Location>

For security reasons, we also need to make sure and hide the .svn folders from having apache publicly display them:

Apache:
  1. # Disallow browsing of Subversion working copy administrative dirs.
  2. <DirectoryMatch "^/.*/\.svn/">
  3.     Order deny,allow
  4.     Deny from all
  5. </DirectoryMatch>

Restart apache and when you try to access SVN you should be prompted for authentication. Great, SVN is now secure! You can also add more security by adding SSL to your dedicated server.

Creating a Subversion (SVN) Hook to Auto Update Your Server

I want to automatically deploy my files to my httpdocs without having to manually go in and update the files. So to accomplish this you need to create a SVN hook that runs after every commit.

First, copy the post-commit template from your svnroot/hooks folder. And lets also use chmod to give the file rights to execute.

Bash:
  1. cd /svnroot/hooks/
  2. cp post-commit.tmpl post-commit
  3. chmod +x post-commit

Now we need to create a little C program. You should have GCC installed. This will compile our little C program. Create a new svnupdate.c file and put this code in. Don't forget to change the directory path: "public_html_directory"

C:
  1. #include <stddef.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. int main(void)
  5. {
  6.   execl("/usr/bin/svn", "svn", "update", "/public_html_direcotry/",
  7.         (const char *) NULL);
  8.   return(EXIT_FAILURE);
  9. }

Complile the file with this command. It will take the whatever.c and compile it into a binary file.

Bash:
  1. gcc -o svnupdate svnupdate.c

Try executing the binary to see if it works with the env command.

Bash:
  1. env - ./svnupdate

Alright now copy the whatever binary into your hooks folder. And we'll also run a couple commands to give it proper priveledges:

Bash:
  1. cp whatever /svnroot/hooks
  2. cd /svnroot/hooks
  3. chown root:root svnupdate
  4. chmod +s svnupdate

Then lets tell the post commit hook to run our binary file name, "svnupdate".

Bash:
  1. vi /svnroot/hooks/post-commit

Inside it the post-commit hook tell it to run the binary using the absolute path:

C:
  1. /svnroot/hooks/svnupdate

Test it out by checking in a file to subversion. It should automatically update at the directory we set in the C program.

Comments

#1. Kolky on 4/3/08
Thanks for the hook script, been searching for this for ages!
#2. Dallas Clark on 8/5/08
I'm receiving the error "the directory is not a working copy" which leads me to these questions. Does the "public_html_directory" need to be the full file path? Does the location of where you want your SVN to be copied to need to be a checked out svn directory?
#3. Dallas Clark on 8/5/08
haha! why my message submitted 3 times, I don't know I realise yes it has to be a checkout directory of the SVN, but it's still not working, possibly permission errors but I'm not sure. When someone commits to the SVN and it executes the post-commit file, does it execute it as apache or the user?
#4. Marc on 9/5/08
I'm not sure what user it executes as.
#5. Toti on 11/5/08
Subversion executes hooks as the same user who owns the process which is accessing the Subversion repository. In most cases, the repository is being accessed via a Subversion server, so this user is the same user as which that server runs on the system.
#6. Toti on 11/5/08
The hooks themselves will need to be configured with OS-level permissions that allow that user to execute them. Also, this means that any file or programs (including the Subversion repository itself) accessed directly or indirectly by the hook will be accessed as the same user. In other words, be alert to potential permission-related problems that could prevent the hook from performing the tasks it is designed to perform.
#7. Toti on 11/5/08
Marc thanks for the article although that's a bit all over the place and it's not very clear. For instance when you say "Create a new whatever.c file and put this code in. Don't forget to change the directory path." ...change directory path of what? There is no mention of where to create the file in the 1st place :P
#8. Toti on 11/5/08
...And here I am lost: Compile the file with this command. It will take the whatever.c and compile it into a binary file. gcc -o svnupdate svnupdate.c (Wait wasn't I compiling whatever.c? Why the command reads svnupdate.c? Should I rename whatever.c to svnupdate.c??)
#9. Toti on 11/5/08
Try executing the binary to see if it works with the env command. $ env - ./updatesvn (After compiling with gcc the resulting compiled binary should be called "svnupdate" why should I try to use "updatesvn" with the env command?) Alright now copy the whatever binary into your hooks folder. And we'll also run a couple commands to give it proper priveledges: I am really lost... could anyone summarize a step by step process for dummies? Thanks!
#10. Toti on 12/5/08
By the way what's the syntax to place in the post-commit to make it run the compiled binary?
#11. Marc Grabanski on 19/5/08
@Toti: /public_html_direcotry/ is the directory path in the C code. whatever.c was meant to mean name it whatever you want. svnupdate.c was what I named it. Note this article was logged for my own memory and wasn't meant to be the end all article. It is a compilation of notes on the subject into a format that is hopefully helpful.
#12. Darko Ljubic on 30/5/08
Script never managed to update a working copy due to permissions issue. It kept trying to do svn update as apache user. After a little tweaking of C program, this is the solution I came up with: int main(void) { execl("/usr/bin/svn", "svn", "update", "/public_html_direcotry/", "--username", "autoupdater", "--password", "autoupdater_password", (const char *) NULL); return(EXIT_FAILURE); } Notice, I only added username and password of a svn user to the original script .
#13. Philip on 11/6/08
Will this method work across servers? I have a standalone svn server and I want to automatically update.
#14. zyk on 5/8/08
What if the destination directory to updat is on another computer , will changing the parameter of the function "/public_html_direcotry/" to be "root@192.168.200.55:password" work ? if not , what is the right to do ?
thx
#15. Marc Grabanski on 5/8/08
I'm not sure, zyk. All you can do is try and see if it works.
#16. Andrej Pintar on 3/11/08
Hello.

I lost a few days on configurng SVN hooks for a test repository.

Scheme:

1. SVN repo (Server1-Centos)-Mounted Share

2. Develop server (Server2,Apache2,PHP)-Windows2000,Server, Shared Folder

So I mounted Development Site inside centos using cifs mount. Works great. And SVN update was
messing me around because of these permissions. When Loaded in terminal works fine. I got Log and Echo and everything set up fine. But when updated using SVN clients(RapidSVN,Tortoise on Windows) hooks did not execute. However they did execute only ECHO commands.



echo "SVN START:" > /tmp/svn.log #Works

#svn update /mnt/a/temp/a --non-interactive --username xxxx --password xxxx >> /tmp/svn.log#Doesnt work

/srv/uniline/repositories/its/hooks/svnupdate >> /tmp/svn.log #Works

echo "SVN END" >> /tmp/svn.log #Works

/mnt/a/temp/a is just a folder i tested to update (not whole site until i see that autoupdate is ok).

sou you need to mount share as:

# mount -t cifs //web1/dev08$/ /mnt/site -o username=Administrator,password=xxxxxxx,rw



Thanks for the C code for making autoupdate work that way.



Leave a Comment