From 8d5f052b08078f0f356b30fb3fed60eab4490f6d Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 19 Jan 2010 15:43:48 -0800 Subject: Adding Transaction::ResourceHarness class This is the interface class between Transactions and Resources. It's a relatively ugly class, but it will hopefully allow us to move most/all of the messy interface code into this one, relatively small class. Signed-off-by: Luke Kanies --- lib/puppet/transaction/resource_harness.rb | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 lib/puppet/transaction/resource_harness.rb (limited to 'lib/puppet') diff --git a/lib/puppet/transaction/resource_harness.rb b/lib/puppet/transaction/resource_harness.rb new file mode 100644 index 000000000..da859910c --- /dev/null +++ b/lib/puppet/transaction/resource_harness.rb @@ -0,0 +1,62 @@ +require 'puppet/resource/status' + +class Puppet::Transaction::ResourceHarness + extend Forwardable + def_delegators :@transaction, :relationship_graph + + attr_reader :transaction + + def allow_changes?(resource) + return true unless resource.purging? and resource.deleting? + return true unless deps = relationship_graph.dependents(resource) and ! deps.empty? and deps.detect { |d| ! d.deleting? } + + deplabel = deps.collect { |r| r.ref }.join(",") + plurality = deps.length > 1 ? "":"s" + resource.warning "#{deplabel} still depend#{plurality} on me -- not purging" + return false + end + + def apply_changes(status, changes) + changes.each do |change| + status << change.apply + end + status.changed = true + end + + def changes_to_perform(status, resource) + current = resource.retrieve + + if param = resource.parameter(:ensure) + insync = param.insync?(current[:ensure]) + return [Puppet::Transaction::Change.new(param, current[:ensure])] unless insync + return [] if param.should == :absent + end + + resource.properties.reject { |p| p.name == :ensure }.find_all do |param| + ! param.insync?(current[param.name]) + end.collect do |param| + Puppet::Transaction::Change.new(param, current[param.name]) + end + end + + def evaluate(resource) + status = Puppet::Resource::Status.new(resource) + + if changes = changes_to_perform(status, resource) and ! changes.empty? + status.out_of_sync = true + apply_changes(status, changes) + resource.cache(:synced, Time.now) + resource.flush if resource.respond_to?(:flush) + end + return status + rescue => detail + resource.fail "Could not create resource status: #{detail}" unless status + resource.err "Could not evaluate: #{detail}" + status.failed = true + return status + end + + def initialize(transaction) + @transaction = transaction + end +end -- cgit