Welcome to Machine Unix

Search Below

System Administration Tasks with Ruby + Capistrano

It’s been quite a while I have not programmed in ruby, but there comes a time you need some tools or programming skills to get your simple system administration tasks done easily and faster. I stumbled upon Capistrano a while ago, used it for one thing, and forgot about it. Now I am facing some package installation across hundres of servers and I am thinking maybe I should revisit Capistrano.

A quick background on Capistrano: A very useful gem you can install with ruby that will let you automate some of the mundane system administration tasks. Let it be package installation, sys info gathering, you name it; You can do it with Capistrano.

As I mentioned before, I have this application I need to install across servers and decided that Capistrano is the right tool. First thing you need to do is to get the gem if you already have ruby installed in your system. I have ruby 1.8.7 installed in my system

# gem install capistrano

# gem list | grep cap

capistrano (2.5.19, 2.5.2)

Capistrano will need a file called a “cap” file either where you installed the gem or in your pwd. This where most of your sys admin stuff will go with some ruby code. According to Capistrano web site, there are some assumptions it makes for Capistrano to work properly. For example, you use SSH to administer your servers not telnet or ftp. A posix compatible shell in your default path at the servers are also required. Please make sure to check what these assumptions are.

For the sake of easiness, I will install just one package, apache, across three servers. It will go and fetch the package from sunfreeware.com, unzip and install it. So use your editor of your choice and add some code to a file called “capfile” similiar to the following:

# cat capfile
set :user, “root”
role :servers,

task :get_server_info, :roles => :servers do

run “cd /root;wget -q ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/apache-2.2.16-sol10-sparc-local.gz ;cd /; gunzip apache-2.2.16-sol10-sparc-local.gz ; pkgadd -d apache-2.2*”

The way that you can run this file to do its tasks is as follows:

# cap get_server_info

I am using the name of the task to call what commands are needed to be executed. You can have multiple tasks and roles in there. For more information check out the getting started section in Capistrano website.

What this will do is that, for each server you defined at the top of the file, capistrano will login as root to these servers, fetch the package from sunfreeware.com, unzip it and install the packages for you. It is a very sweet way of doing things as long as you are careful with what you are doing. I realize that Capistrano is actually not only used for system administration tasks, but also used to “capify” your rails applications. Not sure if there is still development going on there but definitely worth to check out and play with it.