Search code examples
chef-infralwrp

chef LWRP remote_file_notification


I have an LWRP that downloads a file as part of its step, and I want to use that to indicate if the resource has changed

action: install do

  # some other stuff here

  remote_file "/some/file" do
    source node[:mycookbook][:source_file]
    mode 00755
    action :create
    notifies :run, 'ruby_block[set_status]', :immediately
  end

  ruby_block 'set_status' do
    block do
      new_resource.updated_by_last_action(true)
    end
  end
end

And in my cookbook my I have:

 my_provider do
    # configure
    notifies :run, 'something_else', :immediately
 end

It doesn't seem to matter if the remote_file runs or not, something_else is not notified, but I'm not sure why.


Solution

  • I'm not sure you can delay new_resource.updated_by_last_action using a ruby_block (you're trying to run it outside the execution of your provider?). Since your provider action is running at converge time already, I wouldn't normally use a ruby block here. I'd do something like:

    action: install do
    
      # some other stuff here
    
      some_file = remote_file "/some/file" do
        source node[:mycookbook][:source_file]
        mode 00755
        action :nothing
        notifies :run, 'ruby_block[set_status]', :immediately
      end
      some_file.run_action(:create)
      new_resource.updated_by_last_action(true) if some_file.updated_by_last_action?
    
    end
    

    The other benefit of calling run_action on remote_file immediately is that you're no longer using the DSL to create and add the remote_file resource to the resource collection, and then waiting for chef to converge it at some future time (and then waiting for your ruby_block after that). You're converging the resource you care about right then & there, and checking to see if it was changed (and updating your custom resource accordingly).