Class: Kitchen::StateFile

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

Overview

State persistence manager for instances between actions and invocations.

Author:

Instance Method Summary collapse

Constructor Details

#initialize(kitchen_root, name) ⇒ StateFile

Constructs an new instance taking the kitchen root and instance name.

Parameters:

  • kitchen_root (String)

    path to the Kitchen project’s root directory

  • name (String)

    name of the instance representing this state



33
34
35
36
37
# File 'lib/kitchen/state_file.rb', line 33

def initialize(kitchen_root, name)
  @file_name = File.expand_path(
    File.join(kitchen_root, ".kitchen", "#{name}.yml")
  )
end

Instance Method Details

#destroyObject

Destroys a state file on disk if it exists.



65
66
67
# File 'lib/kitchen/state_file.rb', line 65

def destroy
  FileUtils.rm_f(file_name) if File.exist?(file_name)
end

#diagnoseHash

Returns a Hash of configuration and other useful diagnostic information.

Returns:

  • (Hash)

    a diagnostic hash



72
73
74
75
76
77
# File 'lib/kitchen/state_file.rb', line 72

def diagnose
  raw = read
  result = {}
  raw.keys.sort.each { |k| result[k] = raw[k] }
  result
end

#readHash

Reads and loads an instance’s state into a Hash data structure which is returned.

Returns:

  • (Hash)

    a hash representation of an instance’s state

Raises:

  • (StateFileLoadError)

    if there is a problem loading the state file from disk and loading it into a Hash



45
46
47
48
49
50
51
# File 'lib/kitchen/state_file.rb', line 45

def read
  if File.exist?(file_name) && !File.zero?(file_name)
    Util.symbolized_hash(deserialize_string(read_file))
  else
    {}
  end
end

#write(state) ⇒ Object

Serializes the state hash and writes a state file to disk.

Parameters:

  • state (Hash)

    the current state of the instance



56
57
58
59
60
61
62
# File 'lib/kitchen/state_file.rb', line 56

def write(state)
  dir = File.dirname(file_name)
  serialized_string = serialize_hash(Util.stringified_hash(state))

  FileUtils.mkdir_p(dir) unless File.directory?(dir)
  File.open(file_name, "wb") { |f| f.write(serialized_string) }
end