How to use hand-written cookbooks when using berkshelf in chef?
Asked Answered
T

2

14

I'm using vagrant+chef. My chef cookbooks worked perfectly. Then I installed vagrant-berkshelf plugin and from that moment I could not use own cookbooks. Berkshelf overrides cookbooks directory and chef does not see my cookbooks when I use them. My config is like this:

config.berkshelf.enabled = true

config.vm.provision :chef_solo do |chef|
   chef.add_recipe "qbaka-frontend"
end

With this configuration chef works only with cookbooks specified in Berksfile but can't see my cookbooks in cookbooks directory.

How can I work simultaneously with my & Berkshelf's cookbooks?

Transistorize answered 28/11, 2013 at 15:18 Comment(0)
S
45

The vagrant-berkshelf plugin and Berkshelf in general is very cookbook-centric. Chef, however, is cookbook-repo centric. Installing the vagrant-berkshelf plugin encourages you to use the cookbook-centric approach, treating each cookbook like its own software project.

You need to add each of the cookbooks in your cookbooks directory to your Berksfile. There are a couple of approaches here:

  1. If you only need one or two cookbooks, just add them using the Berkshelf path location:

    cookbook 'bacon', path: '~/cookbooks/bacon'
    
  2. If you want all your cookbooks, you can leverage Ruby here. A little-known secret is that the Berksfile is executed as Ruby, so you can loop and be magical:

    Dir['/Users/sethvargo/cookbooks/**'].each do |path|
      cookbook File.basename(path), path: path
    end
    

    That will load each cookbook in that directory into your Berksfile (and thus Berkshelf)

Sources:

  • I'm a core committer to the Berkshelf project
  • I worked for Chef at the time of this post :)
Schematic answered 28/11, 2013 at 18:25 Comment(4)
Thanks for your answer and for this tool :). But why does it work so? I'd expect complete transparency from such kind of tools, like it is done in Maven.Transistorize
@Transistorize I'm not 100% sure what you're asking. Why is Berkshelf cookbook-centric?Schematic
Wouldn't this be better? Also I got a glob error using square braces Dir.glob('custom_cookbooks/**').each do |path| cookbook File.basename(path), path: path endDiggs
From Berkshelf.com: "The Chef Solo provisioner’s cookbook_path attribute is hijacked when using the Vagrant Berkshelf plugin. Cookbooks resolved from your Berksfile will automatically be made available to your Vagrant virtual machine."Stingaree
A
3

You have to specify the path to your cookbooks in your Berksfile.

 cookbook "artifact", path: "/Users/reset/code/artifact-cookbook"

See here: http://berkshelf.com/#path_location

Aridatha answered 28/11, 2013 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.