Being the code-monkey that I am I haven’t been exposed to very much of deployment or configuration activities and have also being quite intimidated by all the hassle that usually is associated with these activities (at least on the Windows OS).
However, lately I’ve been introduced to Puppet and Vagrant which are really powerful tools in the devops’ tools belt. Puppet is a provision manager which means it can be used for configuring a machine. You program Puppet by writing manifests which contains declarative specifications to describe the machine configuration. That is instead of instructing Puppet to “you should first install this and then if this condition is met then install this otherwise put a file in this directory. When all this is done, start this service and then …” you write like “Service X must be installed, requires Y and ensure it’s running. File Z must be present and have content C….”
Vagrant is used for managing virtual machines. It uses Oracle’s VirtualBox and either Puppet or Chef for provisioning. It makes setting up a machine and tear it down again real easy with just a few commands.
I wanted to test to set up a virtual machine and running Mosquitto which is a lightweight broker that can be installed on devices that runs on low powered devices like sensors or micro controllers.
Here’s the setup:
Virtual machine manager → Vagrant with VirtualBox
Provision manager → Puppet
Hardware OS → Windows 7
which will install both Vagrant, VirtualBox and also PuTTY which is needed on Windows to ssh into the VM.
Vagrant comes with Puppet built-in to it so no need to install that. It also comes with a pre-installed box which is an Ubuntu machine. To download a box manually just type:
vagrant box add lucid32 http://files.vagrantup.com/lucid32.box
This will get Ubuntu lucid 32 (for a list of available boxes look here).
This box will now act as a base box meaning that it will be used by projects to create specialized virtual machines.
Now it’s time to create a VagrantFile which contains the build scripts for the virtual machine:
The vagrant file initially only contains a reference to the virtual base box:
config.vm.box = "base"
A VM can now be launched with:
Inorder to login with SSH to the VM an SSH client is needed. Here I use PuTTY which comes with the Vagrant package from Chocolatey. The key, with name insecure_private_key, is located in the user’s .vagrant.d folder (e.g. C:Users<user_name>.vagrant.d. This needs to be converted to PuTTY’s own format. Just start PuTTYgen from the command line, load the above mentioned key and save with preferred name (below it is assumed the key is named key.ppk). To access the VM type:
putty localhost -P 2222 -l vagrant -i "C:Users<user_name>.vagrant.dkey.ppk"
Type the password when prompted and you’re in!
Now it’s time to setup a Puppet manifest to configure the new machine. Before doing this, take down the running VM:
(other options are vagrant suspend and vagrant halt)
in the Vagrantfile.
Puppet finds out what to install on the VM by reading a Puppet manifest. Vagrant will look for a manifest called default.pp in the manifests folder which must be located in the same folder as the Vagrantfile. What Mosquitto requires can be learned on Mosquitto’s download pages. Basically we need to install a Personal Package Archive since Mosquitto is not included in our Ubuntu version’s repositories. This in turn needs a package called package-software-properties which manages independent software vendor software sources.
Below is what the manifest looks like. The class is used for grouping Puppet resources. A resource is something that describe what the system should look like (what files should be there, what package should be installed, what services should be executed etc.). Here we use the exec and the package resources which states that something should execute and that some package should be installed. apt is a package manager used on Linux.
(This manifest could probably be written much more clean by someone who’s not a Puppet-novice like myself!)
Now that everything is in place we can kick off a VM with vagrant up. To test that the broker is running and functioning start two terminals using the PuTTY command above. In one of the terminals, start a subscriber:
mosquitto_sub -d -t my_topic
and in the other one a publisher:
mosquitto_pub -d -t my_topic - m "Message the subscribers of my_topic will get!"
The published message should now appear on the subscriber’s terminal window.
So there you have it, a bit of Vagrant and Puppet provides you with great virtualization and provisioning powers!