diff options
Diffstat (limited to 'lib/puppet/indirector/file_bucket_file/file.rb')
-rw-r--r-- | lib/puppet/indirector/file_bucket_file/file.rb | 218 |
1 files changed, 109 insertions, 109 deletions
diff --git a/lib/puppet/indirector/file_bucket_file/file.rb b/lib/puppet/indirector/file_bucket_file/file.rb index ed978695e..318858aaf 100644 --- a/lib/puppet/indirector/file_bucket_file/file.rb +++ b/lib/puppet/indirector/file_bucket_file/file.rb @@ -4,143 +4,143 @@ require 'puppet/util/checksums' require 'fileutils' module Puppet::FileBucketFile - class File < Puppet::Indirector::Code - include Puppet::Util::Checksums + class File < Puppet::Indirector::Code + include Puppet::Util::Checksums - desc "Store files in a directory set based on their checksums." + desc "Store files in a directory set based on their checksums." - def initialize - Puppet.settings.use(:filebucket) - end - - def find( request ) - checksum, path = request_to_checksum_and_path( request ) - find_by_checksum( checksum, request.options ) - end + def initialize + Puppet.settings.use(:filebucket) + end - def save( request ) - checksum, path = request_to_checksum_and_path( request ) + def find( request ) + checksum, path = request_to_checksum_and_path( request ) + find_by_checksum( checksum, request.options ) + end - instance = request.instance - instance.checksum = checksum if checksum - instance.path = path if path + def save( request ) + checksum, path = request_to_checksum_and_path( request ) - save_to_disk(instance) - instance.to_s - end + instance = request.instance + instance.checksum = checksum if checksum + instance.path = path if path - private + save_to_disk(instance) + instance.to_s + end - def find_by_checksum( checksum, options ) - model.new( nil, :checksum => checksum ) do |bucket_file| - bucket_file.bucket_path = options[:bucket_path] - filename = contents_path_for( bucket_file ) + private - return nil if ! ::File.exist? filename + def find_by_checksum( checksum, options ) + model.new( nil, :checksum => checksum ) do |bucket_file| + bucket_file.bucket_path = options[:bucket_path] + filename = contents_path_for( bucket_file ) - begin - contents = ::File.read filename - Puppet.info "FileBucket read #{bucket_file.checksum}" - rescue RuntimeError => e - raise Puppet::Error, "file could not be read: #{e.message}" - end + return nil if ! ::File.exist? filename - if ::File.exist?(paths_path_for( bucket_file) ) - ::File.open(paths_path_for( bucket_file) ) do |f| - bucket_file.paths = f.readlines.map { |l| l.chomp } - end - end + begin + contents = ::File.read filename + Puppet.info "FileBucket read #{bucket_file.checksum}" + rescue RuntimeError => e + raise Puppet::Error, "file could not be read: #{e.message}" + end - bucket_file.contents = contents - end + if ::File.exist?(paths_path_for( bucket_file) ) + ::File.open(paths_path_for( bucket_file) ) do |f| + bucket_file.paths = f.readlines.map { |l| l.chomp } + end end - def save_to_disk( bucket_file ) - # If the file already exists, just return the md5 sum. - if ::File.exist?(contents_path_for( bucket_file) ) - verify_identical_file!(bucket_file) - else - # Make the directories if necessary. - unless ::File.directory?( path_for( bucket_file) ) - Puppet::Util.withumask(0007) do - ::FileUtils.mkdir_p( path_for( bucket_file) ) - end - end - - Puppet.info "FileBucket adding #{bucket_file.path} as #{bucket_file.checksum}" - - # Write the file to disk. - Puppet::Util.withumask(0007) do - ::File.open(contents_path_for(bucket_file), ::File::WRONLY|::File::CREAT, 0440) do |of| - of.print bucket_file.contents - end - end - end - - save_path_to_paths_file(bucket_file) - bucket_file.checksum_data + bucket_file.contents = contents + end + end + + def save_to_disk( bucket_file ) + # If the file already exists, just return the md5 sum. + if ::File.exist?(contents_path_for( bucket_file) ) + verify_identical_file!(bucket_file) + else + # Make the directories if necessary. + unless ::File.directory?( path_for( bucket_file) ) + Puppet::Util.withumask(0007) do + ::FileUtils.mkdir_p( path_for( bucket_file) ) + end end - def request_to_checksum_and_path( request ) - return [request.key, nil] if checksum?(request.key) + Puppet.info "FileBucket adding #{bucket_file.path} as #{bucket_file.checksum}" - checksum_type, checksum, path = request.key.split(/\//, 3) - return(checksum_type.to_s == "" ? nil : [ "{#{checksum_type}}#{checksum}", path ]) + # Write the file to disk. + Puppet::Util.withumask(0007) do + ::File.open(contents_path_for(bucket_file), ::File::WRONLY|::File::CREAT, 0440) do |of| + of.print bucket_file.contents + end end + end - def path_for(bucket_file, subfile = nil) - bucket_path = bucket_file.bucket_path || Puppet[:bucketdir] - digest = bucket_file.checksum_data + save_path_to_paths_file(bucket_file) + bucket_file.checksum_data + end - dir = ::File.join(digest[0..7].split("")) - basedir = ::File.join(bucket_path, dir, digest) + def request_to_checksum_and_path( request ) + return [request.key, nil] if checksum?(request.key) - return basedir unless subfile - ::File.join(basedir, subfile) - end + checksum_type, checksum, path = request.key.split(/\//, 3) + return(checksum_type.to_s == "" ? nil : [ "{#{checksum_type}}#{checksum}", path ]) + end - def contents_path_for(bucket_file) - path_for(bucket_file, "contents") - end + def path_for(bucket_file, subfile = nil) + bucket_path = bucket_file.bucket_path || Puppet[:bucketdir] + digest = bucket_file.checksum_data - def paths_path_for(bucket_file) - path_for(bucket_file, "paths") - end + dir = ::File.join(digest[0..7].split("")) + basedir = ::File.join(bucket_path, dir, digest) - def content_check? - true - end + return basedir unless subfile + ::File.join(basedir, subfile) + end - # If conflict_check is enabled, verify that the passed text is - # the same as the text in our file. - def verify_identical_file!(bucket_file) - return unless content_check? - disk_contents = ::File.read(contents_path_for(bucket_file)) - - # If the contents don't match, then we've found a conflict. - # Unlikely, but quite bad. - if disk_contents != bucket_file.contents - raise Puppet::FileBucket::BucketError, "Got passed new contents for sum #{bucket_file.checksum}", caller - else - Puppet.info "FileBucket got a duplicate file #{bucket_file.path} (#{bucket_file.checksum})" - end - end + def contents_path_for(bucket_file) + path_for(bucket_file, "contents") + end - def save_path_to_paths_file(bucket_file) - return unless bucket_file.path + def paths_path_for(bucket_file) + path_for(bucket_file, "paths") + end + + def content_check? + true + end - # check for dupes - if ::File.exist?(paths_path_for( bucket_file) ) - ::File.open(paths_path_for( bucket_file) ) do |f| - return if f.readlines.collect { |l| l.chomp }.include?(bucket_file.path) - end - end + # If conflict_check is enabled, verify that the passed text is + # the same as the text in our file. + def verify_identical_file!(bucket_file) + return unless content_check? + disk_contents = ::File.read(contents_path_for(bucket_file)) + + # If the contents don't match, then we've found a conflict. + # Unlikely, but quite bad. + if disk_contents != bucket_file.contents + raise Puppet::FileBucket::BucketError, "Got passed new contents for sum #{bucket_file.checksum}", caller + else + Puppet.info "FileBucket got a duplicate file #{bucket_file.path} (#{bucket_file.checksum})" + end + end + + def save_path_to_paths_file(bucket_file) + return unless bucket_file.path - # if it's a new file, or if our path isn't in the file yet, add it - ::File.open(paths_path_for(bucket_file), ::File::WRONLY|::File::CREAT|::File::APPEND) do |of| - of.puts bucket_file.path - end + # check for dupes + if ::File.exist?(paths_path_for( bucket_file) ) + ::File.open(paths_path_for( bucket_file) ) do |f| + return if f.readlines.collect { |l| l.chomp }.include?(bucket_file.path) end + end + # if it's a new file, or if our path isn't in the file yet, add it + ::File.open(paths_path_for(bucket_file), ::File::WRONLY|::File::CREAT|::File::APPEND) do |of| + of.puts bucket_file.path + end end + + end end |