Class: Puppet::FileBucket::Dipper Private

Inherits:
Object
  • Object
show all
Includes:
Util::Checksums
Defined in:
lib/puppet/file_bucket/dipper.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary

Constants included from Util::Checksums

Util::Checksums::KNOWN_CHECKSUMS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Checksums

checksum?, checksum_file, checksum_stream, ctime, ctime?, ctime_file, ctime_stream, known_checksum_types, md5, md5?, md5_file, md5_hex_length, md5_stream, md5lite, md5lite?, md5lite_file, md5lite_hex_length, md5lite_stream, mtime, mtime?, mtime_file, mtime_stream, none, none?, none_file, none_stream, sha1, sha1?, sha1_file, sha1_hex_length, sha1_stream, sha1lite, sha1lite?, sha1lite_file, sha1lite_hex_length, sha1lite_stream, sha224, sha224?, sha224_file, sha224_hex_length, sha224_stream, sha256, sha256?, sha256_file, sha256_hex_length, sha256_stream, sha256lite, sha256lite?, sha256lite_file, sha256lite_hex_length, sha256lite_stream, sha384, sha384?, sha384_file, sha384_hex_length, sha384_stream, sha512, sha512?, sha512_file, sha512_hex_length, sha512_stream, sumdata, sumtype, valid_checksum?

Constructor Details

#initialize(hash = {}) ⇒ Dipper

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a bucket client



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/file_bucket/dipper.rb', line 18

def initialize(hash = {})
  # Emulate the XMLRPC client
  server      = hash[:Server]
  port        = hash[:Port] || Puppet[:serverport]

  if hash.include?(:Path)
    @local_path = hash[:Path]
    @rest_path  = nil
  else
    @local_path = nil
    @rest_path = "filebucket://#{server}:#{port}/"
  end
  @checksum_type = Puppet[:digest_algorithm].to_sym
  @digest = method(@checksum_type)
end

Instance Attribute Details

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



15
16
17
# File 'lib/puppet/file_bucket/dipper.rb', line 15

def name
  @name
end

Instance Method Details

#backup(file) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Backs up a file to the file bucket

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/file_bucket/dipper.rb', line 39

def backup(file)
  file_handle = Puppet::FileSystem.pathname(file)
  raise(ArgumentError, _("File %{file} does not exist") % { file: file }) unless Puppet::FileSystem.exist?(file_handle)

  begin
    file_bucket_file = Puppet::FileBucket::File.new(file_handle, :bucket_path => @local_path)
    files_original_path = absolutize_path(file)
    dest_path = "#{@rest_path}#{file_bucket_file.name}/#{files_original_path}"
    file_bucket_path = "#{@rest_path}#{file_bucket_file.checksum_type}/#{file_bucket_file.checksum_data}/#{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.indirection.head(file_bucket_path, :bucket_path => file_bucket_file.bucket_path)
      Puppet::FileBucket::File.indirection.save(file_bucket_file, dest_path)
    end

    file_bucket_file.checksum_data
  rescue => detail
    message = _("Could not back up %{file}: %{detail}") % { file: file, detail: detail }
    Puppet.log_exception(detail, message)
    raise Puppet::Error, message, detail.backtrace
  end
end

#diff(checksum_a, checksum_b, file_a, file_b) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Diffs two filebucket files identified by their sums

Raises:

  • (RuntimeError)


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
97
98
99
100
101
102
103
104
# File 'lib/puppet/file_bucket/dipper.rb', line 64

def diff(checksum_a, checksum_b, file_a, file_b)
  raise RuntimeError, _("Diff is not supported on this platform") if Puppet[:diff] == ""

  if checksum_a
    source_path = "#{@rest_path}#{@checksum_type}/#{checksum_a}"
    if checksum_b
      file_diff = Puppet::FileBucket::File.indirection.find(
        source_path,
        :bucket_path => @local_path,
        :diff_with => checksum_b
      )
    elsif file_b
      tmp_file = ::Tempfile.new('diff')
      begin
        restore(tmp_file.path, checksum_a)
        file_diff = Puppet::Util::Diff.diff(tmp_file.path, file_b)
      ensure
        tmp_file.close
        tmp_file.unlink
      end
    else
      raise Puppet::Error, _("Please provide a file or checksum to diff with")
    end
  elsif file_a
    if checksum_b
      tmp_file = ::Tempfile.new('diff')
      begin
        restore(tmp_file.path, checksum_b)
        file_diff = Puppet::Util::Diff.diff(file_a, tmp_file.path)
      ensure
        tmp_file.close
        tmp_file.unlink
      end
    elsif file_b
      file_diff = Puppet::Util::Diff.diff(file_a, file_b)
    end
  end
  raise Puppet::Error, _("Failed to diff files") unless file_diff

  file_diff.to_s
end

#get_bucket_file(sum) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves a FileBucket::File by sum.

Raises:



112
113
114
115
116
117
118
119
# File 'lib/puppet/file_bucket/dipper.rb', line 112

def get_bucket_file(sum)
  source_path = "#{@rest_path}#{@checksum_type}/#{sum}"
  file_bucket_file = Puppet::FileBucket::File.indirection.find(source_path, :bucket_path => @local_path)

  raise Puppet::Error, _("File not found") unless file_bucket_file

  file_bucket_file
end

#getfile(sum) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves a file by sum.



107
108
109
# File 'lib/puppet/file_bucket/dipper.rb', line 107

def getfile(sum)
  get_bucket_file(sum).to_s
end

#list(fromdate, todate) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List Filebucket content.

Raises:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/puppet/file_bucket/dipper.rb', line 162

def list(fromdate, todate)
  raise Puppet::Error, _("Listing remote file buckets is not allowed") unless local?

  source_path = "#{@rest_path}#{@checksum_type}/"
  file_bucket_list = Puppet::FileBucket::File.indirection.find(
    source_path,
    :bucket_path => @local_path,
    :list_all => true,
    :fromdate => fromdate,
    :todate => todate
  )
  raise Puppet::Error, _("File not found") unless file_bucket_list

  file_bucket_list.to_s
end

#local?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


34
35
36
# File 'lib/puppet/file_bucket/dipper.rb', line 34

def local?
  !!@local_path
end

#restore(file, sum) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Restores the file



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/puppet/file_bucket/dipper.rb', line 122

def restore(file, sum)
  restore = true
  file_handle = Puppet::FileSystem.pathname(file)
  if Puppet::FileSystem.exist?(file_handle)
    cursum = Puppet::FileBucket::File.new(file_handle).checksum_data()

    # if the checksum has changed...
    # this might be extra effort
    if cursum == sum
      restore = false
    end
  end

  if restore
    newcontents = get_bucket_file(sum)
    if newcontents
      newsum = newcontents.checksum_data
      changed = nil
      if Puppet::FileSystem.exist?(file_handle) and !Puppet::FileSystem.writable?(file_handle)
        changed = Puppet::FileSystem.stat(file_handle).mode
        ::File.chmod(changed | 0o200, file)
      end
      ::File.open(file, ::File::WRONLY | ::File::TRUNC | ::File::CREAT) { |of|
        of.binmode
        newcontents.stream do |source_stream|
          FileUtils.copy_stream(source_stream, of)
        end
      }
      ::File.chmod(changed, file) if changed
    else
      Puppet.err _("Could not find file with checksum %{sum}") % { sum: sum }
      return nil
    end
    newsum
  else
    nil
  end
end