diff options
| author | Luke Kanies <luke@madstop.com> | 2007-09-21 15:15:40 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-09-21 15:15:40 -0500 |
| commit | da0555d948b837e7c16bdca164780c9e71353e4a (patch) | |
| tree | d4c8c995a99246097f198be4015249c1a8305595 /lib | |
| parent | 0a48e5f5bf5885353edc20f020ae27eb682176f7 (diff) | |
Adding the first top-level terminus (yaml). It works
and is tested, so now it is time to migrate the
Facts YAML Terminus to use the <terminus>/<indirection>
file structure instead of <indirection>/<terminus>.
In this case, that means that I am moving the functionality
in lib/puppet/indirector/facts/yaml.rb to
lib/puppet/indirector/yaml/facts.rb, and the class in that
new file will subclass Puppet::Indirector::Yaml.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/puppet/defaults.rb | 6 | ||||
| -rw-r--r-- | lib/puppet/indirector/indirection.rb | 6 | ||||
| -rw-r--r-- | lib/puppet/indirector/terminus.rb | 21 | ||||
| -rw-r--r-- | lib/puppet/indirector/yaml.rb | 47 |
4 files changed, 76 insertions, 4 deletions
diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index 4b442d094..fa119bb5a 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -499,9 +499,9 @@ module Puppet "The backend store to use for client facts."] ) - self.setdefaults(:yamlfacts, - :yamlfactdir => ["$vardir/facts", - "The directory in which client facts are stored when the yaml fact store is used."] + self.setdefaults(:yaml, + :yamldir => ["$vardir/yaml", + "The directory in which YAML data is stored, usually in a subdirectory."] ) self.setdefaults(:rails, diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 7128346ac..0cf8c7a27 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -6,6 +6,12 @@ class Puppet::Indirector::Indirection def self.clear_cache @@indirections.each { |ind| ind.clear_cache } end + + # Find an indirection by name. This is provided so that Terminus classes + # can specifically hook up with the indirections they are associated with. + def self.instance(name) + @@indirections.find { |i| i.name == name } + end attr_accessor :name, :termini diff --git a/lib/puppet/indirector/terminus.rb b/lib/puppet/indirector/terminus.rb index d311eb60b..4d550e9ba 100644 --- a/lib/puppet/indirector/terminus.rb +++ b/lib/puppet/indirector/terminus.rb @@ -1,4 +1,5 @@ require 'puppet/indirector' +require 'puppet/indirector/indirection' # A simple class that can function as the base class for indirected types. class Puppet::Indirector::Terminus @@ -6,7 +7,25 @@ class Puppet::Indirector::Terminus extend Puppet::Util::Docs class << self - attr_accessor :name, :indirection + attr_accessor :name + attr_reader :indirection + + # Look up the indirection if we were only provided a name. + def indirection=(name) + if name.is_a?(Puppet::Indirector::Indirection) + @indirection = name + elsif ind = Puppet::Indirector::Indirection.instance(name) + @indirection = ind + else + raise ArgumentError, "Could not find indirection instance %s" % name + end + end + end + + def initialize + unless indirection + raise Puppet::DevError, "Indirection termini cannot be used without an associated indirection" + end end def name diff --git a/lib/puppet/indirector/yaml.rb b/lib/puppet/indirector/yaml.rb new file mode 100644 index 000000000..f7f7d290f --- /dev/null +++ b/lib/puppet/indirector/yaml.rb @@ -0,0 +1,47 @@ +require 'puppet/indirector/terminus' + +# The base class for YAML indirection termini. +class Puppet::Indirector::Yaml < Puppet::Indirector::Terminus + def initialize + super + + # Make sure our base directory exists. + Puppet.config.use(:yaml) + end + + # Read a given name's file in and convert it from YAML. + def find(name) + raise ArgumentError.new("You must specify the name of the object to retrieve") unless name + file = path(name) + return nil unless FileTest.exist?(file) + + begin + return YAML.load(File.read(file)) + rescue => detail + raise Puppet::Error, "Could not read YAML data for %s(%s): %s" % [indirection.name, name, detail] + end + end + + # Convert our object to YAML and store it to the disk. + def save(object) + raise ArgumentError.new("You can only save objects that respond to :name") unless object.respond_to?(:name) + + file = path(object.name) + + basedir = File.dirname(file) + + # This is quite likely a bad idea, since we're not managing ownership or modes. + unless FileTest.exist?(basedir) + Dir.mkdir(basedir) + end + + File.open(file, "w", 0660) { |f| f.print YAML.dump(object) } + end + + private + + # Return the path to a given node's file. + def path(name) + File.join(Puppet[:yamldir], indirection.name.to_s, name.to_s + ".yaml") + end +end |
