Class: Kitchen::Box::Cache

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

Overview

Stores verified archive copies. Drivers keep a lease until destroy, including after partial creation, so a prune plan can account for in-flight work.

Constant Summary collapse

ENV_KEY =
"KITCHEN_BOX_CACHE".freeze
DIGEST =
/\A[0-9a-f]{64}\z/
TOKEN =
/\A[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Cache

Returns a new instance of Cache.



23
24
25
# File 'lib/kitchen/box/cache.rb', line 23

def initialize(path = nil)
  @root = File.expand_path(path || ENV[ENV_KEY] || "~/.cache/test-kitchen/boxes")
end

Instance Attribute Details

#root ⇒ Object (readonly)

Returns the value of attribute root.



21
22
23
# File 'lib/kitchen/box/cache.rb', line 21

def root
  @root
end

Instance Method Details

#fetch(catalog, entry, token) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kitchen/box/cache.rb', line 33

def fetch(catalog, entry, token)
  digest = entry.fetch("sha256")
  validate_token(token)
  directory = entry_path(digest)
  validate_location(directory, catalog.directory)
  FileUtils.mkdir_p(root, mode: 0o700)
  create_directory(directory)

  with_lock(directory) do
    leases = File.join(directory, "leases")
    create_directory(leases)
    create_lease(File.join(leases, token))
    archive = File.join(directory, "archive.box")
    populate(directory, catalog, entry) unless verified?(archive, entry)
    remember_identity(directory, entry)
    archive
  end
rescue SystemCallError, IOError => e
  raise UserError, "Cannot cache box in #{root}: #{e.message}"
end

#prune_plan(**filters) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kitchen/box/cache.rb', line 70

def prune_plan(**filters)
  return { removable: [], protected: [], reclaimable_bytes: 0 } unless File.directory?(root) && !File.symlink?(root)

  removable = []
  protected = []
  Dir.children(root).sort.each do |digest|
    next unless DIGEST.match?(digest)

    candidate, reason = inspect_entry(digest, filters)
    next unless candidate

    if reason
      protected << candidate.merge(reason: reason)
    else
      removable << candidate
    end
  end
  { removable: removable, protected: protected, reclaimable_bytes: removable.sum { |row| row.fetch(:bytes) } }
rescue SystemCallError, IOError => e
  raise UserError, "Cannot inspect box cache #{root}: #{e.message}"
end

#release(digest, token) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kitchen/box/cache.rb', line 54

def release(digest, token)
  validate_token(token)
  directory = entry_path(digest)
  raise UserError, "Invalid box cache directory: #{root}" if File.symlink?(root)
  return unless existing_directory?(directory)

  leases = File.join(directory, "leases")
  return unless existing_directory?(leases)

  with_lock(directory, create: false) do
    remove_lease(File.join(leases, token))
  end
rescue SystemCallError, IOError => e
  raise UserError, "Cannot release cached box in #{root}: #{e.message}"
end

#validate_destination(catalog, entry) ⇒ Object



27
28
29
30
31
# File 'lib/kitchen/box/cache.rb', line 27

def validate_destination(catalog, entry)
  validate_location(entry_path(entry.fetch("sha256")), catalog.directory)
rescue SystemCallError, IOError => e
  raise UserError, "Cannot inspect box cache destination in #{root}: #{e.message}"
end