Managing cookbook dependencies with Berkshelf
It's a pain to manually ensure that you have installed all the cookbooks that another cookbook depends on. You have to download each and every one of them manually only to find out that with each downloaded cookbook, you inherit another set of dependent cookbooks.
And even if you use knife cookbook site install
, which installs all the dependencies locally for you, your cookbook directory and your repository get cluttered with all those cookbooks. Usually, you don't really care about all those cookbooks and don't want to see or manage them.
This is where Berkshelf comes into play. It works like Bundler for Ruby gems, managing cookbook dependencies for you. Berkshelf downloads all the dependencies you defined recursively and helps you to upload all cookbooks to your Chef server.
Instead of polluting your Chef repository, it stores all the cookbooks in a central location. You just commit your Berkshelf dependency file (called Berksfile
) to your repository, and every colleague or build server can download and install all those dependent cookbooks based on it.
Let's see how to use Berkshelf to manage the dependencies of your cookbook.
Make sure you have a cookbook named my_cookbook
and the run_list
of your node includes my_cookbook
, as described in the Creating and using cookbooks recipe.
Berkshelf helps you to keep those utility cookbooks out of your Chef repository. This makes it much easier to maintain the cookbooks, which really matter.
Let's see how to write a cookbook by running a bunch of utility recipes and manage the required cookbooks with Berkshelf:
- Edit your cookbook's metadata:
- Edit your cookbook's default recipe:
- Run Berkshelf to install all the required cookbooks:
- Upload all the cookbooks on the Chef server:
Berkshelf comes with the Chef DK.
We create our cookbook and tell it to use a few basic cookbooks.
Instead of making us manually install all the cookbooks using knife cookbook site install
, chef generate
creates a Berksfile, besides the metadata.rb
file.
The Berksfile is pretty simple. It tells Berkshelf to use the Chef supermarket as the default source for all cookbooks:
It tells Berkshelf to read the metadata.rb
file to find all the required cookbooks. This is the simplest way when working inside a single cookbook. Please see the following There's more… section to find an example of a more advanced usage of the Berksfile.
After telling Berkshelf where to find all the required cookbook names, we use it to install all those cookbooks:
Berkshelf stores cookbooks in ~/.berkshelf/cookbooks
, by default. This keeps your Chef repository clutter-free. Instead of having to manage all the required cookbooks inside your own Chef repository, Berkshelf takes care of them. You simply need to check in Berksfile
with your recipe, and everyone using your recipe can download all the required cookbooks by using Berkshelf.
To make sure that there's no mix-up with different cookbook versions when sharing your cookbook, Berkshelf creates a file called Berksfile.lock
alongside Berksfile
. Here, you'll find the exact versions of all the cookbooks that Berkshelf installed:
Berkshelf will only use the exact versions specified in the Berksfile.lock file, if it finds this file.
Finally, we use Berkshelf to upload all the required cookbooks on the Chef server:
Berkshelf integrates tightly with Vagrant via the vagrant-berkshelf
plugin. You can set up Berkshelf and Vagrant in such a way that Berkshelf installs and uploads all the required cookbooks on your Chef server whenever you execute vagrant up
or vagrant provision
. You'll save all the work of running berks install
and berks upload
manually before creating your node with Vagrant.
Let's see how you can integrate Berkshelf and Vagrant.
First, you need to install the Berkshelf plugin for Vagrant:
Then, you need to tell Vagrant that you want to use the plugin. You do this by enabling the plugin in Vagrantfile
:
Then, you need a Berksfile in the root directory of your Chef repository to tell Berkshelf which cookbooks to install on each Vagrant run:
Eventually, you can start your VM using Vagrant. Berkshelf will first download and then install all the required cookbooks in the Berkshelf, and upload them on the Chef server. Only after all the cookbooks are made available on the Chef server by Berkshelf, will Vagrant go on:
This way, using Berkshelf together with Vagrant, saves a lot of manual steps and gets faster cycle times for your cookbook development.