Capistrano on EC2

28 November 2011 by Gaurav Gaglani No comments

This blog is about deploying a ruby on rails application on an amazon instance using capistrano.

The biggest issue that I was facing was configuring the ssh options for cap as the amazon box require’s identity file for you to ssh in. I also had one more issue that; I had not dealt in the past which was using rvm local, staging servers and the server did not have rvm but a standard installation. So, to get started with here are the things you will need.

1) Add capistrano and capistrano-ext to your to the ‘development’ group of your Gemfile. For example.

   group :development do
    gem 'capistrano', '2.8.0'
    gem 'capistrano-ext', '1.2.1' 

2) bundle install

3) Open config/deploy.rb with your favorite editor.
here is how config/deploy/development.rb looks:

    server "localhost", :app, :web, :memcached, :sphnx, :db, :primary => 

   set :user, "developer"
   set :rails_env, 'development'
   set :application, "mydiffenvsettings"
   set :repository,  "your_version_control_system"
   set :deploy_to, "/usr/local/xxx"

The ‘rails_env’ config is the only important thing.

4) I started with the intention that I will get going with a deployment locally followed by Staging and then to Prodution not remembering that the production ENV did not rvm. There are few dependencies that come to get cap working with rvm which will be indicated to you by a simple google search but still listing it below:

  $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
  require "rvm/capistrano"
  set :rvm_ruby_string, "1.8.7-p330@rails3"
  set :rvm_type, :user
  set :use_sudo, false

but the above bombed in production so, below is the hack I put and not sure if its the best way to do it.

#only include rvm dependencies if the machine has rvm.
if ENV['rvm_path'] != nil && ENV['rvm_path'] != ''
  $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
  require "rvm/capistrano"
  set :rvm_ruby_string, "1.8.7-p330@rails3"
  set :rvm_type, :user
  set :use_sudo, false

5) You will then prepare with your standard config type stuff

set :stages, %w(development staging production)
set :default_stage, "development"

require 'capistrano/ext/multistage'

set :application, "mydiffenvsettings"
set :repository,  ""
set :scm, "your_version_control_system"
set :scm_username, "my"
set :scm_password, "credentials"
set :user, "ec2user"
set :use_sudo, false
set :deploy_to, "/usr/local/xxx"
set :deploy_via, :remote_cache
set :repository_cache, "copy_of_your_checkout_instance"
set :keep_releases, 3

6) This is the biggest challenge I ran into #ssh options… Below is how I did it:

#set the ssh options
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
#Configure SSH options for ec2
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "")] 
unless 'development' == rails_env

Yes those are the ssh options that you need to get it working on an EC2 instance.
Heh I found that funny as I was expecting I will have to look into some recipe someone wrote with a bunch of options and stuff and I’ll never be to get it working in a short period of time.

7) Then comes all the standard stuff… making calls to the before and after hooks for the 3 standard cap tasks.

I would be surely interested to know if there is a better way to do what I am doing. Thanks for taking the time to read the blog hoping it helps.

Gaurav Gaglani

Follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home


Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)