Class: Puppet::FileBucket::Dipper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Checksums

#checksum?, #ctime, #ctime_file, #known_checksum_types, #md5, #md5_file, #md5_hex_length, #md5_stream, #md5lite, #md5lite_file, #mtime, #mtime_file, #mtime_stream, #none, #none_file, #none_stream, #sha1, #sha1_file, #sha1_hex_length, #sha1_stream, #sha1lite, #sha1lite_file, #sha256, #sha256_file, #sha256_hex_length, #sha256_stream, #sha256lite, #sha256lite_file, #sumdata, #sumtype

Constructor Details

#initialize(hash = {}) ⇒ Dipper

Create our bucket client



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puppet/file_bucket/dipper.rb', line 14

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
  @checksum_type = Puppet[:digest_algorithm].to_sym
  @digest = method(@checksum_type)
end

Instance Attribute Details

#nameObject

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



11
12
13
# File 'lib/puppet/file_bucket/dipper.rb', line 11

def name
  @name
end

Instance Method Details

#backup(file) ⇒ Object

Back up a file to our bucket

Raises:

  • (ArgumentError)


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

def backup(file)
  file_handle = Puppet::FileSystem.pathname(file)
  raise(ArgumentError, "File #{file} does not exist") unless Puppet::FileSystem.exist?(file_handle)
  contents = Puppet::FileSystem.binread(file_handle)
  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}"
    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)
      Puppet::FileBucket::File.indirection.save(file_bucket_file, dest_path)
    end

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

#getfile(sum) ⇒ Object

Retrieve a file by sum.

Raises:



61
62
63
64
65
66
67
# File 'lib/puppet/file_bucket/dipper.rb', line 61

def getfile(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.to_s
end

#local?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/puppet/file_bucket/dipper.rb', line 31

def local?
  !! @local_path
end

#restore(file, sum) ⇒ Object

Restore the file



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 70

def restore(file,sum)
  restore = true
  file_handle = Puppet::FileSystem.pathname(file)
  if Puppet::FileSystem.exist?(file_handle)
    cursum = @digest.call(Puppet::FileSystem.binread(file_handle))

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

  if restore
    if newcontents = getfile(sum)
      newsum = @digest.call(newcontents)
      changed = nil
      if Puppet::FileSystem.exist?(file_handle) and ! Puppet::FileSystem.writable?(file_handle)
        changed = Puppet::FileSystem.stat(file_handle).mode
        ::File.chmod(changed | 0200, file)
      end
      ::File.open(file, ::File::WRONLY|::File::TRUNC|::File::CREAT) { |of|
        of.binmode
        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