diff options
author | Mal Graty <mal.graty@googlemail.com> | 2013-03-01 23:29:04 +0000 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2013-05-24 07:12:44 -0700 |
commit | 655444799b7acb90e40161684211352025d008f1 (patch) | |
tree | a4763032f8eb37461cd7db906660b61039d8f31d /lib | |
parent | 4079a344f2001c1927132e7ed6b63453f459609f (diff) | |
download | chef-655444799b7acb90e40161684211352025d008f1.tar.gz |
Fixes CHEF-3307
Use metadata name when loading cookbook
If a name attribute is loaded from a cookbooks metadata, use this for
dependency checks and references as specified in documentation. Ensure real
pathname is retained so that manifests can still be generated correctly.
In addition we must read the metadata implicitly when cookbooks are loaded to
ensure we are using the correct name.
Also include a deprecation warning when encountering missing names in metadata
as this is a planned breaking change in Chef 12.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/cookbook/cookbook_version_loader.rb | 12 | ||||
-rw-r--r-- | lib/chef/cookbook/file_system_file_vendor.rb | 8 | ||||
-rw-r--r-- | lib/chef/cookbook/metadata.rb | 2 | ||||
-rw-r--r-- | lib/chef/cookbook/syntax_check.rb | 8 | ||||
-rw-r--r-- | lib/chef/cookbook_uploader.rb | 2 | ||||
-rw-r--r-- | lib/chef/cookbook_version.rb | 10 |
6 files changed, 26 insertions, 16 deletions
diff --git a/lib/chef/cookbook/cookbook_version_loader.rb b/lib/chef/cookbook/cookbook_version_loader.rb index e98da77d7f..a990489ff6 100644 --- a/lib/chef/cookbook/cookbook_version_loader.rb +++ b/lib/chef/cookbook/cookbook_version_loader.rb @@ -18,13 +18,15 @@ class Chef :provider_filenames] + attr_reader :cookbook_pathname attr_reader :cookbook_name attr_reader :cookbook_settings attr_reader :metadata_filenames def initialize(path, chefignore=nil) @cookbook_path = File.expand_path( path ) - @cookbook_name = File.basename( path ) + @cookbook_pathname = File.basename( path ) + @cookbook_name = @cookbook_pathname @chefignore = chefignore @metadata = Hash.new @relative_path = /#{Regexp.escape(@cookbook_path)}\/(.+)$/ @@ -64,14 +66,18 @@ class Chef if empty? Chef::Log.warn "found a directory #{cookbook_name} in the cookbook path, but it contains no cookbook files. skipping." + else + metadata = @metadata.is_a?(Metadata) ? @metadata : metadata(nil) + @cookbook_name = metadata.name.to_sym unless metadata.name.nil? end + @cookbook_settings end def cookbook_version return nil if empty? - Chef::CookbookVersion.new(@cookbook_name.to_sym).tap do |c| + Chef::CookbookVersion.new(@cookbook_pathname.to_sym).tap do |c| c.root_dir = @cookbook_path c.attribute_filenames = cookbook_settings[:attribute_filenames].values c.definition_filenames = cookbook_settings[:definition_filenames].values @@ -84,6 +90,7 @@ class Chef c.root_filenames = cookbook_settings[:root_filenames].values c.metadata_filenames = @metadata_filenames c.metadata = metadata(c) + c.name = c.metadata.name.to_sym end end @@ -100,6 +107,7 @@ class Chef raise RuntimeError, "Invalid metadata file: #{metadata_file} for cookbook: #{cookbook_version}" end end + Chef::Log.warn "Inferred cookbook names are deprecated, please set a name in metadata" unless @metadata.name @metadata end diff --git a/lib/chef/cookbook/file_system_file_vendor.rb b/lib/chef/cookbook/file_system_file_vendor.rb index 8896e3ed30..926e26e03d 100644 --- a/lib/chef/cookbook/file_system_file_vendor.rb +++ b/lib/chef/cookbook/file_system_file_vendor.rb @@ -41,10 +41,10 @@ class Chef # Chef::Config.cookbook_path file hierarchy for the requested # file. def get_filename(filename) - location = @repo_paths.inject(nil) do |memo, basepath| - candidate_location = File.join(basepath, @cookbook_name, filename) - memo = candidate_location if File.exist?(candidate_location) - memo + cookbooks = Chef::CookbookLoader.new(@repo_paths).load_cookbooks + if cookbooks.has_key?(@cookbook_name) + location = File.join(cookbooks[@cookbook_name].root_dir, filename) + location = nil unless File.exist?(location) end raise "File #{filename} does not exist for cookbook #{@cookbook_name}" unless location diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb index 18368bd99f..0cd740ea8f 100644 --- a/lib/chef/cookbook/metadata.rb +++ b/lib/chef/cookbook/metadata.rb @@ -91,7 +91,7 @@ class Chef # metadata<Chef::Cookbook::Metadata> def initialize(cookbook=nil, maintainer='YOUR_COMPANY_NAME', maintainer_email='YOUR_EMAIL', license='none') @cookbook = cookbook - @name = cookbook ? cookbook.name : "" + @name = cookbook ? cookbook.name : nil @long_description = "" self.maintainer(maintainer) self.maintainer_email(maintainer_email) diff --git a/lib/chef/cookbook/syntax_check.rb b/lib/chef/cookbook/syntax_check.rb index 40ba070796..09905bd9a6 100644 --- a/lib/chef/cookbook/syntax_check.rb +++ b/lib/chef/cookbook/syntax_check.rb @@ -84,14 +84,14 @@ class Chef # validated. attr_reader :validated_files - # Creates a new SyntaxCheck given the +cookbook_name+ and a +cookbook_path+. + # Creates a new SyntaxCheck given the +cookbook_pathname+ 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) + def self.for_cookbook(cookbook_pathname, cookbook_path=nil) cookbook_path ||= Chef::Config.cookbook_path unless cookbook_path - raise ArgumentError, "Cannot find cookbook #{cookbook_name} unless Chef::Config.cookbook_path is set or an explicit cookbook path is given" + raise ArgumentError, "Cannot find cookbook #{cookbook_pathname} unless Chef::Config.cookbook_path is set or an explicit cookbook path is given" end - new(File.join(cookbook_path, cookbook_name.to_s)) + new(File.join(cookbook_path, cookbook_pathname.to_s)) end # Create a new SyntaxCheck object diff --git a/lib/chef/cookbook_uploader.rb b/lib/chef/cookbook_uploader.rb index 9ba5b2bd8b..456c8b6c07 100644 --- a/lib/chef/cookbook_uploader.rb +++ b/lib/chef/cookbook_uploader.rb @@ -159,7 +159,7 @@ class Chef def validate_cookbooks cookbooks.each do |cb| - syntax_checker = Chef::Cookbook::SyntaxCheck.for_cookbook(cb.name, @user_cookbook_path) + syntax_checker = Chef::Cookbook::SyntaxCheck.for_cookbook(cb.pathname, @user_cookbook_path) Chef::Log.info("Validating ruby files") exit(1) unless syntax_checker.validate_ruby_files Chef::Log.info("Validating templates") diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb index a70c892e1b..13d62053f2 100644 --- a/lib/chef/cookbook_version.rb +++ b/lib/chef/cookbook_version.rb @@ -50,6 +50,7 @@ class Chef attr_accessor :resource_filenames attr_accessor :provider_filenames attr_accessor :root_filenames + attr_accessor :pathname attr_accessor :name attr_accessor :metadata attr_accessor :metadata_filenames @@ -193,7 +194,8 @@ class Chef # === Returns # object<Chef::CookbookVersion>:: Duh. :) def initialize(name) - @name = name + @pathname = name + @name = @pathname @frozen = false @attribute_filenames = Array.new @definition_filenames = Array.new @@ -707,11 +709,11 @@ class Chef specificity = "default" if segment == :root_files - matcher = segment_file.match(".+/#{Regexp.escape(name.to_s)}/(.+)") + matcher = segment_file.match(".+/#{Regexp.escape(pathname.to_s)}/(.+)") file_name = matcher[1] path = file_name elsif segment == :templates || segment == :files - matcher = segment_file.match("/#{Regexp.escape(name.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+?)/(.+))") + matcher = segment_file.match("/#{Regexp.escape(pathname.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+?)/(.+))") unless matcher Chef::Log.debug("Skipping file #{segment_file}, as it isn't in any of the proper directories (platform-version, platform or default)") Chef::Log.debug("You probably need to move #{segment_file} into the 'default' sub-directory") @@ -721,7 +723,7 @@ class Chef specificity = matcher[2] file_name = matcher[3] else - matcher = segment_file.match("/#{Regexp.escape(name.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+))") + matcher = segment_file.match("/#{Regexp.escape(pathname.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+))") path = matcher[1] file_name = matcher[2] end |