Chef 'cookbook' in Berksfile vs 'depends' in metadata.rb

17,952

Solution 1

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).

Solution 2

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.

Share:
17,952
tokenvolt
Author by

tokenvolt

Updated on October 13, 2022

Comments

  • tokenvolt
    tokenvolt over 1 year

    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'
    

    ?

  • tokenvolt
    tokenvolt over 9 years
    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?
  • Ganesh Hegde
    Ganesh Hegde almost 9 years
    Thanks for the detailed explanation! This cleared a lot of doubts
  • Asaf
    Asaf over 8 years
    I don't understand why use berks at all, since metadata has source_url for custom urls
  • Kenny Evitt
    Kenny Evitt over 8 years
    @Asaf 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.
  • J.Z.
    J.Z. about 8 years
    @Asaf - 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.
  • dragon788
    dragon788 over 6 years
    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.
  • KingAndrew
    KingAndrew about 6 years
    Excellent! That really explains why we have both. +1
  • Kamil Roman
    Kamil Roman over 5 years
    This answer, unfortunately, still does not explain whether one should declare the dependencies in both of the files
  • neaGaze
    neaGaze almost 5 years
    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 trick