Update
According to Jeff, Dreamhost has disabled setuid for home directories - which they probably should’ve done years ago.
As a side note, some other alternatives include the following:
In any case, I’ve since moved half my sites to WebFaction so if you know of a solution, post it in the comments and I’ll write a new blog post about it.
Since Dreamhost has integrated Subversion into their services, I finally decided to take the leap and move my local repository to my hosted account. After all, I was tired of always having to remember to transfer my updated files via scp. With my repository moved over to Dreamhost, I figured I would take advantage of Subversion’s post-commit hooks to automagically update my live beta site.
After reading a myriad of solutions (like here and here ), I found the Dreamhost wiki entry the most helpful. So without further adieu, here’s a small, quick and dirty tutorial on how to automagically get post-commit working with Subversion on your Dreamhost account.
The setup
The very first thing you need to do is create a directory on your Dreamhost account to checkout the source files to (I’m assuming you’ve already created your Subversion repository, have checked files into it, and are ssh’ed into your Dreamhost account.). In my situation, I created a subdomain in Dreamhost called beta.localkinegrinds.com. Dreamhost automatically creates a directory named beta.localkinegrinds.com within the root of the admin account. I cd’ed into that directory and issued the following command:
svn co PATH_TO_FILES_IN_REPOSITORY . --username USERNAME --password PASSWORD
Once checked out, I switched into my Subversion repository directory; it was named svn in the root of my admin account. From there, I went into my named repository, and then into the ‘hooks’ directory.
Here’s where all the fun begins!
First, I created a script called ‘post-commit.script’ that looked like the following:
#!/bin/sh
/usr/bin/svn up PATH_TO_DIR --username SVN_USERNAME --password PASSWORD
I exited my text editor, then issued the following command:
chmod +x post-commit.script
From reading all the forums, the main issue with the post-commit hooks is that svnserve will run the hooks as the Apache user; in Dreamhost’s case, the dhapache user. Thus, we have the following options as described here. We can:
- Run password-less sudo (which we don’t have access to on Dreamhost)
- Change the group permissions (which we don’t have access to on Dreamhost)
- Checkout the files as the dhapache user from a post-commit hook, then change it to update as described here (which I thought was a pain in the rear, not to mention having the directory be world writeable)
- Compile a small C program with its setuid bit set
Being the programmer that I am, I chose the last option. Basically, as Wikipedia describes, “when a binary executable file has been given the setuid attribute, normal users on the system can execute this file and gain the privileges of the user who owns the file (commonly root) within the created process.” This means that even though the Apache user will run the post-commit hook, it’ll assume the privileges of the user who owns the file (my account) and be able to execute the post-commit.script file we just created!
Neat!
So… taking a look at the Dreamhost wiki, I created the following c program named post-commit.c
#define PATH_TO_POST_COMMIT_SCRIPT "/PATH/TO/POST_COMMIT.SCRIPT"
#include <sys/types.h>
#include <unistd.h>
main( ac, av ) char **av;
{
execv( PATH_TO_POST_COMMIT_SCRIPT, av );
}
I then issued the following command to compile the program into a binary called post-commit:
gcc -o post-commit post-commit.c
And then to set the setuid bit on the post-commit file:
chmod 4755 post-commit
Voila!
Assuming you set all the correct paths, on a commit to your Subversion repository - your live site will be updated!
As a side note, most of the sites previously mentioned didn’t have any troubleshooting tips. Here’s a few that I ran into along the way:
- Always test the scripts out first, ie, execute them from the command line and make sure they are doing what you want them to.
- Apparently, according to the Subversion book, “Subversion repository executes hook scripts with an empty environment.” This is something good to know.
- If everything was done right, and it’s still not working, try to update your checked out Subversion directory. Sometimes you need to clean up before any other updates can occur. That problem took me a whole half-an-hour to figure out.
In any case, if done correctly, you’ll be on your way to a better development environment!