Thursday,Jun5,

Automatic Deployment of Rails Application by Capistrano

Previously we deployed our application manually in our Linux box. This was taking a lot of time and after every deploy we were facing some serious issues. Automatic deployment by Capistrano has made deployment life easy. Here i have shared my experience about deployment by Capistrano.

Requirements:
Install Capistrano in local machine by : gem install capistrano
Install subversion command line client in local machine
Install subversion in Linux box. I have done it by a rpm

Assumptions:

  • You are deploying to a single server, at least initially.
  • Your username is the same for both your subversion repository and your deployment machine.
  • Your subversion repository is accessible from both your local host, and the host that you will be deploying to
  • You have configured a public key to use for SSH access to your target host


Run Capistrano:

From command prompt go to your application directory then run .capify.This will create a capfile in the application root directory and deploy.rb in config folder.


Configure deploy.rb:

Now modify the deploy.rb file to deploy your application. Here is my deploy.rb

require 'mongrel_cluster/recipes‘

set :application, “your application name"
set :repository, “url of your repository"
set :scm_username, “name"
set :scm_password, “password"
set :rails_env, "production --trace"
set :deploy_to, "/var/www/rails/#{application}"

#ssh configuration
set :user, ‘user_name'
ssh_options[:port] = 22
ssh_options[:username] = ‘user_name‘
ssh_options[:host_key] = 'ssh-dss'

#set app, web, db role
role :app, "www.scrumpad.com"
role :web, "www.scrumpad.com"
role :db, "www.scrumpad.com", :primary => true

#configure for mongrel cluster
set :mongrel_servers, 3
set :mongrel_port, 6010
set :mongrel_environment, 'production'
set :mongrel_conf,"#{deploy_to}/current/config/mongrel_cluster.yml"

Now the time for deploying your application:

Go to your project directory and run cap deploy:setup Capistrano will now log into your server and try to set up a simple directory hierarchy. The basic layout is:
#{deploy_to}/
#{deploy_to}/releases
#{deploy_to}/shared
#{deploy_to}/shared/log
#{deploy_to}/shared/system
#{deploy_to}/shared/pids

Every time you deploy your application, a new directory is created under the releases directory, and then a symlink is created at “#{deploy_to}/current that points at the newr elease. This current symlink should be considered the root directory of the currently active release.

run cap -q deploy:check to check dependencies.

Cold deploy: The first time your application is not running. Things are “cold”. For this case, run cap deploy:cold

Subsequent Deploys: After the first deploy every time you want to deploy just
run cap deploy

Roll back your application to the previous release cap deploy:rollback

Problem I faced:
After deploying my application on a Linux box, I faced some permission related problem to run the application. To solve this I had to put the required file in a directory and modify my deploy.rb in the following way:


after "deploy:symlink", :copy_permitted_files
task :copy_permitted_files, :roles => :app do
run "cp -rpf /var/www/rails/Permitted/public/* /var/www/rails/# application}/current/public/"
run "cp -pf /var/www/rails/Permitted/database.yml /var/www/rails/#{application}/current/config/"
run "cp -rpf /var/www/rails/Permitted/log/* /var/www/rails/#{application}/current/log/"
end

1 comments:

Anonymous June 19, 2008 at 9:10 PM  

Thanks for the helpful post. keep it up zawoad.


Waiting for the next post on Amazon EC2. :-)

Total Pageviews

446,991

Tags

Abstract Entities ActiveMessaging Amazon SQS Asynchronous mail sending Context Menu Contiuous Integration Deploy Rails Application Deployment by Capistrano Design pattern Ext GWT Ext GWT DnD Framework Filter Grid GWT GXT Chart GXT Editable Grid GXT Example GXT Grid Grouping GXT Tree Filter GlassFish Google App Engine Grid GroupLayout JDO JPA JPQL JTable JavaMail API ListView Local Paging Mapped Superclass Open flash chart Reflection Remote pagination Session Management Strategy pattern XTemplate cruisecontrol.rb filter JTable Row flash chart gxt java reCAPTCHA
Twitter Updates
    follow me on Twitter

    Followers