Large development teams are often coping with the “It works on my machine” syndrome. One solution to solve these kind of issues is by give each single developer the same VM, which is most preferably the same as your production server. So imagine your company is building a web application. The web application is hosted on a Debian server using Apache, MySQL and PHP. So considering these preconditions I will give you a simple example to get your machines scripted en fully provisioned. In this first part of these series we will zoom in on the packer.io builders.
So let me first explain you what packer is by quoting some statements of their webpage.
Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
Packer is easy to use and automates the creation of any type of machine image. It embraces modern configuration management by encouraging you to use automated scripts to install and configure the software within your Packer-made images. Packer brings machine images into the modern age, unlocking untapped potential and opening new opportunities.
Out of the box Packer comes with support to build images for Amazon EC2, DigitalOcean, VirtualBox, and VMware. Support for more platforms is on the way, and anyone can add new platforms via plugins.
In order to create an image for a specific platform packer uses builders. Since I don’t want to zoom in on creating your own builder plugin for your own platform and I don’t want you to have more cost to get up your VM I will use the VirtualBox builder in this example. VirtualBox is free to use, so if you don’t have it already installed on your machine please first install VirtualBox to continue with this example. VirtualBox will run on following OSes: Windows, Linux and Mac, so no matter what OS you’re on, you can continue reading.
Please download the proper package for your operating system and architecture. Packer has packages for MacOS, Linux, Windows, FreeBSD and OpenBSD. Once you have installed packer we can start with creating our packer script. First create a folder on your machine called “my-first-packer-machine”. In this folder we create a file “packer-debian-x64-webserver.json”. This JSON file is used to define our packer script to build and provision our VM. Let’s start with defining our VirtualBox builder.
As you can see we use the latest stable Debian iso (“Wheezy”) to install on our VM. The checksum is required, so packer can check for its integrity as soon it is downloaded. We configure our VirtualBox image to have a 20GB SATA harddisk (Don’t worry it will be a dynamic disk and will dynamically grow so it won’t consume 20GB immediately). We configure the VM to have 1 GB of memory, 1CPU and 10MB of Video memory (minimum requirement for VirtualBox Seamless mode). The most important is the boot_command, since this is how we make our Debian installation unattended. You can grab an example Debian preseed file here. We will create the file debian64-webserver.cfg in a sub folder “http”, in our “my-first-packer-machine” folder, which packer will serve via HTTP during build. Some important options to configure are the following. Make sure you have a long ssh_wait_timeout because packer will abort the build after this timeout.
In case you want to use another OS you can use vboxmanage from your command line to figure out which guest OS types are supported.
As you can see we make sure the Debian installation is in the right timezone and has the right packages installed. I also choose to install samba so we can have a shared folder which can be accessed by our host and vim to have a decent text editor to change configuration files like php.ini etc. Feel free to add any php packages you need (php5-memcache etc.) to the “di pkgsel/include string” line.
Now we are good to test our packer.io script. To do so first open a shell, or on windows your command line, and navigate to your “my-first-packer-machine” folder. In this folder we can execute the following packer commands.
The first command will validate your JSON file, the latter will run the builds specified in your packer template file. If you have multiple builders specified and only want to execute one you can use the “-only=debian64-webserver-vbox” command line parameter to only build using the builder with specified name. As you may see the debian.iso will be cached by packer in the “packer_cache” folder, so it won’t have to download it again when you need to rerun your packer build. So please don’t throw them away since you will need them for the next part of these series. The result of this build should be a my-webserver.ova file with a size of about 700MB. By double clicking this file you can import this VirtualBox appliance and start using your VM.
Next week we will have a look at packer variables and provisioners, in the next part, to further automate our machine building.