Mongrel and Capistrano
Posted by Michael Buffington on September 11, 2006 at 04:35 PM
Rails and Capistrano, by default, don't quite handle the concept of deploying to multiple production environments. I often find myself deploying production ready applications to a staging virtual host on the same machine the production virtual host runs on so the client can approve work before pushing it live.
I settled on a pretty clean setup today to make this kind of thing dead simple. I'm assuming you're already familiar with how to setup mongrel clusters, and that you've got the balancer in Apache or whatever forward httpd server figured out and running.
## Step one: Create two mongrel cluster config files
### Call the first one staging.cluster.yml
—-
cwd: "/path/to/staging/yourapp/current"
port: "8000"
environment: production
address: 127.0.0.1
pid_file: "log/staging.pid"
servers: 3
### Call the second one production.cluster.yml
—-
cwd: "/path/to/production/yourapp/current"
port: "8003"
environment: production
address: 127.0.0.1
pid_file: "log/production.pid"
servers: 3
## Step two: Modify deploy.rb
require 'mongrel_cluster/recipes'
ENV['DEPLOY_TO'] ||= 'staging'
set :application, "yourapp"
set :repository, "http://path/to/svn/repo"
role :web, "192.168.1.1"
role :app, "192.168.1.1"
set :deploy_to, "/path/to/#{ENV['DEPLOY_TO']}/#{application}"
set :mongrel_conf, "#{current_path}/config/#{ENV['DEPLOY_TO']}.cluster.yml"
set :svn, "/usr/local/bin/svn"
## Step three: deploy
No need to setup any custom tasks for each production environment, I simply set an environment variable and tell capistrano to deploy. Because I default ENV['DEPLOY_TO'] to staging, deploying to staging is dead simple. I made it harder to deploy to production because one would think that if you have a staging step in your deployment processes at all, chances are typing the few extra characters to indicate a production deployment are worth it.
DEPLOY_TO=production cap deploy