class Kitchen::StateFile
State persistence manager for instances between actions and invocations.
@author Fletcher Nichol <fnichol@nichol.ca>
Attributes
@return [String] absolute path to the yaml state file on disk @api private
Public Class Methods
Constructs an new instance taking the kitchen root and instance name.
@param kitchen_root [String] path to the Kitchen
project's root directory @param name [String] name of the instance representing this state
# 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
Public Instance Methods
Destroys a state file on disk if it exists.
# File lib/kitchen/state_file.rb, line 65 def destroy FileUtils.rm_f(file_name) if File.exist?(file_name) end
Returns a Hash
of configuration and other useful diagnostic information.
@return [Hash] a diagnostic hash
# File lib/kitchen/state_file.rb, line 72 def diagnose raw = read result = {} raw.keys.sort.each { |k| result[k] = raw[k] } result end
Reads and loads an instance's state into a Hash
data structure which is returned.
@return [Hash] a hash representation of an instance's state @raise [StateFileLoadError] if there is a problem loading the state file
from disk and loading it into a Hash
# 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
Serializes the state hash and writes a state file to disk.
@param state [Hash] the current state of the instance
# 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
Private Instance Methods
Parses a YAML string and returns a Hash
.
@param string [String] a yaml document as a string @return [Hash] a hash @raise [StateFileLoadError] if the string document cannot be parsed @api private
# File lib/kitchen/state_file.rb, line 97 def deserialize_string(string) YAML.safe_load(string) rescue SyntaxError, Psych::SyntaxError, Psych::DisallowedClass => ex raise StateFileLoadError, "Error parsing #{file_name} (#{ex.message})" end
@return [String] a string representation of the yaml state file @api private
# File lib/kitchen/state_file.rb, line 87 def read_file IO.read(file_name) end
Serializes a Hash
into a YAML string.
@param hash [Hash] a hash @return [String] a yaml document as a string @api private
# File lib/kitchen/state_file.rb, line 108 def serialize_hash(hash) ::YAML.dump(hash) end