Cap Deployment

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Capistrano Deployment

By Sharma
Application Deployment
 Application deployment is one of those things
that becomes more and more complicated as the
scale of our application increases. With just a
single box running our database and our
application, it’s very easy to handle.
 But when we start putting our database on a
different server, and then separating our web
servers from our application servers, and
eventually splitting our database into master and
slave servers… It can get to where we almost
don’t want to deploy our application any more.
When we need to Deploy
 Whenever we make any changes in any of
the components/files which are part of the
repository (like SVN, Git) then we need to
publish it, otherwise changes might not be
visible.
What is Capistrano
 Capistrano is an open source tool for running
scripts on multiple servers and its main use is
deploying web applications.
 It is a standalone utility that can also integrate
nicely with Rails. We simply provide Capistrano
with a deployment “recipe” or “formula” that
describes our various servers and their roles. It
is a single-command deployment. It even allows
us to roll a bad version out of production and it
revert back to the previous release very easily.
Capistrano Deployment
 The main functionality of the Capistrano is
to Deploy the rails application which we
have already developed and we are using
the "SVN" or “GIT” to manage the code.

It will transfer all the files of our rails


application which we have developed in
our local host to server directly by simply
executing a simple command in our
command prompt.
Capistrano Deployment
 Capistrano is originally called
SwitchTower, the name was changed to
Capistrano in March 2006 because of
some trademark conflict.
 The original author, Jamis Buck,
announced in the year 2009.
Steps to deploy a rails app
> gem install capistrano
Capistrano Deployment
 Now, we need to capistranize our rails
application using the following command
>capify .
 It will creates two files:
-- config/deploy.rb
-- capfile
How to set the deploy.rb file
require 'rubygems'
require 'activesupport'

set :application, “<application_name>"


set :scm_username, “<username>“
set :use_sudo, false
set :repository,
"http://#{scm_username}@www.example.com/
svn/trunk"
deploy.rb file
 :scm_username is your server user name.
 :application is an arbitrary name you
create to identify your application on the
server
 :use_sudo specifies to Capistrano that it
does not need to append 'sudo' before all
the commands it will run.
 :repository identifies where your
Subversion repository is located.
deploy.rb file
If we aren't deploying to
/u/apps/#{application} on the target
servers (which is the default path), we
need to specify the actual location by
using deploy_to variable like this…

set :deploy_to, "/var/www/#{application}"


set :deploy_via, :checkout
deploy.rb file
If we aren't using Subversion(SVN) to manage
our source code, specify the SCM by using the
“scm” variable…
set :scm, :git
 Set up the roles
Roles are named sets of servers that you can
target your tasks to execute against.
set :user, "root"
role :app, "divvyhub.com"
role :web, "divvyhub.com"
role :db, "divvyhub.com", :primary => true
Capistrano Deployment
 The :app role is where our ruby/rails
daemons are run from.
 :web role this is for defining which
server(s) are running your front-end web
server or load-balancing proxy server. A
few examples include Apache, Nginx.
 :db role is simply for telling capistrano
which server should be used to run Rails
migrations.
deploy.rb file
 These are the default roles created by the
capistrano, but we can add others if we want.
The default roles are:

role :app, “<domain_name>"


role :web, “<domain_name>"
role :db, “<domain_name>", :primary => true

 Since most rails users will have the same domain


name for their web, app, and database, we can
simply use our domain variable we set earlier.
deploy.rb file
namespace :migrations do
desc "Run the Migrations"
task :up, :roles => :app do
run "cd #{current_path}; rake db:auto:migrate;"
end
task :down, :roles => :app do
run "cd #{current_path}; rake db:drop; rake
db:create"
end
end
Capistrano Deployment
After completion of our settings in the
deploy.rb file, we need to commit the
application by using “svn commit”
command if we use SVN.
 Then we need to run the following
command:
> cap deploy:setup
It is used to create the directory structure
in server.
Capistrano Deployment
 > cap deploy:check

It checks all the dependencies/things like directory


permissions and necessary utilities to deploy the
application by using Capistrano.

If everything is successful, you should see a message


like…

You appear to have all necessary dependencies


installed
Capistrano Deployment
 And finally deploy the application by using
the following command:
> cap deploy
…………….
……………..
Command finished successfully…..
Some Important commands
 To Install the gem
> gem install capistrano
 Add our application to Capistrano
> capify .
 Execute the setup tasks
> cap deploy:setup
Capistrano Commands
 > cap deploy:setup
it will create the directory structure in
server like this…
Capistrano Commands
 Deploy our application
> cap deploy
 Runs the migrate task on the new release
before updating the symlink
> rake remote:deploy_with_migrations
 Rollback a release from production
> rake rollback
Capistrano Commands
 Clean up the releases directory, leaving
the five most recent releases
> cap cleanup
Capistrano Commands
 Prints the difference between what was
last deployed, and what is currently in
your repository
> cap diff_from_last_deploy
Advantages
 It can execute commands in parallel on
multiple servers.
 It can perform migrations during
deployment. It can restart mongrels/fcgi.
 It uses repository which allows us to easy
upgradation of source code.
Disadvantages & Alternatives
 lack of scalability: when you're dealing
with hundreds of servers, it starts showing
its limits, unless it makes heavy use of
threading or multi-processing.
Altarnatives :
 inploy ---- gem install inploy
 vlad ----- gem install vlad
 phd ----- Passenger-based Heroku-
like Deployment
References
 http://www.capify.org

 http://ruby-toolbox.com/categories/deploy
ment_automation.html
Thank You…..

You might also like