diff options
| author | Luke Kanies <luke@madstop.com> | 2007-09-22 20:00:15 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-09-22 20:00:15 -0500 |
| commit | 84146d00eed4765e6dbe05dd8de9f4c3625463b7 (patch) | |
| tree | 56bbe17f2c596478e646207f132dc3bce4d4d492 /lib | |
| parent | 3a18348fdbea39f56857b03c8f531bd5a2a8105d (diff) | |
Adding the first version of checksum support, which will
acquire the behaviour of FileBuckets.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/puppet/checksum.rb | 37 | ||||
| -rw-r--r-- | lib/puppet/indirector/file.rb | 34 | ||||
| -rw-r--r-- | lib/puppet/indirector/file/checksum.rb | 33 |
3 files changed, 95 insertions, 9 deletions
diff --git a/lib/puppet/checksum.rb b/lib/puppet/checksum.rb new file mode 100644 index 000000000..65fb7ef76 --- /dev/null +++ b/lib/puppet/checksum.rb @@ -0,0 +1,37 @@ +# +# Created by Luke Kanies on 2007-9-22. +# Copyright (c) 2007. All rights reserved. + +require 'puppet' +require 'puppet/indirector' + +# A checksum class to model translating checksums to file paths. This +# is the new filebucket. +class Puppet::Checksum + extend Puppet::Indirector + + indirects :checksum + + attr_accessor :name, :content + attr_reader :algorithm + + def algorithm=(value) + value = value.intern if value.respond_to?(:intern) + @algorithm = value + end + + def initialize(name) + raise ArgumentError.new("You must specify the checksum") unless name + + if name =~ /^\{(\w+)\}(.+$)$/ + @algorithm, @name = $1.intern, $2 + else + @name = name + @algorithm = :md5 + end + end + + def to_s + "Checksum<{%s}%s>" % [algorithm, name] + end +end diff --git a/lib/puppet/indirector/file.rb b/lib/puppet/indirector/file.rb index 75fcf1ddf..4f231e9ec 100644 --- a/lib/puppet/indirector/file.rb +++ b/lib/puppet/indirector/file.rb @@ -3,16 +3,27 @@ require 'puppet/indirector/terminus' # An empty terminus type, meant to just return empty objects. class Puppet::Indirector::File < Puppet::Indirector::Terminus def destroy(file) - raise Puppet::Error.new("File %s does not exist; cannot destroy" % [file.name]) unless File.exist?(file.path) + if respond_to?(:path) + path = path(file.name) + else + path = file.path + end + raise Puppet::Error.new("File %s does not exist; cannot destroy" % [file]) unless File.exist?(path) begin - File.unlink(file.path) + File.unlink(path) rescue => detail - raise Puppet::Error, "Could not remove %s: %s" % [file.path, detail] + raise Puppet::Error, "Could not remove %s: %s" % [file, detail] end end - def find(path) + def find(name) + if respond_to?(:path) + path = path(name) + else + path = name + end + return nil unless File.exist?(path) begin @@ -21,20 +32,25 @@ class Puppet::Indirector::File < Puppet::Indirector::Terminus raise Puppet::Error, "Could not retrieve path %s: %s" % [path, detail] end - file = model.new(path) + file = model.new(name) file.content = content return file end def save(file) - dir = File.dirname(file.path) + if respond_to?(:path) + path = path(file.name) + else + path = file.path + end + dir = File.dirname(path) - raise Puppet::Error.new("Cannot save %s; parent directory %s does not exist" % [file.name, dir]) unless File.directory?(dir) + raise Puppet::Error.new("Cannot save %s; parent directory %s does not exist" % [file, dir]) unless File.directory?(dir) begin - File.open(file.path, "w") { |f| f.print file.content } + File.open(path, "w") { |f| f.print file.content } rescue => detail - raise Puppet::Error, "Could not write %s: %s" % [file.path, detail] + raise Puppet::Error, "Could not write %s: %s" % [file, detail] end end end diff --git a/lib/puppet/indirector/file/checksum.rb b/lib/puppet/indirector/file/checksum.rb new file mode 100644 index 000000000..2f0974ced --- /dev/null +++ b/lib/puppet/indirector/file/checksum.rb @@ -0,0 +1,33 @@ +require 'puppet/checksum' +require 'puppet/indirector/file' + +class Puppet::Indirector::File::Checksum < Puppet::Indirector::File + desc "Store files in a directory set based on their checksums." + + def initialize + Puppet.settings.use(:filebucket) + end + + def path(checksum) + path = [] + path << Puppet[:bucketdir] # Start with the base directory + path << checksum[0..7].split("").join(File::SEPARATOR) # Add sets of directories based on the checksum + path << checksum # And the full checksum name itself + path << "contents" # And the actual file name + + path.join(File::SEPARATOR) + end + + def save(file) + path = File.dirname(path(file.name)) + + # Make the directories if necessary. + unless FileTest.directory?(path) + Puppet::Util.withumask(0007) do + FileUtils.mkdir_p(path) + end + end + + super + end +end |
