Top 5 uses for Capistrano

Tags
Posted
Thu 2 Oct, 2008
Comments
3

This is a technical post about Capistrano, a tool that makes deploying software to servers more manageable.

I use Capistrano to deploy changes to Helicoid’s applications to our servers. However, Capistrano also comes in handy for lots of things beyond deployment. Here’s my top 5 Capistrano tips beyond deployment.

1. Signup statistics

When running advertising campaigns I find it’s useful to see the impact on signup statistics. I used Capistrano to connect to servers and query our databases to gather registration data. It’s fairly easy to output tables and graphs as well.

In Analytics with Capistrano I wrote about this in more detail with some code snippets.

2. Server backups

For a small or medium-sized project, Capistrano can be used to automate off-site backups. I wrote a Capfile to backup one server to another one in another datacentre using rsync and mysql dumps. By relying on existing backup software (or the often used rsync) Capistrano is simply used to orchestrate remote and local backup-related tasks.

You can prompt for database passwords if you don’t want to store them in your Capfiles:

set(:mysql_password) do
  Capistrano::CLI.ui.ask 'Enter mysql admin password: '
end

run "mysqldump -u username -p my_database > /home/alex/Backups/my_database.sql" do |ch, stream, out|
  ch.send_data "#{mysql_password}\n" if out =~ /^Enter password:/
end

sudo "gzip /home/alex/Backups/my_database.sql" 

Using rsync to backup files to the local machine would call system rather than run:

system "rsync -a -e ssh alex@example.com:/home/alex/Backups/ #{BACKUP_TARGET}/files/" 

This came in handy when I wanted an additional backup to the service the datacentre provided internally.

3. Quickly viewing remote Rails objects

When addressing a bug report I occasionally need to see the properties of live data. I use the following little snippet to let me do this:

cap show -s command='User.find(1)'

task :show, :roles => :app do
  run <<-COMMAND
    /u/apps/tiktrac/current/script/runner -e production 'require "pp"; pp #{command}'
  COMMAND
end

4. cap shell, distributed uptime

This is an obvious one, but if you haven’t tried it before it’s worth adding to your toolbox.

Typing cap shell will give you a Capistrano shell on your remote machines. Issuing a command will issue it on each machine. Typing with role_name before the command will run it on one machine.

Seeing as the commands are executed on each machine, an innocuous issuing of uptime will let you see the state of several servers at once.

5. Upgrade more than one machine at once

Now apply the previous tip to sysadmin tasks. Consider this:

cap shell
sudo apt-get update

See where I’m going with that? More proof that Capistrano and similar tools are another step to Henry Ford’ing your most repetitive drudge work.


Mike

Oct 3

Can't you do the same stuff with "Vlad the Deployer?"

Alex

Oct 3

Of course, but Capistrano is what I use :)

There's also Puppet:

http://reductivelabs.com/projects/puppet/

Karl

Oct 3

I've been using Capistrano to run backups, log rotations, and some non-cron worthy maintenance scrips. Thanks for a few more ideas.

Security Code