Chef 'cookbook' in Berksfile vs 'depends' in metadata.rb
Asked Answered
S

3

63

What's the difference between adding cookbooks to Berksfile using 'cookbook' and adding cookbooks to metadata.rb using 'depends'? For example, if I add to metadata.rb

depends 'nginx'

do I need to add it to Berksfile using

cookbook 'nginx'

?

Stoltzfus answered 16/8, 2014 at 18:20 Comment(0)
B
79

The Berksfile is Berkshelf specific, while the metadata file is built into Chef.

Adding your dependencies to the metadata file allows other applications, like librarian-chef or the supermarket, to read your dependencies as well.

Note that Berkshelf reads the dependencies from metadata as well, as long as you add the metadata line to the Berksfile.

I strongly recommend specifying all dependencies in your metadata file, and using your Berksfile to point to where specific cookbooks are stored if they're not available in the supermarket (like Github, or a local path).

Baffle answered 16/8, 2014 at 18:37 Comment(7)
So, if I want to use 'nginx' cookbook from supermarket, I only need to add depends 'nginx' in metadata.rb. But in case 'nginx' is located somewhere else (e.g. Github), I need also to add cookbook 'nginx', github: <nginx-url-repo>. Is it right?Stoltzfus
Thanks for the detailed explanation! This cleared a lot of doubtsDevoted
I don't understand why use berks at all, since metadata has source_url for custom urlsDhammapada
@Dhammapada I'm pretty sure the source_url in metadata.rb is just a link to the source code of a cookbook, not a URL from which Chef will actually download the cookbook itself. In other words, Chef itself (without Berkshelf or something similar) won't resolve cookbook dependencies; all dependencies are assumed to either be installed on your server or available from the official Chef Supermarket. You can run your own Supermarket server but otherwise Berkshelf or similar is needed to use cookbooks from other locations.Jelle
@Dhammapada - Berks can recursively download any dependent cookbooks for you to develop locally, and also handle uploading all of those to your Chef server. I don't think there's a corresponding tool in the Chef toolkit to do the same. Berks is part of the Chef DK. Thanks for the notes everyone.Damiandamiani
Keep in mind you may need to specify a version in the Berksfile if you want to override the source and use a different branch.Baccate
If anyone's like me trying to use a cookbook that's inside the chef-infra-client but outside of supermarket, then adding source :chef_server in the Berksfile does the trickBernhard
B
20

Berksfile and metadata.rb have different purposes to solve and comes into picture at different stages of cookbook life-cycle.

  1. Berksfile is for dependency management for cookbooks. Consider a case where my cookbook is using a community cookbook from chef supermarket. In this case, first I need to download that community cookbook from supermarket and upload it along with my own cookbook to chef server. Berksfile simplifies this workflow for you. With single command (berks install), it downloads all dependent cookbooks (and their dependent cookbooks -- transitive dependencies) from their respective sources (may be from git repository or from supermarket). With another single command berks upload it uploads all these cookbooks to chef server. You do not have to upload them individually with knife cookbook upload. Role of Berksfile in particular cookbooks life-cycle finishes here.

  2. metadata.rb is referred by chef-client while actually converging the node. It uses this file to download all the required cookbooks from chef server (assuming that these cookbooks are now available on chef server by using berkshelf or knife ) to the node to successfully complete the chef-client run.

Bream answered 31/3, 2016 at 10:33 Comment(2)
Excellent! That really explains why we have both. +1Cidevant
This answer, unfortunately, still does not explain whether one should declare the dependencies in both of the filesDistrait
H
0

You don't need to declare dependencies in both of the files @Kamil Declare your dependencies in metadata.rb and source your local in house supermarket or chef supermaket in berksfile which will download dependencies for you.

Hypolimnion answered 15/6, 2020 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.