26
Capistrano Capistrano Deployment Deployment By Sharma By Sharma

Capistrano Deployment By Nyros Developer

Embed Size (px)

DESCRIPTION

Capistrano Deployment By Nyros Developer

Citation preview

Page 1: Capistrano Deployment By Nyros Developer

Capistrano Capistrano Deployment Deployment

By SharmaBy Sharma

Page 2: Capistrano Deployment By Nyros Developer

Application DeploymentApplication Deployment Application deployment is one of those things Application deployment is one of those things

that becomes more and more complicated as that becomes more and more complicated as the scale of our application increases. With the scale of our application increases. With just a single box running our database and just a single box running our database and our application, it’s very easy to handle. our application, it’s very easy to handle.

But when we start putting our database on a But when we start putting our database on a different server, and then separating our web different server, and then separating our web servers from our application servers, and servers from our application servers, and eventually splitting our database into master eventually splitting our database into master and slave servers… It can get to where we and slave servers… It can get to where we almost don’t want to deploy our application almost don’t want to deploy our application any more.any more.

Page 3: Capistrano Deployment By Nyros Developer

When we need to DeployWhen we need to Deploy

Whenever we make any changes in Whenever we make any changes in any of the components/files which any of the components/files which are part of the repository (like SVN, are part of the repository (like SVN, Git) then we need to publish it, Git) then we need to publish it, otherwise changes might not be otherwise changes might not be visible. visible.

Page 4: Capistrano Deployment By Nyros Developer

What is CapistranoWhat is Capistrano

CapistranoCapistrano is an open source tool for running is an open source tool for running scripts on multiple servers and its main use is scripts on multiple servers and its main use is deploying web applications.deploying web applications.

It is a standalone utility that can also integrate It is a standalone utility that can also integrate nicely with Rails. We simply provide nicely with Rails. We simply provide Capistrano with a deployment “recipe” or Capistrano with a deployment “recipe” or “formula” that describes our various servers “formula” that describes our various servers and their roles. It is a single-command and their roles. It is a single-command deployment. It even allows us to roll a bad deployment. It even allows us to roll a bad version out of production and it revert back to version out of production and it revert back to the previous release very easily.the previous release very easily.

Page 5: Capistrano Deployment By Nyros Developer

Capistrano DeploymentCapistrano Deployment

The main functionality of the Capistrano The main functionality of the Capistrano is to Deploy the rails application which is to Deploy the rails application which we have already developed and we are we have already developed and we are using the "SVN" or “GIT” to manage the using the "SVN" or “GIT” to manage the code. code.

It will transfer all the files of our rails It will transfer all the files of our rails application which we have developed in application which we have developed in our local host to server directly by our local host to server directly by simply executing a simple command in simply executing a simple command in our command prompt.our command prompt.

Page 6: Capistrano Deployment By Nyros Developer

Capistrano DeploymentCapistrano Deployment

Capistrano is originally called Capistrano is originally called SwitchTower, the name was changed SwitchTower, the name was changed to Capistrano in March 2006 because to Capistrano in March 2006 because of some trademark conflict.of some trademark conflict.

The original author, Jamis Buck, The original author, Jamis Buck, announced in the year 2009.announced in the year 2009.

Page 7: Capistrano Deployment By Nyros Developer

Steps to deploy a rails appSteps to deploy a rails app

> gem install capistrano> gem install capistrano

Page 8: Capistrano Deployment By Nyros Developer

Capistrano DeploymentCapistrano Deployment

Now, we need to capistranize our Now, we need to capistranize our rails application using the following rails application using the following commandcommand

>capify .>capify . It will creates two files:It will creates two files:

-- config/deploy.rb-- config/deploy.rb-- capfile-- capfile

Page 9: Capistrano Deployment By Nyros Developer

How to set the deploy.rb fileHow to set the deploy.rb file

require 'rubygems'require 'rubygems'

require 'activesupport'require 'activesupport'

set :application, “<application_name>"set :application, “<application_name>"

set :scm_username, “<username>“set :scm_username, “<username>“

set :use_sudo, falseset :use_sudo, false

set :repository, set :repository, "http://#{scm_username}@www.example."http://#{scm_username}@www.example.com/svn/trunk"com/svn/trunk"

Page 10: Capistrano Deployment By Nyros Developer

deploy.rb filedeploy.rb file

:scm_username is your server user :scm_username is your server user name.name.

:application is an arbitrary name you :application is an arbitrary name you create to identify your application on create to identify your application on the serverthe server

:use_sudo specifies to Capistrano that it :use_sudo specifies to Capistrano that it does not need to append 'sudo' before does not need to append 'sudo' before all the commands it will run.all the commands it will run.

:repository identifies where your :repository identifies where your Subversion repository is located.Subversion repository is located.

Page 11: Capistrano Deployment By Nyros Developer

deploy.rb filedeploy.rb file

If we aren't deploying to If we aren't deploying to /u/apps/#{application} on the target /u/apps/#{application} on the target servers (which is the default path), we servers (which is the default path), we need to specify the actual location by need to specify the actual location by using deploy_to variable like this…using deploy_to variable like this…

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

set :deploy_via, :checkoutset :deploy_via, :checkout

Page 12: Capistrano Deployment By Nyros Developer

deploy.rb filedeploy.rb file

If we aren't using Subversion(SVN) to If we aren't using Subversion(SVN) to manage our source code, specify the manage our source code, specify the SCM by using the “scm” variable…SCM by using the “scm” variable…

