Chef: Nested LWRP issue with cookbook_file/template resources

532 Views Asked by At

I wrote a couple of LWRP cookbooks to install and manage some services (Monit and Omnicheck). One of the functions they provide is for you to supply a file that will be used with the template or cookbook_file resources.

I found that when I call either of those resources I have to supply "cookbook new_resource.cookbook" to get it to look in the calling cookbook for the given file rather than in the LWRP's cookbook.

This all works fine until, apparently, the calls get nested. In this case if you use the Omnicheck LWRP to setup a new instance and choose to run it through Monit, it will update Monit for you via the Monit LWRP. This works fine for the default Omnicheck job that gets created as part of the LWRP's cookbook, but when I try to create a new Omnicheck job from another cookbook the Monit LWRP is choking and complaining about not being able to find the file for the template resource.

The issue is that the template file in question is in the Omnicheck cookbook, but new_resource.cookbook in the Monit LWRP is evaluating to the top level cookbook rather than the Omnicheck cookbook.

So the path looks like:

application -> Omnicheck -> Monit

In this instance in the Monit LWRP the new_resource.cookbook value is "application" instead of "Omnicheck".

Since it appears that new_resource.cookbook is not what I need/want, how can I determine the cookbook of the LWRP caller?

The relevant error looks something like:

[2014-08-21T00:46:15+00:00] ERROR: my_omnicheck_service[chef] (application::default line 30) had an error: Chef::Exceptions::FileNotFound: template[/some/path/omnicheck/etc/stage/chef.conf] (/var/chef/cache/cookbooks/my_omnicheck/providers/service.rb line 10) had an error: Chef::Exceptions::FileNotFound: Cookbook 'application' (0.1.5) does not contain a file at any of these locations:
    templates/centos-5.8/omnicheck.erb
    templates/centos/omnicheck.erb
    templates/default/omnicheck.erb
1

There are 1 best solutions below

0
On

I don't really like this and I hope someone knows of a better way to solve the issue, but I found that I can interrogate the new_resource.source_line to get the information I'm after:

callingCookbook = new_resource.cookbook_name
if (new_resource.source_line =~ /\/cookbooks\/([^\/]+)\//) then
  callingCookbook = $1
end

Then I can use callingCookbook where I was previously using new_resource.cookbook_name.