October 1, 2013

Sharing Chef Cookbooks

I’ve been trying to publish my Dokku Cookbook to Opscode Community since I open sourced it a couple weeks ago but didn’t have much success until last night. The initial configuration was not so straightforward to me so I thought I’d write it down in case I need to do it again. The information on this post is probably documented somewhere but I failed to find it, feel free to comment with links to official docs or other blog posts related :)

chef-dokku is the result of my initial interaction with Chef which I’ve been using as part of another project which I plan to release “soon”. It allows you to manage a Dokku installation, allowing configuration of application’s environment variables and installation of Dokku plugins.

Since I come from a Puppet background, I had no idea what to do in order to publish the Cookbook to Opscode Community. Thanks to @juanjeojeda I got to know the command used for publishing but sadly that wasn’t enough to make it work.

I’ve been using the plugin from within Vagrant machines only and didn’t have the knife command available on my laptop so first thing I had to do was a gem install chef to get knife around. With Chef installed, my first attempt was to run the command Juanje suggested from a clean git clone but had no luck:

It turns out that knife doesn’t like the chef-dokku folder name and you need to pass in some extra options, so I created a chef-cookbooks folder under my ~/projects to keep Chef cookbooks, moved chef-dokku’s code into ~/projects/chef-cookbooks/dokku and the error message changed:

That error message led me to the knife.rb docs and I found out that I need to specify the client_key config on ~/.chef/knife.rb or on .chef/knife.rb under the project’s root. I also found out that I could save a few keystrokes and avoid the -o parameter by specifying the cookbook_path option. As a result, I’ve added my client key to ~/.chef/client.pem and created a ~/.chef/knife.rb file with:

client_key "#{ENV['HOME']}/.chef/client.pem"
cookbook_path "#{ENV['HOME']}/projects/chef-cookbooks"

With that in place I tried to share the Cookbook again without luck:

It turns out that knife / chef needs some folders in order to do its job. I couldn’t find a way to change the folder it uses so after some trial and error I ended up creating the directories required with sudo mkdir -p /var/chef/cache/checksums, chowned it to my user with sudo chown -R fabio: /var/chef to avoid using sudo and the error message changed again:

I’ve tried to enable debug logging with -l debug as the error message suggests but it didn’t work. Looking at knife -h I found out that the proper way to enable debugging is to provide -VV to the knife command and that gave me a pretty stacktrace to look into:

Looking at that stacktrace I noticed that the User-id was missing and I went off to Chef sources to find out where that should come from. Looking at these lines I found out that that parameter comes from the node_name defined on our knife configs and I added it my ~/.chef/knife.rb:

# Replace with your Opscode username
node_name "fgrehm"

But that still wasn’t enough and the error message changed one more time:

Some googling led me to this issue on Chef’s issue tracker and I found out that I had to downgrade to Ruby 1.9 as I was using 2.0. After downgrading and installing Chef again I was finally able to share the cookbook \o/

Summing up, here’s what you need to share your cookbook:

  • Ruby 1.9
  • Chef gem
  • An account at Opscode
  • Make sure the cookbooks are kept on a folder with the same name as the cookbook itself (ex: chef-dokku should be cloned to dokku)
  • ~/.chef/knife.rb or ./.chef/knife.rb configured with:
    • client_key: you should have seen it while registering with Opscode, if you didn’t save it get a new one at https://www.opscode.com/account/password
    • node_name: your Opscode username
    • cookbook_path: this is optional and will allow you to omit the -o parameter when sharing your cookbooks

© Fabio Rehm 2013-2022

Powered by Hugo & Kiss.