class ChefDK::Policyfile::CookbookLock

Base class for CookbookLock implementations

Constants

REQUIRED_LOCK_DATA_KEYS

Attributes

dotted_decimal_identifier[RW]

A string in “X.Y.Z” version number format that uniquely identifies the cookbook version. This is for compatibility with Chef Infra Server 11.x, where cookbooks are stored by x.y.z version numbers.

identifier[RW]

A string that uniquely identifies the cookbook version. If not explicitly set, an identifier is generated based on the cookbook's content.

name[R]

The cookbook name (without any version or other info suffixed)

source_options[RW]

Options specifying the source and revision of this cookbook. These can be passed to a CookbookLocationSpecification to create an object that can install the same revision of the cookbook on another machine.

storage_config[R]
version[RW]

Public Class Methods

new(name, storage_config) click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 65
def initialize(name, storage_config)
  @name = name
  @version = nil
  @source_options = 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-dk/policyfile/cookbook_locks.rb, line 116
def build_from_lock_data(lock_data)
  raise NotImplementedError, "#{self.class} must override #build_from_lock_data with a specific implementation"
end
chefignore() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 161
def chefignore
  @chefignore ||= Chef::Cookbook::Chefignore.new(File.join(cookbook_path, "chefignore"))
end
cookbook_loader() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 152
def cookbook_loader
  @cookbook_loader ||=
    begin
      loader = Chef::Cookbook::CookbookVersionLoader.new(cookbook_path, chefignore)
      loader.load!
      loader
    end
end
cookbook_location_spec() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 82
def cookbook_location_spec
  raise InvalidCookbookLockData, "Cannot create CookbookLocationSpecification for #{name} without version" if version.nil?
  raise InvalidCookbookLockData, "Cannot create CookbookLocationSpecification for #{name} without source options" if source_options.nil?

  @location_spec ||= CookbookLocationSpecification.new(name, "= #{version}", source_options, storage_config)
end
cookbook_path() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 103
def cookbook_path
  raise NotImplementedError, "#{self.class} must override #cookbook_path with a specific implementation"
end
cookbook_version() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 148
def cookbook_version
  @cookbook_version ||= cookbook_loader.cookbook_version
end
dependencies() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 89
def dependencies
  cookbook_location_spec.dependencies
end
gather_profile_data() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 93
def gather_profile_data
  @identifier ||= identifiers.content_identifier
  @dotted_decimal_identifier ||= identifiers.dotted_decimal_identifier
  @version ||= identifiers.semver_version
end
identifier_updated?() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 132
def identifier_updated?
  false
end
identifiers() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 99
def identifiers
  @identifiers ||= CookbookProfiler::Identifiers.new(cookbook_version)
end
install_locked() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 78
def install_locked
  cookbook_location_spec.ensure_cached
end
installed?() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 74
def installed?
  cookbook_location_spec.installed?
end
lock_data() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 112
def lock_data
  raise NotImplementedError, "#{self.class} must override #lock_data a specific implementation"
end
refresh!() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 124
def refresh!
  raise NotImplementedError, "#{self.class} must override #refresh! with a specific implementation"
end
symbolize_source_options_keys(source_options_from_json) click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 140
def symbolize_source_options_keys(source_options_from_json)
  source_options_from_json ||= {}
  source_options_from_json.inject({}) do |normalized_source_opts, (key, value)|
    normalized_source_opts[key.to_sym] = value
    normalized_source_opts
  end
end
to_lock() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 107
def to_lock
  validate!
  lock_data
end
updated?() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 128
def updated?
  false
end
validate!() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 120
def validate!
  raise NotImplementedError, "#{self.class} must override #validate! with a specific implementation"
end
version_updated?() click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 136
def version_updated?
  false
end

Private Instance Methods

assert_required_keys_valid!(lock_data) click to toggle source
# File lib/chef-dk/policyfile/cookbook_locks.rb, line 167
def assert_required_keys_valid!(lock_data)
  missing_keys = REQUIRED_LOCK_DATA_KEYS.reject { |key| lock_data.key?(key) }
  unless missing_keys.empty?
    raise InvalidLockfile, "Lockfile cookbook_lock for #{name} missing required attributes `#{missing_keys.join("', `")}'"
  end

  version = lock_data["version"]
  unless version.is_a?(String)
    raise InvalidLockfile, "Lockfile cookbook_lock for #{name} `version' attribute must be a string (got: #{version})"
  end

  identifier = lock_data["identifier"]
  unless identifier.is_a?(String)
    raise InvalidLockfile, "Lockfile cookbook_lock for #{name} `identifier' attribute must be a string (got: #{identifier})"
  end

  cache_key = lock_data["cache_key"]
  unless cache_key.is_a?(String) || cache_key.nil?
    raise InvalidLockfile, "Lockfile cookbook_lock for #{name} `cache_key' attribute must be a string (got: #{cache_key})"
  end

  source_options = lock_data["source_options"]
  unless source_options.is_a?(Hash)
    raise InvalidLockfile, "Lockfile cookbook_lock for #{name} `source_options' attribute must be a Hash (JSON object) (got: #{source_options})"
  end
end