LWRP and new_resource.updated_by_last_action


#1

I’m trying to get my LWRP resource - which just a collection of other resources

  • to tell it’s “invoker” that it changed. The invoker does this:

ruby_block “display_changed” do
action :nothing
block do
Chef::Log.info "RESOURCE CHANGED!!!"
end
end

myresource “myresource_name” do
action :install
version "360"
notifies :create, resources(:ruby_block => “display_changed”),
:immediately
end

if, in the “myresource” provider :install action I do a direct
new_resource.updated_by_last_action(true), then the "display_changed"
ruby_block does get executed BUT before all the LWRP resources get processed.

If however I do THIS in the provider;

ruby_block "set_changed" do
	action :nothing
	block do
		new_resource.updated_by_last_action(true)
		Chef::Log.info "PROVIDER CHANGED!!!!!"
	end
end

execute "install_component" do
	command "tar --no-same-owner -xzf #{component_local_archive}"
	cwd "#{component_path}"
	user "#{component_owner}"
	group "#{component_group}"
	not_if "test -d {component_path}/bin"
	notifies :create, resources(:ruby_block => "set_changed"),

:immediately
end

ruby_block "display_changed" do
	block do
		Chef::Log.info "CHANGED =

#{new_resource.updated_by_last_action?}"
end
end

and {component_path}/bin does not exist, then “set_changed” gets executed and
display_changed displays “CHANGED = true” (i.e. business as expected) BUT the
"invoking resource" does NOT see the change.

if {component_path}/bin does exist then “set_changed” does not get executed and
display_changed displays “CHANGED = false” i.e. business as expected.

Any way out of this?


#2

Creating resources inside provider is not a best approach and thus the problems you encounter. You should look into resource definitions instead.
Also, if you want immediate notifications on a group of resources, there was a discussion on that in this mailing list last month called “Resource groups”. There were several solutions proposed.

On Nov 30, 2011, at 19:01, martin.j.bartlett@gmail.com martin.j.bartlett@gmail.com wrote:

I’m trying to get my LWRP resource - which just a collection of other resources

  • to tell it’s “invoker” that it changed. The invoker does this:

ruby_block “display_changed” do
action :nothing
block do
Chef::Log.info "RESOURCE CHANGED!!!"
end
end

myresource “myresource_name” do
action :install
version "360"
notifies :create, resources(:ruby_block => “display_changed”),
:immediately
end

if, in the “myresource” provider :install action I do a direct
new_resource.updated_by_last_action(true), then the "display_changed"
ruby_block does get executed BUT before all the LWRP resources get processed.

If however I do THIS in the provider;

ruby_block “set_changed” do
action :nothing
block do
new_resource.updated_by_last_action(true)
Chef::Log.info "PROVIDER CHANGED!!!"
end
end

execute “install_component” do
command "tar --no-same-owner -xzf #{component_local_archive}"
cwd "#{component_path}"
user "#{component_owner}"
group "#{component_group}"
not_if "test -d {component_path}/bin"
notifies :create, resources(:ruby_block => “set_changed”),
:immediately
end

ruby_block “display_changed” do
block do
Chef::Log.info "CHANGED =
#{new_resource.updated_by_last_action?}"
end
end

and {component_path}/bin does not exist, then “set_changed” gets executed and
display_changed displays “CHANGED = true” (i.e. business as expected) BUT the
"invoking resource" does NOT see the change.

if {component_path}/bin does exist then “set_changed” does not get executed and
display_changed displays “CHANGED = false” i.e. business as expected.

Any way out of this?