Archive for February 5th, 2008

Moving to Capistrano 2 deployment, at last

Tuesday, February 5th, 2008

Though I have moved my latest Rails apps to Rails 2.0, I've been pretty slow moving my deployment recipes from older Capistrano 1.x to the new Capistrano 2.x series.

Capistrano ultimately lacks any documentation which can even remotely be called "complete" or "easy to understand", so it definitely takes much more time that it actually should take, to understand how the bloody thing works. Sure, this of course includes annoying trips into Cap's source code and stuff like that, which is not a great time saver.. End of rant.

Anyways, you can be a lazy folk like myself lately, and just deploy your Rails 2.x compatible app using old Capistrano 1.2 recipes:

cap _1.2.0_ deploy

But the time will come and you'll want to ultimately move the stuff to the latest and greatest. And actually, it's pretty easy to do, _once_ you got the idea what needs to be done.

First, you need to capify your application, using

capify .

command in the top level of your application.

Next, you can replace contents of your old deploy.rb file with the following (or modified the following).

set :application, "your_application"

set :repository, "https://your-repository.com/svn/#{application}/trunk"
set :deploy_to, "/var/www/html/#{application}"

set :mongrel_config, "#{current_path}/config/mongrel_cluster.yml"
set :runner, "mike" #the default user is 'app'
set :deploy_via, :export
set :deploy_via, :remote_cache

set :keep_releases, "3"

role :web, "appserv1"
role :app, "appserv1"
role :db,  "dbserv1", :primary => true

namespace :deploy do

desc "Create symlinks for shared image upload directories"
task :after_update_code do
%w{gallery_items attachments image}.each do |share|
run "rm -rf #{release_path}/public/#{share}"
run "mkdir -p #{shared_path}/system/#{share}"
run "ln -nfs #{shared_path}/system/#{share} #{release_path}/public/#{share}"
end
end

desc "Restarts the pack of mongrels"
task :restart, :roles => :app do
restart_mongrel_cluster
restart_apache
end

desc "Restarts apache web server"
task :restart_apache, :roles => :app do
sudo "/usr/sbin/apachectl graceful"
end

end

desc "Start the mongrels"
task :start_mongrel_cluster, :roles => :app do
sudo "mongrel_rails cluster::start -C #{mongrel_config}"
end

desc "Stop the mongrels"
task :stop_mongrel_cluster, :roles => :app do
sudo "mongrel_rails cluster::stop -C #{mongrel_config}"
end

desc "Restart the mongrels"
task :restart_mongrel_cluster, :roles => :app do
stop_mongrel_cluster
sudo "rm -rf #{current_path}/tmp/pids/mongrel.*.pid"
start_mongrel_cluster
end

This assumes you run your apps on mongrels cluster, and that you wish to restart your apache server after every deployment (somehow, just restarting mongrels just don't work for me sometimes). Also, I use different servers for application and database. Other than that, it should work for you as is.

But of course, don't really believe me, and look thru this config before messing up with your deployment stuff :)