/usr/lib/ruby/vendor_ruby/berkshelf/api/dependency_cache.rb is in berkshelf-api 2.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | module Berkshelf::API
# @note
# DependencyCache stores its data internally as a Mash
# The structure is as follows
#
# {
# "cookbook_name" => {
# "x.y.z" => {
# :endpoint_priority => 1,
# :dependencies => { "cookbook_name" => "constraint" },
# :platforms => { "platform" => "constraint" }
# }
# }
# }
class DependencyCache
class << self
# Read an archived cache and re-instantiate it
#
# @param [#to_s] filepath
# path to an archived cache
#
# @raise [Berkshelf::API::SaveNotFoundError]
# @raise [Berkshelf::API::InvalidSaveError]
#
# @return [DependencyCache]
def from_file(filepath)
contents = JSON.parse(File.read(filepath.to_s))
new(contents)
rescue Errno::ENOENT => ex
raise SaveNotFoundError.new(ex)
rescue JSON::ParserError => ex
raise InvalidSaveError.new(ex)
end
end
include Berkshelf::API::Logging
extend Forwardable
def_delegators :@cache, :[], :[]=
# @param [Hash] contents
def initialize(contents = {})
@warmed = false
@cache = Hash[contents].with_indifferent_access
if @cache['endpoints_checksum'] && (@cache['endpoints_checksum'] != Application.config.endpoints_checksum)
log.warn "Endpoints in config have changed - invalidating cache"
@cache.clear
end
@cache.delete('endpoints_checksum')
end
# @param [RemoteCookbook] cookbook
# @param [Ridley::Chef::Cookbook::Metadata] metadata
#
# @return [Hash]
def add(cookbook, metadata)
platforms = metadata.platforms || Hash.new
dependencies = metadata.dependencies || Hash.new
@cache[cookbook.name.to_s] ||= Hash.new
@cache[cookbook.name.to_s][cookbook.version.to_s] = {
endpoint_priority: cookbook.priority,
platforms: platforms,
dependencies: dependencies,
location_type: cookbook.location_type,
location_path: cookbook.location_path
}
end
# Clear any items added to this instance
#
# @return [Hash]
def clear
@cache.clear
end
# Check if the cache knows about the given cookbook version
#
# @param [#to_s] name
# @param [#to_s] version
#
# @return [Boolean]
def has_cookbook?(name, version)
unless cookbook = @cache[name.to_s]
return false
end
cookbook.has_key?(version.to_s)
end
# @param [String] name
# @param [String] version
#
# @return [Hash]
def remove(name, version)
@cache[name.to_s].delete(version.to_s)
if @cache[name.to_s].empty?
@cache.delete(name.to_s)
end
@cache
end
# @return [Boolean]
def empty?
@cache.empty?
end
# @return [Hash]
def to_hash
@cache.to_hash
end
# @param [Hash] options
#
# @return [String]
def to_json(options = {})
output = to_hash
output['endpoints_checksum'] = Application.config.endpoints_checksum if options[:saving]
JSON.generate(output, options)
end
# @param [Hash] options
#
# @return [String]
def to_msgpack(options = {})
output = to_hash
output['endpoints_checksum'] = Application.config.endpoints_checksum if options[:saving]
MessagePack.pack(output)
end
# @return [Array<RemoteCookbook>]
def cookbooks
[].tap do |remote_cookbooks|
@cache.each_pair do |name, versions|
versions.each do |version, metadata|
remote_cookbooks << RemoteCookbook.new(name, version, metadata[:location_type], metadata[:location_path], metadata[:endpoint_priority])
end
end
end
end
def save(path)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w+') { |f| f.write(self.to_json(saving: true)) }
end
def warmed?
@warmed
end
def set_warmed
@warmed = true
end
end
end
|