Class: Derelict::Parser::Status

Inherits:
Derelict::Parser show all
Extended by:
Memoist
Defined in:
lib/derelict/parser/status.rb,
lib/derelict/parser/status/invalid_format.rb

Overview

Parses the output of “vagrant status”

Defined Under Namespace

Classes: InvalidFormat

Constant Summary collapse

PARSE_LIST_FROM_OUTPUT =

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

/Current machine states:\n\n((?:.*\n)+)\n/i
PARSE_STATE_FROM_LIST_ITEM =

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

%r[
^(.*?) # VM name starts at the start of the line,
\s{2,} # to the first instance of 2 or more spaces.
(.*?)  # VM state starts after the whitespace,
\s+\(  # continuing until whitespace and open bracket.
  (.*) # The provider name starts after the bracket,
\)$    # and ends at a closing bracket at line end.
]x

Instance Attribute Summary

Attributes inherited from Derelict::Parser

#output

Instance Method Summary collapse

Methods inherited from Derelict::Parser

#initialize

Methods included from Utils::Logger

#logger

Constructor Details

This class inherits a constructor from Derelict::Parser

Instance Method Details

#descriptionObject

Provides a description of this Parser

Mainly used for log messages.



53
54
55
# File 'lib/derelict/parser/status.rb', line 53

def description
  "Derelict::Parser::Status instance"
end

#exists?(vm_name = nil) ⇒ Boolean

Determines if a particular virtual machine exists in the output

* vm_name: The name of the virtual machine to look for

Returns:

  • (Boolean)


32
33
34
35
# 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) ⇒ Object

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


42
43
44
45
46
47
48
# 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_namesObject

Retrieves the names of all virtual machines in the output

The names are returned as a Set of symbols.



25
26
27
# File 'lib/derelict/parser/status.rb', line 25

def vm_names
  Set[*states.keys]
end