Friday, September 2, 2011

Manage your Server Farm with CapistranO

Capistrano is an open source tool for running scripts on multiple servers; its main use is deploying web applications. It automates the process of making a new version of an application available on one or more web servers, including supporting tasks such as changing databases. Capistrano is written in the Ruby language and is distributed using the RubyGems distribution channel. It is an outgrowth of the Ruby on Rails web application framework, but has also been used to deploy web applications written using other frameworks, including ones written in PHP. The usage on the bash command line is easy to learn. When used with the Ruby on Rails Framework many default Capistrano recipes can be used, e.g. to deploy current changes to the web application or roll back to the previous deployment state.


#apt-get install ruby1.8 ruby1.8-dev rubygems1.8 libruby-extras libruby1.8-extras
(Ubuntu / Debian)

#yum install ruby1.8 ruby1.8-dev rubygems1.8 libruby-extras libruby1.8-extras 
(Centos / Redhat)

Check the Ruby Details

#ruby -v
ruby 1.8.7 

OK,Lets move to install Capistrano Boy

#gem install -y capistrano echoe

Capistrano makes a few assumptions about your servers. In order to use Capistrano, you will need to comply with these assumptions:

  • You are using SSH to access your remote machines. Telnet and FTP are not supported.
  • Your remote servers have a POSIX-compatible shell installed. The shell must be called “sh” and must reside in the default system path.
  • If you are using passwords to access your servers, they must all have the same password. Because this is not generally a good idea, the preferred way of accessing your
  • servers is with a public key. Make sure you’ve got a good passphrase on your key.

We are going to trigger some examples here. So my remote servers are and (You can Add any number here).

In the following example we are going to check the uptime of above servers.

Copy paste the following code into a text editor (Vim,Emacs).

task :health, :hosts => "" , "" do
run "uptime"

Save the file with name "capfile" without any extension.

Wakeup the code

#cap health

I found that some times while you apply this command shell returned an error "Command not found",then do the following

#vi ~/.bashrc

Copy paste the following at the end of the File

export PATH=$PATH:/var/lib/gems/1.8/bin

Then rebuild the bashrc

# source ~/.bashrc

Then Re-run the command,If you are against a password access server,it will ask for the password,enter it,other wise the result will be like following

[] executing command
[] executing command
** [out ::] 11:30:55 up 27 days, 22:40, 0 users, load average: 0.01, 0.01, 0.00
** [out ::] 11:30:55 up 37 days, 08:40, 0 users, load average: 0.05, 0.01, 0.08
command finished

We Can Do any command by the above said method,I hope you will be happy if you have a critical update on all of your 100 servers ...
Is int it ?