diff options
-rw-r--r-- | lib/chef/cookbook/cookbook_collection.rb | 6 | ||||
-rw-r--r-- | lib/chef/cookbook/metadata.rb | 20 | ||||
-rw-r--r-- | lib/chef/exceptions.rb | 12 | ||||
-rw-r--r-- | lib/chef/policy_builder/expand_node_object.rb | 2 | ||||
-rw-r--r-- | lib/chef/policy_builder/policyfile.rb | 1 |
5 files changed, 41 insertions, 0 deletions
diff --git a/lib/chef/cookbook/cookbook_collection.rb b/lib/chef/cookbook/cookbook_collection.rb index ae63abfc93..325142ce0b 100644 --- a/lib/chef/cookbook/cookbook_collection.rb +++ b/lib/chef/cookbook/cookbook_collection.rb @@ -41,5 +41,11 @@ class Chef cookbook_versions.each{ |cookbook_name, cookbook_version| self[cookbook_name] = cookbook_version } end + def validate! + each do |cookbook_name, cookbook_version| + cookbook_version.metadata.validate_chef_version! + cookbook_version.metadata.validate_ohai_version! + end + end end end diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb index b7d61bd09e..93c62862a1 100644 --- a/lib/chef/cookbook/metadata.rb +++ b/lib/chef/cookbook/metadata.rb @@ -650,8 +650,28 @@ class Chef ) end + def validate_ohai_version! + unless gem_dep_matches?("ohai", Gem::Version.new(Ohai::VERSION), *ohai_versions) + raise Exceptions::CookbookOhaiVersionMismatch.new(Ohai::VERSION, *ohai_versions) + end + end + + def validate_chef_version! + unless gem_dep_matches?("chef", Gem::Version.new(Chef::VERSION), *chef_versions) + raise Exceptions::CookbookChefVersionMismatch.new(Chef::VERSION, *chef_versions) + end + end + private + def gem_dep_matches?(what, version, *deps) + return true unless deps.length > 0 + deps.each do |dep| + return true if dep.match?(what, version) + end + return false + end + def run_validation if name.nil? @errors = ["The `name' attribute is required in cookbook metadata"] diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 855c86d9cc..ef73caa276 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -482,6 +482,18 @@ class Chef end end + class CookbookChefVersionMismatch < RuntimeError + def initialize(chef_version, *constraints) + super "chef version #{chef_version} is badness" + end + end + + class CookbookOhaiVersionMismatch < RuntimeError + def initialize(ohai_version, *constraints) + super "ohai version #{ohai_version} is badness" + end + end + class MultipleDscResourcesFound < RuntimeError attr_reader :resources_found def initialize(resources_found) diff --git a/lib/chef/policy_builder/expand_node_object.rb b/lib/chef/policy_builder/expand_node_object.rb index 2c6d644e42..848dd00684 100644 --- a/lib/chef/policy_builder/expand_node_object.rb +++ b/lib/chef/policy_builder/expand_node_object.rb @@ -74,11 +74,13 @@ class Chef cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path]) cl.load_cookbooks cookbook_collection = Chef::CookbookCollection.new(cl) + cookbook_collection.validate! run_context = Chef::RunContext.new(node, cookbook_collection, @events) else Chef::Cookbook::FileVendor.fetch_from_remote(api_service) cookbook_hash = sync_cookbooks cookbook_collection = Chef::CookbookCollection.new(cookbook_hash) + cookbook_collection.validate! run_context = Chef::RunContext.new(node, cookbook_collection, @events) end diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb index d6dcdf67b2..3633110d6c 100644 --- a/lib/chef/policy_builder/policyfile.rb +++ b/lib/chef/policy_builder/policyfile.rb @@ -153,6 +153,7 @@ class Chef Chef::Cookbook::FileVendor.fetch_from_remote(http_api) sync_cookbooks cookbook_collection = Chef::CookbookCollection.new(cookbooks_to_sync) + cookbook_collection.validate! run_context = Chef::RunContext.new(node, cookbook_collection, events) setup_chef_class(run_context) |