System Provisioning with Vagrant
In my last blog post, I explained how to build a Vagrant box for PS2 development from scratch. While the steps to create such a virtual machine are quite simple and only take about 20 minutes to complete, our innate desire to automate things tells us that entering all those commands for every new Ubuntu release is probably a bad idea. Fortunately, Vagrant really shines at setting up virtual machines with everything we need – in an automated and repeatable way – through the use of provisioners.
At the point of writing, Vagrant supports five different provisioners out of the box:
- Chef Solo
- Chef Server
- Puppet Standalone
- Puppet Server
For this post, I’ve chosen the shell provisioner to introduce you to the wonderful world of system provisioning. In particular, I’ll show you how to create a VM for PS2 development (yes, again!); only that this time we’re going the lazy, fully automated way.
The Very Basic Basics
Here are a handful of things you need to know about provisioning with Vagrant:
- In the Vagrantfile, the configuration directive
config.vm.provisionis used to enable one or more provisioners for the VM.
- The command
vagrant upcreates and boots the VM, and then runs all provisioners. The latter can be skipped with
- When the VM is already running,
vagrant provisioncan be used to just run the provisioners.
- In case you screwed up your VM and want to start all over again:
vagrant destroy --force && vagrant up
(You may want to read the full documentation for all the dirty details.)
The Shell Provisioner
Of all the provisioners that come with Vagrant, the shell provisioner is the most basic and simplest one. It allows to upload and execute a shell script as the root user in the VM. So if you’re familiar with shell scripting, you already have what it takes to set up custom virtual machines with it.
Now let’s put the shell provisioner to use. First off, wrap up all commands
required to create a Vagrant box for PS2 development into a single script.
ps2dev.sh, can be seen below. Read the code and maybe the few
comments describing what’s going on:
That script will actually transform any recent Debian or Ubuntu OS into a full-fledged PS2 development environment. You may call it multiple times, but the provisioning process is stopped immediately in case the toolchain is already installed (lines 8-10).
ps2dev.sh in our hands, the only thing left is to tell Vagrant about it.
As mentioned earlier, that’s what
config.vm.provision is for:
The next time you bring up the VM, the provisioner will dutifully upload and execute our script:
Last but not least, let’s prove that the toolchain isn’t built twice, saving us a lot of time:
To be honest, this blog post was supposed to be much longer. I also wanted to tell you about Chef – the awesome systems integration framework by Opscode – and the merits of Vagrant’s Chef Solo provisioner. I, however, decided to to leave it at that and delight you with more in the future, so be sure to check back later on.
If there’s anything I want you to take away from this, it’s that provisioning with Vagrant is simple and fun – go and try it yourself!