chef / chef

Chef Infra, a powerful automation platform that transforms infrastructure into code automating how infrastructure is configured, deployed and managed across any environment, at any scale
http://www.chef.io/chef/
Apache License 2.0
7.61k stars 2.51k forks source link

updated_by_last_action? of resource in LWRP always returns false #3748

Closed metmajer closed 9 years ago

metmajer commented 9 years ago

Version:

Chef: 12.4.1 (not tested in other versions)

Environment:

I am using a Dockerized Debian 7.4 to test our custom Dynatrace Cookbook via Test Kitchen.

Scenario:

I wrote an LWRP to configure and deploy a set of init scripts (templates) for each of our system services. What I would like to achieve is that our services should only get restarted if a template has changed in a Chef run:

# recipes/wsagent_package.rb

dynatrace_configure_init_scripts "#{name}" do
  ...
  notifies :restart, "service[#{name}]"
end

service "#{name}" do
  service_name service
  ...
  action [:enable]
end
# providers/configure_init_scripts.rb

action :run do
  new_resource.scripts.each do |script|
    t = template "Configure and copy the #{new_resource.name}'s '#{script}' init script" do
      source "init.d/#{script}.erb"
      path   "#{new_resource.installer_prefix_dir}/dynatrace/init.d/#{script}"
      ...
      action :create
    end
    new_resource.updated_by_last_action(true) if t.updated_by_last_action?
end

The problem here is that t.updated_by_last_action is always false, even if:

Please find a temporary branch here: https://github.com/dynaTrace/Dynatrace-Chef/tree/bugfix/restartIfRequired.

In order to reproduce:

1) git clone https://github.com/dynaTrace/Dynatrace-Chef.git -b bugfix/restartIfRequired 2) Download the installer artifact from http://bit.ly/1hfKlte and place the file as dynatrace-wsagent.tar in the Cookbook's files directory 3) bundle install && kitchen converge wsagent-package-debian

coderanger commented 9 years ago

Yes, that pattern never worked. Use use_inline_resources or similar if you want this kind of behavior.

metmajer commented 9 years ago

Using use_inline_resources in providers/configure_init_scripts.rb, such as:

# providers/configure_init_scripts.rb

use_inline_resources

action :run do
...

does not solve the problem. @coderanger can you be more specific? Thanks.

coderanger commented 9 years ago

use_inline_resources automatically sets the updated flag on the LWResource if any resource used in the action method is updated.

stevendanna commented 9 years ago

Specifically, using use_inline_resources, the following should correctly set the updated flag on the resource if any of your script resource executed.

use_inline_resources 

action :run do
  new_resource.scripts.each do |script|
    template "Configure and copy the #{new_resource.name}'s '#{script}' init script" do
      source "init.d/#{script}.erb"
      path   "#{new_resource.installer_prefix_dir}/dynatrace/init.d/#{script}"
      ...
      action :create
    end
  end
end