set :scm, :gitset :scm, :git

set :user, "root"set :user, "root"role :app, "divvyhub.com"role :app, "divvyhub.com"role :web, "divvyhub.com"role :web, "divvyhub.com"role :db, "divvyhub.com", :primary => truerole :db, "divvyhub.com", :primary => true

Page 13: Capistrano Deployment By Nyros Developer

deploy.rb filedeploy.rb file These are the default roles created by the These are the default roles created by the

capistrano, but we can add others if we capistrano, but we can add others if we want. The default roles are:want. The default roles are:

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

Since most rails users will have the same Since most rails users will have the same domain name for their web, app, and domain name for their web, app, and database, we can simply use our domain database, we can simply use our domain variable we set earlier. variable we set earlier.

Page 14: Capistrano Deployment By Nyros Developer

deploy.rb filedeploy.rb file

namespace :migrations donamespace :migrations do desc "Run the Migrations"desc "Run the Migrations" task :up, :roles => :app dotask :up, :roles => :app do

run "cd #{current_path}; rake run "cd #{current_path}; rake db:auto:migrate;"db:auto:migrate;"

endend task :down, :roles => :app dotask :down, :roles => :app do

run "cd #{current_path}; rake db:drop; rake run "cd #{current_path}; rake db:drop; rake db:create"db:create"

endendendend

Page 15: Capistrano Deployment By Nyros Developer

Capistrano DeploymentCapistrano Deployment

After completion of our settings in After completion of our settings in the deploy.rb file, we need to commit the deploy.rb file, we need to commit the application by using “svn the application by using “svn commit” command if we use SVN.commit” command if we use SVN.

Then we need to run the following Then we need to run the following command:command:

> cap deploy:setup> cap deploy:setupIt is used to create the directory It is used to create the directory structure in server.structure in server.

Page 16: Capistrano Deployment By Nyros Developer

Capistrano DeploymentCapistrano Deployment > cap deploy:check> cap deploy:check

It checks all the dependencies/things like It checks all the dependencies/things like directory permissions and necessary utilities to directory permissions and necessary utilities to deploy the application by using Capistrano.deploy the application by using Capistrano.

If everything is successful, you should see a If everything is successful, you should see a message like… message like…

YouYou appear to have all necessary appear to have all necessary dependencies installeddependencies installed

Page 17: Capistrano Deployment By Nyros Developer

Capistrano DeploymentCapistrano Deployment

And finally deploy the application by And finally deploy the application by using the following command:using the following command: > cap deploy> cap deploy

…………………………..

…………………………....

Command finished successfully…..Command finished successfully…..

Page 18: Capistrano Deployment By Nyros Developer

Some Important commandsSome Important commands

To Install the gemTo Install the gem

> gem install capistrano> gem install capistrano Add our application to CapistranoAdd our application to Capistrano

> capify . > capify . Execute the setup tasks Execute the setup tasks

> cap deploy:setup > cap deploy:setup

Page 19: Capistrano Deployment By Nyros Developer

Capistrano CommandsCapistrano Commands

> cap deploy:setup> cap deploy:setup

it will create the directory structure in it will create the directory structure in server like this…server like this…

Page 20: Capistrano Deployment By Nyros Developer

Capistrano CommandsCapistrano Commands

Deploy our applicationDeploy our application

> cap deploy > cap deploy Runs the migrate task on the new Runs the migrate task on the new

release before updating the symlinkrelease before updating the symlink

> rake > rake remote:deploy_with_migrations remote:deploy_with_migrations

Rollback a release from production Rollback a release from production

> rake rollback > rake rollback

Page 21: Capistrano Deployment By Nyros Developer

Capistrano CommandsCapistrano Commands

Clean up the releases directory, Clean up the releases directory, leaving the five most recent releasesleaving the five most recent releases

> cap cleanup > cap cleanup

Page 22: Capistrano Deployment By Nyros Developer

Capistrano CommandsCapistrano Commands

Prints the difference between what Prints the difference between what was last deployed, and what is was last deployed, and what is currently in your repositorycurrently in your repository

> cap diff_from_last_deploy > cap diff_from_last_deploy

Page 23: Capistrano Deployment By Nyros Developer

AdvantagesAdvantages

It can execute commands in parallel It can execute commands in parallel on multiple servers. on multiple servers.

It can perform migrations during It can perform migrations during deployment. It can restart deployment. It can restart mongrels/fcgi. mongrels/fcgi.

It uses repository which allows us to It uses repository which allows us to easy upgradation of source code. easy upgradation of source code.

Page 24: Capistrano Deployment By Nyros Developer

Disadvantages & Disadvantages & AlternativesAlternatives

lack of scalabilitylack of scalability: when you're : when you're dealing with hundreds of servers, it dealing with hundreds of servers, it starts showing its limits, unless it starts showing its limits, unless it makes heavy use of threading or multi-makes heavy use of threading or multi-processing.processing.

Altarnatives :Altarnatives : inploy ---- gem install inployinploy ---- gem install inploy vladvlad ----- gem install vlad----- gem install vlad phdphd ---------- Passenger-based Heroku-like Passenger-based Heroku-like

Deployment Deployment

Page 25: Capistrano Deployment By Nyros Developer

ReferencesReferences

http://www.capify.org

http://ruby-toolbox.com/categories/deployment_automation.html

Page 26: Capistrano Deployment By Nyros Developer

ThankThank YouYou