class ChefCLI::Policyfile::RemoteLockFetcher

A policyfile lock fetcher that can read a lock from a remote location.

Attributes

name[R]
source_options[R]

Public Class Methods

new(name, source_options) click to toggle source

Initialize a RemoteLockFetcher

@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
# File lib/chef-cli/policyfile/remote_lock_fetcher.rb, line 39
def initialize(name, source_options)
  @name = name
  @source_options = source_options
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/remote_lock_fetcher.rb, line 72
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/remote_lock_fetcher.rb, line 53
def errors
  error_messages = []

  [:remote].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/remote_lock_fetcher.rb, line 77
def lock_data
  fetch_lock_data.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?
        raise ChefCLI::InvalidLockfile, "Invalid cookbook path: #{cookbook_path}. Remote Policyfiles should only use remote cookbooks."
      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/remote_lock_fetcher.rb, line 64
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/remote_lock_fetcher.rb, line 46
def valid?
  errors.empty?
end

Private Instance Methods

fetch_lock_data() click to toggle source
# File lib/chef-cli/policyfile/remote_lock_fetcher.rb, line 91
def fetch_lock_data
  FFI_Yajl::Parser.parse(http_client.get(""))
rescue Net::ProtocolError => e
  if e.respond_to?(:response) && e.response.code.to_s == "404"
    raise ChefCLI::PolicyfileLockDownloadError.new("No remote policyfile lock '#{name}' found at #{http_client.url}")
  else
    raise ChefCLI::PolicyfileLockDownloadError.new("HTTP error attempting to fetch policyfile lock from #{http_client.url}")
  end
rescue => e
  raise e
end
http_client() click to toggle source
# File lib/chef-cli/policyfile/remote_lock_fetcher.rb, line 103
def http_client
  @http_client ||= Chef::HTTP.new(source_options[:remote])
end