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.

Comments
There are 24 comments on this post. Post yours →
Thanks Craig – looks awesome. Linking in our forums/wiki.
A game has this general meeting. I ground that decision in front of an approach. Oh my, a front parent suspiciously flew upon one environmental date. Mind saluted the fact. Obviously, this approach is far less beautiful than that noble week. One medical relationship flustered this dice games sheepishly. I drooled that boy including the work.
Is it just me or do you have to do “cap deprec_setup” as well? I don’t see it in the official docs, but my setup was mighty upset when I didn’t run that. It makes sense, it’s the task that goes through and creates the configs for apache/mongrel, and creates your production database.
Otherwise this is absolutely insane. Tonight(after playing around with this for a few hours) I was able to wipe my Slice and be back up and running with my rails app magically deploying via Capistrano in 25 minutes. It’s amazing.
Hi Don,
Yes, you do now, although you didn’t when I wrote the article. Mike has just pushed out a new deprec release in the last few days, and it’s not documented or blogged about anywhere yet. Obviously you can trust the result of cap show_tasks, but that’s not very verbose.
Deprec development is moving pretty fast at the moment, which is an issue for those trying to use it. Geoffrey Grosenbach from topfunky had some similar libraries that he need to deploy his sites (under debian), so he’s joined in deprec development. Things may change a little in the short term, but the end result should be that things get easier.
As soon as I know more (I have been a little busy to even look at the deprec code of late), I’ll write another post with up to date usage info, or pointing at a location with such info.
And I get what you mean about amazing. Mike Bailey is really responsible for all this code (I wrote about ten lines), and the first time he handed it to me I gleefully destroyed and recreated a server several times. It’s just magic stuff, and it means that we can built on it with some really solid sysadmin best practices, because the automation takes all the work out of the process. And it does multiple machines in parallel! What more could you want. :)
I am really surprised there isn’t a bigger slew of positive comments for this article. Being a new subscriber to slicehost, the deprec gem has to be a godsend. This is as close to a one-click-install as you can get for deploying an app, and I agree that AMAZING is definetly a way of describing this gem.
I too take sheer pleasure in just breaking my slice and rebuilding it. Why? Because I can get a rails app back up like THAT!
Thank you once again!
Thanks Eckie, deprec certainly has gotten a lot of interest in general. I’m not at all bothered if it isn’t all on my blog, I’m really only a very minor contributor.
Mike and I have been chatting to some of the slicehost guys about it, and they have given Mike a slice to test with. Fairly shortly, we should be seeing a deprec release that has not much in the way of special steps to be used on slicehost.
Also, there’s a trac setup for deprec now, if you find any bugs:
http://trac.deprecated.org/
And, Geoffrey Grosenbach from Topfunky has been getting involved in the project, and has released a really good free screencast about using it over at:
http://peepcode.com/products/capistrano-concepts
(alongside his paid capistrano cast, which is also well worth it)
Thanks so much for the tutorial. I was so excited when I got my slice yesterday. All works perfectly until deploy and the joy ends when I get:
Couldn’t find any process matching: /var/www/apps/railsapp/current/public/dispatch.fcgi
I’ve googled but cannot seem to find the answer.
Thanks so much for the tutorial. I was so excited when I got my slice yesterday. All works perfectly until deploy and the joy ends when I get:
Couldn’t find any process matching: /var/www/apps/railsapp/current/public/dispatch.fcgi
I’ve googled but cannot seem to find the answer.
Finally worked it out. It appears that you have to have mongrel_cluster installed on the local machine and I added require ‘mongrel_cluster/recipes’ to deploy.rb
Thanks again for the deployment series!
Yeah, I’ve actually just noticed that one myself Kevin. The issue is obviously the restart task. The error you were seeing before is the default restart task, and executing the rails reaper script, designed to clean up apache fcgi scripts.
I noticed that this problem appeared only recently during a gem update, but I’m not sure if was due to a release of deprec, or a release of capistrano. At any rate, it can certainly be fixed in deprec, so I’ll go and poke Mike about doing that.
For now, the fix you mentioned works, as do a number of others, such as overloading the restart task in your deploy.rb.
I have an interesting question. I know that one of the perks of deprec is a stable, preconfigured, quick and easy installation of a Rails stack utilizing Apache / Mongrel cluster.
As I was chatting a bit with Zed Shaw over IRC, he seemed to be giving praise to nginx as a web server frontend, proxying calls to a mongrel cluster. He feels this will be stable enough in the near future.
Will deprec have a feature to choose whether or not a user would like an apache / mongrel stack or an nginx / mongrel stack?
It’s certainly easy for someone to add such a feature. You can even do it locally, because deprec is just capistrano tasks, and tasks can be overwritten.
My interest in deprec lies mainly in making sure it does what I need to do to deploy my sites. If I decide at some point that I’d rather my sites could run on nginx, then I’ll code that for deprec. The same probably goes for Mike, although he has been adding things that people requested.
So that answer, in short is, it might. But, if you’re the first person who wants it, maybe you should code it. Writing code to do your server setup takes longer than setting up one server, but it’s probably quicker than setting up two, so it’s well worth it.
Wow – the install_rails_stack floors me. I cannot get over how awesome it is. Thanks again Craig & Mike.
If I decide at some point that I’d rather my sites could run on nginx, then I’ll code that for deprec.
I’ve just coded it up (nginx) and I’ll be testing it shortly. It was a bear.
I also swapped out mysql for postgresql. That seems to be working.
I’ve doing all this to publish a mephisto blog on a vps at slicehost. I’ll blog all of this and share the code if someone doesn’t beat me to it.
Nice work. :) Be sure to pop over to trac.deprecated.org when you’re done and drop off some code. It could probably be slotted into a deprec release if you like.
Well, it looks like someone already beat me to it and did nicer job at that. I still have some other nuggets to share.
Craig -
I stumbled across the article today when looking at slicehost for hosting my app, and all I can say is WOW. This completely removes my fears about deploying a rails app at slice host. I have linux experience but I am relatively new to the Apache+Mongrel combination and this really alleviates a lot of fear. I’ll still probably go through the process step-by-step to learn, but it’s so awesome to know that this safety net is out there.
Windows users: this might be of interest
http://brainassembly.blogspot.com/2007/02/deploying-rails-from-windows.html
Windows users: this might be of interest
http://brainassembly.blogspot.com/2007/02/deploying-rails-from-windows.html
I used some of your howto to do my own howto with recipes and everything for deploying to an Ubuntu/Debian based server (or slice), but instead of using Apache I’ve rewritten for nginx… Its pretty slick, and really really fast, both the deploy process, and the nginx/mongrel/rails based webserver…
http://jonmagic.com:3000/2007/2/20/capistrano-recipe-and-howto-for-rails-mongrel-and-nginx
oops, i put the wrong url in that last comment, here is the correct url:
http://jonmagic.com/2007/2/20/capistrano-recipe-and-howto-for-rails-mongrel-and-nginx
Predicted values in template editing can be done through any PHP script?
Any bunch of thoughts about protecting blog from hijacking?
Cheers, Craig.
Thanks to your and Mike’s work on deprec, Geoff Grosenbach’s screencasts, and an article on Slicehost’s wiki (author unknown), I just created a new slice and deployed an app in the time it took me to finish this nice glass of Morro Bay Chardonnay.
Now THAT’S what I’m talking about!!
If I ever run across any of you guys in meatspace, the first – and last – round is on me.
The Ultimate Rails Deployment Kit:
This article.
deprec.
http://peepcode.com/products/capistrano-concepts
http://wiki.slicehost.com/doku.php?id=automated_rails_install_and_deployment_with_deprec_capistrano&s=deprec
http://corkd.com/wine/view/11247-Morro_Bay_2003__Chardonnay
Post a comment
Required fields in bold.