Class: ChefCLI::Policyfile::ChefServerLockFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/policyfile/chef_server_lock_fetcher.rb

Overview

A policyfile lock fetcher that can read a lock from a chef server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source_options, chef_config) ⇒ ChefServerLockFetcher

Initialize a LocalLockFetcher

along with :policy_name and either :policy_group or :policy_revision_id. If :policy_name is not provided, name is used.

Examples:

ChefServerLockFetcher for a policyfile with a specific revision id

ChefServerLockFetcher.new("foo",
  {server: "http://example.com", policy_revision_id: "abcdabcdabcd"},
  chef_config)

ChefServerLockFetcher.new("foo",
  {server: "http://example.com", policy_name: "foo", policy_revision_id: "abcdabcdabcd"},
  chef_config)

ChefServerLockFetcher for a policyfile with the latest revision_id for a policy group

ChefServerLockFetcher.new("foo",
  {server: "http://example.com", policy_group: "dev"},
  chef_config)

ChefServerLockFetcher.new("foo",
  {server: "http://example.com", policy_name: "foo", policy_group: "dev"},
  chef_config)

Parameters:

  • name (String)

    The name of the policyfile

  • source_options (Hash)

    A hash with a :server key pointing at the chef server,

  • chef_config (Chef::Config, ChefConfig::Config)


56
57
58
59
60
61
62
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 56

def initialize(name, source_options, chef_config)
  @name = name
  @source_options = source_options
  @chef_config = chef_config

  @source_options[:policy_name] ||= name
end

Instance Attribute Details

#chef_configObject

Returns the value of attribute chef_config.



29
30
31
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 29

def chef_config
  @chef_config
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 27

def name
  @name
end

#source_optionsObject

Returns the value of attribute source_options.



28
29
30
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 28

def source_options
  @source_options
end

Instance Method Details

#apply_locked_source_options(options_from_lock) ⇒ Object

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

Parameters:

  • options_from_lock (Hash)

    The source options loaded from a policyfile lock

Raises:



98
99
100
101
102
103
104
105
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 98

def apply_locked_source_options(options_from_lock)
  options = options_from_lock.inject({}) do |acc, (key, value)|
    acc[key.to_sym] = value
    acc
  end
  source_options.merge!(options)
  raise ChefCLI::InvalidLockfile, "Invalid source_options provided from lock data: #{options_from_lock_file.inspect}" unless valid?
end

#errorsArray<String>

Check the options provided when creating this class for errors

Returns:

  • (Array<String>)

    A list of errors found



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 73

def errors
  error_messages = []

  %i{server policy_name}.each do |key|
    error_messages << "include_policy for #{name} is missing key #{key}" unless source_options[key]
  end

  if %i{policy_revision_id policy_group}.all? { |key| source_options[key].nil? }
    error_messages << "include_policy for #{name} must specify policy_revision_id or policy_group"
  end

  error_messages
end

#lock_dataString

Returns of the policyfile lock data.

Returns:

  • (String)

    of the policyfile lock data



108
109
110
111
112
113
114
115
116
117
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 108

def lock_data
  @lock_data ||= fetch_lock_data.tap do |data|
    data["cookbook_locks"].each do |cookbook_name, cookbook_lock|
      cookbook_lock["source_options"] = {
        "chef_server_artifact" => server,
        "identifier" => cookbook_lock["identifier"],
      }
    end
  end
end

#source_options_for_lockHash

Returns The source_options that describe how to fetch this exact lock again.

Returns:

  • (Hash)

    The source_options that describe how to fetch this exact lock again



88
89
90
91
92
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 88

def source_options_for_lock
  source_options.merge({
    policy_revision_id: lock_data["revision_id"],
  })
end

#valid?True, False

Returns:

  • (True)

    if there were no errors with the provided source_options

  • (False)

    if there were errors with the provided source_options



66
67
68
# File 'lib/chef-cli/policyfile/chef_server_lock_fetcher.rb', line 66

def valid?
  errors.empty?
end