summaryrefslogtreecommitdiffstats
path: root/lib/puppet/file_bucket
diff options
context:
space:
mode:
authorMarkus Roberts <Markus@reality.com>2010-07-09 18:12:17 -0700
committerMarkus Roberts <Markus@reality.com>2010-07-09 18:12:17 -0700
commit3180b9d9b2c844dade1d361326600f7001ec66dd (patch)
tree98fe7c5ac7eb942aac9c39f019a17b0b3f5a57f4 /lib/puppet/file_bucket
parent543225970225de5697734bfaf0a6eee996802c04 (diff)
downloadpuppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.gz
puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.xz
puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.zip
Code smell: Two space indentation
Replaced 106806 occurances of ^( +)(.*$) with The ruby community almost universally (i.e. everyone but Luke, Markus, and the other eleven people who learned ruby in the 1900s) uses two-space indentation. 3 Examples: The code: end # Tell getopt which arguments are valid def test_get_getopt_args element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args") becomes: end # Tell getopt which arguments are valid def test_get_getopt_args element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args") The code: assert_equal(str, val) assert_instance_of(Float, result) end # Now test it with a passed object becomes: assert_equal(str, val) assert_instance_of(Float, result) end # Now test it with a passed object The code: end assert_nothing_raised do klass[:Yay] = "boo" klass["Cool"] = :yayness end becomes: end assert_nothing_raised do klass[:Yay] = "boo" klass["Cool"] = :yayness end
Diffstat (limited to 'lib/puppet/file_bucket')
-rw-r--r--lib/puppet/file_bucket/dipper.rb150
-rw-r--r--lib/puppet/file_bucket/file.rb260
-rw-r--r--lib/puppet/file_bucket/file/indirection_hooks.rb6
3 files changed, 208 insertions, 208 deletions
diff --git a/lib/puppet/file_bucket/dipper.rb b/lib/puppet/file_bucket/dipper.rb
index 08192aa89..dbfcdcd43 100644
--- a/lib/puppet/file_bucket/dipper.rb
+++ b/lib/puppet/file_bucket/dipper.rb
@@ -3,97 +3,97 @@ require 'puppet/file_bucket/file'
require 'puppet/indirector/request'
class Puppet::FileBucket::Dipper
- # This is a transitional implementation that uses REST
- # to access remote filebucket files.
+ # This is a transitional implementation that uses REST
+ # to access remote filebucket files.
- attr_accessor :name
+ attr_accessor :name
- # Create our bucket client
- def initialize(hash = {})
- # Emulate the XMLRPC client
- server = hash[:Server]
- port = hash[:Port] || Puppet[:masterport]
- environment = Puppet[:environment]
+ # Create our bucket client
+ def initialize(hash = {})
+ # Emulate the XMLRPC client
+ server = hash[:Server]
+ port = hash[:Port] || Puppet[:masterport]
+ environment = Puppet[:environment]
- if hash.include?(:Path)
- @local_path = hash[:Path]
- @rest_path = nil
- else
- @local_path = nil
- @rest_path = "https://#{server}:#{port}/#{environment}/file_bucket_file/"
- end
+ if hash.include?(:Path)
+ @local_path = hash[:Path]
+ @rest_path = nil
+ else
+ @local_path = nil
+ @rest_path = "https://#{server}:#{port}/#{environment}/file_bucket_file/"
end
+ end
- def local?
- !! @local_path
- end
+ def local?
+ !! @local_path
+ end
- # Back up a file to our bucket
- def backup(file)
- raise(ArgumentError, "File #{file} does not exist") unless ::File.exist?(file)
- contents = ::File.read(file)
- begin
- file_bucket_file = Puppet::FileBucket::File.new(contents, :bucket_path => @local_path, :path => absolutize_path(file) )
- dest_path = "#{@rest_path}#{file_bucket_file.name}"
+ # Back up a file to our bucket
+ def backup(file)
+ raise(ArgumentError, "File #{file} does not exist") unless ::File.exist?(file)
+ contents = ::File.read(file)
+ begin
+ file_bucket_file = Puppet::FileBucket::File.new(contents, :bucket_path => @local_path, :path => absolutize_path(file) )
+ dest_path = "#{@rest_path}#{file_bucket_file.name}"
- file_bucket_file.save(dest_path)
- return file_bucket_file.checksum_data
- rescue => detail
- puts detail.backtrace if Puppet[:trace]
- raise Puppet::Error, "Could not back up #{file}: #{detail}"
- end
+ file_bucket_file.save(dest_path)
+ return file_bucket_file.checksum_data
+ rescue => detail
+ puts detail.backtrace if Puppet[:trace]
+ raise Puppet::Error, "Could not back up #{file}: #{detail}"
end
+ end
- # Retrieve a file by sum.
- def getfile(sum)
- source_path = "#{@rest_path}md5/#{sum}"
- file_bucket_file = Puppet::FileBucket::File.find(source_path, :bucket_path => @local_path)
+ # Retrieve a file by sum.
+ def getfile(sum)
+ source_path = "#{@rest_path}md5/#{sum}"
+ file_bucket_file = Puppet::FileBucket::File.find(source_path, :bucket_path => @local_path)
- raise Puppet::Error, "File not found" unless file_bucket_file
- file_bucket_file.to_s
- end
+ raise Puppet::Error, "File not found" unless file_bucket_file
+ file_bucket_file.to_s
+ end
- # Restore the file
- def restore(file,sum)
- restore = true
- if FileTest.exists?(file)
- cursum = Digest::MD5.hexdigest(::File.read(file))
+ # Restore the file
+ def restore(file,sum)
+ restore = true
+ if FileTest.exists?(file)
+ cursum = Digest::MD5.hexdigest(::File.read(file))
- # if the checksum has changed...
- # this might be extra effort
- if cursum == sum
- restore = false
- end
- end
+ # if the checksum has changed...
+ # this might be extra effort
+ if cursum == sum
+ restore = false
+ end
+ end
- if restore
- if newcontents = getfile(sum)
- tmp = ""
- newsum = Digest::MD5.hexdigest(newcontents)
- changed = nil
- if FileTest.exists?(file) and ! FileTest.writable?(file)
- changed = ::File.stat(file).mode
- ::File.chmod(changed | 0200, file)
- end
- ::File.open(file, ::File::WRONLY|::File::TRUNC|::File::CREAT) { |of|
- of.print(newcontents)
- }
- ::File.chmod(changed, file) if changed
- else
- Puppet.err "Could not find file with checksum #{sum}"
- return nil
- end
- return newsum
- else
- return nil
+ if restore
+ if newcontents = getfile(sum)
+ tmp = ""
+ newsum = Digest::MD5.hexdigest(newcontents)
+ changed = nil
+ if FileTest.exists?(file) and ! FileTest.writable?(file)
+ changed = ::File.stat(file).mode
+ ::File.chmod(changed | 0200, file)
end
+ ::File.open(file, ::File::WRONLY|::File::TRUNC|::File::CREAT) { |of|
+ of.print(newcontents)
+ }
+ ::File.chmod(changed, file) if changed
+ else
+ Puppet.err "Could not find file with checksum #{sum}"
+ return nil
+ end
+ return newsum
+ else
+ return nil
end
+ end
- private
- def absolutize_path( path )
- require 'pathname'
- Pathname.new(path).realpath
- end
+ private
+ def absolutize_path( path )
+ require 'pathname'
+ Pathname.new(path).realpath
+ end
end
diff --git a/lib/puppet/file_bucket/file.rb b/lib/puppet/file_bucket/file.rb
index 52ee4f711..96fd8e225 100644
--- a/lib/puppet/file_bucket/file.rb
+++ b/lib/puppet/file_bucket/file.rb
@@ -3,134 +3,134 @@ require 'puppet/indirector'
require 'puppet/util/checksums'
class Puppet::FileBucket::File
- include Puppet::Util::Checksums
-
- # This class handles the abstract notion of a file in a filebucket.
- # There are mechanisms to save and load this file locally and remotely in puppet/indirector/filebucketfile/*
- # There is a compatibility class that emulates pre-indirector filebuckets in Puppet::FileBucket::Dipper
- extend Puppet::Indirector
- require 'puppet/file_bucket/file/indirection_hooks'
- indirects :file_bucket_file, :terminus_class => :file, :extend => Puppet::FileBucket::File::IndirectionHooks
-
- attr :path, true
- attr :paths, true
- attr :contents, true
- attr :checksum_type
- attr :bucket_path, true
-
- def self.default_checksum_type
- "md5"
- end
-
- def initialize( contents, options = {} )
- @bucket_path = options[:bucket_path]
- @path = options[:path]
- @paths = options[:paths] || []
-
- @checksum = options[:checksum]
- @checksum_type = options[:checksum_type]
-
- self.contents = contents
-
- yield(self) if block_given?
-
- validate!
- end
-
- def validate!
- validate_checksum_type!(checksum_type)
- validate_checksum!(checksum) if checksum
- end
-
- def contents=(str)
- raise "You may not change the contents of a FileBucket File" if @contents
- validate_content!(str)
- @contents = str
- end
-
- def checksum
- return @checksum if @checksum
- @checksum = calculate_checksum if contents
- @checksum
- end
-
- def checksum=(checksum)
- validate_checksum!(checksum)
- @checksum = checksum
- end
-
- def checksum_type=( new_checksum_type )
- @checksum = nil
- @checksum_type = new_checksum_type
- end
-
- def checksum_type
- unless @checksum_type
- if @checksum
- @checksum_type = sumtype(checksum)
- else
- @checksum_type = self.class.default_checksum_type
- end
- end
- @checksum_type
- end
-
- def checksum_data
- sumdata(checksum)
- end
-
- def to_s
- contents
- end
-
- def name
- [checksum_type, checksum_data, path].compact.join('/')
- end
-
- def name=(name)
- data = name.split('/',3)
- self.path = data.pop
- @checksum_type = nil
- self.checksum = "{#{data[0]}}#{data[1]}"
- end
-
- def conflict_check?
- true
- end
-
- def self.from_s( contents )
- self.new( contents )
- end
-
- def to_pson
- hash = { "contents" => contents }
- hash["path"] = @path if @path
- hash.to_pson
- end
-
- def self.from_pson( pson )
- self.new( pson["contents"], :path => pson["path"] )
- end
-
- private
-
- def calculate_checksum
- "{#{checksum_type}}" + send(checksum_type, contents)
- end
-
- def validate_content!(content)
- raise ArgumentError, "Contents must be a string" if content and ! content.is_a?(String)
- end
-
- def validate_checksum!(new_checksum)
- newtype = sumtype(new_checksum)
-
- unless sumdata(new_checksum) == (calc_sum = send(newtype, contents))
- raise Puppet::Error, "Checksum #{new_checksum} does not match contents #{calc_sum}"
- end
- end
-
- def validate_checksum_type!(type)
- raise ArgumentError, "Invalid checksum type #{type}" unless respond_to?(type)
- end
+ include Puppet::Util::Checksums
+
+ # This class handles the abstract notion of a file in a filebucket.
+ # There are mechanisms to save and load this file locally and remotely in puppet/indirector/filebucketfile/*
+ # There is a compatibility class that emulates pre-indirector filebuckets in Puppet::FileBucket::Dipper
+ extend Puppet::Indirector
+ require 'puppet/file_bucket/file/indirection_hooks'
+ indirects :file_bucket_file, :terminus_class => :file, :extend => Puppet::FileBucket::File::IndirectionHooks
+
+ attr :path, true
+ attr :paths, true
+ attr :contents, true
+ attr :checksum_type
+ attr :bucket_path, true
+
+ def self.default_checksum_type
+ "md5"
+ end
+
+ def initialize( contents, options = {} )
+ @bucket_path = options[:bucket_path]
+ @path = options[:path]
+ @paths = options[:paths] || []
+
+ @checksum = options[:checksum]
+ @checksum_type = options[:checksum_type]
+
+ self.contents = contents
+
+ yield(self) if block_given?
+
+ validate!
+ end
+
+ def validate!
+ validate_checksum_type!(checksum_type)
+ validate_checksum!(checksum) if checksum
+ end
+
+ def contents=(str)
+ raise "You may not change the contents of a FileBucket File" if @contents
+ validate_content!(str)
+ @contents = str
+ end
+
+ def checksum
+ return @checksum if @checksum
+ @checksum = calculate_checksum if contents
+ @checksum
+ end
+
+ def checksum=(checksum)
+ validate_checksum!(checksum)
+ @checksum = checksum
+ end
+
+ def checksum_type=( new_checksum_type )
+ @checksum = nil
+ @checksum_type = new_checksum_type
+ end
+
+ def checksum_type
+ unless @checksum_type
+ if @checksum
+ @checksum_type = sumtype(checksum)
+ else
+ @checksum_type = self.class.default_checksum_type
+ end
+ end
+ @checksum_type
+ end
+
+ def checksum_data
+ sumdata(checksum)
+ end
+
+ def to_s
+ contents
+ end
+
+ def name
+ [checksum_type, checksum_data, path].compact.join('/')
+ end
+
+ def name=(name)
+ data = name.split('/',3)
+ self.path = data.pop
+ @checksum_type = nil
+ self.checksum = "{#{data[0]}}#{data[1]}"
+ end
+
+ def conflict_check?
+ true
+ end
+
+ def self.from_s( contents )
+ self.new( contents )
+ end
+
+ def to_pson
+ hash = { "contents" => contents }
+ hash["path"] = @path if @path
+ hash.to_pson
+ end
+
+ def self.from_pson( pson )
+ self.new( pson["contents"], :path => pson["path"] )
+ end
+
+ private
+
+ def calculate_checksum
+ "{#{checksum_type}}" + send(checksum_type, contents)
+ end
+
+ def validate_content!(content)
+ raise ArgumentError, "Contents must be a string" if content and ! content.is_a?(String)
+ end
+
+ def validate_checksum!(new_checksum)
+ newtype = sumtype(new_checksum)
+
+ unless sumdata(new_checksum) == (calc_sum = send(newtype, contents))
+ raise Puppet::Error, "Checksum #{new_checksum} does not match contents #{calc_sum}"
+ end
+ end
+
+ def validate_checksum_type!(type)
+ raise ArgumentError, "Invalid checksum type #{type}" unless respond_to?(type)
+ end
end
diff --git a/lib/puppet/file_bucket/file/indirection_hooks.rb b/lib/puppet/file_bucket/file/indirection_hooks.rb
index ab0912b39..58c292745 100644
--- a/lib/puppet/file_bucket/file/indirection_hooks.rb
+++ b/lib/puppet/file_bucket/file/indirection_hooks.rb
@@ -3,7 +3,7 @@ require 'puppet/file_bucket/file'
# This module is used to pick the appropriate terminus
# in filebucket indirections.
module Puppet::FileBucket::File::IndirectionHooks
- def select_terminus(request)
- return(request.protocol == 'https' ? :rest : Puppet::FileBucket::File.indirection.terminus_class)
- end
+ def select_terminus(request)
+ return(request.protocol == 'https' ? :rest : Puppet::FileBucket::File.indirection.terminus_class)
+ end
end