Introduction to Berkshelf

25 / Aug / 2016 by Amit Naudiyal 0 comments

People working with Chef Configuration Management tool know how Chef has significantly improved their ways of building servers quickly and reliably using cookbooks rather than using a number of scripts on a single or sometimes on multiple machines.  Recipes which are nested under cookbooks, are an integral part of Chef and helps in defining the way a particular Infrastructure code will be built. Once these cookbooks become large and highly interdependent, it becomes necessary to manage these cookbooks itself. Berkshelf is the tool which makes ‘the management’ & dependency management between cookbooks easy.

chef berkshelf

 

Let’s first talk about how a cookbook is used in Chef, for example, Nginx. Cookbooks are fed into the Chef-Client or Chef-Solo. It contains how to configure your application or service on your server. Recipes which are the components of the application/service goes inside the cookbooks. These recipes contain Resource Collections which are like files, package, services, repositories, etc.

Cookbook-flow

LWRPs have made it easy for us to create Chef recipes using a minimal amount of code since the major work of writing the Resource, and Provider is already done. However working with these recipes/cookbooks can become a mess when there is a lack of proper management tool on it. Yes, management tool on the top of management tool!

The idea behind Berkshelf is: Do not put cookbooks in a Chef repository and instead treat them like their own piece of software or project, things will become better. The goal of Berkshelf is to manage a cookbook outside of Chef repository in isolation.  

Berkshelf is a Command Line tool which can act as a source code management tool or a package manager like gem, apt, yum, etc. It replaces portions of knife like generating, uploading & downloading Cookbooks. 

Installing Berkshelf

You will need Ruby installed on your system. Also, Berkshelf comes integrated with Chefdk, so installing Chefdk will automatically give you berks installed:

chef_dk_install

Berksfile

Berksfile is the most crucial component of Berkshelf! It’s just like metadata for Chef. However, the usage is somewhat different. Berksfile makes it simple to download a dependency cookbook from chef supermarket (or other places) and upload to the cookbook repository on the server. Here is the usual content inside it:

berksfile 

It involves mainly three settings:

  • source : Place from where these cookbooks and dependencies will be fetched if those are not available locally with Berkshelf.
  • metadata : This points to Berkshelf itself saying that for every dependency just look at me and I will look into metadata.rb placed just along with me. It’s just like gemspec for Gemfile.
  • cookbook : List of all the cookbooks/dependencies required.


To Generate a Berksfile on existing Cookbook.

Run below command in the directory containing your cookbook folder:

$ chef generate cookbook <cookbook-folder>

berks_on_existing

Creating a Cookbook with Berkshelf

$ chef generate cookbook <desired-cookbook-name>

berks_new

This is almost the same like Knife creates cookbooks, however, it will have some more files/directories available inside the cookbook. We are only concerned with Berksfile.

Installing Cookbooks and their dependencies

So, this is the thing we are mainly interested in. This will resolve all the dependencies and install cookbooks whatever is available in the Berksfile. 

$ berks install

berks_install

Once you have installed all the cookbook and its dependencies, you can find all that placed inside ~/.berksfile folder which can be altered by an environment variable: BERKSHELF_PATH which can later be uploaded to Chef server or provided to Chef Solo.

If in any case you would like to package all of your dependencies along with the cookbook into an archive, berks give you a tool called ‘berks package.’ Just run this command inside the cookbook where metadata.rb have dependencies listed:
 berks_package

This generated archive can be provided to Chef Solo or uploaded to Chef Server after extraction anywhere.

Uploading the cookbook and its dependencies

Once you have installed your cookbooks and dependencies, it is time to upload them to Chef server. Just go inside your cookbook where all of your dependencies are mentioned in Berksfile and where you ran install command. Run below command.

$ berks upload

berks_upload

If you do not have dependencies or have a single cookbook dependency, Berkshelf will not excite you much. However, if you have multiple dependencies, and you want to save your time and effort, you should start using Berkshelf.

FOUND THIS USEFUL? SHARE IT

Leave a comment -