diff options
author | John Keiser <john@johnkeiser.com> | 2015-07-28 15:06:19 -0600 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-07-31 12:42:28 -0600 |
commit | 6aa94e70cde907e9c890307a82fb50af3d46f9df (patch) | |
tree | b503be312a5e3c86500719ead2ecc52eb0d30941 | |
parent | 9ea798f020edb2ab1de36ef2bf88e030274c7e99 (diff) | |
download | chef-6aa94e70cde907e9c890307a82fb50af3d46f9df.tar.gz |
Move converge_if_changed to Chef::Provider proper
-rw-r--r-- | lib/chef/provider.rb | 51 | ||||
-rw-r--r-- | lib/chef/resource/action_provider.rb | 51 |
2 files changed, 51 insertions, 51 deletions
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index 089c28be50..8c51acb71f 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -175,6 +175,57 @@ class Chef converge_actions.add_action(descriptions, &block) end + # + # Handle patchy convergence safely. + # + # - Does *not* call the block if the current_resource's properties match + # the properties the user specified on the resource. + # - Calls the block if current_resource does not exist + # - Calls the block if the user has specified any properties in the resource + # whose values are *different* from current_resource. + # - Does *not* call the block if why-run is enabled (just prints out text). + # - Prints out automatic green text saying what properties have changed. + # + # @param properties An optional list of property names (symbols). If not + # specified, `new_resource.class.state_properties` will be used. + # @param converge_block The block to do the converging in. + # + # @return [Boolean] whether the block was executed. + # + def converge_if_changed(*properties, &converge_block) + properties = new_resource.class.state_properties.map { |p| p.name } if properties.empty? + properties = properties.map { |p| p.to_sym } + if current_resource + # Collect the list of modified properties + specified_properties = properties.select { |property| new_resource.property_is_set?(property) } + modified = specified_properties.select { |p| new_resource.send(p) != current_resource.send(p) } + if modified.empty? + Chef::Log.debug("Skipping update of #{new_resource.to_s}: has not changed any of the specified properties #{specified_properties.map { |p| "#{p}=#{new_resource.send(p).inspect}" }.join(", ")}.") + return false + end + + # Print the pretty green text and run the block + property_size = modified.map { |p| p.size }.max + modified = modified.map { |p| " set #{p.to_s.ljust(property_size)} to #{new_resource.send(p).inspect} (was #{current_resource.send(p).inspect})" } + converge_by([ "update #{new_resource.to_s}" ] + modified, &converge_block) + + else + # The resource doesn't exist. Mark that we are *creating* this, and + # write down any properties we are setting. + created = [] + properties.each do |property| + if new_resource.property_is_set?(property) + created << " set #{property.to_s.ljust(property_size)} to #{new_resource.send(property).inspect}" + else + created << " default #{property.to_s.ljust(property_size)} to #{new_resource.send(property).inspect}" + end + end + + converge_by([ "create #{new_resource.to_s}" ] + created, &converge_block) + end + true + end + def self.provides(short_name, opts={}, &block) Chef.provider_handler_map.set(short_name, self, opts, &block) end diff --git a/lib/chef/resource/action_provider.rb b/lib/chef/resource/action_provider.rb index 544f7c1285..c756a81b6f 100644 --- a/lib/chef/resource/action_provider.rb +++ b/lib/chef/resource/action_provider.rb @@ -47,57 +47,6 @@ class Chef @current_resource = current_resource end - # - # Handle patchy convergence safely. - # - # - Does *not* call the block if the current_resource's properties match - # the properties the user specified on the resource. - # - Calls the block if current_resource does not exist - # - Calls the block if the user has specified any properties in the resource - # whose values are *different* from current_resource. - # - Does *not* call the block if why-run is enabled (just prints out text). - # - Prints out automatic green text saying what properties have changed. - # - # @param properties An optional list of property names (symbols). If not - # specified, `new_resource.class.state_properties` will be used. - # @param converge_block The block to do the converging in. - # - # @return [Boolean] whether the block was executed. - # - def converge_if_changed(*properties, &converge_block) - properties = new_resource.class.state_properties.map { |p| p.name } if properties.empty? - properties = properties.map { |p| p.to_sym } - if current_resource - # Collect the list of modified properties - specified_properties = properties.select { |property| new_resource.property_is_set?(property) } - modified = specified_properties.select { |p| new_resource.send(p) != current_resource.send(p) } - if modified.empty? - Chef::Log.debug("Skipping update of #{new_resource.to_s}: has not changed any of the specified properties #{specified_properties.map { |p| "#{p}=#{new_resource.send(p).inspect}" }.join(", ")}.") - return false - end - - # Print the pretty green text and run the block - property_size = modified.map { |p| p.size }.max - modified = modified.map { |p| " set #{p.to_s.ljust(property_size)} to #{new_resource.send(p).inspect} (was #{current_resource.send(p).inspect})" } - converge_by([ "update #{new_resource.to_s}" ] + modified, &converge_block) - - else - # The resource doesn't exist. Mark that we are *creating* this, and - # write down any properties we are setting. - created = [] - properties.each do |property| - if new_resource.property_is_set?(property) - created << " set #{property.to_s.ljust(property_size)} to #{new_resource.send(property).inspect}" - else - created << " default #{property.to_s.ljust(property_size)} to #{new_resource.send(property).inspect}" - end - end - - converge_by([ "create #{new_resource.to_s}" ] + created, &converge_block) - end - true - end - def self.included(other) other.extend(ClassMethods) other.use_inline_resources |