Home
Articles
Code

Foreword

Chef cookbooks can get pretty complicated between all the directories, nomenclature, and of course the meat: the code itself. This series will hopefully demystify some of that by walking through the creation of two cookbooks that depend on each other: one to manage the popular VirtualBox virtualization software, and another to launch virtual machines with the popular ruby-based virtualbox frontend Vagrant.

I'm not a very formal guy so I'm not really feeling the notion of determining how many parts there are to this beforehand. Simply put, if there's more to write that I haven't covered in a previous blog entry, I will create a new one. To look at it another way, this is partially a diary of the development of these two cookbooks.

If you're new to Chef or Chef cookbook writing, don't forget the OpsCode Wiki on Cookbooks. Even though I'm blogging again, I'm a firm protestor of blogs-as-documentation and I think you should be too. This series is intended to document one specific case for educational and reflective purposes, not teach you all there is to know about cookbooks.

Let's start with the basics of Chef 0.10.0, then. Part 2 will be a little more meat for those of you who already know this stuff.

Chef Anatomy

Chef is a configuration management platform both written in, and driven by ruby. It organizes it's operating parameters into two parts: data structures of various types which summarize a node's (a machine) configuration, and executable code in the form of a "cookbook". Configuration can be either in ruby or JSON, and cookbook components are -- with rare exception -- completely in ruby.

Chef the configuration management stack has 3 parts (unless you use Chef Solo, which is standalone and in many ways performs the functions of all 3 parts). Namely, chef-server which is the repository for cookbooks and configuration, knife which is used to talk to chef-server, performing various create, retrieve, update, and delete (CRUD) operations on the chef-server repository. chef-client is used to play the combination of cookbook and configuration on the desired node.

Cookbook Filesystem Anatomy

knife cookbook create can be used to create a new cookbook, but it's just as critical to understand what all those directories are for. Cookbooks are partitioned per-directory with section-specific code for each directory (role for the cookbook; not chef roles). For now we'll just introduce a few sections and discuss them at length.

A cookbook directory looks something like this:


      erikh@highland cookbooks/vagrant% ls -l
      total 0
      drwxr-xr-x  2 erikh  staff  102 Jun 19 03:50 attributes/
      drwxr-xr-x  2 erikh  staff  136 Jun 19 03:45 recipes/
      drwxr-xr-x  2 erikh  staff   68 Jun 19 03:36 templates/
      

It will frequently have more or less directories than this, the names being the critical component. Here are what the directory names mean. Note that most of the directory entries listed at the URL are not used here; save the recipes directory, they are optional.

Inside our directories, there are a few conventions. For files, attributes, and templates, there are subdirectories or specially-named files which scope the various kinds of these items. This is described pretty well on the opscode Wiki.

We will go into additional detail in part 2 of this series, which will go into the creation of a basic recipe to install VirtualBox.. TTFN!