Class: Kitchen::Box::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/box/catalog.rb

Constant Summary collapse

IDENTITY =
%w{name version provider architecture}.freeze
FIELDS =
(IDENTITY + %w{file bytes sha256}).freeze
ENV_KEY =
"KITCHEN_BOX_MANIFEST".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest) ⇒ Catalog

Returns a new instance of Catalog.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kitchen/box/catalog.rb', line 26

def initialize(manifest)
  raise UserError, "Specify a box manifest with --manifest or #{ENV_KEY}" if manifest.to_s.empty?

  @manifest = File.realpath(manifest)
  @directory = File.dirname(@manifest)
  records = JSON.parse(File.read(@manifest))
  raise UserError, "Box manifest must contain an array: #{@manifest}" unless records.is_a?(Array)

  @entries = records.map.with_index(1) { |record, index| parse_entry(record, index) }
  keys = @entries.map { |entry| entry.values_at(*IDENTITY) }
  raise UserError, "Duplicate name, version, provider and architecture in #{@manifest}" unless keys.uniq.size == keys.size
rescue JSON::ParserError, SystemCallError, IOError => e
  raise UserError, "Cannot read box manifest #{manifest}: #{e.message}"
end

Instance Attribute Details

#directory ⇒ Object (readonly)

Returns the value of attribute directory.



16
17
18
# File 'lib/kitchen/box/catalog.rb', line 16

def directory
  @directory
end

Class Method Details

.host_architecture ⇒ Object



18
19
20
21
22
23
24
# File 'lib/kitchen/box/catalog.rb', line 18

def self.host_architecture
  case RbConfig::CONFIG.fetch("host_cpu")
  when "aarch64", "arm64" then "arm64"
  when "x86_64", "amd64" then "amd64"
  else RbConfig::CONFIG.fetch("host_cpu")
  end
end

Instance Method Details

#entries(name: nil, version: nil, provider: nil, architecture: nil) ⇒ Object



41
42
43
44
# File 'lib/kitchen/box/catalog.rb', line 41

def entries(name: nil, version: nil, provider: nil, architecture: nil)
  filters = { "name" => name, "version" => version, "provider" => provider, "architecture" => architecture }.compact
  @entries.select { |entry| filters.all? { |key, value| entry[key] == value } }
end

#find(name:, version:, provider:, architecture: self.class.host_architecture) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kitchen/box/catalog.rb', line 50

def find(name:, version:, provider:, architecture: self.class.host_architecture)
  unless [name, version, provider, architecture].all? { |value| value.is_a?(String) && !value.empty? }
    raise UserError, "Box resolution requires a name, exact version, provider and architecture"
  end

  entry = entries(name: name, version: version, provider: provider, architecture: architecture).first
  unless entry
    raise UserError, "No box matches #{name} #{version} (#{provider}/#{architecture}) in #{@manifest}"
  end

  entry
end

#resolve(name:, version:, provider:, architecture: self.class.host_architecture) ⇒ Object



46
47
48
# File 'lib/kitchen/box/catalog.rb', line 46

def resolve(name:, version:, provider:, architecture: self.class.host_architecture)
  verify(find(name: name, version: version, provider: provider, architecture: architecture), checksum: true)
end

#verify(entry, checksum: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kitchen/box/catalog.rb', line 63

def verify(entry, checksum: false)
  path = File.realpath(File.join(directory, entry.fetch("file")))
  unless path.start_with?("#{directory.delete_suffix(File::SEPARATOR)}#{File::SEPARATOR}") && File.file?(path) && File.readable?(path)
    raise UserError, "Box must be a regular file inside the manifest directory: #{entry.fetch("file")}"
  end
  raise UserError, "Size mismatch for #{path}" unless File.size(path) == entry.fetch("bytes")
  if checksum && Digest::SHA256.file(path).hexdigest != entry.fetch("sha256")
    raise UserError, "SHA-256 mismatch for #{path}"
  end

  path
rescue SystemCallError, IOError => e
  raise UserError, "Cannot read box #{entry.fetch("file")}: #{e.message}"
end