Class: Kitchen::StateFile
- Inherits:
-
Object
- Object
- Kitchen::StateFile
- Defined in:
- lib/kitchen/state_file.rb
Overview
State persistence manager for instances between actions and invocations.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroys a state file on disk if it exists.
-
#diagnose ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information.
-
#initialize(kitchen_root, name) ⇒ StateFile
constructor
Constructs an new instance taking the kitchen root and instance name.
-
#read ⇒ Hash
Reads and loads an instance's state into a Hash data structure which is returned.
-
#write(state) ⇒ Object
Serializes the state hash and writes a state file to disk.
Constructor Details
#initialize(kitchen_root, name) ⇒ StateFile
Constructs an new instance taking the kitchen root and instance name.
41 42 43 44 45 |
# File 'lib/kitchen/state_file.rb', line 41 def initialize(kitchen_root, name) @file_name = File.( File.join(kitchen_root, ".kitchen", "#{name}.yml") ) end |
Instance Method Details
#destroy ⇒ Object
Destroys a state file on disk if it exists.
73 74 75 |
# File 'lib/kitchen/state_file.rb', line 73 def destroy FileUtils.rm_f(file_name) if File.exist?(file_name) end |
#diagnose ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information.
80 81 82 83 84 85 |
# File 'lib/kitchen/state_file.rb', line 80 def diagnose raw = read result = Hash.new raw.keys.sort.each { |k| result[k] = raw[k] } result end |
#read ⇒ Hash
Reads and loads an instance's state into a Hash data structure which is returned.
53 54 55 56 57 58 59 |
# File 'lib/kitchen/state_file.rb', line 53 def read if File.exist?(file_name) && !File.zero?(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.
64 65 66 67 68 69 70 |
# File 'lib/kitchen/state_file.rb', line 64 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 |