Adding a not_if guard to a wrapped resource


Hi Chef users mail distribution list,

I am wrapping a bash resource in a compiled cookbook that recompiles from source and installs the compiled artifact regardless of whether or not the version it is compiling is already present on disk. I am trying to use the wrapper method to add a not_if guard that would prevent the behavior, a la:

s = resources(bash: “compile_package_source”)
s.environment ({ ‘GOPATH’ => “#{node[‘package’][‘gopath’]}” })
s.cwd “#{node[‘package’][‘gopath’]}/src/"
s.not_if “{ ::File.exists?(”#{node[‘package’][‘dir’]}/package_binary_name}”)"
rescue Chef::Exceptions::ResourceNotFound
puts "Bash Prometheus compile resource not found!"

The wrapped code block is of the form:

bash ‘compile_package_source’ do
cwd "#{Chef::Config[:file_cache_path]}/package-#{node[‘package’][‘version’]}"
code <<-EOH
make build &&
mv package_binary_name #{node[‘package’][‘dir’]} &&
cp -R a_dir #{node[‘package’][‘dir’]} &&
cp -R a_dir #{node[‘package’][‘dir’]}

notifies :restart, 'service[package_service_name]'

The compile is still executing on every run. Is this becuase the branching decision has already been made? The wrapped code block is present in the run_list prior to the wrapper that attempts to modify it’s behavior.

Thanks in advance for any assistance anyone can proffer me.
Joseph Hammerman


This is not a direct answer to your question, but since you are checking for file existence, it is generally better to use creates instead of not_if.
This would look likecreates "#{node['package']['dir']}/package_binary_name" in your bash resource.
Hope that helps,Ameir


It looks to me like the bash resource will exist (and execute) whether or not the wrapper executes. If I understand it correctly, the wrapper doesn’t create the resource, but clones it.

One workaround (which wouldn’t actually solve the underlying problem, but fix your symptom) might be to move the guard from the wrapper to the wrapped resource.

Kevin Keane
Whom the IT Pros Call
The NetTech
Our values: Privacy, Liberty, Justice


This worked. Thanks!