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.

Instance Method Summary collapse

Constructor Details

#initialize(kitchen_root, name) ⇒ StateFile

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



39
40
41
42
43
# File 'lib/kitchen/state_file.rb', line 39

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.



71
72
73
# File 'lib/kitchen/state_file.rb', line 71

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

#diagnoseHash

Returns a Hash of configuration and other useful diagnostic information.



78
79
80
81
82
83
# File 'lib/kitchen/state_file.rb', line 78

def diagnose
  raw = read
  result = Hash.new
  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.

Raises:

  • (StateFileLoadError)

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



51
52
53
54
55
56
57
# File 'lib/kitchen/state_file.rb', line 51

def read
  if File.exists?(file_name)
    Util.symbolized_hash(deserialize_string(read_file))
  else
    Hash.new
  end
end

#write(state) ⇒ Object

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



62
63
64
65
66
67
68
# File 'lib/kitchen/state_file.rb', line 62

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

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