class ChefCLI::Policyfile::LocalCookbook

LocalCookbook objects represent cookbooks that are sourced from the local filesystem and are assumed to be under active development.

Attributes

source[RW]

A relative or absolute path to the cookbook. If a relative path is given, it is resolved relative to relative_paths_root

Public Class Methods

new(name, storage_config) click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 283
def initialize(name, storage_config)
  @name = name
  @identifier = nil
  @storage_config = storage_config

  @identifier_updated = false
  @version_updated = false
  @cookbook_in_git_repo = nil
  @scm_info = nil
end

Public Instance Methods

build_from_lock_data(lock_data) click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 327
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"]
  @source = lock_data["source"]
  @source_options = symbolize_source_options_keys(lock_data["source_options"])
  @scm_info = lock_data["scm_info"]
end
cookbook_path() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 294
def cookbook_path
  File.expand_path(source, relative_paths_root)
end
identifier_updated?() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 373
def identifier_updated?
  @identifier_updated
end
lock_data() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 315
def lock_data
  {
    "version" => version,
    "identifier" => identifier,
    "dotted_decimal_identifier" => dotted_decimal_identifier,
    "source" => source,
    "cache_key" => nil,
    "scm_info" => scm_info,
    "source_options" => source_options,
  }
end
refresh!() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 352
def refresh!
  old_identifier, old_version = @identifier, @version
  @identifier, @dotted_decimal_identifier, @version = nil, nil, nil
  gather_profile_data
  if @identifier != old_identifier
    @identifier_updated = true
  end
  if @version != old_version
    @version_updated = true
  end
  self
end
scm_info() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 306
def scm_info
  @scm_info
end
scm_profiler() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 298
def scm_profiler
  if cookbook_in_git_repo?
    CookbookProfiler::Git.new(cookbook_path)
  else
    CookbookProfiler::NullSCM.new(cookbook_path)
  end
end
to_lock() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 310
def to_lock
  refresh_scm_info
  super
end
updated?() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 365
def updated?
  @identifier_updated || @version_updated
end
validate!() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 338
def validate!
  if source.nil?
    raise LocalCookbookNotFound, "Cookbook `#{name}' does not have a `source` set, cannot locate cookbook"
  end
  unless File.exist?(cookbook_path)
    raise LocalCookbookNotFound, "Cookbook `#{name}' not found at path source `#{source}` (full path: `#{cookbook_path}')"
  end

  unless cookbook_version.name.to_s == name
    msg = "The cookbook at path source `#{source}' is expected to be named `#{name}', but is now named `#{cookbook_version.name}' (full path: #{cookbook_path})"
    raise MalformedCookbook, msg
  end
end
version_updated?() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 369
def version_updated?
  @version_updated
end

Private Instance Methods

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

  source = lock_data["source"]
  if source.nil?
    raise InvalidLockfile, "Lockfile cookbook_lock for #{name} is invalid. Lock data for a local cookbook must have a `source' attribute"
  end

  unless source.is_a?(String)
    raise InvalidLockfile, "Lockfile cookbook_lock for #{name} is invalid: `source' attribute must be a String (got: #{source.inspect})"
  end
end
cookbook_in_git_repo?() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 396
def cookbook_in_git_repo?
  return @cookbook_in_git_repo unless @cookbook_in_git_repo.nil?

  @cookbook_in_git_repo = false

  dot_git = Pathname.new(".git")
  Pathname.new(cookbook_path).ascend do |parent_dir|
    possible_git_dir = parent_dir + dot_git
    if possible_git_dir.exist?
      @cookbook_in_git_repo = true
      break
    end
  end

  @cookbook_in_git_repo
end
refresh_scm_info() click to toggle source
# File lib/chef-cli/policyfile/cookbook_locks.rb, line 379
def refresh_scm_info
  @scm_info = scm_profiler.profile_data
end