# Adding a not\_if guard to a wrapped resource

**URL:** <https://discourse.chef.io/t/adding-a-not-if-guard-to-a-wrapped-resource/9825>\
**Category:** Chef Infra (archive)\
**Created:** [November 5, 2016, 8:31pm UTC](https://discourse.chef.io/t/adding-a-not-if-guard-to-a-wrapped-resource/9825 "2016-11-05T20:31:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_Hammerman1](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Joseph\_Hammerman1](https://discourse.chef.io/u/Joseph_Hammerman1)\
**Post date:** [November 5, 2016, 8:31pm UTC](https://discourse.chef.io/t/adding-a-not-if-guard-to-a-wrapped-resource/9825/1 "2016-11-05T20:31:30Z")

</div>

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:

begin  
s = resources(bash: “compile\_package\_source”)  
s.environment ({ ‘GOPATH’ =\> “#{node[‘package’][‘gopath’]}” })  
s.cwd “#{node[‘package’][‘gopath’]}/src/github.com/package/package"  
s.not\_if “{ ::File.exists?(”#{node[‘package’][‘dir’]}/package\_binary\_name}”)"  
rescue Chef::Exceptions::ResourceNotFound  
puts "Bash Prometheus compile resource not found!"  
end

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’]}  
EOH

notifies :restart, 'service[package\_service\_name]'  
end

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

---

<div class="post-metadata">

**Author:** ![ameir](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ameir](https://discourse.chef.io/u/ameir)\
**Post date:** [November 5, 2016, 8:44pm UTC](https://discourse.chef.io/t/adding-a-not-if-guard-to-a-wrapped-resource/9825/2 "2016-11-05T20:44:45Z")

</div>

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 like`creates "#{node['package']['dir']}/package_binary_name"` in your bash resource.  
Hope that helps,Ameir

---

<div class="post-metadata">

**Author:** ![kkeane](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@kkeane](https://discourse.chef.io/u/kkeane)\
**Post date:** [November 5, 2016, 10:14pm UTC](https://discourse.chef.io/t/adding-a-not-if-guard-to-a-wrapped-resource/9825/3 "2016-11-05T22:14:06Z")

</div>

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  
[http://www.4nettech.com](http://www.4nettech.com)  
Our values: Privacy, Liberty, Justice  
See [https://www.4nettech.com/corp/the-nettech-values.html](https://www.4nettech.com/corp/the-nettech-values.html)

---

<div class="post-metadata">

**Author:** ![Joseph\_Hammerman1](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Joseph\_Hammerman1](https://discourse.chef.io/u/Joseph_Hammerman1)\
**Post date:** [November 7, 2016, 7:01pm UTC](https://discourse.chef.io/t/adding-a-not-if-guard-to-a-wrapped-resource/9825/4 "2016-11-07T19:01:16Z")

</div>

This worked. Thanks!  
Joe
