Class: Puppet::FileBucket::Dipper
- Defined in:
- lib/puppet/file_bucket/dipper.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
This is a transitional implementation that uses REST to access remote filebucket files.
Instance Method Summary collapse
-
#backup(file) ⇒ Object
Back up a file to our bucket.
-
#getfile(sum) ⇒ Object
Retrieve a file by sum.
-
#initialize(hash = {}) ⇒ Dipper
constructor
Create our bucket client.
- #local? ⇒ Boolean
-
#restore(file, sum) ⇒ Object
Restore the file.
Constructor Details
#initialize(hash = {}) ⇒ Dipper
Create our bucket client
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/puppet/file_bucket/dipper.rb', line 12 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 end |
Instance Attribute Details
#name ⇒ Object
This is a transitional implementation that uses REST to access remote filebucket files.
9 10 11 |
# File 'lib/puppet/file_bucket/dipper.rb', line 9 def name @name end |
Instance Method Details
#backup(file) ⇒ Object
Back up a file to our bucket
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/puppet/file_bucket/dipper.rb', line 32 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) files_original_path = absolutize_path(file) dest_path = "#{@rest_path}#{file_bucket_file.name}#{files_original_path}" # Make a HEAD request for the file so that we don't waste time # uploading it if it already exists in the bucket. unless Puppet::FileBucket::File.head("#{@rest_path}#{file_bucket_file.checksum_type}/#{file_bucket_file.checksum_data}#{files_original_path}") file_bucket_file.save(dest_path) end 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 |
#getfile(sum) ⇒ Object
Retrieve a file by sum.
54 55 56 57 58 59 60 |
# File 'lib/puppet/file_bucket/dipper.rb', line 54 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 |
#local? ⇒ Boolean
27 28 29 |
# File 'lib/puppet/file_bucket/dipper.rb', line 27 def local? !! @local_path end |
#restore(file, sum) ⇒ Object
Restore the file
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/puppet/file_bucket/dipper.rb', line 63 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 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 |