Painless Server Setup with Babushka
Web development is fun. At least, it should be. It involves some tasks, however—setting up a new server or VM with all the required software packages, config files, and the like—that are a little more on the tedious side.Enter Babushka. Babushka is a little piece of Ruby software written by Ben Hoskings. It lets you write simple little recipes (which Babushka calls “deps”) for installing specific software on your server. In its most basic form, each dep is made up of two parts: a way of determining if the dependency is already met, and, if not, a way of meeting it. That way, you can maintain a library of deps that allow you to quickly get your finely tuned server up and running just the way you like it, without spending two hours trying to sort through missing dependencies and build errors.For example, let’s say that as part of my setup I want to ensure that the server’s time is accurate. Here’s a quick dep for checking the system time against Apple’s time servers:
dep 'time' do server = 'time.apple.com' met? { log_shell("Checking clock against #{server}", "ntpdate -q #{server} | tail -n1") =~ /offset -?(d+)(.d+)? sec/ $1.to_i < 10 } meet { sudo 'service ntp stop' log_shell "Setting time from #{server}", "ntpdate #{server}", :sudo => true sudo 'service ntp start' }end
As you can probably guess, the met?
block is what runs to determine if the dependency is met. It should always return a Boolean value; in this case, we’re checking to see if the offset between our system’s time and the server’s time is less than ten seconds. If met?
returns false
, Babushka will run the code inside meet
. This is where you’d put all the code required to meet the dependency. Once that’s been executed, it will then run met?
again to see if it worked.You might be thinking, “Hey, that’s cool!” But wait, there’s more. Babushka will transparently handle installing software, either via your system’s package manager or from source.Let’s say I want to install memcached. This requires libevent as a dependency, so I’ll need to grab that via the package manager. Here’s a simplified way of doing all that with Babushka:
dep 'memcached.src' do requires 'libevent.managed' source 'http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz' provides 'memcached'enddep 'libevent.managed' do installs { via :apt, 'libevent-dev' via :brew, 'libevent' } provides []end
There are quite a few interesting things going on in there. For one, you’ll notice that I’m simply pointing Babushka at the memcached source, and the rest is being taken care of transparently. Babushka will download the tarball, extract it, and then run ./configure
, make
, and make install
. This is thanks to that .src
bit on the end of the dep name; this tells Babushka I want to use the source template to meet this dependency. Babushka comes with templates for installing from source, installing through system package managers, and a few others, but you can also define your own templates and use them in your deps.A bevy of options are available for you to provide to any of your deps. For example, if I wanted to provide a ./configure
argument, I could do that with a line like:
configure_args "--enable-sasl"
The other interesting bit of syntax in there is the requires
line. That makes your dep dependent on another dep. If I were to run babushka memcached.managed
from the command line, it will know that it needs to check for libevent first.The libevent dep is straightforward: the installs
declaration lets me specify alternate package names depending on the package manager in use on the system (in this case I’ve specified apt for Linux and brew for Mac OS X).Those are really simple examples; Babushka has an amazing array of little helpers to interact with the file system and the shell, and the ability to define your own templates makes it incredibly flexible. Right now the documentation is rather limited, but Ben’s own babushka-deps repository on GitHub has quite a few real-life examples (though they’re mostly aimed at deploying a Rails/Passenger/Nginx stack, they can still give you a good idea how all different helpers and options work).To install Babushka, run the following from the Linux command line:
bash -c "`wget -O - babushka.me/up`"
On Mac OS X, that’s:
bash -c "`curl babushka.me/up`"
While it can be a little work and a lot of trial and error to come up with a set of deps you’re happy with, firing up a new VM and having your perfect server setup totally configured with a few quick commands is a hugely satisfying experience.
If you want to read more from Louis, subscribe to our weekly tech geek newsletter, Tech Times.