class Derelict::VirtualMachine
A Vagrant virtual machine in a particular Derelict
connection
Constants
- COMMANDS
Attributes
Public Class Methods
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)
# File lib/derelict/virtual_machine.rb, line 31 def initialize(connection, name) @connection = connection @name = name logger.debug "Successfully initialized #{description}" end
Public Instance Methods
Provides a description of this Connection
Mainly used for log messages.
# File lib/derelict/virtual_machine.rb, line 124 def description "Derelict::VirtualMachine '#{name}' from #{connection.description}" end
Determines whether this Vagrant virtual machine exists
Returns true
if the connection reports a virtual machine with the requested name, otherwise returns false
.
# File lib/derelict/virtual_machine.rb, line 55 def exists? status.exists? name end
Determines whether this virtual machine is currently running
# File lib/derelict/virtual_machine.rb, line 69 def running? (state == :running) end
Gets the current state of this Vagrant virtual machine
The state is returned as a symbol, e.g. :running.
# File lib/derelict/virtual_machine.rb, line 63 def state status.state name end
Retrieves the (parsed) status from the connection
# 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
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
# 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
Private Instance Methods
Retrieves the arguments for a particular action
* action: The symbol representing the action (one of :up, :halt, :destroy, :reload, :suspend, :resume)
# File lib/derelict/virtual_machine.rb, line 167 def arguments_for(action) case action when :destroy then ['--force'] else [] end end
Executes a command on the connection for this VM
* command: The command to execute (as a symbol) * options: A Hash of options, with the following optional keys: * log: Logs the output of the command if true (defaults to false) * log_mode: Controls how commands are logged (one of either :chars or :lines, defaults to :lines) * color: Uses color in the log output (defaults to false, only relevant if log is true) * provider: The Vagrant provider to use, one of "virtualbox" or "vmware_fusion" (defaults to "virtualbox")
# File lib/derelict/virtual_machine.rb, line 142 def execute!(command, options) # Build up the arguments to pass to connection.execute! arguments = [command, name, *arguments_for(command)] arguments << "--color" if options[:color] if options[:provider] arguments << "--provider" arguments << options[:provider] end if options[:log_mode] arguments << {:mode => options[:log_mode]} end # Set up the block to use when executing -- if logging is # enabled, use a block that logs the output; otherwise no block. block = options[:log] ? shell_log_block : nil # Execute the command connection.execute! *arguments, &block end
Retrieves the correct log message for a particular action
* action: The symbol representing the action (one of :up, :halt, :destroy, :reload, :suspend, :resume)
# File lib/derelict/virtual_machine.rb, line 178 def log_message_for(action) case action when :up then "Bringing up #{description}" when :halt then "Halting #{description}" when :destroy then "Destroying #{description}" when :reload then "Reloading #{description}" when :suspend then "Suspending #{description}" when :resume then "Resuming #{description}" else nil end end