diff options
author | danielsdeleo <dan@opscode.com> | 2012-12-20 16:12:34 -0800 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2012-12-21 12:15:18 -0800 |
commit | 2c460c191bcdb1acfe6978a6e7e7ff8e0273f973 (patch) | |
tree | 4a4a2d5e30377c6eb2e2f59e42674b789f2c07f6 /lib | |
parent | 6bdcab78353213c189c4feb9097949633b8e8e7b (diff) | |
download | chef-2c460c191bcdb1acfe6978a6e7e7ff8e0273f973.tar.gz |
[CHEF-3715] remove caching of sha256 cksums
This also eliminates the dependency on moneta.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/checksum_cache.rb | 131 | ||||
-rw-r--r-- | lib/chef/config.rb | 4 | ||||
-rw-r--r-- | lib/chef/monkey_patches/moneta.rb | 50 |
3 files changed, 8 insertions, 177 deletions
diff --git a/lib/chef/checksum_cache.rb b/lib/chef/checksum_cache.rb index 6db7115a56..23ac195892 100644 --- a/lib/chef/checksum_cache.rb +++ b/lib/chef/checksum_cache.rb @@ -18,84 +18,13 @@ # limitations under the License. # -require 'set' -require 'fileutils' -require 'chef/log' -require 'chef/config' -require 'chef/client' -require 'chef/mixin/convert_to_class_name' -require 'singleton' -require 'moneta' +require 'digest' class Chef class ChecksumCache - include Chef::Mixin::ConvertToClassName - include ::Singleton - attr_reader :moneta - - def initialize(*args) - self.reset!(*args) - end - - 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 - - def self.reset_cache_validity - @valid_cached_checksums = nil - end - - Chef::Client.when_run_starts do |run_status| - reset_cache_validity - end - - def self.valid_cached_checksums - @valid_cached_checksums ||= Set.new - end - - def self.validate_checksum(checksum_key) - valid_cached_checksums << checksum_key - end - - 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 - - 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 - - Chef::Client.when_run_completes_successfully do |run_status| - cleanup_checksum_cache - end - - def self.remove_unused_checksum(checksum_file) - Chef::Log.debug("Removing unused checksum cache file #{checksum_file}") - FileUtils.rm(checksum_file) + def self.instance + @instance ||= new end def self.checksum_for_file(*args) @@ -106,31 +35,12 @@ class Chef self.class.validate_checksum(*args) end - 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 - - def lookup_checksum(key, fstat) - cached = fetch(key) - if cached && file_unchanged?(cached, fstat) - validate_checksum(key) - cached["checksum"] - else - nil - end - end - - 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 + def checksum_for_file(file) + generate_checksum(file) end - def generate_key(file, group="chef") - "#{group}-file-#{file.gsub(/(#{File::SEPARATOR}|\.)/, '-')}" + def generate_checksum(file) + checksum_file(file, Digest::SHA256.new) end def self.generate_md5_checksum_for_file(*args) @@ -147,26 +57,6 @@ class Chef private - def fetch(key) - @moneta.fetch(key) - rescue ArgumentError => e - Log.warn "Error loading cached checksum for key #{key.inspect}" - Log.warn(e) - repair_checksum_cache - nil - end - - def repair_checksum_cache - Chef::Log.info("Removing invalid checksum cache files") - Dir["#{Chef::Config[:cache_options][:path]}/*"].each do |file_path| - File.unlink(file_path) unless File.size?(file_path) - end - end - - def file_unchanged?(cached, fstat) - cached["mtime"].to_f == fstat.mtime.to_f - end - def checksum_file(file, digest) File.open(file, 'rb') { |f| checksum_io(f, digest) } end @@ -181,10 +71,3 @@ class Chef end end -module Moneta - module Defaults - def default - nil - end - end -end diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 85dce44474..74b10a4250 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -310,9 +310,7 @@ class Chef start_handlers [] # Checksum Cache - # Uses Moneta on the back-end - cache_type "BasicFile" - cache_options({ :path => platform_specific_path("/var/chef/cache/checksums"), :skip_expires => true }) + cache_options({ :path => platform_specific_path("/var/chef/cache/checksums") }) # Set to false to silence Chef 11 deprecation warnings: chef11_deprecation_warnings true diff --git a/lib/chef/monkey_patches/moneta.rb b/lib/chef/monkey_patches/moneta.rb deleted file mode 100644 index 1c2895db56..0000000000 --- a/lib/chef/monkey_patches/moneta.rb +++ /dev/null @@ -1,50 +0,0 @@ -# -# Author:: Seth Chisamore (<schisamo@opscode.com>) -# Copyright:: Copyright (c) 2011 Opscode, Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -# ensure data is written and read in binary mode -# stops "dump format error for symbol(0x75)" errors -module Moneta - class BasicFile - - def store(key, value, options = {}) - ensure_directory_created(::File.dirname(path(key))) - ::File.open(path(key), "wb") do |file| - if @expires - data = {:value => value} - if options[:expires_in] - data[:expires_at] = Time.now + options[:expires_in] - end - contents = Marshal.dump(data) - else - contents = Marshal.dump(value) - end - file.puts(contents) - end - end - - def raw_get(key) - if ::File.respond_to?(:binread) - data = ::File.binread(path(key)) - else - data = ::File.open(path(key),"rb") { |f| f.read } - end - Marshal.load(data) - end - - end -end |