[chef] chef resources inherit not_if and only_if from previous copies


Chronological Thread 
  • From: Jeremy Deininger < >
  • To:
  • Subject: [chef] chef resources inherit not_if and only_if from previous copies
  • Date: Thu, 11 Feb 2010 16:22:30 -0800

Greetings fellow chefs,

I believe I've found a bug in the way that resources are copied from one to the next during a recipe run.  This bug only manifests itself when you are re-using a resource with the same name.  My opinion is that only_if and not_if should be used to control the flow of actions that you use on the current_resource, not inherited from previous resources.  The primary example I use is the service "mysql" resource.  Since this service is likely to be called multiple times in a recipe with different actions it can be confusing when it inherits a conditional from a prior action ..  I've filed a bug
here: http://tickets.opscode.com/browse/CHEF-894 and I'm pasting an example below that's taken from the Rightscale public cookbooks for db_mysql.

Do the maintainers approve this change?  Any alternatives or suggestions?  Right now I have to be really careful with my not/only_ifs and override the un-wanted copies to get the behavior I need.. 

=== SNIP from http://github.com/rightscale/cookbooks_public/blob/master/cookbooks/db_mysql/recipes/install_mysql.rb#L125 ===>
service "mysql" do
  only_if do
    right_platform = node[:platform] == "ubuntu" &&
                    (node[:platform_version] == "8.04" ||
                     node[:platform_version] == "8.10")
 
    right_platform && node[:db_mysql][:kill_bug_mysqld_safe]
  end
 
  action :stop
end
 
ruby_block "fix buggy mysqld_safe" do
  only_if do
    right_platform = node[:platform] == "ubuntu" &&
                    (node[:platform_version] == "8.04" ||
                     node[:platform_version] == "8.10")
 
    right_platform && node[:db_mysql][:kill_bug_mysqld_safe]
  end
  block do
...
node[:db_mysql][:kill_bug_mysqld_safe] = false
end
end

service
"mysql" do
  # override this back to the default for future copies of the resource
  only_if do true end
  not_if do ::File.symlink?(node[:db_mysql][:datadir]) end
  action :stop
end
... more recipe code ...
service "mysql" do
  # override this back to the default for future copies of the resource
  not_if do false end
  Chef::Log.info "Attempting to start mysql service"
  action :start
end





Archive powered by MHonArc 2.6.16.

§