How To Manage Chef Cookbooks With Berkshelf

Berkshelf is a great way to manage your chef cookbooks with dependencies. If your cookbook is dependent on one or more dependencies, you can manage it without any overheads using the berkshelf way.  All you have to do is, just mention the dependent cookbooks with its version number if needed. Berkshelf will install all the dependent cookbooks from the Opscode cookbook community. In this tutorial i will explain how to set up berkshelf in your workstation and managing your cookbooks with it.

Installing Berkshelf:

Install berkshelf in your chef workstation using the following command.

gem install berkshelf

Managing Existing Cookbooks:

You can manage the existing cookbook using berkshelf. 

1. CD in to your cookbook directory eg: cd cookbooks/powershell and issue the following command to create the berks and related files for your cookbook.

berks init

2. Open the berksfile that is created inside your cookbook and mention all the dependencies .In our example , for powershell, windows and chef-handler cookbooks are the dependencies. So inside the powershell berksfile you can mention the dependencies in the following format.


site :opscode

metadata

cookbook 'windows'

3.  Windows cookbook has a chef-handler dependency , but you dont have to mention it in the berksfile. It will automatically install the relevant chef-handler cookbook for the windows cookbook. You just have to mention the direct dependencies inside the berksfile.

5. By default , berksfile configures itself with the default knife.rb settings. You can check the config  in ~/.berksfile/config.json .The config.json contains a validator ,chef url settings. You can override the default settings using the following command.

berks configure

4. Once the dependencies are mentioned in a berksfile, you can install the dependencies using the following command.

berks install

Creating And Managing New Cookbooks With Berkshelf:

1. Make your current directory as cookbook directory of the chef-repo, because berks command will create the cookbook and related files in the current working directory. Create a new cookbook with berkshelf with the following command. 
berks cookbook cookbook_name

2. Let say, you want to create a cookbook for your new php application. Apache2 , php and Msql will the dependencies for your phpapp. So you can mention the dependencies in the berksfile of the newly created cookbook using berkshelf.

 <--berksfile-->

site :opscode metadata

cookbook 'Apache2'

cookbook 'php'

cookbook 'mysql'

3.Install the dependencies using the berks install command. As explained earlier, the dependent cookbooks will get installed in ~/.berksfile/cookbooks folder.

In windows systems it get installed in C:UsersAdminstrator.berksfilecookbooks location

4. Once your cookbook is ready , you can upload the with all the dependent cookbooks using the following command.

berks upload phpapp

The above command will upload the phpapp cookbook and all the other dependent cookbooks from the berkshelf’s cookbook folder.

5. Ther are various other commands associated with berkshelf. It will be used in different use cases. The commands are given below.

 --Berkshelf Commands--

berks apply ENVIRONMENT # Apply the cookbook version locks from Berksfi...
berks configure # Create a new Berkshelf configuration file
berks contingent COOKBOOK # List all cookbooks that depend on the given c...
berks cookbook NAME # Create a skeleton for a new cookbook
berks help [COMMAND] # Describe available commands or one specific c...
berks init [PATH] # Initialize Berkshelf in the given directory
berks install # Install the cookbooks specified in the Berksfile
berks list # List all cookbooks (and dependencies) specifi...
berks outdated [COOKBOOKS] # Show outdated cookbooks (from the community s...
berks package [COOKBOOK] # Package a cookbook (and dependencies) as a ta...
berks shelf SUBCOMMAND # Interact with the cookbook store
berks show [COOKBOOK] # Display name, author, copyright, and dependen...
berks update [COOKBOOKS] # Update the cookbooks (and dependencies) speci...
berks upload [COOKBOOKS] # Upload the cookbook specified in the Berksfil...
berks version # Display version and copyright information

For more information, visit the official berkshelf site Berkshelf.com

POST REPLY

Pin It on Pinterest

Shares
Share This