1
votes

We can already override Cookbook Attributes in a Role or Node Object. What about Recipes? Can we use resources (LWRP) provided in a Cookbook without having to use a Cookbook Wrapper?

For instance, I'd like to install Jenkins with some plugins. The Jenkins cookbook shows that there's an attribute that can be used

node['jenkins']['server']['plugins']

It is however limited in only allowing plugin name and version. There is also a Resource documented in the Cookbook that seems to do what I want. Eg:

jenkins_plugin 'custom_plugin' do
  action :install
  version '0.3'
  url 'http://myrepo/jenkins/plugins/0.3/custom_plugin.hpi'
end

Do I need to create a whole Wrapper Cookbook and put this code in the /recipes/default.rb just to add this functionality to a Role or Node? This seems to be overkill.

1
The application cookbook pattern is an emerging chef best practice. Good community cookbooks are written in such a way that their behaviour can be customized with node attributes and they might provide LWRP that make your cookbook simpler to write. Use a tool like Berkshelf to manage your dependencies and writing application cookbooks becomes a pleasure not a chore :-) - Mark O'Connor
@Mark. Thanks for the info. I'm already using Berkshelf and Application Cookbooks. It just seems overkill in some cases though, as when we need to use just one resource call that's not part of an existing recipe. I guess I have ruby mixin's in mind. - Bernard
I treat community cookbooks like libraries (in fact it's called the library pattern) and use berkshelf like Maven to manage these 3rd party dependencies. My "code" is the application cookbook that is used to describe and implement my desired infrastructure, my listing the resources I want created. This is where leveraging LWRP's comes in handy. Makes your cookbook declarative more readable. - Mark O'Connor

1 Answers

3
votes

When using any community cookbook, you should always read the recipes in depth as well. You would notice that the jenkins::server recipe allows you to set url in that hash as well as name and version.

In this particular case, you could override attributes like so:

node.override['jenkins']['server']['plugins'] = [
  {
    'name' => 'custom_plugin', 
    'version' => '0.3', 
    'url' => 'http://myrepo/jenkins/plugins/0.3/custom_plugin.hpi'
  }]

In the general case however, if this recipe wasn't flexible like this, then you would have to create a cookbook, that depends on the other cookbook (you don't have to execute the recipes, just depend on it), and define the resources using the LWRP in your own recipe.