March 14, 2012
This article was contributed by Koen Vervloesem
If you want to get up and running quickly with virtual machines, Vagrant could come in handy. After two years of development, the project has announced Vagrant 1.0, which is the first stable release and the first release for which the developers promise backward compatibility.
Vagrant starts from the idea that many developers do their development
and/or testing in virtual machines to work with different distributions,
avoid reboots or polluting their main
workstation operating system with conflicting dependencies or simply bad
packages. But having a couple of virtual machines installed requires
managing them. And each time you have to install a fresh developer VM, you
have to spend some time installing and configuring it. That's where Vagrant
comes in: it's a tool that can automatically set up pre-configured virtual
machine instances for developing and testing purposes, based on one of many
VM templates. According to the 1.0 release announcement, Vagrant is in use
by Mozilla, LivingSocial, EventBrite, Yammer, Disqus, and many more
organizations.
For the moment, Vagrant is focused on the creation of virtual machines
for Oracle's VirtualBox, so you
need VirtualBox installed (version 4.0 or higher). The Vagrant web site
offers rpm, deb and Arch Linux packages of version 1.0 for 32
and 64-bit x86 Linux, as well as packages for Mac OS X and Windows. Alternatively, you can also install Vagrant with Ruby's package manager RubyGems (gem install vagrant), as Vagrant is written in Ruby.
Getting started
The project has published excellent and up-to-date documentation on its web site, as well as a "Getting Started" guide. Vagrant is controlled through subcommands of the vagrant command and the configuration is done per project (preferably with each project in a separate directory) in a Vagrantfile, which has a similar goal as a Makefile for a development project. A Vagrantfile is actually a file containing Ruby code, which configures the project's virtual machine. Vagrant is able to create an initial Vagrantfile with the vagrant init command, which results in a Vagrantfile that documents the most common configuration options in long comments.
Another important concept is that of "base boxes." Instead of creating a
virtual machine instance from scratch, Vagrant bases its instances on
templates, which are called base boxes. A base box is basically a tar ball
containing a root file system and a VM configuration with things like RAM
and disk size. With a "vagrant box add" command you can
download a base box from an HTTP URI or a local filesystem and copy it to
your local Vagrant installation. After that, you can use this base box as a
template for any of your projects by specifying its name in the
Vagrantfile. The Vagrant web site contains a 32-bit, 259 MB Ubuntu
Lucid Lynx base box, as well as a 64-bit variant.
When running vagrant up for the first time in a project,
Vagrant creates a virtual machine based on the base box and starts a
headless instance using VirtualBox. Now you can do some work with the
virtual machine. You can suspend and resume it
("vagrant suspend" and "vagrant resume"), or you
can completely halt it with "vagrant halt", which shuts down
the VM. If the virtual machine has been shut down, a
"vagrant up" doesn't re-create the machine but reboots it
instead. Another option is to completely delete the virtual machine with
"vagrant destroy", which of course deletes the whole VM image
and thus the files included in it. After a virtual machine is deleted, a
"vagrant up" command will re-create it based on
the configuration in the Vagrantfile.
An advantage of Vagrant is that these virtual machines are easily
shareable, for instance with co-workers: you can package a virtual machine
with the "vagrant package" command. Beyond that, you can create your own base
boxes for your favorite Linux distribution. That way you can package a complete development environment in a Vagrant box and distribute it to others who can use this reproducible environment with a single command.
Configuration management
If the virtual machines you could create with Vagrant were limited to copies of the base boxes, this wouldn't be so useful, as you would have to create a base box for every configuration you need. Thankfully, Vagrant allows you to provision your virtual machines using the configuration management systems Puppet or Chef. This allows you to use a base box with the very basic functionality that all your virtual machines need, and then add extra packages and configuration changes using a Puppet manifest or a Chef cookbook that you refer to in the Vagrantfile.
Provisioning is done when you enter "vagrant up" or
"vagrant reload" (which reloads the VM's complete
configuration in the Vagrantfile), but you can also use
vagrant provision to reload only the Puppet or Chef
configuration after you have changed it. Vagrant can provision your virtual
machines even if you don't want to run a Puppet or Chef server: it calls
these modes Chef Solo
provisioning and Puppet
provisioning. The only thing you have to do is add the location of your
manifests or cookbooks to the Vagrantfile. Of course Vagrant is also able
to provision your virtual machines using an existing Puppet or Chef
server.
Talking to the virtual machine
But Vagrant isn't just about creating virtual machines based on templates and provisioning them. Its most powerful idea is that it sets up some channels to communicate with your virtual machines. For instance, it provides SSH access: with the command vagrant ssh, it logs you into the virtual machine so you'll be able to enter commands. X11 forwarding isn't enabled by default, but this can be configured in the Vagrantfile, which could come in handy if you have X installed in the virtual machine and you want to run graphical programs using ssh -X.
Moreover, Vagrant automatically configures your project's directory as a VirtualBox shared folder and mounts it in the virtual machine on /vagrant. The virtual machine has both read and write access to this directory, so you can easily use this to exchange files between your host system and the virtual machine. If the performance of the VirtualBox shared folder is not enough (which is typically the case when you have thousands of files), you can also set up NFS shared folders.
Vagrant also allows you to configure port forwarding in the
Vagrantfile. For example, you could fire up a virtual machine with a test
web server, forward its port 80 to a port on your host system, and then
easily access the web server using a localhost URI, so you don't have to
remember the virtual machine's IP address. It's also possible to create a
multi-VM environment
with multiple virtual machines (for instance a web and a database server)
communicating with each other.
Development
Vagrant is open source, as it uses the MIT
License. The code is on
GitHub, and its README file offers some help on how to contribute to
Vagrant. There's the #vagrant IRC channel on Freenode and the mailing list for
questions; the project also has an issue tracker for reporting bugs.
Vagrant was started in January 2010 by Mitchell Hashimoto and John
Bender. The first release was version 0.1.0 on March 7, 2010, and exactly
two years later it saw a 1.0 release. Vagrant development is not backed by
any single company, but it's sponsored by Engine Yard and
Kiip and has attracted contributions from over a hundred individuals during
those two years. One of these outside contributions is the Veewee tool, created by
Patrick Debois to make building your own base boxes easier:
Veewee tries to automate this and to share the knowledge and sources you need to create a basebox. Instead of creating custom ISOs from your favorite distribution, it leverages the 'keyboardputscancode' command of Virtualbox to send the actual 'boot prompt' keysequence to boot an existing iso.
Veewee comes with a lot of templates
for various Linux distributions, including CentOS, Debian, Fedora, Arch
Linux, Gentoo, openSUSE, Ubuntu, and so on, as well as FreeBSD, OpenBSD,
OpenIndiana, and even Windows. The best thing about these templates is that
you can see how they are made, so you can adapt them to your needs.
Other contributors have created plugins for Vagrant. A simple:
gem list -r | grep vagrant
command reveals more than a dozen RubyGems for Vagrant plugins. For
example, Igor Sobreira has
created
the
vagrant-screenshot
plugin to take a screenshot from a running virtual machine to help debug booting issues. And Tyler Croy has
integrated Vagrant with the continuous integration tool Jenkins.
The Vagrant project welcomes any contribution: code, documentation,
as well as financial
aid. There's a rather detailed explanation about how companies can
support the project financially, by donating, sponsoring,
and paying
for specific feature implementations or bug fixes. The project is also
very open about its current and
future costs.
The future
While previous Vagrant releases regularly changed the syntax of the Vagrantfile, which could lead to some frustrations if you were an early adopter, the 1.0 release marks the end of this time of experimenting, according to the release announcement:
Equally important is that Vagrant 1.0 is the first release where backwards compatibility for the Vagrantfile will be maintained for the far future. Backwards incompatible changes to the Vagrantfile will no longer happen (exactly how this will be achieved will be revealed in the future, as I've devised a way to do so without compromising innovation).
Currently Vagrant only supports VirtualBox, but the plan is to support
additional hypervisors, such as KVM, VMWare Fusion, VMWare vSphere, and so
on. If you need extra functionality, you can add it using Vagrant's
plugin system. All in all, the basic idea of
distributable boxes coupled to the extensibility thanks to plugins makes
Vagrant a handy tool for development and testing. Add to this the excellent
documentation and the ecosystem of Veewee templates, and Vagrant may well
be able to save you a lot of time.
(
Log in to post comments)