{"id":39349,"date":"2016-08-25T10:36:01","date_gmt":"2016-08-25T05:06:01","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=39349"},"modified":"2016-08-25T12:16:31","modified_gmt":"2016-08-25T06:46:31","slug":"introduction-to-berkshelf","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/introduction-to-berkshelf\/","title":{"rendered":"Introduction to Berkshelf"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">People <a href=\"http:\/\/www.tothenew.com\/blog\/understanding-chef-and-writing-cookbooks\/\">working with <\/a><\/span><a href=\"http:\/\/www.tothenew.com\/blog\/understanding-chef-and-writing-cookbooks\/\"><span style=\"font-weight: 400;\">Chef<\/span><\/a><span style=\"font-weight: 400;\"> Configuration Management tool know how Chef has significantly improved their ways of building servers quickly and reliably using cookbooks rather than using a\u00a0number of scripts on a single or sometimes on multiple machines. \u00a0<\/span>Recipes which are nested under cookbooks, are an integral part of Chef and helps in\u00a0defining the way a particular Infrastructure code will be built. Once these cookbooks become\u00a0large and highly interdependent, it becomes necessary to manage these cookbooks itself. Berkshelf is the tool which makes \u2018the management\u2019 &amp; dependency management between cookbooks easy.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-39353 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef.png\" alt=\"chef\" width=\"225\" height=\"225\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef.png 225w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef-150x150.png 150w\" sizes=\"(max-width: 225px) 100vw, 225px\" \/>\u00a0<img decoding=\"async\" loading=\"lazy\" class=\"alignright wp-image-39352 \" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berkshelf.png\" alt=\"berkshelf\" width=\"321\" height=\"214\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s first talk about how a cookbook is <a title=\"chef devops\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">used in Chef<\/a>, 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\u00a0Resource Collections which are like files, package, services, repositories, etc.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39351\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Cookbook-flow.png\" alt=\"Cookbook-flow\" width=\"778\" height=\"613\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Cookbook-flow.png 778w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Cookbook-flow-300x236.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Cookbook-flow-624x491.png 624w\" sizes=\"(max-width: 778px) 100vw, 778px\" \/><\/p>\n<p>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\u00a0lack of proper management tool on it. Yes, management tool on the top of management tool!<\/p>\n<p><span style=\"font-weight: 400;\">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. \u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">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 <\/span><a href=\"http:\/\/www.tothenew.com\/blog\/understanding-chef-and-writing-cookbooks\/\"><span style=\"font-weight: 400;\">knife<\/span><\/a><span style=\"font-weight: 400;\"> like generating, uploading &amp; downloading Cookbooks.<\/span><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<h2><span style=\"font-weight: 400;\">Installing Berkshelf<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">You will need Ruby installed on your system. Also, Berkshelf comes integrated with Chefdk, so installing <\/span><a href=\"https:\/\/downloads.chef.io\/chef-dk\/\"><span style=\"font-weight: 400;\">Chefdk<\/span><\/a><span style=\"font-weight: 400;\"> will automatically give you berks installed:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39359\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef_dk_install.png\" alt=\"chef_dk_install\" width=\"1292\" height=\"246\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef_dk_install.png 1292w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef_dk_install-300x57.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef_dk_install-1024x194.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/chef_dk_install-624x118.png 624w\" sizes=\"(max-width: 1292px) 100vw, 1292px\" \/><\/p>\n<h2>Berksfile<\/h2>\n<p><span style=\"font-weight: 400;\">Berksfile is the most crucial component of Berkshelf! It\u2019s just like metadata for Chef. However, the usage is somewhat different. Berksfile makes it simple to download a dependency cookbook from <\/span><a href=\"https:\/\/supermarket.chef.io\/\"><span style=\"font-weight: 400;\">chef supermarket<\/span><\/a><span style=\"font-weight: 400;\"> (or other places) and upload to the cookbook repository on the server. Here is the usual content inside it:<\/span><\/p>\n<p><strong><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39358\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berksfile.png\" alt=\"berksfile\" width=\"830\" height=\"278\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berksfile.png 830w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berksfile-300x100.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berksfile-624x209.png 624w\" sizes=\"(max-width: 830px) 100vw, 830px\" \/>\u00a0<\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">It involves mainly three settings:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>source <\/b><span style=\"font-weight: 400;\">: Place from where these cookbooks and dependencies will be fetched if those are not available locally with Berkshelf.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>metadata <\/b><span style=\"font-weight: 400;\">: 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\u2019s just like <\/span><a href=\"https:\/\/gist.github.com\/maguec\/1263648\"><span style=\"font-weight: 400;\">gemspec<\/span><\/a><span style=\"font-weight: 400;\"> for <\/span><a href=\"https:\/\/gist.github.com\/boriscy\/827469\"><span style=\"font-weight: 400;\">Gemfile<\/span><\/a><span style=\"font-weight: 400;\">. <\/span><\/li>\n<li style=\"font-weight: 400;\"><b>cookbook <\/b><span style=\"font-weight: 400;\">: List of all the cookbooks\/dependencies required.<\/span><\/li>\n<\/ul>\n<h2><span style=\"font-weight: 400;\"><br \/>\nTo Generate a Berksfile on existing Cookbook.<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Run below command in the directory containing your cookbook folder:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$ chef generate cookbook &lt;cookbook-folder&gt;<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39357\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_on_existing.png\" alt=\"berks_on_existing\" width=\"1352\" height=\"248\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_on_existing.png 1352w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_on_existing-300x55.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_on_existing-1024x187.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_on_existing-624x114.png 624w\" sizes=\"(max-width: 1352px) 100vw, 1352px\" \/><\/p>\n<h2><span style=\"font-weight: 400;\">Creating a Cookbook with Berkshelf<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">$ chef generate cookbook &lt;desired-cookbook-name&gt;<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39356\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_new.png\" alt=\"berks_new\" width=\"1392\" height=\"246\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_new.png 1392w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_new-300x53.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_new-1024x180.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_new-624x110.png 624w\" sizes=\"(max-width: 1392px) 100vw, 1392px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Installing Cookbooks and their dependencies <\/span><\/h2>\n<p><span style=\"font-weight: 400;\">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.<\/span><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">$ berks install<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39355\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_install.png\" alt=\"berks_install\" width=\"1180\" height=\"478\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_install.png 1180w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_install-300x121.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_install-1024x414.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_install-624x252.png 624w\" sizes=\"(max-width: 1180px) 100vw, 1180px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Once you have installed all the cookbook and its dependencies, you can find all that placed inside <\/span><b>~\/.berksfile <\/b><span style=\"font-weight: 400;\">folder which can be altered by an\u00a0environment variable: BERKSHELF_PATH which can later be uploaded to Chef server or provided to Chef Solo.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">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 \u2018berks package.&#8217; Just run this command inside the cookbook where metadata.rb have dependencies listed:<br \/>\n<\/span><strong><strong>\u00a0<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39354\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_package.png\" alt=\"berks_package\" width=\"1664\" height=\"84\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_package.png 1664w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_package-300x15.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_package-1024x51.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_package-624x31.png 624w\" sizes=\"(max-width: 1664px) 100vw, 1664px\" \/><\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">This generated archive can be provided to Chef Solo or uploaded to Chef Server after extraction anywhere.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Uploading the cookbook and its dependencies<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$ berks upload<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39361\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_upload.png\" alt=\"berks_upload\" width=\"1162\" height=\"374\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_upload.png 1162w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_upload-300x96.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_upload-1024x329.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/berks_upload-624x200.png 624w\" sizes=\"(max-width: 1162px) 100vw, 1162px\" \/><\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0number of scripts on a single or sometimes on multiple machines. \u00a0Recipes which are nested under cookbooks, are an integral part of Chef and helps in\u00a0defining the way a [&hellip;]<\/p>\n","protected":false},"author":181,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":14},"categories":[2348,1],"tags":[3908,3906,3915,3916,1910,2606,3910,3907,3909],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39349"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/181"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=39349"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39349\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=39349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=39349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=39349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}