One click automated Vagrant Base Box builds with VeeWee and Jenkins

Code
Howard Tyson
Howard Tyson

At Zivtech we use Vagrant for all of our development. It allows us to effortlessly spin up new development environments on many different systems and ensures that we have the exact same software available in development that we will in production.

VirtualBox is updated all the freaking time and Vagrant requires the VirtualBox guest image to have VirtualBox guest additions installed and at the same version as the parent. If your guest additions are not up to date, there are annoying error messages and you can get strange side effects. Enter VeeWee.

VeeWee

VeeWee is a tool that automates the process of building a vagrant base box (a virtual machine image onto which your stack/app is deployed) by downloading an image of the linux distro desired and then scripting the build process by controlling the virtual machine's keyboard and by making all of the necessary selections to do a clean install, download the newest guest additions, and then install all of the general Vagrant guest dependencies (ssh server, nfs, smb, ruby, puppet, chef, etc.). After a while, though, even doing this every week or two can get tedious. Enter Jenkins.

Jenkins

Jenkins allows you to automate repetitive tasks. It was originally created to watch a project perform software builds and automated test runs so that developers didn't have to go through these tedious and repetitive tasks. It is flexible enough, though, in that it can automate running just about any scriptable task and can report back to you on the results. Perfect for rebuilding VirtualBox instances.

Setup 

Fair warning, to set this up yourself you may have to sort out a few permissions issues for yourself. Also, you'll need to install VeeWee, Bundler, Vagrant, and Jenkins yourself because the setup of these tools is outside the scope of this article. Personally, I have a Jenkins instance running under my own user on OS X. What I describe here is how I have set this up with Jenkins running as my user and you may need to adapt it depending on your permissions, optional use of rvm, etc. I switched the ownership of `/Users/Shared/Jenkins/Home/jobs/`**[YOUR JOB NAME]**`/workspace/` to be user and group owned by my user. Stackoverflow has instructions on running jenkins as your own user.

To set this up yourself, create the Jenkins job as a "free style" software project and add an "Execute shell" build step. This will create the working directory where we will setup our project for building. After creating this job you can setup the required code in the working directory of the job using the instructions below.

cd ~
gem install bundler
git clone git://github.com/jedi4ever/veewee.git
cd veewee
git checkout v0.3.1
cd /Users/Shared/Jenkins/Home/jobs/[YOUR JOB NAME]/workspace/
mv ~/veewee/* .
mkdir boxes

Now comes the actual work of the Jenkins job. What we want to do is dynamically determine the version, build the box for that version, validate it by running VeeWee's Vagrant compatibility tests, power it off, export it, delete it from VirtualBox (because we'll just be importing it again via Vagrant builds) and then import that box into Vagrant for local use. Place shell snippet below in the `Execute shell` build step in your Jenkins job. This example only builds an Ubuntu 12.04 32 bit box, but it wouldn't be hard to make edit the job (or make it parameterized) for other flavors. You can see the `templates` folder in the working directory for a list of currently supported distros.

VERSION=`VBoxManage -v | sed 's/\(.*\)r\([0-9]*\)/\1/'`
bundle install
bundle exec veewee vbox define "precise-vbox-$VERSION" "ubuntu-12.04.1-server-i386"
bundle exec veewee vbox build "precise-vbox-$VERSION"
bundle exec veewee vbox validate "precise-vbox-$VERSION"
VBoxManage controlvm "precise-vbox-$VERSION" poweroff
vagrant package --base 'precise-vbox-4.2.4' --output 'boxes/precise-vbox-4.2.4.box'
vagrant box add "precise-vbox-$VERSION" "boxes/precise-vbox-$VERSION.box"
VBoxManage unregistervm precise-vbox-$VERSION --delete

It's worth noting that the build will fail if you try to run it a second time with the same VirtualBox release because you will already have a box created by this name. So go ahead VirtualBox, do your releases. We're ready.

Ready to get started?

Tell us about your project