class ChefCLI::Policyfile::CachedCookbook

CachedCookbook objects represent a cookbook that has been fetched from an upstream canonical source and stored (presumed unmodified).

Attributes

cache_key[RW]

The directory name in the cookbook cache where the cookbook is stored. By convention, this should be the name of the cookbook followed by a hyphen and then some sort of version identifier (depending on the cookbook source).

origin[RW]

A URI pointing to the canonical source of the cookbook.

Public Class Methods

new(name, storage_config) click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 209
def initialize(name, storage_config)
  @name = name
  @version = nil
  @origin = nil
  @source_options = nil
  @cache_key = nil
  @identifier = nil
  @dotted_decimal_identifier = nil
  @storage_config = storage_config
end

Public Instance Methods

build_from_lock_data(lock_data) click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 228
def build_from_lock_data(lock_data)
  assert_required_keys_valid!(lock_data)

  @version = lock_data["version"]
  @identifier = lock_data["identifier"]
  @dotted_decimal_identifier = lock_data["dotted_decimal_identifier"]
  @cache_key = lock_data["cache_key"]
  @origin = lock_data["origin"]
  @source_options = symbolize_source_options_keys(lock_data["source_options"])
end
cookbook_path() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 220
def cookbook_path
  if cache_key.nil?
    raise MissingCookbookLockData, "Cannot locate cached cookbook `#{name}' because the `cache_key' attribute is not set"
  end

  File.join(cache_path, cache_key)
end
lock_data() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 239
def lock_data
  {
    "version" => version,
    "identifier" => identifier,
    "dotted_decimal_identifier" => dotted_decimal_identifier,
    "cache_key" => cache_key,
    "origin" => origin,
    "source_options" => source_options,
  }
end
refresh!() click to toggle source

We do not expect the cookbook to get mutated out-of-band, so refreshing the data generally should have no affect. If the cookbook has been mutated, though, then a CachedCookbookModified exception is raised.

# File lib/chef-cli/policyfile/cookbook_locks.rb, line 262
def refresh!
  # This behavior fits better with the intent of the #validate! method,
  # but we cannot check for modifications there because the user may be
  # setting custom identifiers.
  if @identifier && identifiers.content_identifier != @identifier
    message = "Cached cookbook `#{name}' (#{version}) has been modified since the lockfile was generated. " +
      "Cached cookbooks cannot be modified. (full path: `#{cookbook_path}')"
    raise CachedCookbookModified, message
  end
end
validate!() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 250
def validate!
  if cache_key.nil?
    raise CachedCookbookNotFound, "Cookbook `#{name}' does not have a `cache_key` set, cannot locate cookbook"
  end
  unless File.exist?(cookbook_path)
    raise CachedCookbookNotFound, "Cookbook `#{name}' not found at expected cache location `#{cache_key}' (full path: `#{cookbook_path}')"
  end
end