Class: ChefCLI::PolicyfileLock

Inherits:
Object
  • Object
show all
Includes:
ChefCLI::Policyfile::StorageConfigDelegation
Defined in:
lib/chef-cli/policyfile_lock.rb

Defined Under Namespace

Classes: InstallReport

Constant Summary collapse

RUN_LIST_ITEM_FORMAT =
/\Arecipe\[[^\s]+::[^\s]+\]\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChefCLI::Policyfile::StorageConfigDelegation

#cache_path, #policyfile_expanded_path, #policyfile_filename, #policyfile_lock_expanded_path, #relative_paths_root

Constructor Details

#initialize(storage_config, ui: nil) ⇒ PolicyfileLock

Returns a new instance of PolicyfileLock.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef-cli/policyfile_lock.rb', line 99

def initialize(storage_config, ui: nil)
  @name = nil
  @run_list = []
  @named_run_lists = {}
  @cookbook_locks = {}
  @relative_paths_root = Dir.pwd
  @storage_config = storage_config
  @ui = ui || UI.null

  @default_attributes = {}
  @override_attributes = {}

  @solution_dependencies = Policyfile::SolutionDependencies.new

  @included_policy_locks = []

  @install_report = InstallReport.new(ui: @ui, policyfile_lock: self)
end

Instance Attribute Details

#cookbook_locksObject (readonly)

Returns the value of attribute cookbook_locks.



93
94
95
# File 'lib/chef-cli/policyfile_lock.rb', line 93

def cookbook_locks
  @cookbook_locks
end

#default_attributesObject

Returns the value of attribute default_attributes.



86
87
88
# File 'lib/chef-cli/policyfile_lock.rb', line 86

def default_attributes
  @default_attributes
end

#included_policy_locksObject (readonly)

Returns the value of attribute included_policy_locks.



95
96
97
# File 'lib/chef-cli/policyfile_lock.rb', line 95

def included_policy_locks
  @included_policy_locks
end

#install_reportObject (readonly)

Returns the value of attribute install_report.



97
98
99
# File 'lib/chef-cli/policyfile_lock.rb', line 97

def install_report
  @install_report
end

#nameObject

Returns the value of attribute name.



83
84
85
# File 'lib/chef-cli/policyfile_lock.rb', line 83

def name
  @name
end

#named_run_listsObject

Returns the value of attribute named_run_lists.



85
86
87
# File 'lib/chef-cli/policyfile_lock.rb', line 85

def named_run_lists
  @named_run_lists
end

#override_attributesObject

Returns the value of attribute override_attributes.



87
88
89
# File 'lib/chef-cli/policyfile_lock.rb', line 87

def override_attributes
  @override_attributes
end

#run_listObject

Returns the value of attribute run_list.



84
85
86
# File 'lib/chef-cli/policyfile_lock.rb', line 84

def run_list
  @run_list
end

#solution_dependenciesObject (readonly)

Returns the value of attribute solution_dependencies.



89
90
91
# File 'lib/chef-cli/policyfile_lock.rb', line 89

def solution_dependencies
  @solution_dependencies
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



91
92
93
# File 'lib/chef-cli/policyfile_lock.rb', line 91

def storage_config
  @storage_config
end

Class Method Details

.build(storage_config) {|lock| ... } ⇒ Object

Yields:

  • (lock)


69
70
71
72
73
# File 'lib/chef-cli/policyfile_lock.rb', line 69

def self.build(storage_config)
  lock = new(storage_config)
  yield lock
  lock
end

.build_from_compiler(compiler, storage_config) ⇒ Object



75
76
77
78
79
# File 'lib/chef-cli/policyfile_lock.rb', line 75

def self.build_from_compiler(compiler, storage_config)
  lock = new(storage_config)
  lock.build_from_compiler(compiler)
  lock
end

Instance Method Details

#build_from_archive(lock_data) ⇒ Object



279
280
281
282
283
284
285
286
287
288
# File 'lib/chef-cli/policyfile_lock.rb', line 279

