Skip to content

Commit

Permalink
Merge branch 'CHEF-3715'
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Dec 21, 2012
2 parents eed92c7 + 72cf70f commit 5c17ea2
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 630 deletions.
3 changes: 1 addition & 2 deletions chef.gemspec
Expand Up @@ -26,9 +26,8 @@ Gem::Specification.new do |s|
s.add_dependency "net-ssh-multi", "~> 1.1.0"
# CHEF-3027: The knife-cloud plugins require newer features from highline, core chef should not.
s.add_dependency "highline", ">= 1.6.9"
%w{erubis moneta}.each { |gem| s.add_dependency gem }
s.add_dependency "erubis"

# development_dependency thin: eventmachine 0.12.10 doesn't support Ruby 1.9 on Windows
%w(rdoc sdoc ronn rake rack rspec_junit_formatter).each { |gem| s.add_development_dependency gem }
%w(rspec-core rspec-expectations rspec-mocks).each { |gem| s.add_development_dependency gem, "~> 2.8.0" }

Expand Down
11 changes: 3 additions & 8 deletions distro/common/markdown/man1/knife.mkd
Expand Up @@ -98,14 +98,9 @@ If the config file exists, knife uses these settings for __GENERAL OPTIONS__ def
* `chef_server_url`:
URL of the Chef server. Corresponds to the `-s` or `--server-url`
option. This is requested from the user when running this sub-command.
* `cache_type`:
The type of cache to use. Default is BasicFile. This can be any type of
Cache that moneta supports: BasicFile, Berkeley, Couch, DataMapper,
File, LMC, Memcache, Memory, MongoDB, Redis, Rufus, S3, SDBM, Tyrant,
Xattr, YAML.
* `cache_options`:
Specifies various options to use for caching. These options are
dependent on the `cache_type`.
* `syntax_check_cache_path`:
Specifies the path to a directory where knife caches information
about files that it has syntax checked.
* `validation_client_name`:
Specifies the name of the client used to validate new clients.
* `validation_key`:
Expand Down
190 changes: 0 additions & 190 deletions lib/chef/checksum_cache.rb

This file was deleted.

20 changes: 13 additions & 7 deletions lib/chef/config.rb
Expand Up @@ -48,9 +48,6 @@ def self.inspect
end

def self.platform_specific_path(path)
#10.times { puts "* " * 40}
#pp caller

if RUBY_PLATFORM =~ /mswin|mingw|windows/
# turns /etc/chef/client.rb into C:/chef/client.rb
system_drive = ENV['SYSTEMDRIVE'] ? ENV['SYSTEMDRIVE'] : ""
Expand Down Expand Up @@ -309,10 +306,19 @@ def self.formatters
# Start handlers
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 })
# Syntax Check Cache. Knife keeps track of files that is has already syntax
# checked by storing files in this directory. `syntax_check_cache_path` is
# the new (and preferred) configuration setting. If not set, knife will
# fall back to using cache_options[:path].
#
# Because many users will have knife configs with cache_options (generated
# by `knife configure`), the default for now is to *not* set
# syntax_check_cache_path, and thus fallback to cache_options[:path]. We
# leave that value to the same default as was previously set.
syntax_check_cache_path nil

# Deprecated:
cache_options({ :path => platform_specific_path("/var/chef/cache/checksums") })

# Set to false to silence Chef 11 deprecation warnings:
chef11_deprecation_warnings true
Expand Down
75 changes: 61 additions & 14 deletions lib/chef/cookbook/syntax_check.rb
Expand Up @@ -16,19 +16,74 @@
# limitations under the License.
#

require 'chef/checksum_cache'
require 'chef/mixin/shell_out'
require 'chef/mixin/checksum'

class Chef
class Cookbook
# == Chef::Cookbook::SyntaxCheck
# Encapsulates the process of validating the ruby syntax of files in Chef
# cookbooks.
class SyntaxCheck

# == Chef::Cookbook::SyntaxCheck::PersistentSet
# Implements set behavior with disk-based persistence. Objects in the set
# are expected to be strings containing only characters that are valid in
# filenames.
#
# This class is used to track which files have been syntax checked so
# that known good files are not rechecked.
class PersistentSet

