Freelancing on Rails
Posted by Craig Ambrose on December 12, 2006 at 03:05 AM
I’ve just released the second episode of my podcast, with ideas and discussion for freelance computer programmers (particularly ruby on rails ones). I figure two episodes on schedule means that it’s serious enough that it’s worth me blogging about it.
If you haven’t already have a listen, have a look over at http://www.craigambrose.com/podcasts for all the details. The first episode was a bit longer, but I’ve settled into a ten minute format, with a fairly snappy coverage of a particular topic each episode.
Scalable Rails Deployment: Part 3, Automation
Posted by Craig Ambrose on December 02, 2006 at 01:09 AM
This is part 3 of an ongoing series of articles on Ruby on Rails deployment. It is useful by itself, or start reading from Part 1.
Doing Things the Easy Way
In previous articles, I’ve walked through the steps involve in installing the rails stack on a new server or VPS. After learning how to do this, and realising that this was something that something that I would be needing to do a lot, it became time to automate the process. Luckily for me, I have clever friends, so I’ve latched onto the new deprec gem from Mike Bailey. Deprec, or Deployment Recipies, is a library of capistrano tasks for building a new server or VPS from scratch, assuming (for the moment), that it’s a stock Ubuntu server installation.
Words cannot convey how amazingly cool this is. I can now build myself a new VPS in ten minutes, with only a few lines of shell script executed on my local machine. Plus, since I’m using Slice Host, they provide a nice little web interface for destroying and re-creating my VPS, so I’ve been merrily destroying it and rebuilding it with much glee, just because I can.
Slicehost Specific Setup
There is one step to this setup process that is specific to slicehost. This is because slicehost isn’t quite a stock ubuntu installation. Normally, ubuntu server installs with no root account. Instead, you create a user account during setup, which is given sudo access. Slicehost has chosen instead to deploy what is otherwise a normal ubuntu server installation, except that you have a root account, and no user with sudo. This is fine, but to make it work with deprec, we have to create that non-root user ourselves.
So, ssh into your new slicehost VPS as root, and create a user as follows. If your name isn’t craig, you’ll want to pick something else of course.
useradd -m craig
passwd craig
# you will be prompted for craig's new password, type it now
visudo
# add craig to sudoers list, described below
The -m in the useradd command above ensures that the new user will be given a home directory. The visudo command opens the sudoers list in an editor (pico by default) and you can just copy the line that’s already in there, using the new username. For example, mine looks like this: “craig ALL=(ALL) ALL”.
Now, we have a user as normal, and we can build our VPS with deprec.
Installing deprec
Deprec is a gem that you install on your development machine. Once installed, the deprec_dotfiles command creates a .caprc config file in your home directory, and patches capistrano to load this config file. Future version of capistrano will include this change. This means that you can run some cap commands outside the context of a particular rails app. If you don’t want to do this, you don’t have to, but all the commands below will have to be excecuted from the root of a rails application on your development machine. The .caprc file also gives you the convenience of being able to add any capistrano helper tasks that you might find useful.
sudo gem install deprec -y # installs what you need
deprec_dotfiles # patches capistrano + creates your ~/.caprc
cap show_tasks # now you have deprec tasks included
Using deprec
From the deprec usage instructions, found Here :
cd /path/to/railsapp
deprec —apply-to . —name projectname —domain www.projectname.com
# edit config/deploy.rb to put in details of your subversion repository
cap setup_ssh_keys # copy out your public keys
cap install_rails_stack
cap setup
cap deploy_with_migrations
cap restart_apache
It really is that simple. Note that you use the "deprec —apply-to" command, instead of the usual "cap —apply-to". This actually does the same thing, but it creates a deploy.rb file in your rails app that’s got a few more configuration options in it that capistrano knows about. If you already use capistrano for your app, be sure to rename your existing deploy.rb file first or deprec will stomp on it.
The rest of the cap tasks above do all the things that I’ve described in this series of articles. You end up with a properly setup server, using your ssh keys for login, and your application deployed and running using mongrel and apache load balancing. I have my apps up and running using this code with no extra effort, and I’m very pleased with the performance.
At the moment, if you have other dependencies, such as rmagick, you’ll have to install these yourself.
Coming Soon in Deprec
I’ve been helping out a bit with the deprec source code, and I’m pretty pleased with the way it’s coming along. The next release of the gem reduces the number of commands needed in the above description, and also includes tasks to automate installing rmagic, to automate the slicehost step above, and to install postfix for handling SMTP email.
The next release of deprec (1.2) will also be fully re-runnable, so that you’ll be able to run it over the top of your existing deprec installed site as many times as you want, to keep your config up to date with the latest best practices.
Questions about deprec should be directed to Mike Bailey, over at his blog: http://codemode.blogspot.com
Oh, btw, here’s my Technorati Profile.
