Posted on by & filed under Information Technology, infrastructure, IT, Tech, testing.

Developing Chef cookbooks gets easier every day… once you know the rules.  Over the past 2 years, we at Safari have seen great things from the Chef community and it keeps getting better.  However, because the community is innovating so quickly, much of the How-To’s in the wild are out of date.  Articles documenting the “old way” of doing things outnumber those that describe the current best practices.  This article will outline the configuration that we use to develop cookbooks for our infrastructure at Safari.  We hope this will save you time getting up and running with local Cookbook development, Vagrant and Test Kitchen.

There are three parts to this tutorial:

Part 1 – Getting the Vagrant and tools – This post
Part 2 – Getting Test Kitchen configure – Coming soon
Part 3 – Basic Test Kitchen configuration – Coming less soon

The following procedure will get your local workstation configured and ready to develop cookbooks with Test Kitchen, Vagrant and Chef-zero.  Note that this is not a tutorial on how to write cookbooks.

There are many good resources out there on the details of Cookbook authoring. (Shameless Plug for Safari)

The Tools

Vagrant – We use Vagrant and Virtualbox to test our cookbook code and run our tests on our local workstations.  The creator of Vagrant wrote the definitive guide for us all.

Test Kitchen – Test Kitchen is the tool we use to manage our Vagrant configurations, configure our environment to mirror Production in a meaningful way and run our tests.

Chef-Zero – Chef-Zero is a lightweight local Chef Server that our testing VMs use to mimic our live Chef environment.

ChefDK – The Chef Development Kit provided by Opscode bundles various Chef tools together.  The beauty of ChefDK is that it comes with an embedded Ruby environment and the necessary Ruby Gems required by the tools.  This is a great improvement if you’ve ever had to deal with pre-ChefDK local Ruby/RVM configurations, fluctuating gem dependancies and breaking changes between tools.

Getting Vagrant, ChefDK and Tools

ChefDK Installation

First, download and install ChefDK from https://downloads.getchef.com/chef-dk.  ChefDK comes with chef-client, Berkshelf, Test Kitchen, ChefSpec, Foodcritic and more.

Vagrant Installation and Configuration

Next, install Vagrant. Head over to https://www.vagrantup.com/downloads.html and download and install the appropriate package for your system.

Once you have Vagrant installed you’ll run the following commands in your (bash) shell to install the supporting plug-ins that we need to integrate Vagrant and Chef.

After installing these, you should logout of your shell and relaunch it to pickup the new environment variables. Alternately, you can reload your ~/.bashrc file to import them into your current session:

Virtualbox Installation and Setup

Next, download and install VirtualBox from https://www.virtualbox.org/wiki/Downloads.

Follow the directions for your platform but skip the VMWare portion and go to the Test Your Setup section below if you’ve opted to use Virtualbox.

Alternately, VMWare installation and Setup

(only do this if you use VMWare rather than Virtualbox)

Download and install VMWare from the web site and have your Vagrant VMWare license handy.  You need to purchase the Vagrant VMWare integration license from HashiCorp (the company that makes Vagrant).

Copy your Vagrant VMWare license to ~/.vagrant.d/vmware-license.lic then follow the steps for your specific setup:

For VMWare Workstation, add the following to your ~/.bashrc file ( or ~/.bash_profile on OS X):

Then run:

For VMWare Fusion, add the following to your ~/.bashrc file ( or ~/.bash_profile on OS X):

Then run:

Finally, everyone using VMWare should reboot their machine and launch VMWare for the first time, accepting the license agreement and allowing it to initialize itself once. Do this before running Vagrant. FYI: Vagrant has plans to consolidate Fusion and Workstation at some point in the near future.

Prepare Chef Tools

As you work more with Chef, you will eventually run into repositories that depend on Chef recipes that require your a broad range of tools to be available and configured on your workstation.  Run through the following steps now so they are available when you need them.

Configure Knife

(do this only if you don’t have a knife configuration already)

Setup Knife and accept the defaults:

You must place your validation key in the following location before generating instance data with Knife:

Create .pem File

Create a .pem for Chef Zero to use when faking Chef server for local configuration.

Test Your New Setup

You should now create a temporary directory locally for Vagrant VMs and then test that you can create a new Vagrant project.  There are many GitHub projects with Vagrant file templates.  I recommend that you read up on Vagrant and create your own simple configuration file but if you want to test you can use the one in the next step.

Create an empty directory (~/vagrant_vm, say) for the configuration.  This location will contain a Vagrantfile and will eventually contain the running VM as well.

Preparing your Vagrantfile

Copy a Vagrant file from a GitHub repo that suits you and put it in a new file called Vagrantfile in the ~/vagrant_vm directory. (You must name the file Vagrantfile. You cannot always leave it the same name as it is in GitHub.)  You can find some generic Vagrantfile examples and the ssh key for the images here: https://github.com/patrickdlee/vagrant-examples.

Or you can use the example one below:

Using the Virtual Machine

Next, launch the VM from the ~/vagrant_vm directory using vagrant up:

There will be lots of output and once the machine has booted you can ssh to it.

Vagrant exports ~/vagrant_vm from your local your local machine to the VM.  Check that you see the the Vagrantfile and .vagrant directories in the VM by running:

Create a file there to see how it works

Exit from the VM

See the file on your workstation

Once you have successfully booted and logged into your new virtual machine we will destroy it and clean up our directories.

Then delete the temporary directory.

 

Congrats! You’ve now got Test Kitchen, Vagrant and Chef-zero configured so you’re ready to develop cookbooks.

Tags: chef, happy thanksgiving, test-kitchen, vagrant,

Comments are closed.