Class: Chef::CookbookVersion
- Inherits:
-
Object
- Object
- Chef::CookbookVersion
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/chef/cookbook_version.rb
Overview
Chef::CookbookVersion
CookbookVersion is a model object encapsulating the data about a Chef cookbook. Chef supports maintaining multiple versions of a cookbook on a single server; each version is represented by a distinct instance of this class.
Constant Summary collapse
- COOKBOOK_SEGMENTS =
[ :resources, :providers, :recipes, :definitions, :libraries, :attributes, :files, :templates, :root_files ]
Instance Attribute Summary collapse
-
#all_files ⇒ Object
Returns the value of attribute all_files.
-
#attribute_filenames_by_short_filename ⇒ Object
readonly
Returns the value of attribute attribute_filenames_by_short_filename.
-
#chef_server_rest ⇒ Object
REST API.
-
#identifier ⇒ Object
private
The ‘identifier` field is used for cookbook_artifacts, which are organized on the chef server according to their content.
-
#metadata ⇒ Object
A Chef::Cookbook::Metadata object.
-
#name ⇒ Object
Returns the value of attribute name.
-
#recipe_filenames_by_name ⇒ Object
readonly
Returns the value of attribute recipe_filenames_by_name.
-
#root_paths ⇒ Object
Returns the value of attribute root_paths.
Class Method Summary collapse
-
.available_versions(cookbook_name) ⇒ Object
Given a
cookbook_name, get a list of all versions that exist on the server. - .cache ⇒ Object
-
.checksum_cookbook_file(filepath) ⇒ Object
This is the one and only method that knows how cookbook files’ checksums are generated.
- .chef_server_rest ⇒ Object
- .from_cb_artifact_data(o) ⇒ Object
- .from_hash(o) ⇒ Object
-
.list ⇒ Object
(also: latest_cookbooks)
The API returns only a single version of each cookbook in the result from the cookbooks method.
- .list_all_versions ⇒ Object
- .load(name, version = "_latest") ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#checksums ⇒ Object
Returns a hash of checksums to either nil or the on disk path (which is done by generate_manifest).
- #cookbook_manifest ⇒ Object
- #destroy ⇒ Object
- #display ⇒ Object
- #freeze_version ⇒ Object
-
#frozen_version? ⇒ Boolean
Indicates if this version is frozen or not.
- #full_name ⇒ Object
-
#fully_qualified_recipe_names ⇒ Object
Return recipe names in the form of cookbook_name::recipe_name.
-
#has_cookbook_file_for_node?(node, cookbook_filename) ⇒ Boolean
Query whether a cookbook_file file
cookbook_filenameis available. -
#has_template_for_node?(node, template_filename) ⇒ Boolean
Query whether a template file
template_filenameis available. -
#initialize(name, *root_paths, chef_server_rest: nil) ⇒ CookbookVersion
constructor
Creates a new Chef::CookbookVersion object.
-
#load_recipe(recipe_name, run_context) ⇒ Object
called from DSL.
- #manifest ⇒ Object
- #manifest=(new_manifest) ⇒ Object
- #manifest_records_by_path ⇒ Object
- #metadata_json_file ⇒ Object
- #metadata_rb_file ⇒ Object
- #preferred_filename_on_disk_location(node, segment, filename, current_filepath = nil) ⇒ Object
-
#preferred_manifest_record(node, segment, filename) ⇒ Object
Determine the most specific manifest record for the given segment/filename, given information in the node.
-
#preferred_manifest_records_for_directory(node, segment, dirname) ⇒ Object
Determine the manifest records from the most specific directory for the given node.
- #relative_filenames_in_preferred_directory(node, segment, dirname) ⇒ Object
- #reload_metadata! ⇒ Object
-
#root_dir ⇒ Object
The first root path is the primary cookbook dir, from which metadata is loaded.
- #segment_filenames(segment) ⇒ Object
- #version ⇒ Object
- #version=(new_version) ⇒ Object
Constructor Details
#initialize(name, *root_paths, chef_server_rest: nil) ⇒ CookbookVersion
Creates a new Chef::CookbookVersion object.
Returns
- object<Chef::CookbookVersion>
-
Duh. :)
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/chef/cookbook_version.rb', line 99 def initialize(name, *root_paths, chef_server_rest: nil) @name = name @root_paths = root_paths @frozen = false @all_files = Array.new @file_vendor = nil @cookbook_manifest = Chef::CookbookManifest.new(self) = Chef::Cookbook::Metadata.new @chef_server_rest = chef_server_rest end |
Instance Attribute Details
#all_files ⇒ Object
Returns the value of attribute all_files.
47 48 49 |
# File 'lib/chef/cookbook_version.rb', line 47 def all_files @all_files end |
#attribute_filenames_by_short_filename ⇒ Object (readonly)
Returns the value of attribute attribute_filenames_by_short_filename.
58 59 60 |
# File 'lib/chef/cookbook_version.rb', line 58 def attribute_filenames_by_short_filename @attribute_filenames_by_short_filename end |
#chef_server_rest ⇒ Object
REST API
457 458 459 |
# File 'lib/chef/cookbook_version.rb', line 457 def chef_server_rest @chef_server_rest end |
#identifier ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The ‘identifier` field is used for cookbook_artifacts, which are organized on the chef server according to their content. If the policy_mode option to CookbookManifest is set to true it will include this field in the manifest Hash and in the upload URL.
This field may be removed or have different behavior in the future, don’t use it in 3rd party code.
70 71 72 |
# File 'lib/chef/cookbook_version.rb', line 70 def identifier @identifier end |
#metadata ⇒ Object
A Chef::Cookbook::Metadata object. It has a setter that fixes up the metadata to add descriptions of the recipes contained in this CookbookVersion.
55 56 57 |
# File 'lib/chef/cookbook_version.rb', line 55 def end |
#name ⇒ Object
Returns the value of attribute name.
50 51 52 |
# File 'lib/chef/cookbook_version.rb', line 50 def name @name end |
#recipe_filenames_by_name ⇒ Object (readonly)
Returns the value of attribute recipe_filenames_by_name.
57 58 59 |
# File 'lib/chef/cookbook_version.rb', line 57 def recipe_filenames_by_name @recipe_filenames_by_name end |
#root_paths ⇒ Object
Returns the value of attribute root_paths.
49 50 51 |
# File 'lib/chef/cookbook_version.rb', line 49 def root_paths @root_paths end |
Class Method Details
.available_versions(cookbook_name) ⇒ Object
Given a cookbook_name, get a list of all versions that exist on the server.
Returns
- [String]
-
Array of cookbook versions, which are strings like ‘x.y.z’
- nil
-
if the cookbook doesn’t exist. an error will also be logged.
495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/chef/cookbook_version.rb', line 495 def self.available_versions(cookbook_name) chef_server_rest.get("cookbooks/#{cookbook_name}")[cookbook_name]["versions"].map do |cb| cb["version"] end rescue Net::HTTPServerException => e if e.to_s =~ /^404/ Chef::Log.error("Cannot find a cookbook named #{cookbook_name}") nil else raise end end |
.cache ⇒ Object
91 92 93 |
# File 'lib/chef/cookbook_version.rb', line 91 def self.cache Chef::FileCache end |
.checksum_cookbook_file(filepath) ⇒ Object
This is the one and only method that knows how cookbook files’ checksums are generated.
84 85 86 87 88 89 |
# File 'lib/chef/cookbook_version.rb', line 84 def self.checksum_cookbook_file(filepath) Chef::Digester.generate_md5_checksum_for_file(filepath) rescue Errno::ENOENT Chef::Log.debug("File #{filepath} does not exist, so there is no checksum to generate") nil end |
.chef_server_rest ⇒ Object
461 462 463 |
# File 'lib/chef/cookbook_version.rb', line 461 def self.chef_server_rest Chef::ServerAPI.new(Chef::Config[:chef_server_url], { version_class: Chef::CookbookManifestVersions }) end |
.from_cb_artifact_data(o) ⇒ Object
435 436 437 |
# File 'lib/chef/cookbook_version.rb', line 435 def self.from_cb_artifact_data(o) from_hash(o) end |
.from_hash(o) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/chef/cookbook_version.rb', line 420 def self.from_hash(o) cookbook_version = new(o["cookbook_name"] || o["name"]) # We want the Chef::Cookbook::Metadata object to always be inflated cookbook_version.manifest = o cookbook_version. = Chef::Cookbook::Metadata.from_hash(o["metadata"]) cookbook_version.identifier = o["identifier"] if o.key?("identifier") # We don't need the following step when we decide to stop supporting deprecated operators in the metadata (e.g. <<, >>) cookbook_version.manifest["metadata"] = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(cookbook_version.)) cookbook_version.freeze_version if o["frozen?"] cookbook_version end |
.list ⇒ Object Also known as: latest_cookbooks
The API returns only a single version of each cookbook in the result from the cookbooks method
476 477 478 |
# File 'lib/chef/cookbook_version.rb', line 476 def self.list chef_server_rest.get("cookbooks") end |
.list_all_versions ⇒ Object
485 486 487 |
# File 'lib/chef/cookbook_version.rb', line 485 def self.list_all_versions chef_server_rest.get("cookbooks?num_versions=all") end |
.load(name, version = "_latest") ⇒ Object
470 471 472 473 |
# File 'lib/chef/cookbook_version.rb', line 470 def self.load(name, version = "_latest") version = "_latest" if version == "latest" from_hash(chef_server_rest.get("cookbooks/#{name}/#{version}")) end |
Instance Method Details
#<=>(other) ⇒ Object
508 509 510 511 512 513 514 |
# File 'lib/chef/cookbook_version.rb', line 508 def <=>(other) raise Chef::Exceptions::CookbookVersionNameMismatch if name != other.name # FIXME: can we change the interface to the Metadata class such # that metadata.version returns a Chef::Version instance instead # of a string? Chef::Version.new(version) <=> Chef::Version.new(other.version) end |
#checksums ⇒ Object
Returns a hash of checksums to either nil or the on disk path (which is done by generate_manifest).
173 174 175 |
# File 'lib/chef/cookbook_version.rb', line 173 def checksums cookbook_manifest.checksums end |
#cookbook_manifest ⇒ Object
516 517 518 |
# File 'lib/chef/cookbook_version.rb', line 516 def cookbook_manifest @cookbook_manifest ||= CookbookManifest.new(self) end |
#destroy ⇒ Object
465 466 467 468 |
# File 'lib/chef/cookbook_version.rb', line 465 def destroy chef_server_rest.delete("cookbooks/#{name}/#{version}") self end |
#display ⇒ Object
410 411 412 413 414 415 416 417 418 |
# File 'lib/chef/cookbook_version.rb', line 410 def display output = Mash.new output["cookbook_name"] = name output["name"] = full_name output["frozen?"] = frozen_version? output["metadata"] = .to_hash output["version"] = version output.merge(cookbook_manifest.by_parent_directory) end |
#freeze_version ⇒ Object
123 124 125 |
# File 'lib/chef/cookbook_version.rb', line 123 def freeze_version @frozen = true end |
#frozen_version? ⇒ Boolean
Indicates if this version is frozen or not. Freezing a coobkook version indicates that a new cookbook with the same name and version number shoule
119 120 121 |
# File 'lib/chef/cookbook_version.rb', line 119 def frozen_version? @frozen end |
#full_name ⇒ Object
132 133 134 |
# File 'lib/chef/cookbook_version.rb', line 132 def full_name "#{name}-#{version}" end |
#fully_qualified_recipe_names ⇒ Object
Return recipe names in the form of cookbook_name::recipe_name
182 183 184 185 186 187 188 189 |
# File 'lib/chef/cookbook_version.rb', line 182 def fully_qualified_recipe_names files_for("recipes").inject([]) do |memo, recipe| rname = recipe[:name].split("/")[1] rname = File.basename(rname, ".rb") memo << "#{name}::#{rname}" memo end end |
#has_cookbook_file_for_node?(node, cookbook_filename) ⇒ Boolean
Query whether a cookbook_file file cookbook_filename is available. File specificity for the given node is obeyed in the lookup.
221 222 223 |
# File 'lib/chef/cookbook_version.rb', line 221 def has_cookbook_file_for_node?(node, cookbook_filename) !!find_preferred_manifest_record(node, :files, cookbook_filename) end |
#has_template_for_node?(node, template_filename) ⇒ Boolean
Query whether a template file template_filename is available. File specificity for the given node is obeyed in the lookup.
215 216 217 |
# File 'lib/chef/cookbook_version.rb', line 215 def has_template_for_node?(node, template_filename) !!find_preferred_manifest_record(node, :templates, template_filename) end |
#load_recipe(recipe_name, run_context) ⇒ Object
called from DSL
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/chef/cookbook_version.rb', line 192 def load_recipe(recipe_name, run_context) unless recipe_filenames_by_name.has_key?(recipe_name) raise Chef::Exceptions::RecipeNotFound, "could not find recipe #{recipe_name} for cookbook #{name}" end Chef::Log.debug("Found recipe #{recipe_name} in cookbook #{name}") recipe = Chef::Recipe.new(name, recipe_name, run_context) recipe_filename = recipe_filenames_by_name[recipe_name] unless recipe_filename raise Chef::Exceptions::RecipeNotFound, "could not find #{recipe_name} files for cookbook #{name}" end recipe.from_file(recipe_filename) recipe end |
#manifest ⇒ Object
163 164 165 |
# File 'lib/chef/cookbook_version.rb', line 163 def manifest cookbook_manifest.manifest end |
#manifest=(new_manifest) ⇒ Object
167 168 169 |
# File 'lib/chef/cookbook_version.rb', line 167 def manifest=(new_manifest) cookbook_manifest.update_from(new_manifest) end |
#manifest_records_by_path ⇒ Object
177 178 179 |
# File 'lib/chef/cookbook_version.rb', line 177 def manifest_records_by_path cookbook_manifest.manifest_records_by_path end |
#metadata_json_file ⇒ Object
439 440 441 |
# File 'lib/chef/cookbook_version.rb', line 439 def File.join(root_paths[0], "metadata.json") end |
#metadata_rb_file ⇒ Object
443 444 445 |
# File 'lib/chef/cookbook_version.rb', line 443 def File.join(root_paths[0], "metadata.rb") end |
#preferred_filename_on_disk_location(node, segment, filename, current_filepath = nil) ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/chef/cookbook_version.rb', line 276 def preferred_filename_on_disk_location(node, segment, filename, current_filepath = nil) manifest_record = preferred_manifest_record(node, segment, filename) if current_filepath && (manifest_record["checksum"] == self.class.checksum_cookbook_file(current_filepath)) nil else file_vendor.get_filename(manifest_record["path"]) end end |
#preferred_manifest_record(node, segment, filename) ⇒ Object
Determine the most specific manifest record for the given segment/filename, given information in the node. Throws FileNotFound if there is no such segment and filename in the manifest.
A manifest record is a Mash that follows the following form:
:name => "example.rb",
:path => "files/default/example.rb",
:specificity => "default",
:checksum => "1234"
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/chef/cookbook_version.rb', line 237 def preferred_manifest_record(node, segment, filename) found_pref = find_preferred_manifest_record(node, segment, filename) if found_pref manifest_records_by_path[found_pref] else if segment == :files || segment == :templates = "Cookbook '#{name}' (#{version}) does not contain a file at any of these locations:\n" error_locations = if filename.is_a?(Array) filename.map { |name| " #{File.join(segment.to_s, name)}" } else [ " #{segment}/#{node[:platform]}-#{node[:platform_version]}/#{filename}", " #{segment}/#{node[:platform]}/#{filename}", " #{segment}/default/#{filename}", " #{segment}/#{filename}", ] end << error_locations.join("\n") existing_files = segment_filenames(segment) # Strip the root_dir prefix off all files for readability pretty_existing_files = existing_files.map do |path| if root_dir path[root_dir.length + 1..-1] else path end end # Show the files that the cookbook does have. If the user made a typo, # hopefully they'll see it here. unless pretty_existing_files.empty? << "\n\nThis cookbook _does_ contain: ['#{pretty_existing_files.join("','")}']" end raise Chef::Exceptions::FileNotFound, else raise Chef::Exceptions::FileNotFound, "cookbook #{name} does not contain file #{segment}/#{filename}" end end end |
#preferred_manifest_records_for_directory(node, segment, dirname) ⇒ Object
Determine the manifest records from the most specific directory for the given node. See #preferred_manifest_record for a description of entries of the returned Array.
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/chef/cookbook_version.rb', line 325 def preferred_manifest_records_for_directory(node, segment, dirname) preferences = preferences_for_path(node, segment, dirname) records_by_pref = Hash.new preferences.each { |pref| records_by_pref[pref] = Array.new } files_for(segment).each do |manifest_record| manifest_record_path = manifest_record[:path] # extract the preference part from the path. if manifest_record_path =~ /(#{Regexp.escape(segment.to_s)}\/[^\/]+\/#{Regexp.escape(dirname)})\/.+$/ # Note the specificy_dirname includes the segment and # dirname argument as above, which is what # preferences_for_path returns. It could be # "files/ubuntu-9.10/dirname", for example. specificity_dirname = $1 # Record the specificity_dirname only if it's in the list of # valid preferences if records_by_pref[specificity_dirname] records_by_pref[specificity_dirname] << manifest_record end end end best_pref = preferences.find { |pref| !records_by_pref[pref].empty? } raise Chef::Exceptions::FileNotFound, "cookbook #{name} (#{version}) has no directory #{segment}/default/#{dirname}" unless best_pref records_by_pref[best_pref] end |
#relative_filenames_in_preferred_directory(node, segment, dirname) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/chef/cookbook_version.rb', line 285 def relative_filenames_in_preferred_directory(node, segment, dirname) preferences = preferences_for_path(node, segment, dirname) filenames_by_pref = Hash.new preferences.each { |pref| filenames_by_pref[pref] = Array.new } files_for(segment).each do |manifest_record| manifest_record_path = manifest_record[:path] # find the NON SPECIFIC filenames, but prefer them by filespecificity. # For example, if we have a file: # 'files/default/somedir/somefile.conf' we only keep # 'somedir/somefile.conf'. If there is also # 'files/$hostspecific/somedir/otherfiles' that matches the requested # hostname specificity, that directory will win, as it is more specific. # # This is clearly ugly b/c the use case is for remote directory, where # we're just going to make cookbook_files out of these and make the # cookbook find them by filespecificity again. but it's the shortest # path to "success" for now. if manifest_record_path =~ /(#{Regexp.escape(segment.to_s)}\/[^\/]+\/#{Regexp.escape(dirname)})\/.+$/ specificity_dirname = $1 non_specific_path = manifest_record_path[/#{Regexp.escape(segment.to_s)}\/[^\/]+\/#{Regexp.escape(dirname)}\/(.+)$/, 1] # Record the specificity_dirname only if it's in the list of # valid preferences if filenames_by_pref[specificity_dirname] filenames_by_pref[specificity_dirname] << non_specific_path end end end best_pref = preferences.find { |pref| !filenames_by_pref[pref].empty? } raise Chef::Exceptions::FileNotFound, "cookbook #{name} has no directory #{segment}/default/#{dirname}" unless best_pref filenames_by_pref[best_pref] end |
#reload_metadata! ⇒ Object
447 448 449 450 451 |
# File 'lib/chef/cookbook_version.rb', line 447 def if File.exists?() .from_json(IO.read()) end end |
#root_dir ⇒ Object
The first root path is the primary cookbook dir, from which metadata is loaded
73 74 75 |
# File 'lib/chef/cookbook_version.rb', line 73 def root_dir root_paths[0] end |
#segment_filenames(segment) ⇒ Object
209 210 211 |
# File 'lib/chef/cookbook_version.rb', line 209 def segment_filenames(segment) files_for(segment).map { |f| f["full_path"] || File.join(root_dir, f["path"]) } end |
#version ⇒ Object
112 113 114 |
# File 'lib/chef/cookbook_version.rb', line 112 def version .version end |
#version=(new_version) ⇒ Object
127 128 129 130 |
# File 'lib/chef/cookbook_version.rb', line 127 def version=(new_version) cookbook_manifest.reset! .version(new_version) end |