class Derelict::Parser::Status

Parses the output of “vagrant status”

Constants

PARSE_LIST_FROM_OUTPUT

Regexp to extract the VM list from the “vagrant status” output

PARSE_STATE_FROM_LIST_ITEM

Regexp to extract the state from a line in the VM list

Public Instance Methods

description() click to toggle source

Provides a description of this Parser

Mainly used for log messages.

# File lib/derelict/parser/status.rb, line 53
def description
  "Derelict::Parser::Status instance"
end
exists?(vm_name = nil) click to toggle source

Determines if a particular virtual machine exists in the output

* vm_name: The name of the virtual machine to look for
# File lib/derelict/parser/status.rb, line 32
def exists?(vm_name = nil)
  return (vm_names.count > 0) if vm_name.nil?
  vm_names.include? vm_name.to_sym
end
state(vm_name) click to toggle source

Determines the state of a particular virtual machine

The state is returned as a symbol, e.g. :running.

* vm_name: The name of the virtual machine to retrieve state
# File lib/derelict/parser/status.rb, line 42
def state(vm_name)
  unless states.include? vm_name.to_sym
    raise Derelict::VirtualMachine::NotFound.new vm_name
  end

  states[vm_name.to_sym]
end
vm_names() click to toggle source

Retrieves the names of all virtual machines in the output

The names are returned as a Set of symbols.

# File lib/derelict/parser/status.rb, line 25
def vm_names
  Set[*states.keys]
end

Private Instance Methods

parse_line(match) click to toggle source
# File lib/derelict/parser/status.rb, line 81
def parse_line(match)
  raise InvalidFormat.new "Couldn't parse VM list" if match.nil?
  Hash[*match.captures[0..1].map {|value| sanitize value }]
end
sanitize(value) click to toggle source
# File lib/derelict/parser/status.rb, line 86
def sanitize(value)
  value.to_s.gsub(/\s+/, "_").downcase.to_sym
end
states() click to toggle source

Retrieves the state data for all virtual machines in the output

The state is returned as a Hash, mapping virtual machine names (as symbols) to their state (also as a symbol). Both of these symbols have spaces converted to underscores (for convenience when writing literals in other code).

# File lib/derelict/parser/status.rb, line 73
def states
  logger.debug "Parsing states from VM list using #{description}"
  vm_lines.inject Hash.new do |hash, line|
    hash.merge! parse_line(line.match PARSE_STATE_FROM_LIST_ITEM)
  end
end
vm_lines() click to toggle source

Retrieves the virtual machine list section of the output

# File lib/derelict/parser/status.rb, line 59
def vm_lines
  output.match(PARSE_LIST_FROM_OUTPUT).tap {|list|
    logger.debug "Parsing VM list from output using #{description}"
    raise InvalidFormat.new "Couldn't find VM list" if list.nil?
  }.captures[0].lines
end