collusioni.st

Mongrel and Capistrano

Monday, September 11 2006

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.

<ol>
	<li>Step one: Create two mongrel cluster config files
	<ol>
		<li>Call the first one staging.cluster.yml</li>
	</ol></li>
</ol><hr />

cwd: “/path/to/staging/yourapp/current”
port: “8000”
environment: production
address: 127.0.0.1
pid_file: “log/staging.pid”
servers: 3

	<ol>
		<li>Call the second one production.cluster.yml</li>
	</ol><hr />

cwd: “/path/to/production/yourapp/current”
port: “8003”
environment: production
address: 127.0.0.1
pid_file: “log/production.pid”
servers: 3

<ol>
	<li>Step two: Modify deploy.rb</li>
</ol><p>require &#8216;mongrel_cluster/recipes&#8217;<br /> <span class="caps">ENV</span>[&#8216;DEPLOY_TO&#8217;] ||= &#8216;staging&#8217;</p>

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”

<ol>
	<li>Step three: deploy</li>
</ol><p>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 <span class="caps">ENV</span>[&#8216;DEPLOY_TO&#8217;] 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.</p>

DEPLOY_TO=production cap deploy