Built In Checksum


Hello all,

This is a quickie…I wrote a small guard:

not_if “[ -f #{Chef::Config[:file_cache_path]}/#{node[:deploy_war]}.war ] && diff -q #{Chef::Config[:file_cache_path]}/#{node[:deploy_war]}.war #{node[:deployed_war_file]}”

—> what would be the most portable and common way of handling that diff?

Is there s Chef diff call to use? Or a proper checksum that I should call to compare?

I thought that I found it in this, from the net:

require 'chef/checksum_cache’
sha256 = Chef::ChecksumCache.checksum_for_file("/path/to/file")

but that call threw an error…we are on 12.0.3…too old?

Well, this is an openin to a convo more than a good question. All comments appreciated.



Checksum is an internal api, and might fail on upgrades. Is it possible to use the remote_file resource? it does checksum comparison internally, but you have to provide the checksum upfront. In the example, your source file is located on the chef caching directory, what installs the file there? that resource can itself trigger the update, that way you wont need guards.
note: remote_file can also use file:/// as source. allowing you to copy a local file, in which case chef should do the checksum bits internally.


Ranjib, As it turns out, I did move to that resource, for the “auto-guard” you mentioned.

Thank you so much for the time.