attr_reader :cache_path

# Create a new PersistentSet. Values in the set are persisted by
# creating a file in the +cache_path+ directory. If not given, the
# value of Chef::Config[:syntax_check_cache_path] is used; if that
# value is not configured, the value of
# Chef::Config[:cache_options][:path] is used.
#--
# history: prior to Chef 11, the cache implementation was based on
# moneta and configured via cache_options[:path]. Knife configs
# generated with Chef 11 will have `syntax_check_cache_path`, but older
# configs will have `cache_options[:path]`. `cache_options` is marked
# deprecated in chef/config.rb but doesn't currently trigger a warning.
# See also: CHEF-3715
def initialize(cache_path=nil)
@cache_path = cache_path || Chef::Config[:syntax_check_cache_path] || Chef::Config[:cache_options][:path]
@cache_path_created = false
end

# Adds +value+ to the set's collection.
def add(value)
ensure_cache_path_created
FileUtils.touch(File.join(cache_path, value))
end

# Returns true if the set includes +value+
def include?(value)
File.exist?(File.join(cache_path, value))
end

private

def ensure_cache_path_created
return true if @cache_path_created
FileUtils.mkdir_p(cache_path)
@cache_path_created = true
end

end

include Chef::Mixin::ShellOut
include Chef::Mixin::Checksum

attr_reader :cookbook_path

# A PersistentSet object that tracks which files have already been
# validated.
attr_reader :validated_files

# Creates a new SyntaxCheck given the +cookbook_name+ and a +cookbook_path+.
# If no +cookbook_path+ is given, +Chef::Config.cookbook_path+ is used.
def self.for_cookbook(cookbook_name, cookbook_path=nil)
Expand All @@ -44,10 +99,7 @@ def self.for_cookbook(cookbook_name, cookbook_path=nil)
# cookbook_path::: the (on disk) path to the cookbook
def initialize(cookbook_path)
@cookbook_path = cookbook_path
end

def cache
Chef::ChecksumCache.instance
@validated_files = PersistentSet.new
end

def ruby_files
Expand Down Expand Up @@ -81,16 +133,11 @@ def untested_template_files
end

def validated?(file)
!!cache.lookup_checksum(cache_key(file), File.stat(file))
validated_files.include?(checksum(file))
end

def validated(file)
cache.generate_checksum(cache_key(file), file, File.stat(file))
end

def cache_key(file)
@cache_keys ||= {}
@cache_keys[file] ||= cache.generate_key(file, "chef-test")
validated_files.add(checksum(file))
end

def validate_ruby_files
Expand Down Expand Up @@ -118,7 +165,7 @@ def validate_template(erb_file)
result.stderr.each_line { |l| Chef::Log.fatal(l.chomp) }
false
end

def validate_ruby_file(ruby_file)
Chef::Log.debug("Testing #{ruby_file} for syntax errors...")
result = shell_out("ruby -c #{ruby_file}")
Expand All @@ -130,7 +177,7 @@ def validate_ruby_file(ruby_file)
result.stderr.each_line { |l| Chef::Log.fatal(l.chomp) }
false
end

end
end
end
2 changes: 1 addition & 1 deletion lib/chef/cookbook_uploader.rb
Expand Up @@ -3,7 +3,7 @@
require 'rest_client'
require 'chef/exceptions'
require 'chef/knife/cookbook_metadata'
require 'chef/checksum_cache'
require 'chef/digester'
require 'chef/cookbook_version'
require 'chef/cookbook/syntax_check'
require 'chef/cookbook/file_system_file_vendor'
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/cookbook_version.rb
Expand Up @@ -69,7 +69,7 @@ class CookbookVersion
# This is the one and only method that knows how cookbook files'
# checksums are generated.
def self.checksum_cookbook_file(filepath)
Chef::ChecksumCache.generate_md5_checksum_for_file(filepath)
Chef::Digester.generate_md5_checksum_for_file(filepath)
rescue Errno::ENOENT
Chef::Log.debug("File #{filepath} does not exist, so there is no checksum to generate")
nil
Expand Down

0 comments on commit 5c17ea2

Please sign in to comment.