1

I am writing a cookbook that uses the Custom Resources present in Chef 12.5. I have a custom resource that uses a template resource and as such has a notifies that reloads the associated service.

However the associated service is managed in the main recipe. Currently this fails as it complains that there is no service.

In previous versions when using LWRPs this was caused by using use_inline_resources, and I believe you could side-step this particular issue by NOT including this function in your LWRP.

Now I am NOT including this line in my custom resource but the behaviour is present. Is there any way to turn off this behaviour?

Alternatively is there some other way of "including" the service in multiple resources so I don't have to have the exact same code in many many places?

kemra102
  • 211
  • 4
  • 12
  • A few related Chef PRs & issues that you might want to peruse: [4741](https://github.com/chef/chef/pull/4741), [4718](https://github.com/chef/chef/issues/4718), [4669](https://github.com/chef/chef/issues/4669), [4017](https://github.com/chef/chef/issues/4017). Scoping of resources and the `resource_collection` inside an LWRP seems to be a bit of an outstanding issue. – jayhendren Apr 26 '16 at 16:47

3 Answers3

4

Based on delerious010's answer myself and a colleague were able to come up with a nicer work around for this particular situation:

action :create do
  global_nginx = resources('service[nginx]')

  template "/etc/nginx/conf.d/#{name.tr(' ', '_')}.conf" do
    cookbook 'nginx_server'
    source 'server_block.conf.erb'
    owner 'root'
    group 'root'
    mode '0644'
    variables(
      listen: listen,
      server_name: real_server_name,
      root: root,
      index: index,
      config: config
    )
    notifies :reload, global_nginx, :delayed
  end
end

Obviously this example has only a single resource being managed but should allow you to have multiple and only notify on the ones you want to.

EDIT: Note that this relies on a bug (https://github.com/chef/chef/issues/4669) that the Chef Developers will fix. Because in 12.9.38 searches on the resource collection were made to recurse upwards to outer run_contexts this code will still work, but will no longer be necessary (and will correctly handle multiply nested subcontexts where you need to grab a resource in the root and not just the parent run_context). Best solution here should just be to upgrade.

lamont
  • 133
  • 5
kemra102
  • 211
  • 4
  • 12
  • Nice, figured it should be doable, but happy to have the confirmation. Thanks for that! – jonathanserafini Oct 17 '15 at 00:17
  • 1
    that actually relies on a bug (that searching the resource collection from a 12.5-style provider searches the parent run_context) that we are going to fix and will have to break this behavior. see https://github.com/chef/chef/issues/4669 – lamont Apr 26 '16 at 15:45
  • actually due to recursive resource searching that was added in 12.9.x we won't wind up breaking this. but still the correct solution is to just upgrade to 12.9.x or later and not have to bother with this at all. – lamont Apr 26 '16 at 16:02
2

In Chef 12.5, use_inline_resources is turned on by default. This has the effect that, whenever a sub-resource is updated, the LWRP will be flagged as having changed. As such, you could always have the resource notification be defined on the "parent" LWRP.

Or, if you'd like the notifies clause defined for all instances of your LWRP, you can always create the notification directly in the action block :

notifies_delayed(:restart, resources("service[rsyslog]"))

However, bear in mind that any resource modified within your LWRP would cause the service to restart. Though it may be possible to use the call to resources to define a notifies property on the child resource. Not entirely sure, as I've not yet tested that.

jonathanserafini
  • 1,738
  • 14
  • 20
0

In 12.9.38 the ability to notify from a sub-resource-collection / use_inline_resources provider to an outer resource_collection was added by:

https://github.com/chef/chef/commit/0ca27b6f30ccd327505bd3a44bd319fb3eba956b

This is now the default behavior of notifications so you should only need to upgrade.

lamont
  • 133
  • 5