def build_from_archive(lock_data)
  set_name_from_lock_data(lock_data)
  set_run_list_from_lock_data(lock_data)
  set_named_run_lists_from_lock_data(lock_data)
  set_cookbook_locks_as_archives_from_lock_data(lock_data)
  set_attributes_from_lock_data(lock_data)
  set_solution_dependencies_from_lock_data(lock_data)
  set_included_policy_locks_from_lock_data(lock_data)
  self
end

#build_from_compiler(compiler) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/chef-cli/policyfile_lock.rb', line 230

def build_from_compiler(compiler)
  @name = compiler.name

  @run_list = compiler.normalized_run_list

  @named_run_lists = compiler.normalized_named_run_lists

  compiler.all_cookbook_location_specs.each do |cookbook_name, spec|
    if spec.mirrors_canonical_upstream?
      cached_cookbook(cookbook_name) do |cached_cb|
        cached_cb.cache_key = spec.cache_key
        cached_cb.origin = spec.uri
        cached_cb.source_options = spec.source_options_for_lock
      end
    else
      local_cookbook(cookbook_name) do |local_cb|
        local_cb.source = spec.relative_path
        local_cb.source_options = spec.source_options_for_lock
      end
    end
  end

  @default_attributes = compiler.default_attributes
  @override_attributes = compiler.override_attributes

  @solution_dependencies = compiler.solution_dependencies

  @included_policy_locks = compiler.included_policies.map do |policy|
    {
      "name" => policy.name,
      "revision_id" => policy.revision_id,
      "source_options" => policy.source_options_for_lock,
    }
  end

  self
end

#build_from_lock_data(lock_data) ⇒ Object



268
269
270
271
272
273
274
275
276
277
# File 'lib/chef-cli/policyfile_lock.rb', line 268

def build_from_lock_data(lock_data)
  set_name_from_lock_data(lock_data)
  set_run_list_from_lock_data(lock_data)
  set_named_run_lists_from_lock_data(lock_data)
  set_cookbook_locks_from_lock_data(lock_data)
  set_attributes_from_lock_data(lock_data)
  set_solution_dependencies_from_lock_data(lock_data)
  set_included_policy_locks_from_lock_data(lock_data)
  self
end

#cached_cookbook(name) {|cached_cookbook| ... } ⇒ Object

Yields:



122
123
124
125
126
# File 'lib/chef-cli/policyfile_lock.rb', line 122

def cached_cookbook(name)
  cached_cookbook = Policyfile::CachedCookbook.new(name, storage_config)
  yield cached_cookbook if block_given?
  @cookbook_locks[name] = cached_cookbook
end

#canonical_revision_stringObject

