class Chef::CookbookSynchronizer
Synchronizes the locally cached copies of cookbooks with the files on the server.
Constants
- CookbookFile
Public Class Methods
new(cookbooks_by_name, events)
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 62 def initialize(cookbooks_by_name, events) @eager_segments = Chef::CookbookVersion::COOKBOOK_SEGMENTS.dup unless Chef::Config[:no_lazy_load] @eager_segments.delete(:files) @eager_segments.delete(:templates) end @eager_segments.freeze @cookbooks_by_name, @events = cookbooks_by_name, events @cookbook_full_file_paths = {} end
Public Instance Methods
cache()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 75 def cache Chef::FileCache end
cached_copy_up_to_date?(local_path, expected_checksum)
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 222 def cached_copy_up_to_date?(local_path, expected_checksum) if cache.has_key?(local_path) current_checksum = CookbookVersion.checksum_cookbook_file(cache.load(local_path, false)) expected_checksum == current_checksum else false end end
clear_obsoleted_cookbooks()
click to toggle source
Iterates over cached cookbooks' files, removing files belonging to
cookbooks that don't appear in cookbook_hash
# File lib/chef/cookbook/synchronizer.rb, line 174 def clear_obsoleted_cookbooks @events.cookbook_clean_start # Remove all cookbooks no longer relevant to this node cache.find(File.join(%w{cookbooks ** *})).each do |cache_file| cache_file =~ /^cookbooks\/([^\/]+)\// unless have_cookbook?($1) Chef::Log.info("Removing #{cache_file} from the cache; its cookbook is no longer needed on this client.") cache.delete(cache_file) @events.removed_cookbook_file(cache_file) end end @events.cookbook_clean_complete end
cookbook_count()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 87 def cookbook_count @cookbooks_by_name.size end
cookbook_names()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 79 def cookbook_names @cookbooks_by_name.keys end
cookbooks()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 83 def cookbooks @cookbooks_by_name.values end
download_file(url, destination)
click to toggle source
Unconditionally download the file from the given URL. File will be downloaded to the path
destination
which is relative to the Chef file cache root.
# File lib/chef/cookbook/synchronizer.rb, line 234 def download_file(url, destination) raw_file = server_api.get_rest(url, true) Chef::Log.info("Storing updated #{destination} in the cache.") cache.move_to(raw_file.path, destination) end
files()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 95 def files @files ||= cookbooks.inject([]) do |memo, cookbook| @eager_segments.each do |segment| cookbook.manifest[segment].each do |manifest_record| memo << CookbookFile.new(cookbook, segment, manifest_record) end end memo end end
files_by_cookbook()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 106 def files_by_cookbook files.group_by { |file| file.cookbook } end
files_remaining_by_cookbook()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 110 def files_remaining_by_cookbook @files_remaining_by_cookbook ||= begin files_by_cookbook.inject({}) do |memo, (cookbook, files)| memo[cookbook] = files.size memo end end end
have_cookbook?(cookbook_name)
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 91 def have_cookbook?(cookbook_name) @cookbooks_by_name.key?(cookbook_name) end
mark_cached_file_valid(cache_filename)
click to toggle source
Marks the given file as valid (non-stale).
# File lib/chef/cookbook/synchronizer.rb, line 242 def mark_cached_file_valid(cache_filename) CookbookCacheCleaner.instance.mark_file_as_valid(cache_filename) end
mark_file_synced(file)
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 119 def mark_file_synced(file) files_remaining_by_cookbook[file.cookbook] -= 1 if files_remaining_by_cookbook[file.cookbook] == 0 @events.synchronized_cookbook(file.cookbook.name) end end
save_full_file_path(file, full_path)
click to toggle source
Saves the full_path to the file of the cookbook to be updated in the manifest later
# File lib/chef/cookbook/synchronizer.rb, line 166 def save_full_file_path(file, full_path) @cookbook_full_file_paths[file.cookbook] ||= { } @cookbook_full_file_paths[file.cookbook][file.segment] ||= [ ] @cookbook_full_file_paths[file.cookbook][file.segment] << full_path end
server_api()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 246 def server_api Chef::REST.new(Chef::Config[:chef_server_url]) end
sync_cookbooks()
click to toggle source
Synchronizes all the cookbooks from the chef-server.
)
=== Returns true:: Always returns true
# File lib/chef/cookbook/synchronizer.rb, line 131 def sync_cookbooks Chef::Log.info("Loading cookbooks [#{cookbooks.map {|ckbk| ckbk.name + '@' + ckbk.version}.join(', ')}]") Chef::Log.debug("Cookbooks detail: #{cookbooks.inspect}") clear_obsoleted_cookbooks queue = Chef::Util::ThreadedJobQueue.new files.each do |file| queue << lambda do |lock| full_file_path = sync_file(file) lock.synchronize { # Save the full_path of the downloaded file to be restored in the manifest later save_full_file_path(file, full_file_path) mark_file_synced(file) } end end @events.cookbook_sync_start(cookbook_count) queue.process(Chef::Config[:cookbook_sync_threads]) # Update the full file paths in the manifest update_cookbook_filenames() rescue Exception => e @events.cookbook_sync_failed(cookbooks, e) raise else @events.cookbook_sync_complete true end
sync_file(file)
click to toggle source
Sync an individual file if needed. If there is an up to date copy locally,
nothing is done. Updates file
's manifest with the full
path to the cached file.
Arguments¶ ↑
file<CookbookFile>
Returns¶ ↑
Full path to the cached file as a String
# File lib/chef/cookbook/synchronizer.rb, line 204 def sync_file(file) cache_filename = File.join("cookbooks", file.cookbook.name, file.manifest_record['path']) mark_cached_file_valid(cache_filename) # If the checksums are different between on-disk (current) and on-server # (remote, per manifest), do the update. This will also execute if there # is no current checksum. if !cached_copy_up_to_date?(cache_filename, file.manifest_record['checksum']) download_file(file.manifest_record['url'], cache_filename) @events.updated_cookbook_file(file.cookbook.name, cache_filename) else Chef::Log.debug("Not storing #{cache_filename}, as the cache is up to date.") end # Load the file in the cache and return the full file path to the loaded file cache.load(cache_filename, false) end
update_cookbook_filenames()
click to toggle source
# File lib/chef/cookbook/synchronizer.rb, line 188 def update_cookbook_filenames @cookbook_full_file_paths.each do |cookbook, file_segments| file_segments.each do |segment, full_paths| cookbook.replace_segment_filenames(segment, full_paths) end end end