diff options
author | Pete Higgins <pete@peterhiggins.org> | 2020-07-21 13:07:03 -0700 |
---|---|---|
committer | Pete Higgins <pete@peterhiggins.org> | 2020-07-21 16:44:54 -0700 |
commit | 4c31f7cc35ed76a59c818eb66389d4454b6b20c9 (patch) | |
tree | 2aabe985e04c57c070115e11e10a23ca37e75a3f /lib/chef/cookbook_loader.rb | |
parent | 6d1ce408e055a4904b9a875c47b0b2a0b21a977c (diff) | |
download | chef-4c31f7cc35ed76a59c818eb66389d4454b6b20c9.tar.gz |
Remove global state from cookbook uploader.fix-cookbook-uploader-global-state
Signed-off-by: Pete Higgins <pete@peterhiggins.org>
Diffstat (limited to 'lib/chef/cookbook_loader.rb')
-rw-r--r-- | lib/chef/cookbook_loader.rb | 44 |
1 files changed, 15 insertions, 29 deletions
diff --git a/lib/chef/cookbook_loader.rb b/lib/chef/cookbook_loader.rb index e6e6a4805d..f7efb2646e 100644 --- a/lib/chef/cookbook_loader.rb +++ b/lib/chef/cookbook_loader.rb @@ -44,14 +44,11 @@ class Chef # @return [Array<String>] the array of repo paths containing cookbook dirs attr_reader :repo_paths - attr_accessor :tmp_working_dir_path - # XXX: this is highly questionable combined with the Hash-style each method include Enumerable # @param repo_paths [Array<String>] the array of repo paths containing cookbook dirs def initialize(*repo_paths) - @tmp_working_dir_path = nil @repo_paths = repo_paths.flatten.compact.map { |p| File.expand_path(p) } raise ArgumentError, "You must specify at least one cookbook repo path" if @repo_paths.empty? end @@ -140,27 +137,23 @@ class Chef # This method creates tmp directory and copies all cookbooks into it and creates cookbook loader object which points to tmp directory def self.copy_to_tmp_dir_from_array(cookbooks) - tmp_cookbook_file = Tempfile.new("tmp_working_dir_path") - tmp_cookbook_file.close - @tmp_working_dir_path = tmp_cookbook_file.path - File.unlink(@tmp_working_dir_path) - FileUtils.mkdir_p(@tmp_working_dir_path) - cookbooks.each do |cookbook| - checksums_to_on_disk_paths = cookbook.checksums - cookbook.each_file do |manifest_record| - path_in_cookbook = manifest_record[:path] - on_disk_path = checksums_to_on_disk_paths[manifest_record[:checksum]] - dest = File.join(@tmp_working_dir_path, cookbook.name.to_s, path_in_cookbook) - FileUtils.mkdir_p(File.dirname(dest)) - FileUtils.cp_r(on_disk_path, dest) + Dir.mktmpdir do |tmp_dir| + cookbooks.each do |cookbook| + checksums_to_on_disk_paths = cookbook.checksums + cookbook.each_file do |manifest_record| + path_in_cookbook = manifest_record[:path] + on_disk_path = checksums_to_on_disk_paths[manifest_record[:checksum]] + dest = File.join(tmp_dir, cookbook.name.to_s, path_in_cookbook) + FileUtils.mkdir_p(File.dirname(dest)) + FileUtils.cp_r(on_disk_path, dest) + end end + tmp_cookbook_loader ||= begin + Chef::Cookbook::FileVendor.fetch_from_disk(tmp_dir) + CookbookLoader.new(tmp_dir) + end + yield tmp_cookbook_loader end - tmp_cookbook_loader ||= begin - Chef::Cookbook::FileVendor.fetch_from_disk(@tmp_working_dir_path) - CookbookLoader.new(@tmp_working_dir_path) - end - tmp_cookbook_loader.tmp_working_dir_path = @tmp_working_dir_path - tmp_cookbook_loader end # generates metadata.json adds it in the manifest @@ -181,13 +174,6 @@ class Chef end end - # removes the tmp_dir_path - def unlink! - raise "Invalid directory path." if @tmp_working_dir_path.nil? - - FileUtils.rm_rf(@tmp_working_dir_path) - end - alias :cookbooks :values private |