Class: Chef::ChecksumCache

Inherits:
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



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

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



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

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

.cleanup_checksum_cacheObject



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

def self.cleanup_checksum_cache
  Chef::Log.debug("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



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

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

.remove_unused_checksum(checksum_file) ⇒ Object



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

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



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

def self.reset_cache_validity
  @valid_cached_checksums = nil
end

.valid_cached_checksumsObject



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

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

.validate_checksum(checksum_key) ⇒ Object



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

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

Instance Method Details

#checksum_for_file(file, key = nil) ⇒ Object



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

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



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

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



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

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

#generate_md5_checksum(io) ⇒ Object



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

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

#generate_md5_checksum_for_file(file) ⇒ Object



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

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

#lookup_checksum(key, fstat) ⇒ Object



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

def lookup_checksum(key, fstat)
  cached = 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
54
# 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')}"
    require 'chef/monkey_patches/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



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

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