Class: ChefCLI::Policyfile::CachedCookbook

Inherits:
CookbookLock show all
Defined in:
lib/chef-cli/policyfile/cookbook_locks.rb

Overview

CachedCookbook objects represent a cookbook that has been fetched from an upstream canonical source and stored (presumed unmodified).

Constant Summary

Constants inherited from CookbookLock

ChefCLI::Policyfile::CookbookLock::REQUIRED_LOCK_DATA_KEYS

Instance Attribute Summary collapse

Attributes inherited from CookbookLock

#dotted_decimal_identifier, #identifier, #name, #source_options, #storage_config, #version

Instance Method Summary collapse

Methods inherited from CookbookLock

#chefignore, #cookbook_loader, #cookbook_location_spec, #cookbook_version, #dependencies, #gather_profile_data, #identifier_updated?, #identifiers, #install_locked, #installed?, #symbolize_source_options_keys, #to_lock, #updated?, #version_updated?

Methods included from StorageConfigDelegation

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

Constructor Details

#initialize(name, storage_config) ⇒ CachedCookbook

Returns a new instance of CachedCookbook.



209
210
211
212
213
214
215
216
217
218
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 209

def initialize(name, storage_config)
  @name = name
  @version = nil
  @origin = nil
  @source_options = nil
  @cache_key = nil
  @identifier = nil
  @dotted_decimal_identifier = nil
  @storage_config = storage_config
end

Instance Attribute Details

#cache_keyObject

The directory name in the cookbook cache where the cookbook is stored. By convention, this should be the name of the cookbook followed by a hyphen and then some sort of version identifier (depending on the cookbook source).



204
205
206
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 204

def cache_key
  @cache_key
end

#originObject

A URI pointing to the canonical source of the cookbook.



207
208
209
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 207

def origin
  @origin
end

Instance Method Details

#build_from_lock_data(lock_data) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 228

def build_from_lock_data(lock_data)
  assert_required_keys_valid!(lock_data)

  @version = lock_data["version"]
  @identifier = lock_data["identifier"]
  @dotted_decimal_identifier = lock_data["dotted_decimal_identifier"]
  @cache_key = lock_data["cache_key"]
  @origin = lock_data["origin"]
  @source_options = symbolize_source_options_keys(lock_data["source_options"])
end

#cookbook_pathObject



220
221
222
223
224
225
226
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 220

def cookbook_path
  if cache_key.nil?
    raise MissingCookbookLockData, "Cannot locate cached cookbook `#{name}' because the `cache_key' attribute is not set"
  end

  File.join(cache_path, cache_key)
end

#lock_dataObject



239
240
241
242
243
244
245
246
247
248
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 239

def lock_data
  {
    "version" => version,
    "identifier" => identifier,
    "dotted_decimal_identifier" => dotted_decimal_identifier,
    "cache_key" => cache_key,
    "origin" => origin,
    "source_options" => source_options,
  }
end

#refresh!Object

We do not expect the cookbook to get mutated out-of-band, so refreshing the data generally should have no affect. If the cookbook has been mutated, though, then a CachedCookbookModified exception is raised.



262
263
264
265
266
267
268
269
270
271
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 262

def refresh!
  # This behavior fits better with the intent of the #validate! method,
  # but we cannot check for modifications there because the user may be
  # setting custom identifiers.
  if @identifier && identifiers.content_identifier != @identifier
    message = "Cached cookbook `#{name}' (#{version}) has been modified since the lockfile was generated. " +
      "Cached cookbooks cannot be modified. (full path: `#{cookbook_path}')"
    raise CachedCookbookModified, message
  end
end

#validate!Object



250
251
252
253
254
255
256
257
# File 'lib/chef-cli/policyfile/cookbook_locks.rb', line 250

def validate!
  if cache_key.nil?
    raise CachedCookbookNotFound, "Cookbook `#{name}' does not have a `cache_key` set, cannot locate cookbook"
  end
  unless File.exist?(cookbook_path)
    raise CachedCookbookNotFound, "Cookbook `#{name}' not found at expected cache location `#{cache_key}' (full path: `#{cookbook_path}')"
  end
end