Class: Derelict::VirtualMachine

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Utils::Logger
Defined in:
lib/derelict/virtual_machine.rb,
lib/derelict/virtual_machine/invalid.rb,
lib/derelict/virtual_machine/not_found.rb

Overview

A Vagrant virtual machine in a particular Derelict connection

Defined Under Namespace

Classes: Invalid, NotFound

Constant Summary collapse

COMMANDS =
[
  :up,
  :halt,
  :destroy,
  :reload,
  :suspend,
  :resume,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Logger

#logger

Constructor Details

#initialize(connection, name) ⇒ VirtualMachine

Initializes a new VirtualMachine for a connection and name

* connection: The +Derelict::Connection+ to use to manipulate
              the VirtualMachine instance
* name:       The name of the virtual machine, used when
              communicating with the connection)


31
32
33
34
35
# File 'lib/derelict/virtual_machine.rb', line 31

def initialize(connection, name)
  @connection = connection
  @name = name
  logger.debug "Successfully initialized #{description}"
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



22
23
24
# File 'lib/derelict/virtual_machine.rb', line 22

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/derelict/virtual_machine.rb', line 23

def name
  @name
end

Instance Method Details

#descriptionObject

Provides a description of this Connection

Mainly used for log messages.



124
125
126
# File 'lib/derelict/virtual_machine.rb', line 124

def description
  "Derelict::VirtualMachine '#{name}' from #{connection.description}"
end

#exists?Boolean

Determines whether this Vagrant virtual machine exists

Returns true if the connection reports a virtual machine with the requested name, otherwise returns false.

Returns:

  • (Boolean)


55
56
57
# File 'lib/derelict/virtual_machine.rb', line 55

def exists?
  status.exists? name
end

#running?Boolean

Determines whether this virtual machine is currently running

Returns:

  • (Boolean)


69
70
71
# File 'lib/derelict/virtual_machine.rb', line 69

def running?
  (state == :running)
end

#stateObject

Gets the current state of this Vagrant virtual machine

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



63
64
65
# File 'lib/derelict/virtual_machine.rb', line 63

def state
  status.state name
end

#statusObject

Retrieves the (parsed) status from the connection



114
115
116
117
118
# File 'lib/derelict/virtual_machine.rb', line 114

def status
  logger.info "Retrieving Vagrant status for #{description}"
  output = connection.execute!(:status).stdout
  Derelict::Parser::Status.new(output)
end

#validate!Object

Validates the data used for this connection

Raises exceptions on failure:

* +Derelict::VirtualMachine::NotFound+ if the connection
  doesn't know about a virtual machine with the requested
  name


44
45
46
47
48
49
# File 'lib/derelict/virtual_machine.rb', line 44

def validate!
  logger.debug "Starting validation for #{description}"
  raise NotFound.new name, connection unless exists?
  logger.info "Successfully validated #{description}"
  self
end