diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/file_serving/content.rb | 3 | ||||
-rw-r--r-- | lib/puppet/file_serving/terminus_selector.rb | 26 | ||||
-rw-r--r-- | lib/puppet/indirector.rb | 12 | ||||
-rw-r--r-- | lib/puppet/indirector/file_content/file.rb | 3 | ||||
-rw-r--r-- | lib/puppet/indirector/indirection.rb | 35 |
5 files changed, 60 insertions, 19 deletions
diff --git a/lib/puppet/file_serving/content.rb b/lib/puppet/file_serving/content.rb index 216072e49..53e52cd2d 100644 --- a/lib/puppet/file_serving/content.rb +++ b/lib/puppet/file_serving/content.rb @@ -4,13 +4,14 @@ require 'puppet/indirector' require 'puppet/file_serving' +require 'puppet/file_serving/terminus_selector' # A class that handles retrieving file contents. # It only reads the file when its content is specifically # asked for. class Puppet::FileServing::Content extend Puppet::Indirector - indirects :file_content, :terminus_class => :file + indirects :file_content, :terminus_class => :file, :extend => Puppet::FileServing::TerminusSelector attr_reader :path diff --git a/lib/puppet/file_serving/terminus_selector.rb b/lib/puppet/file_serving/terminus_selector.rb new file mode 100644 index 000000000..0cec4bf98 --- /dev/null +++ b/lib/puppet/file_serving/terminus_selector.rb @@ -0,0 +1,26 @@ +# +# Created by Luke Kanies on 2007-10-18. +# Copyright (c) 2007. All rights reserved. + +require 'uri' +require 'puppet/file_serving' + +# This module is used to pick the appropriate terminus +# in file-serving indirections. This is necessary because +# the terminus varies based on the URI asked for. +module Puppet::FileServing::TerminusSelector + PROTOCOL_MAP = {"puppet" => :rest, "file" => :local} + + # Pick an appropriate terminus based on the protocol. + def select_terminus(uri) + # Short-circuit to :local if it's a fully-qualified path. + return PROTOCOL_MAP["file"] if uri =~ /^#{::File::SEPARATOR}/ + begin + uri = URI.parse(URI.escape(uri)) + rescue => detail + raise ArgumentError, "Could not understand URI %s: %s" % [uri, detail.to_s] + end + + return PROTOCOL_MAP[uri.scheme] || raise(ArgumentError, "URI protocol '%s' is not supported for file serving" % uri.scheme) + end +end diff --git a/lib/puppet/indirector.rb b/lib/puppet/indirector.rb index 6009a7ba7..c30c097b2 100644 --- a/lib/puppet/indirector.rb +++ b/lib/puppet/indirector.rb @@ -23,17 +23,7 @@ module Puppet::Indirector # instantiate the actual Terminus for that type and this name (:ldap, w/ args :node) # & hook the instantiated Terminus into this class (Node: @indirection = terminus) - @indirection = Puppet::Indirector::Indirection.new(self, indirection) - - unless options.empty? - options.each do |param, value| - case param - when :terminus_class: @indirection.terminus_class = value - else - raise ArgumenError, "Invalid option '%s' to 'indirects'" % param - end - end - end + @indirection = Puppet::Indirector::Indirection.new(self, indirection, options) @indirection end diff --git a/lib/puppet/indirector/file_content/file.rb b/lib/puppet/indirector/file_content/file.rb index 2723142af..bb9dc6998 100644 --- a/lib/puppet/indirector/file_content/file.rb +++ b/lib/puppet/indirector/file_content/file.rb @@ -8,4 +8,7 @@ require 'puppet/indirector/file' class Puppet::Indirector::FileContent::File < Puppet::Indirector::File desc "Retrieve file contents from disk." + + def find(path) + end end diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 0d814c5ef..313117b25 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -48,6 +48,19 @@ class Puppet::Indirector::Indirection def initialize(model, name, options = {}) @model = model @name = name + + @termini = {} + @cache_class = nil + + raise(ArgumentError, "Indirection %s is already defined" % @name) if @@indirections.find { |i| i.name == @name } + @@indirections << self + + if mod = options[:extend] + extend(mod) + options.delete(:extend) + end + + # This is currently only used for cache_class and terminus_class. options.each do |name, value| begin send(name.to_s + "=", value) @@ -55,11 +68,6 @@ class Puppet::Indirector::Indirection raise ArgumentError, "%s is not a valid Indirection parameter" % name end end - @termini = {} - @terminus_types = {} - @cache_class = nil - raise(ArgumentError, "Indirection %s is already defined" % @name) if @@indirections.find { |i| i.name == @name } - @@indirections << self end # Return the singleton terminus for this indirection. @@ -91,11 +99,24 @@ class Puppet::Indirector::Indirection end def find(key, *args) - if cache? and cache.has_most_recent?(key, terminus.version(key)) + # Select the appropriate terminus if there's a hook + # for doing so. This allows the caller to pass in some kind + # of URI that the indirection can use for routing to the appropriate + # terminus. + if respond_to?(:select_terminus) + terminus_name = select_terminus(key) + else + terminus_name = terminus_class + end + + # See if our instance is in the cache and up to date. + if cache? and cache.has_most_recent?(key, terminus(terminus_name).version(key)) Puppet.info "Using cached %s %s" % [self.name, key] return cache.find(key, *args) end - if result = terminus.find(key, *args) + + # Otherwise, return the result from the terminus, caching if appropriate. + if result = terminus(terminus_name).find(key, *args) result.version ||= Time.now.utc if cache? Puppet.info "Caching %s %s" % [self.name, key] |