hamdani hamdani - 1 month ago 7
Ruby Question

How to detect file update when using remote_file

I am using remote_file in a for loop to go through deployment files (specified in node attributes). I verify given checksum (sha256sum) to see whether already existing files are same or not; if not, I update them; if same, it simply skips.

I need to know , in the recipe, whether any file was updated or not so that I restart app server in case of any file changed under deployments directory.

Is there any default attribute that I can use (which tells all file updates were skipped) or alternatively what is the best approach to have it done?

Thanks,

node['app']['deployments'].each do |file, value|
remote_file "/apps/deployments/#{value['file-name']}" do
source "file:///tmp/deployments/#{value['file-name']}"
checksum value['check-sum']
end
end

Answer

There is the concept of notifications in Chef, which helps you in this case:

node['app']['deployments'].each do |file, value|  
    remote_file "/apps/deployments/#{value['file-name']}" do  
        source    "file:///tmp/deployments/#{value['file-name']}"  
        checksum  value['check-sum']
        notifies  :restart, 'service[appserver]'
    end 
end 

service "appserver"

As long as you don't supply a :immediately parameter to notifies, chef-client will queue up all notifications and execute them (aka restart the service) once at the end of the chef run.

Comments