class ChefCLI::Policyfile::LocalLockFetcher

A policyfile lock fetcher that can read a lock from a local disk

Attributes

name[R]
source_options[R]
storage_config[R]

Public Class Methods

new(name, source_options, storage_config) click to toggle source

Initialize a LocalLockFetcher

@param name [String] The name of the policyfile @param source_options [Hash] A hash with a :path key pointing at the location

of the lock

@param storage_config [StorageConfig]

# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 39
def initialize(name, source_options, storage_config)
  @name = name
  @source_options = source_options
  @storage_config = storage_config
end

Public Instance Methods

apply_locked_source_options(options_from_lock) click to toggle source

Applies source options from a lock file. This is used to make sure that the same policyfile lock is loaded that was locked

@param options_from_lock [Hash] The source options loaded from a policyfile lock

# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 73
def apply_locked_source_options(options_from_lock)
  # There are no options the lock could provide
end
errors() click to toggle source

Check the options provided when creating this class for errors

@return [Array<String>] A list of errors found

# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 54
def errors
  error_messages = []

  [:path].each do |key|
    error_messages << "include_policy for #{name} is missing key #{key}" unless source_options[key]
  end

  error_messages
end
lock_data() click to toggle source

@return [String] of the policyfile lock data

# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 78
def lock_data
  FFI_Yajl::Parser.new.parse(content).tap do |data|
    validate_revision_id(data["revision_id"], source_options)
    data["cookbook_locks"].each do |cookbook_name, cookbook_lock|
      cookbook_path = cookbook_lock["source_options"]["path"]
      unless cookbook_path.nil?
        cookbook_lock["source_options"]["path"] = transform_path(cookbook_path)
      end
    end
  end
end
source_options_for_lock() click to toggle source

@return [Hash] The source_options that describe how to fetch this exact lock again

# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 65
def source_options_for_lock
  source_options
end
valid?() click to toggle source

@return [True] if there were no errors with the provided source_options @return [False] if there were errors with the provided source_options

# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 47
def valid?
  errors.empty?
end

Private Instance Methods

abs_path() click to toggle source
# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 129
def abs_path
  Pathname.new(source_options[:path]).expand_path(storage_config.relative_paths_root)
end
content() click to toggle source
# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 104
def content
  IO.read(path)
end
path() click to toggle source
# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 108
def path
  @path ||= begin
    path = abs_path
    if path.directory?
      path = path.join("#{name}.lock.json")
      unless path.file?
        raise ChefCLI::LocalPolicyfileLockNotFound.new(
          "Expected to find file #{name}.lock.json inside #{source_options[:path]}. If the file name is different than this, provide the file name as part of the path."
        )
      end
    else
      unless path.file?
        raise ChefCLI::LocalPolicyfileLockNotFound.new(
          "The provided path #{source_options[:path]} does not exist."
        )
      end
    end
    path
  end
end
transform_path(path_to_transform) click to toggle source

Transforms cookbook paths to a path relative to the current cookbook for which we are generating a new lock file. Eg: '../cookbooks/base_cookbook'

@param path_to_transform [String] Path to dependent cookbook. @return [Pathname] Path to dependent cookbook relative to the current cookbook/Policyfile.

# File lib/chef-cli/policyfile/local_lock_fetcher.rb, line 98
def transform_path(path_to_transform)
  cur_path = Pathname.new(storage_config.relative_paths_root)
  include_path = Pathname.new(path).dirname
  include_path.relative_path_from(cur_path).join(path_to_transform).to_s
end