Class: ChefDK::PolicyfileLock

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

Defined Under Namespace

Classes: InstallReport

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::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.



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

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.



94
95
96
# File 'lib/chef-dk/policyfile_lock.rb', line 94

def cookbook_locks
  @cookbook_locks
end

#default_attributesObject

Returns the value of attribute default_attributes.



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

def default_attributes
  @default_attributes
end

#included_policy_locksObject (readonly)

Returns the value of attribute included_policy_locks.



96
97
98
# File 'lib/chef-dk/policyfile_lock.rb', line 96

def included_policy_locks
  @included_policy_locks
end

#install_reportObject (readonly)

Returns the value of attribute install_report.



98
99
100
# File 'lib/chef-dk/policyfile_lock.rb', line 98

def install_report
  @install_report
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#named_run_listsObject

Returns the value of attribute named_run_lists.



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

def named_run_lists
  @named_run_lists
end

#override_attributesObject

Returns the value of attribute override_attributes.



88
89
90
# File 'lib/chef-dk/policyfile_lock.rb', line 88

def override_attributes
  @override_attributes
end

#run_listObject

Returns the value of attribute run_list.



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

def run_list
  @run_list
end

#solution_dependenciesObject (readonly)

Returns the value of attribute solution_dependencies.



90
91
92
# File 'lib/chef-dk/policyfile_lock.rb', line 90

def solution_dependencies
  @solution_dependencies
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



92
93
94
# File 'lib/chef-dk/policyfile_lock.rb', line 92

def storage_config
  @storage_config
end

Class Method Details

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

Yields:

  • (lock)


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

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

.build_from_compiler(compiler, storage_config) ⇒ Object



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

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



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

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



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
267
# File 'lib/chef-dk/policyfile_lock.rb', line 231

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



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

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:



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

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.



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

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



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

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



135
136
137
# File 'lib/chef-dk/policyfile_lock.rb', line 135

def dependencies
  yield solution_dependencies
end

#ensure_cache_dir_existsObject



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

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



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

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:



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

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



119
120
121
# File 'lib/chef-dk/policyfile_lock.rb', line 119

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



155
156
157
# File 'lib/chef-dk/policyfile_lock.rb', line 155

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

#to_lockObject



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

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



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

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