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
16 def initialize(hash = {})
17   # Emulate the XMLRPC client
18   server      = hash[:Server]
19   port        = hash[:Port] || Puppet[:serverport]
20 
21   if hash.include?(:Path)
22     @local_path = hash[:Path]
23     @rest_path  = nil
24   else
25     @local_path = nil
26     @rest_path = "filebucket://#{server}:#{port}/"
27   end
28   @checksum_type = Puppet[:digest_algorithm].to_sym
29   @digest = method(@checksum_type)
30 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
37 def backup(file)
38   file_handle = Puppet::FileSystem.pathname(file)
39   raise(ArgumentError, _("File %{file} does not exist") % { file: file }) unless Puppet::FileSystem.exist?(file_handle)
40   begin
41     file_bucket_file = Puppet::FileBucket::File.new(file_handle, :bucket_path => @local_path)
42     files_original_path = absolutize_path(file)
43     dest_path = "#{@rest_path}#{file_bucket_file.name}/#{files_original_path}"
44     file_bucket_path = "#{@rest_path}#{file_bucket_file.checksum_type}/#{file_bucket_file.checksum_data}/#{files_original_path}"
45 
46     # Make a HEAD request for the file so that we don't waste time
47     # uploading it if it already exists in the bucket.
48     unless Puppet::FileBucket::File.indirection.head(file_bucket_path, :bucket_path => file_bucket_file.bucket_path)
49       Puppet::FileBucket::File.indirection.save(file_bucket_file, dest_path)
50     end
51 
52     return file_bucket_file.checksum_data
53   rescue => detail
54     message = _("Could not back up %{file}: %{detail}") % { file: file, detail: detail }
55     Puppet.log_exception(detail, message)
56     raise Puppet::Error, message, detail.backtrace
57   end
58 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
61 def diff(checksum_a, checksum_b, file_a, file_b)
62   raise RuntimeError, _("Diff is not supported on this platform") if Puppet[:diff] == ""
63   if checksum_a
64     source_path = "#{@rest_path}#{@checksum_type}/#{checksum_a}"
65     if checksum_b
66       file_diff = Puppet::FileBucket::File.indirection.find(
67         source_path,
68         :bucket_path => @local_path,
69         :diff_with => checksum_b)
70     elsif file_b
71       tmp_file = ::Tempfile.new('diff')
72       begin
73         restore(tmp_file.path, checksum_a)
74         file_diff = Puppet::Util::Diff.diff(tmp_file.path, file_b)
75       ensure
76         tmp_file.close
77         tmp_file.unlink
78       end
79     else
80       raise Puppet::Error, _("Please provide a file or checksum to diff with")
81     end
82   elsif file_a
83     if checksum_b
84       tmp_file = ::Tempfile.new('diff')
85       begin
86         restore(tmp_file.path, checksum_b)
87         file_diff = Puppet::Util::Diff.diff(file_a, tmp_file.path)
88       ensure
89         tmp_file.close
90         tmp_file.unlink
91       end
92     elsif file_b
93       file_diff = Puppet::Util::Diff.diff(file_a, file_b)
94     end
95   end
96   raise Puppet::Error, _("Failed to diff files") unless file_diff
97   file_diff.to_s
98 end
get_bucket_file(sum) click to toggle source

Retrieves a FileBucket::File by sum.

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

Retrieves a file by sum.

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

List Filebucket content.

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

Restores the file

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

Private Instance Methods

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