summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLuke Kanies <luke@reductivelabs.com>2010-01-19 15:43:48 -0800
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commit8d5f052b08078f0f356b30fb3fed60eab4490f6d (patch)
tree1f1e45bebbbfb3b16790ddd6e72bdc2bb59f6ffb /lib
parent6651aa4fc84506b9d20076be28741516214c5d5d (diff)
downloadpuppet-8d5f052b08078f0f356b30fb3fed60eab4490f6d.tar.gz
puppet-8d5f052b08078f0f356b30fb3fed60eab4490f6d.tar.xz
puppet-8d5f052b08078f0f356b30fb3fed60eab4490f6d.zip
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 <luke@reductivelabs.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/transaction/resource_harness.rb62
1 files changed, 62 insertions, 0 deletions
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