Chef, Apache2 cookbook ressource fails to find service[apache2] when called from a custom resource

779 views Asked by At

In my custom chef cookbook (located at https://github.com/sanguis/chef-omeka/tree/lwrp).

I am calling the Apache2 resource web_app from inside a custom resource (LWRP) that is being called from the custom solo.rb recipe.

    include_recipe 'apache2' 
web_app url do
  server_name url
  server_aliases aliaes
  cookbook_name 'apache2'
  docroot dir
  allow_override 'All'
  directory_index 'false'
  # notifies :reload, 'service[apache2]', :delayed
end

This this returns an error:

[#] [2016-02-23T23:02:31+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report

[#] [2016-02-23T23:02:31+00:00] ERROR: instanceomeka.dev had an error: Chef::Exceptions::ResourceNotFound: resource execute[a2enmod headers] is configured to notify resource service[apache2] with action reload, but service[apache2] cannot be found in the resource collection. execute[a2enmod headers] is defined in /tmp/kitchen/cache/cookbooks/apache2/definitions/apache_module.rb:35:in `block in from_file'

However when I call the same resource from directly inside of a custom recipe here (line 126) it works.

My run list is bellow

   #      - recipe[build-essential]
  - recipe[php::default]
  - recipe[apache2]
  - recipe[apache2::mod_rewrite]
    # - recipe[apache2::mod_expires]
  - recipe[apache2::mod_ssl]
  - recipe[apache2::mod_php5]
  - recipe[omeka::default]
  - recipe[omeka::solo]
attributes:    #      - recipe[build-essential]
  - recipe[php::default]
  - recipe[apache2]
  - recipe[apache2::mod_rewrite]
    # - recipe[apache2::mod_expires]
  - recipe[apache2::mod_ssl]
  - recipe[apache2::mod_php5]
  - recipe[omeka::default]
  - recipe[omeka::solo]
attributes: 
  machine_fqdn: omeka.dev
  machine_fqdn_as_hostname: true
  apache2:
    listen_ports: ["80", "443"]

  machine_fqdn: omeka.dev
  machine_fqdn_as_hostname: true
  apache2:
    listen_ports: ["80", "443"]

This fails on both ubuntu 14.04 and centos7.

1

There are 1 answers

2
coderanger On BEST ANSWER

This is a known issue with few workarounds for the moment. The issue is that using the new custom resource system enforces use_inline_resources mode, which is 99% a great idea except for this. That mode creates an isolated run context inside the custom resource so it can't see "out" to the other resources for the purposes of notifications. The Poise helper library offers some tools to get around this, but with Chef core the only major workaround is the super-unsupported (i.e. this might break without a major release):

web_app url do
  # ...
  notifies :reload, Chef.run_context.resource_collection.find('service[apache2]')
end