diff options
Diffstat (limited to 'lib/chef/cookbook_manifest.rb')
-rw-r--r-- | lib/chef/cookbook_manifest.rb | 149 |
1 files changed, 22 insertions, 127 deletions
diff --git a/lib/chef/cookbook_manifest.rb b/lib/chef/cookbook_manifest.rb index 125f353d21..081f0a38ea 100644 --- a/lib/chef/cookbook_manifest.rb +++ b/lib/chef/cookbook_manifest.rb @@ -1,5 +1,5 @@ # Author:: Daniel DeLeo (<dan@chef.io>) -# Copyright:: Copyright 2015-2016, Chef Software Inc. +# Copyright:: Copyright 2015-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,8 +17,8 @@ require "forwardable" require "chef/mixin/versioned_api" require "chef/util/path_helper" -require "chef/cookbook/manifest_v0" -require "chef/cookbook/manifest_v2" +require "chef/cookbook_manifest/file" +require "chef/cookbook_manifest/versions" require "chef/log" class Chef @@ -38,6 +38,10 @@ class Chef def_delegator :@cookbook_version, :full_name def_delegator :@cookbook_version, :version def_delegator :@cookbook_version, :frozen_version? + def_delegator :@cookbook_version, :manifest_records_by_path + def_delegator :@cookbook_version, :files_for + def_delegator :@cookbook_version, :root_files + def_delegator :@cookbook_version, :each_file # Create a new CookbookManifest object for the given `cookbook_version`. # You can subsequently call #to_hash to get a Hash representation of the @@ -65,7 +69,6 @@ class Chef def reset! @manifest = nil @checksums = nil - @manifest_records_by_path = nil true end @@ -101,26 +104,25 @@ class Chef # } # def manifest - @manifest || generate_manifest + generate_manifest @manifest end def checksums - @manifest || generate_manifest + generate_manifest @checksums end - def manifest_records_by_path - @manifest || generate_manifest - @manifest_records_by_path - end - def policy_mode? @policy_mode end + def all_files + @cookbook_version.all_files + end + def to_hash - CookbookManifestVersions.to_hash(self) + CookbookManifest::Versions.to_hash(self) end def to_json(*a) @@ -155,51 +157,21 @@ class Chef # make the corresponding changes to the cookbook_version object. Required # to provide backward compatibility with CookbookVersion#manifest= method. def update_from(new_manifest) - @manifest = Chef::CookbookManifestVersions.from_hash(new_manifest) - @checksums = extract_checksums_from_manifest(@manifest) - @manifest_records_by_path = extract_manifest_records_by_path(@manifest) - end - - def files_for(part) - return root_files if part.to_s == "root_files" - manifest[:all_files].select do |file| - seg = file[:name].split("/")[0] - part.to_s == seg - end - end - - def each_file(excluded_parts: [], &block) - excluded_parts = Array(excluded_parts).map { |p| p.to_s } - - manifest[:all_files].each do |file| - seg = file[:name].split("/")[0] - next if excluded_parts.include?(seg) - yield file if block_given? + manifest = CookbookManifest::Versions.from_hash(new_manifest) + cookbook_version.cb_files = manifest[:all_files].each_with_object([]) do |file, memo| + memo << CookbookManifest::File.from_hash(file) end end def by_parent_directory @by_parent_directory ||= - manifest[:all_files].inject({}) do |memo, file| - parts = file[:name].split("/") - parent = if parts.length == 1 - "root_files" - else - parts[0] - end - + all_files.each_with_object({}) do |file, memo| + parent = file.part memo[parent] ||= [] memo[parent] << file - memo end end - def root_files - manifest[:all_files].select do |file| - file[:name].split("/").length == 1 - end - end - private def cookbook_url_path @@ -214,29 +186,11 @@ class Chef }) @checksums = {} - if !root_paths || root_paths.size == 0 - Chef::Log.error("Cookbook #{name} does not have root_paths! Cannot generate manifest.") - raise "Cookbook #{name} does not have root_paths! Cannot generate manifest." - end - - @cookbook_version.all_files.each do |file| - next if File.directory?(file) - - name, path, specificity = parse_file_from_root_paths(file) - - csum = checksum_cookbook_file(file) + all_files.each do |file| + csum = file.checksum @checksums[csum] = file - rs = Mash.new({ - :name => name, - :path => path, - :checksum => csum, - :specificity => specificity, - # full_path is not a part of the normal manifest, but is very useful to keep around. - # uploaders should strip this out. - :full_path => file, - }) - manifest[:all_files] << rs + manifest[:all_files] << file.to_hash end manifest[:metadata] = metadata @@ -250,67 +204,8 @@ class Chef manifest[:cookbook_name] = name.to_s end - @manifest_records_by_path = extract_manifest_records_by_path(manifest) @manifest = manifest end - def parse_file_from_root_paths(file) - root_paths.each do |root_path| - pathname = Chef::Util::PathHelper.relative_path_from(root_path, file) - - parts = pathname.each_filename.take(2) - # Check if path is actually under root_path - next if parts[0] == ".." - - # if we have a root_file, such as metadata.rb, the first part will be "." - return [ pathname.to_s, pathname.to_s, "default" ] if parts.length == 1 - - segment = parts[0] - - name = File.join(segment, pathname.basename.to_s) - - if segment == "templates" || segment == "files" - # Check if pathname looks like files/foo or templates/foo (unscoped) - if pathname.each_filename.to_a.length == 2 - # Use root_default in case the same path exists at root_default and default - return [ name, pathname.to_s, "root_default" ] - else - return [ name, pathname.to_s, parts[1] ] - end - else - return [ name, pathname.to_s, "default" ] - end - end - Chef::Log.error("Cookbook file #{file} not under cookbook root paths #{root_paths.inspect}.") - raise "Cookbook file #{file} not under cookbook root paths #{root_paths.inspect}." - end - - def extract_checksums_from_manifest(manifest) - manifest[:all_files].inject({}) do |memo, manifest_record| - memo[manifest_record[:checksum]] = nil - memo - end - end - - def checksum_cookbook_file(filepath) - CookbookVersion.checksum_cookbook_file(filepath) - end - - def extract_manifest_records_by_path(manifest) - manifest[:all_files].inject({}) do |memo, manifest_record| - memo[manifest_record[:path]] = manifest_record - memo - end - end - - end - class CookbookManifestVersions - - extend Chef::Mixin::VersionedAPIFactory - add_versioned_api_class Chef::Cookbook::ManifestV0 - add_versioned_api_class Chef::Cookbook::ManifestV2 - - def_versioned_delegator :from_hash - def_versioned_delegator :to_hash end end |