How to use Chef – Berkshelf

How to use Chef – Berkshelf

Chef – Using Berkshelf

What is Berkshelf

Berkshelf is a dependency manager for cookbooks. Chef supermarket is a location for all the community cookbooks, which you can download and install for usage in your environment. Some cookbooks are also available in git repository like github which is primary version control system used by chef community developers.



Which one to use Policyfiles or Berkshelf

As of now we can use both Policyfiles or Berkshelf for dependency management. Policyfiles provides more predictability and improved way of promoting cookbooks to different environments.

The only place where you can only use Berkshelf is when using Chef automate.

1. How to add Cookbook dependencies and install them

Cookbook dependencies can be added at two places in the default generated cookbook structure


@ metadata.rb

@berksfile


The berksfile includes the metadata internally, berksfile additionally can include other cookbook dependencies, which may not be required dependencies for your local cookbook


[user1@host-1 chefspace]$ chef generate cookbook testcookbook

[user1@host-1 testcookbook]$ pwd;ls -ltr

/home/user1/chefspace/testcookbook

total 16

-rw-rw-r–. 1 user1 user1 216 Jun 8 23:22 metadata.rb

-rw-rw-r–. 1 user1 user1 60 Jun 8 23:22 README.md

-rw-rw-r–. 1 user1 user1 1067 Jun 8 23:22 chefignore

-rw-rw-r–. 1 user1 user1 47 Jun 8 23:22 Berksfile

drwxrwxr-x. 3 user1 user1 20 Jun 8 23:22 test

drwxrwxr-x. 3 user1 user1 38 Jun 8 23:22 spec

drwxrwxr-x. 2 user1 user1 23 Jun 8 23:22 recipes


Update the metadata.rb file to include the dependency for apt package


[user1@host-1 testcookbook]$ cat metadata.rb

name ‘testcookbook’

maintainer ‘The Authors’

maintainer_email ‘you@example.com’

license ‘all_rights’

description ‘Installs/Configures testcookbook’

long_description ‘Installs/Configures testcookbook’

version ‘0.1.0’

depends ‘apt’,’~> 5.0′


Source from where the dependency cookbooks are downloaded are defined in the berksfile as below


[user1@host-1 testcookbook]$ cat Berksfile

source ‘https://supermarket.chef.io’


metadata


Run berks install to download the dependency cookbook


[user1@host-1 testcookbook]$ berks install

Resolving cookbook dependencies…

Fetching ‘testcookbook’ from source at .

Fetching cookbook index from https://supermarket.chef.io…

Installing apt (5.1.0)

Installing compat_resource (12.19.0)

Using testcookbook (0.1.0) from source at .

2. How to upload cookbook and its dependencies

The cookbook and its dependencies are downloaded locally at below locaiton


[user1@host-1 testcookbook]$ ls -ltr ~/.berkshelf/cookbooks/

total 8

drwxrwxr-x. 9 user1 user1 4096 Jun 8 23:26 apt-5.1.0

drwxrwxr-x. 4 user1 user1 4096 Jun 8 23:26 compat_resource-12.19.0


To upload the cookbooks to Chef server which will manage the nodes on to which cookbooks gets installed run the below upload command


[user1@host-1 testcookbook]$ berks upload

Uploaded apt (5.0.0) to: ‘https://api.chef.io:443/organizations/example’

Uploaded compat_resource (12.16.2) to: ‘https://api.chef.io:443/organizations/example’

Uploaded testcookbook (0.1.0) to: ‘https://api.chef.io:443/organizations/example’


What is Berksfile

This is a place where source and dependencies for the cookbooks are defined. The source for downloading cookbooks can be local, chef supermarket, private chef supermarket, chef server or any git repository location local or on a remote location



Using private chef supermarket or Chef Server as source of cookbooks

eg.

source “https://supermarket.example.com”

source “https://supermarket.chef.io”



1. Downloading specific cookbook dependencies

  • From git repository

cookbook “library-cookbook”, “~> 0.1.1”, git: “https://github.com/example/library-cookbook.git”

  • From github repository

cookbook “library-cookbook”, “~> 0.1.1”, github: “example/library-cookbook”

  • From locally

cookbook “library-cookbook”, “~> 0.1.1”, path: “../library-cookbook”


2. Grouping Cookbooks for excluding during uploads

group :test do

cookbook “test-cookbook”, path: “test/fixtures/test”

end

or

cookbook “test-cookbook”, path: “test/fixtures/test”, group: :test


[user1@host-1 testcookbook]$ berks install –except test ## This will exclude the cookbooks grouped in test to be excluded from installing

3. Usage of berks CLI


berks cookbook

This command is deprecated for generating a cookbook template. We can use

Chef generate cookbook instead

berks info cookbook

[user1@host-1 testcookbook]$ berks info testcookbook

Name: testcookbook

Version: 0.1.0

Description: Installs/Configures testcookbook

Author: The Authors

Email: you@example.com

License: all_rights

Dependencies: apt (~> 5.0)

berks install

Used to install the cookbooks and its dependencies as shown in above section

berks list

[user1@host-1 testcookbook]$ berks list

Cookbooks installed by your Berksfile:

* apt (5.1.0)

* compat_resource (12.19.0)

* testcookbook (0.1.0) from source at .


berks search

[user1@host-1 testcookbook]$ berks search java_se

java_se (8.131.0)

[user1@host-1 testcookbook]$ berks search tomcat_latest

tomcat_latest (0.1.6)


berks show

[user1@host-1 testcookbook]$ berks show testcookbook

/home/user1/chefspace/testcookbook


berks update

[user1@host-1 testcookbook]$ berks update

Resolving cookbook dependencies…

Fetching ‘testcookbook’ from source at .

Fetching cookbook index from https://supermarket.chef.io…

Using apt (5.1.0)

Using compat_resource (12.19.0)

Using testcookbook (0.1.0) from source at .


berks upload

berks upload ## upload the cookbook to chef server which is configured with workstation


berks version

[user1@host-1 testcookbook]$ berks version

4.3.5


For more information – visit here for detailed and additional information



Hope you enjoyed reading this article. Thank you.