class Puppet::FileBucket::Dipper

Attributes

name[RW]

This is a transitional implementation that uses REST to access remote filebucket files.

Public Class Methods

new(hash = {}) click to toggle source

Creates a bucket client

   # File lib/puppet/file_bucket/dipper.rb
17 def initialize(hash = {})
18   # Emulate the XMLRPC client
19   server      = hash[:Server]
20   port        = hash[:Port] || Puppet[:serverport]
21 
22   if hash.include?(:Path)
23     @local_path = hash[:Path]
24     @rest_path  = nil
25   else
26     @local_path = nil
27     @rest_path = "filebucket://#{server}:#{port}/"
28   end
29   @checksum_type = Puppet[:digest_algorithm].to_sym
30   @digest = method(@checksum_type)
31 end

Public Instance Methods

backup(file) click to toggle source

Backs up a file to the file bucket

   # File lib/puppet/file_bucket/dipper.rb
38 def backup(file)
39   file_handle = Puppet::FileSystem.pathname(file)
40   raise(ArgumentError, _("File %{file} does not exist") % { file: file }) unless Puppet::FileSystem.exist?(file_handle)
41   begin
42     file_bucket_file = Puppet::FileBucket::File.new(file_handle, :bucket_path => @local_path)
43     files_original_path = absolutize_path(file)
44     dest_path = "#{@rest_path}#{file_bucket_file.name}/#{files_original_path}"
45     file_bucket_path = "#{@rest_path}#{file_bucket_file.checksum_type}/#{file_bucket_file.checksum_data}/#{files_original_path}"
46 
47     # Make a HEAD request for the file so that we don't waste time
48     # uploading it if it already exists in the bucket.
49     unless Puppet::FileBucket::File.indirection.head(file_bucket_path, :bucket_path => file_bucket_file.bucket_path)
50       Puppet::FileBucket::File.indirection.save(file_bucket_file, dest_path)
51     end
52 
53     return file_bucket_file.checksum_data
54   rescue => detail
55     message = _("Could not back up %{file}: %{detail}") % { file: file, detail: detail }
56     Puppet.log_exception(detail, message)
57     raise Puppet::Error, message, detail.backtrace
58   end
59 end
diff(checksum_a, checksum_b, file_a, file_b) click to toggle source

Diffs two filebucket files identified by their sums

   # File lib/puppet/file_bucket/dipper.rb
62 def diff(checksum_a, checksum_b, file_a, file_b)
63   raise RuntimeError, _("Diff is not supported on this platform") if Puppet[:diff] == ""
64   if checksum_a
65     source_path = "#{@rest_path}#{@checksum_type}/#{checksum_a}"
66     if checksum_b
67       file_diff = Puppet::FileBucket::File.indirection.find(
68         source_path,
69         :bucket_path => @local_path,
70         :diff_with => checksum_b)
71     elsif file_b
72       tmp_file = ::Tempfile.new('diff')
73       begin
74         restore(tmp_file.path, checksum_a)
75         file_diff = Puppet::Util::Diff.diff(tmp_file.path, file_b)
76       ensure
77         tmp_file.close
78         tmp_file.unlink
79       end
80     else
81       raise Puppet::Error, _("Please provide a file or checksum to diff with")
82     end
83   elsif file_a
84     if checksum_b
85       tmp_file = ::Tempfile.new('diff')
86       begin
87         restore(tmp_file.path, checksum_b)
88         file_diff = Puppet::Util::Diff.diff(file_a, tmp_file.path)
89       ensure
90         tmp_file.close
91         tmp_file.unlink
92       end
93     elsif file_b
94       file_diff = Puppet::Util::Diff.diff(file_a, file_b)
95     end
96   end
97   raise Puppet::Error, _("Failed to diff files") unless file_diff
98   file_diff.to_s
99 end
get_bucket_file(sum) click to toggle source

Retrieves a FileBucket::File by sum.

    # File lib/puppet/file_bucket/dipper.rb
107 def get_bucket_file(sum)
108   source_path = "#{@rest_path}#{@checksum_type}/#{sum}"
109   file_bucket_file = Puppet::FileBucket::File.indirection.find(source_path, :bucket_path => @local_path)
110 
111   raise Puppet::Error, _("File not found") unless file_bucket_file
112   file_bucket_file
113 end
getfile(sum) click to toggle source

Retrieves a file by sum.

    # File lib/puppet/file_bucket/dipper.rb
102 def getfile(sum)
103   get_bucket_file(sum).to_s
104 end
list(fromdate, todate) click to toggle source

List Filebucket content.

    # File lib/puppet/file_bucket/dipper.rb
156 def list(fromdate, todate)
157   raise Puppet::Error, _("Listing remote file buckets is not allowed") unless local?
158 
159   source_path = "#{@rest_path}#{@checksum_type}/"
160   file_bucket_list = Puppet::FileBucket::File.indirection.find(
161     source_path,
162     :bucket_path => @local_path,
163     :list_all => true,
164     :fromdate => fromdate,
165     :todate => todate)
166   raise Puppet::Error, _("File not found") unless file_bucket_list
167   file_bucket_list.to_s
168 end
local?() click to toggle source
   # File lib/puppet/file_bucket/dipper.rb
33 def local?
34   !! @local_path
35 end
restore(file, sum) click to toggle source

Restores the file

    # File lib/puppet/file_bucket/dipper.rb
116 def restore(file, sum)
117   restore = true
118   file_handle = Puppet::FileSystem.pathname(file)
119   if Puppet::FileSystem.exist?(file_handle)
120     cursum = Puppet::FileBucket::File.new(file_handle).checksum_data()
121 
122     # if the checksum has changed...
123     # this might be extra effort
124     if cursum == sum
125       restore = false
126     end
127   end
128 
129   if restore
130     newcontents = get_bucket_file(sum)
131     if newcontents
132       newsum = newcontents.checksum_data
133       changed = nil
134       if Puppet::FileSystem.exist?(file_handle) and ! Puppet::FileSystem.writable?(file_handle)
135         changed = Puppet::FileSystem.stat(file_handle).mode
136         ::File.chmod(changed | 0200, file)
137       end
138       ::File.open(file, ::File::WRONLY|::File::TRUNC|::File::CREAT) { |of|
139         of.binmode
140         newcontents.stream do |source_stream|
141           FileUtils.copy_stream(source_stream, of)
142         end
143       }
144       ::File.chmod(changed, file) if changed
145     else
146       Puppet.err _("Could not find file with checksum %{sum}") % { sum: sum }
147       return nil
148     end
149     return newsum
150   else
151     return nil
152   end
153 end

Private Instance Methods

absolutize_path( path ) click to toggle source
    # File lib/puppet/file_bucket/dipper.rb
171 def absolutize_path( path )
172   Pathname.new(path).realpath
173 end