Generates a string representation of the lock data in a specialized format suitable for generating a checksum of the lock itself. Only data that modifies the behavior of a chef-client using the lockfile is included in this format; for example, a modification to the source options in a ‘Policyfile.rb` that yields identical code (such as switching to a github fork at the same revision) will not cause a change in the PolicyfileLock’s canonical_revision_string.

This format is intended to be used only for generating an identifier for a particular revision of a PolicyfileLock. It should not be used as a serialization format, and is not guaranteed to be a stable interface.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chef-cli/policyfile_lock.rb', line 169

def canonical_revision_string
  canonical_rev_text = ""

  canonical_rev_text << "name:#{name}\n"

  run_list.each do |item|
    canonical_rev_text << "run-list-item:#{item}\n"
  end

  named_run_lists.each do |name, run_list|
    run_list.each do |item|
      canonical_rev_text << "named-run-list:#{name};run-list-item:#{item}\n"
    end
  end

  cookbook_locks_for_lockfile.each do |name, lock|
    canonical_rev_text << "cookbook:#{name};id:#{lock["identifier"]}\n"
  end

  canonical_rev_text << "default_attributes:#{canonicalize(default_attributes)}\n"

  canonical_rev_text << "override_attributes:#{canonicalize(override_attributes)}\n"

  canonical_rev_text
end

#cookbook_locks_for_lockfileObject



195
196
197
198
199
200
201
202
# File 'lib/chef-cli/policyfile_lock.rb', line 195

def cookbook_locks_for_lockfile
  cookbook_locks.inject({}) do |locks_map, (name, location_spec)|
    location_spec.validate!
    location_spec.gather_profile_data
    locks_map[name] = location_spec.to_lock
    locks_map
  end.sort.to_h
end

#dependencies {|solution_dependencies| ... } ⇒ Object



134
135
136
# File 'lib/chef-cli/policyfile_lock.rb', line 134

def dependencies
  yield solution_dependencies
end

#ensure_cache_dir_existsObject



300
301
302
303
304
305
# File 'lib/chef-cli/policyfile_lock.rb', line 300

def ensure_cache_dir_exists
  # note: duplicates PolicyfileCompiler#ensure_cache_dir_exists
  unless File.exist?(cache_path)
    FileUtils.mkdir_p(cache_path)
  end
end

#install_cookbooksObject



290
291
292
293
294
295
296
297
298
# File 'lib/chef-cli/policyfile_lock.rb', line 290

def install_cookbooks
  # note: duplicates PolicyfileCompiler#ensure_cache_dir_exists
  ensure_cache_dir_exists

  cookbook_locks.each do |cookbook_name, cookbook_lock|
    install_report.installing_cookbook(cookbook_lock)
    cookbook_lock.install_locked
  end
end

#local_cookbook(name) {|local_cookbook| ... } ⇒ Object

Yields:



128
129
130
131
132
# File 'lib/chef-cli/policyfile_lock.rb', line 128

def local_cookbook(name)
  local_cookbook = Policyfile::LocalCookbook.new(name, storage_config)
  yield local_cookbook if block_given?
  @cookbook_locks[name] = local_cookbook
end

#lock_data_for(cookbook_name) ⇒ Object



118
119
120
# File 'lib/chef-cli/policyfile_lock.rb', line 118

def lock_data_for(cookbook_name)
  @cookbook_locks[cookbook_name]
end

#revision_idObject

Returns a fingerprint of the PolicyfileLock by computing the SHA1 hash of #canonical_revision_string



154
155
156
# File 'lib/chef-cli/policyfile_lock.rb', line 154

def revision_id
  Digest::SHA256.new.hexdigest(canonical_revision_string)
end

#to_lockObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chef-cli/policyfile_lock.rb', line 138

def to_lock
  {}.tap do |lock|
    lock["revision_id"] = revision_id
    lock["name"] = name
    lock["run_list"] = run_list
    lock["named_run_lists"] = named_run_lists unless named_run_lists.empty?
    lock["included_policy_locks"] = included_policy_locks
    lock["cookbook_locks"] = cookbook_locks_for_lockfile
    lock["default_attributes"] = default_attributes
    lock["override_attributes"] = override_attributes
    lock["solution_dependencies"] = solution_dependencies.to_lock
  end
end

#validate_cookbooks!Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/chef-cli/policyfile_lock.rb', line 204

def validate_cookbooks!
  cookbook_locks.each do |name, cookbook_lock|
    cookbook_lock.validate!
    cookbook_lock.refresh!
  end

  # Check that versions and dependencies are still valid. First we need to
  # refresh the dependency info for everything that has changed, then we
  # check that the new versions and dependencies are valid for the working
  # set of cookbooks. We can't do this in a single loop because the user
  # may have modified two cookbooks such that the versions and constraints
  # are only valid when both changes are considered together.
  cookbook_locks.each do |name, cookbook_lock|
    if cookbook_lock.updated?
      solution_dependencies.update_cookbook_dep(name, cookbook_lock.version, cookbook_lock.dependencies)
    end
  end
  cookbook_locks.each do |name, cookbook_lock|
    if cookbook_lock.updated?
      solution_dependencies.test_conflict!(cookbook_lock.name, cookbook_lock.version)
    end
  end

  true
end