summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-10-06 17:18:30 -0500
committerLuke Kanies <luke@madstop.com>2007-10-06 17:18:30 -0500
commitfc9c850414baff17dc97b0184f34e58b4bec5785 (patch)
tree5a39e0be8cbbdbef3aaf1b06d7d71d835a4e4d21 /lib
parentcdaad286b1fe5fc3c1ab363c890bb6a8a752c9b5 (diff)
downloadpuppet-fc9c850414baff17dc97b0184f34e58b4bec5785.tar.gz
puppet-fc9c850414baff17dc97b0184f34e58b4bec5785.tar.xz
puppet-fc9c850414baff17dc97b0184f34e58b4bec5785.zip
Adding support for versions and freshness-checking
to the indirection layers. This should hopefully enable the different application models we need in our different executables.
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/indirector.rb4
-rw-r--r--lib/puppet/indirector/indirection.rb21
-rw-r--r--lib/puppet/indirector/terminus.rb37
3 files changed, 50 insertions, 12 deletions
diff --git a/lib/puppet/indirector.rb b/lib/puppet/indirector.rb
index 7ceaa43e2..a2eb41763 100644
--- a/lib/puppet/indirector.rb
+++ b/lib/puppet/indirector.rb
@@ -62,6 +62,10 @@ module Puppet::Indirector
end
module InstanceMethods
+ # Make it easy for the model to set versions,
+ # which are used for caching and such.
+ attr_accessor :version
+
# these become instance methods
def save(*args)
self.class.indirection.save(self, *args)
diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb
index 3a6284877..cd71c4d66 100644
--- a/lib/puppet/indirector/indirection.rb
+++ b/lib/puppet/indirector/indirection.rb
@@ -90,12 +90,18 @@ class Puppet::Indirector::Indirection
end
end
- def find(*args)
- terminus.find(*args)
+ def find(key, *args)
+ if cache? and terminus.fresh?(key, cache.version(key))
+ return cache.find(key, *args)
+ end
+ if result = terminus.find(key, *args)
+ result.version ||= Time.now.utc
+ cache.save(result, *args) if cache?
+ return result
+ end
end
def destroy(*args)
- cache.destroy(*args) if cache?
terminus.destroy(*args)
end
@@ -104,9 +110,12 @@ class Puppet::Indirector::Indirection
end
# these become instance methods
- def save(*args)
- cache.save(*args) if cache?
- terminus.save(*args)
+ def save(instance, *args)
+ instance.version ||= Time.now.utc
+ dest = cache? ? cache : terminus
+ return if dest.fresh?(instance.name, instance.version)
+ cache.save(instance, *args) if cache?
+ terminus.save(instance, *args)
end
private
diff --git a/lib/puppet/indirector/terminus.rb b/lib/puppet/indirector/terminus.rb
index 3e0ea447c..5aeb4c6d7 100644
--- a/lib/puppet/indirector/terminus.rb
+++ b/lib/puppet/indirector/terminus.rb
@@ -95,25 +95,50 @@ class Puppet::Indirector::Terminus
end
end
+ # Is this instance fresh? Meaning, is the version we have equivalent or better
+ # than the version being asked about
+ def fresh?(key, vers)
+ raise Puppet::DevError.new("Cannot check update status when no 'version' method is defined") unless respond_to?(:version)
+
+ if existing_version = version(key)
+ existing_version >= vers
+ else
+ false
+ end
+ end
+
+ def indirection
+ self.class.indirection
+ end
+
def initialize
if self.class.abstract_terminus?
raise Puppet::DevError, "Cannot create instances of abstract terminus types"
end
end
- def terminus_type
- self.class.terminus_type
+ def model
+ self.class.model
end
def name
self.class.name
end
- def model
- self.class.model
+ def terminus_type
+ self.class.terminus_type
end
- def indirection
- self.class.indirection
+ # Provide a default method for retrieving an instance's version.
+ # By default, just find the resource and get its version. Individual
+ # terminus types can override this method to provide custom definitions of
+ # 'versions'.
+ def version(name)
+ raise Puppet::DevError.new("Cannot retrieve an instance's version without a :find method") unless respond_to?(:find)
+ if instance = find(name)
+ instance.version
+ else
+ nil
+ end
end
end