diff options
author | Luke Kanies <luke@madstop.com> | 2007-10-22 22:33:06 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2007-10-22 22:33:06 -0500 |
commit | 7fa99b08f9aa3777fba82f24eb5bb391f3758f48 (patch) | |
tree | a9dc23c0cdf3ecbc4a177086a5c699ad4f3b00f2 /lib/puppet | |
parent | 688fcdf11a685dfda297beff50de8d4c751494d9 (diff) | |
download | puppet-7fa99b08f9aa3777fba82f24eb5bb391f3758f48.tar.gz puppet-7fa99b08f9aa3777fba82f24eb5bb391f3758f48.tar.xz puppet-7fa99b08f9aa3777fba82f24eb5bb391f3758f48.zip |
Link handling is now in the file serving classes.
This was done by putting all of the functionality in the
Content and Metadata class (actually, in a new base class
for them).
There are still some issues, and there need to be integration
tests between the :local (soon to be renamed :file) termini for
these classes.
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/file_serving/content.rb | 18 | ||||
-rw-r--r-- | lib/puppet/file_serving/file_base.rb | 46 | ||||
-rw-r--r-- | lib/puppet/file_serving/metadata.rb | 35 | ||||
-rw-r--r-- | lib/puppet/indirector/file_content/local.rb | 2 | ||||
-rw-r--r-- | lib/puppet/indirector/file_metadata/local.rb | 8 | ||||
-rw-r--r-- | lib/puppet/indirector/file_server.rb | 2 | ||||
-rw-r--r-- | lib/puppet/indirector/module_files.rb | 2 |
7 files changed, 81 insertions, 32 deletions
diff --git a/lib/puppet/file_serving/content.rb b/lib/puppet/file_serving/content.rb index 38ca80fb0..3cb428e63 100644 --- a/lib/puppet/file_serving/content.rb +++ b/lib/puppet/file_serving/content.rb @@ -4,30 +4,28 @@ require 'puppet/indirector' require 'puppet/file_serving' +require 'puppet/file_serving/file_base' 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 +class Puppet::FileServing::Content < Puppet::FileServing::FileBase extend Puppet::Indirector indirects :file_content, :extend => Puppet::FileServing::TerminusSelector attr_reader :path - def content - ::File.read(@path) - end - - def initialize(path) - raise ArgumentError.new("Files must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/ - raise ArgumentError.new("Files must exist") unless FileTest.exists?(path) + # Read the content of our file in. + def content(base = nil) + # This stat can raise an exception, too. + raise(ArgumentError, "Cannot read the contents of links unless following links") if stat(base).ftype == "symlink" - @path = path + ::File.read(full_path(base)) end # Just return the file contents as the yaml. This allows us to - # avoid escaping or any such thing. LAK:FIXME Not really sure how + # avoid escaping or any such thing. LAK:NOTE Not really sure how # this will behave if the file contains yaml... I think the far # side needs to understand that it's a plain string. def to_yaml diff --git a/lib/puppet/file_serving/file_base.rb b/lib/puppet/file_serving/file_base.rb new file mode 100644 index 000000000..b2e9a0656 --- /dev/null +++ b/lib/puppet/file_serving/file_base.rb @@ -0,0 +1,46 @@ +# +# Created by Luke Kanies on 2007-10-22. +# Copyright (c) 2007. All rights reserved. + +require 'puppet/file_serving' + +# The base class for Content and Metadata; provides common +# functionality like the behaviour around links. +class Puppet::FileServing::FileBase + attr_accessor :path, :base_path + + def full_path(base = nil) + base ||= base_path || raise(ArgumentError, "You must set or provide a base path") + + full = File.join(base, self.path) + end + + def initialize(path, options = {}) + raise ArgumentError.new("Files must not be fully qualified") if path =~ /^#{::File::SEPARATOR}/ + + @path = path + @links = :manage + + options.each do |param, value| + begin + send param.to_s + "=", value + rescue NoMethodError + raise ArgumentError, "Invalid option %s for %s" % [param, self.class] + end + end + end + + attr_reader :links + def links=(value) + raise(ArgumentError, ":links can only be set to :manage or :follow") unless [:manage, :follow].include?(value) + @links = value + end + + # Stat our file, using the appropriate link-sensitive method. + def stat(base = nil) + unless defined?(@stat_method) + @stat_method = self.links == :manage ? :lstat : :stat + end + File.send(@stat_method, full_path(base)) + end +end diff --git a/lib/puppet/file_serving/metadata.rb b/lib/puppet/file_serving/metadata.rb index 7adb66981..62ebccca9 100644 --- a/lib/puppet/file_serving/metadata.rb +++ b/lib/puppet/file_serving/metadata.rb @@ -5,17 +5,18 @@ require 'puppet' require 'puppet/indirector' require 'puppet/file_serving' +require 'puppet/file_serving/file_base' require 'puppet/util/checksums' require 'puppet/file_serving/terminus_selector' # A class that handles retrieving file metadata. -class Puppet::FileServing::Metadata +class Puppet::FileServing::Metadata < Puppet::FileServing::FileBase include Puppet::Util::Checksums extend Puppet::Indirector indirects :file_metadata, :extend => Puppet::FileServing::TerminusSelector - attr_reader :path, :owner, :group, :mode, :checksum_type, :checksum + attr_reader :path, :owner, :group, :mode, :checksum_type, :checksum, :ftype, :destination def checksum_type=(type) raise(ArgumentError, "Unsupported checksum type %s" % type) unless respond_to?("%s_file" % type) @@ -23,32 +24,36 @@ class Puppet::FileServing::Metadata @checksum_type = type end - def get_attributes - stat = File.stat(path) + # Retrieve the attributes for this file, relative to a base directory. + # Note that File.stat raises Errno::ENOENT if the file is absent and this + # method does not catch that exception. + def collect_attributes(base = nil) + real_path = full_path(base) + stat = stat(base) @owner = stat.uid @group = stat.gid + @ftype = stat.ftype + # Set the octal mode, but as a string. @mode = "%o" % (stat.mode & 007777) - @checksum = get_checksum - end - - def initialize(path = nil) - if path - raise ArgumentError.new("Files must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/ - raise ArgumentError.new("Files must exist") unless FileTest.exists?(path) - - @path = path + if stat.ftype == "symlink" + @destination = File.readlink(real_path) + else + @checksum = get_checksum(real_path) end + end + def initialize(*args) @checksum_type = "md5" + super end private # Retrieve our checksum. - def get_checksum - ("{%s}" % @checksum_type) + send("%s_file" % @checksum_type, @path) + def get_checksum(path) + ("{%s}" % @checksum_type) + send("%s_file" % @checksum_type, path) end end diff --git a/lib/puppet/indirector/file_content/local.rb b/lib/puppet/indirector/file_content/local.rb index c2262c82d..a9c45d59e 100644 --- a/lib/puppet/indirector/file_content/local.rb +++ b/lib/puppet/indirector/file_content/local.rb @@ -17,7 +17,7 @@ class Puppet::Indirector::FileContent::Local < Puppet::Indirector::File def find(key, options = {}) uri = key2uri(key) return nil unless FileTest.exists?(uri.path) - model.new(uri.path) + model.new(uri.path, :links => options[:links]) end def search(key, options = {}) diff --git a/lib/puppet/indirector/file_metadata/local.rb b/lib/puppet/indirector/file_metadata/local.rb index 86e1172f9..d696bc769 100644 --- a/lib/puppet/indirector/file_metadata/local.rb +++ b/lib/puppet/indirector/file_metadata/local.rb @@ -14,12 +14,12 @@ class Puppet::Indirector::FileMetadata::Local < Puppet::Indirector::Code include Puppet::Util::URIHelper include Puppet::FileServing::TerminusHelper - def find(key) + def find(key, options = {}) uri = key2uri(key) return nil unless FileTest.exists?(uri.path) - data = model.new(uri.path) - data.get_attributes + data = model.new(uri.path, :links => options[:links]) + data.collect_attributes return data end @@ -27,6 +27,6 @@ class Puppet::Indirector::FileMetadata::Local < Puppet::Indirector::Code def search(key, options = {}) uri = key2uri(key) return nil unless FileTest.exists?(uri.path) - path2instances(uri.path, options).each { |instance| instance.get_attributes } + path2instances(uri.path, options).each { |instance| instance.collect_attributes } end end diff --git a/lib/puppet/indirector/file_server.rb b/lib/puppet/indirector/file_server.rb index 46b0c40f2..de88bdc18 100644 --- a/lib/puppet/indirector/file_server.rb +++ b/lib/puppet/indirector/file_server.rb @@ -25,7 +25,7 @@ class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus # Find our key using the fileserver. def find(key, options = {}) return nil unless path = find_path(key, options) - return model.new(path) + return model.new(path, :links => options[:links]) end # Search for files. This returns an array rather than a single diff --git a/lib/puppet/indirector/module_files.rb b/lib/puppet/indirector/module_files.rb index 815da2efe..12794e4c7 100644 --- a/lib/puppet/indirector/module_files.rb +++ b/lib/puppet/indirector/module_files.rb @@ -30,7 +30,7 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus def find(key, options = {}) return nil unless path = find_path(key, options) - return model.new(path) + return model.new(path, :links => options[:links]) end # Try to find our module. |