Class: Chef::ChecksumCache

Inherits:
Object
  • Object
show all
Includes:
Mixin::ConvertToClassName, Singleton
Defined in:
lib/chef/checksum_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

#initialize(*args) ⇒ ChecksumCache

Returns a new instance of ChecksumCache.



37
38
39
# File 'lib/chef/checksum_cache.rb', line 37

def initialize(*args)
  self.reset!(*args)
end

Instance Attribute Details

#monetaObject (readonly)

Returns the value of attribute moneta.



35
36
37
# File 'lib/chef/checksum_cache.rb', line 35

def moneta
  @moneta
end

Class Method Details

.all_cached_checksumsObject



71
72
73
74
75
76
77
78
# File 'lib/chef/checksum_cache.rb', line 71

def self.all_cached_checksums
  all_checksums_with_filenames = {}

  Dir[File.join(Chef::Config[:cache_options][:path], '*')].each do |cksum_file|
    all_checksums_with_filenames[File.basename(cksum_file)] = cksum_file
  end
  all_checksums_with_filenames
end

.checksum_for_file(*args) ⇒ Object



100
101
102
# File 'lib/chef/checksum_cache.rb', line 100

def self.checksum_for_file(*args)
  instance.checksum_for_file(*args)
end

.cleanup_checksum_cacheObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/checksum_cache.rb', line 80

def self.cleanup_checksum_cache
  Chef::Log.info("cleaning the checksum cache")
  if (Chef::Config[:cache_type].to_s == "BasicFile")
    all_cached_checksums.each do |cache_key, cksum_cache_file|
      unless valid_cached_checksums.include?(cache_key)
        remove_unused_checksum(cksum_cache_file)
      end
    end
  end
end

.generate_md5_checksum_for_file(*args) ⇒ Object



135
136
137
# File 'lib/chef/checksum_cache.rb', line 135

def self.generate_md5_checksum_for_file(*args)
  instance.generate_md5_checksum_for_file(*args)
end

.remove_unused_checksum(checksum_file) ⇒ Object



95
96
97
98
# File 'lib/chef/checksum_cache.rb', line 95

def self.remove_unused_checksum(checksum_file)
  Chef::Log.debug("removing unused checksum cache file #{checksum_file}")
  FileUtils.rm(checksum_file)
end

.reset_cache_validityObject



55
56
57
# File 'lib/chef/checksum_cache.rb', line 55

def self.reset_cache_validity
  @valid_cached_checksums = nil
end

.valid_cached_checksumsObject



63
64
65
# File 'lib/chef/checksum_cache.rb', line 63

def self.valid_cached_checksums
  @valid_cached_checksums ||= Set.new
end

.validate_checksum(checksum_key) ⇒ Object



67
68
69
# File 'lib/chef/checksum_cache.rb', line 67

def self.validate_checksum(checksum_key)
  valid_cached_checksums << checksum_key
end

Instance Method Details

#checksum_for_file(file, key = nil) ⇒ Object



108
109
110
111
112
# File 'lib/chef/checksum_cache.rb', line 108

def checksum_for_file(file, key=nil)
  key ||= generate_key(file)
  fstat = File.stat(file)
  lookup_checksum(key, fstat) || generate_checksum(key, file, fstat)
end

#generate_checksum(key, file, fstat) ⇒ Object



124
125
126
127
128
129
# File 'lib/chef/checksum_cache.rb', line 124

def generate_checksum(key, file, fstat)
  checksum = checksum_file(file, Digest::SHA256.new)
  moneta.store(key, {"mtime" => fstat.mtime.to_f, "checksum" => checksum})
  validate_checksum(key)
  checksum
end

#generate_key(file, group = "chef") ⇒ Object



131
132
133
# File 'lib/chef/checksum_cache.rb', line 131

def generate_key(file, group="chef")
  "#{group}-file-#{file.gsub(/(#{File::SEPARATOR}|\.)/, '-')}"
end

#generate_md5_checksum(io) ⇒ Object



143
144
145
# File 'lib/chef/checksum_cache.rb', line 143

def generate_md5_checksum(io)
  checksum_io(io, Digest::MD5.new)
end

#generate_md5_checksum_for_file(file) ⇒ Object



139
140
141
# File 'lib/chef/checksum_cache.rb', line 139

def generate_md5_checksum_for_file(file)
  checksum_file(file, Digest::MD5.new)
end

#lookup_checksum(key, fstat) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/chef/checksum_cache.rb', line 114

def lookup_checksum(key, fstat)
  cached = @moneta.fetch(key)
  if cached && file_unchanged?(cached, fstat)
    validate_checksum(key)
    cached["checksum"]
  else
    nil
  end
end

#reset!(backend = nil, options = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/checksum_cache.rb', line 41

def reset!(backend=nil, options=nil)
  backend ||= Chef::Config[:cache_type]
  options ||= Chef::Config[:cache_options]
  
  begin
    require "moneta/#{convert_to_snake_case(backend, 'Moneta')}"
  rescue LoadError => e
    Chef::Log.fatal("Could not load Moneta back end #{backend.inspect}")
    raise e
  end
 
  @moneta = Moneta.const_get(backend).new(options)
end

#validate_checksum(*args) ⇒ Object



104
105
106
# File 'lib/chef/checksum_cache.rb', line 104

def validate_checksum(*args)
  self.class.validate_checksum(*args)
end