Greetings Chefizens,
I’m trying to abstract some WET code I have sprinkled around several of my
recipes into a library. I’m trying to define a method in a “library cookbook”,
and use that method in a different cookbook. But I’m failing. Below I show a few
techniques I’m trying. The examples below are contrived and very simple –
I want to start simple and actually get it, before I try more abstractions
layered on top (say, LWRP).
Docs I’m reading in an attempt to teach myself how to wield library cookbooks in
this manner:
[1] http://stackoverflow.com/questions/15595144/dry-within-a-chef-recipe
[2] http://stackoverflow.com/questions/17333486/how-do-i-share-code-across-chef-cookbooks-in-a-chef-repo
[3] http://www.opscode.com/blog/2013/09/04/demystifying-common-idioms-in-chef-recipes/
[4] http://dougireton.com/blog/2012/12/16/how-to-include-the-windows-cookbook-helper-methods-in-your-chef-recipe/
[5] http://docs.opscode.com/essentials_cookbook_libraries.html
All of my attempts result in:
Recipe Compile Error in /var/cache/chef/cookbooks/hoosier/recipes/default.rb
NameError
uninitialized constant Chef::Recipe::Urmom
And so:
The cookbook “urmom” has a “halper” library: cookbooks/urmom/libraries/halper.rb:
module Halper
def saywhat
return "urmom"
end
end
The cookbook “hoosier” has a dependency set in metadata.rb on “urmom”. It is in
hoosier’s default recipe that I’m trying these tests.
foo = Urmom::Halper.saywhat
Chef::Log.debug(“HIGGS-BOSON: result: #{foo}”)
I run it: chef-client -l debug -L /var/log/chef/client.log -o “recipe[hoosier]”
This technique is how I understood what [1] is saying.
Next I try this in recipe[hoosier::default], based on [3] and [4]:
::Chef::Recipe.send(:include, Urmom::Halper)
foo = Urmom::Halper.saywhat
Chef::Log.debug(“HIGGS-BOSON: result: #{foo}”)
Next I try:
extend Urmom::Halper
foo = Urmom::Halper.saywhat
Chef::Log.debug(“HIGGS-BOSON: result: #{foo}”)
Last, I change the Halper library, per Ranjib Dey’s comment in [3]:
class Chef::Recipe::Halper
def saywhat
return "urmom"
end
end
The calling recipe is still:
foo = saywhat
Chef::Log.debug(“HIGGS-BOSON: result: #{foo}”)
Any help graciously accepted :>
kallen