Module: VirtualBox

Defined in:
lib/virtual_box/vm.rb,
lib/virtual_box/version.rb,
lib/virtual_box/registry.rb,
lib/virtual_box/vm/identity.rb,
lib/virtual_box/command_line.rb,
lib/virtual_box/vm/lifecycle.rb,
lib/virtual_box/vm/general_settings.rb

Overview

:nodoc: namespace

Defined Under Namespace

Classes: VM

Class Method Summary collapse

Class Method Details

._vm_metadata_from_command_result(result) ⇒ Object

Parses a VBoxManage command result into VM metadata information.

This method works for ‘VBoxManage show vms | runningvms’



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/virtual_box/registry.rb', line 12

def self.(result)
  return nil unless result[:status] == 0
  vms = []
  result[:output].each_line do |line|
    line.strip!
    guid_start = line.rindex ?{
    vms << { :name => line[1, guid_start - 3],
             :uuid => line[guid_start + 1, line.length - guid_start - 2] }
  end
  vms
end

.all_vms_metadataObject

The names and UUIDs of all the VMs registered with VirtualBox.

Returns an array with one hash per registered VM. Each hash has the keys :name and :uuid. In case of failure (e.g. VirtualBox is not installed), returns nil.



29
30
31
32
# File 'lib/virtual_box/registry.rb', line 29

def self.
  result = VirtualBox.shell_command('VBoxManage --nologo list vms')    
   result
end

.ose?Boolean

True if the installed VirtualBox is the open-source edition.

The open-source edition of VirtualBox has some limitations, such as no support for RDP and USB devices.

Returns:

  • (Boolean)


15
16
17
# File 'lib/virtual_box/version.rb', line 15

def self.ose?
  version_info ? (version_info[:edition] == 'OSE') : nil
end

.running_vms_metadataObject

The names and UUIDs of the VMs running inside VirtualBox right now.

Returns an array with one hash per running VM. Each hash has the keys :name and :uuid. In case of failure (e.g. VirtualBox is not installed), returns nil.



39
40
41
42
# File 'lib/virtual_box/registry.rb', line 39

def self.
  result = VirtualBox.shell_command('VBoxManage --nologo list runningvms')    
   result    
end

.shell_command(command) ⇒ Object

Runs a command in a sub-shell, waiting until the command completes.

Args:

command:: a string containing the command to be executed

Returns:

a hash with the following keys:
    :status:: the command's exit status
    :output:: a string with the command's output


22
23
24
25
# File 'lib/virtual_box/command_line.rb', line 22

def self.shell_command(command)
  output = Kernel.`(command) 
  { :status => $CHILD_STATUS, :output => output }
end

.version_infoObject

Version information about the installed VirtualBox.

Returns:

false if VirtualBox is not installed; otherwise, a hash with the keys:
    revision:: (number) the SVN revision that VirtualBox is built off of
    edition:: the VirtualBox edition (nil for the personal edition, 'OSE'
              for the open-source edition)
    version:: the VirtualBox version (e.g. '3.0.4')


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/virtual_box/version.rb', line 27

def self.version_info
  return @version_info unless @version_info.nil?
  
  cmd_result = shell_command 'VBoxManage --version'
  return @version_info = false if cmd_result[:status] != 0
  
  output = cmd_result[:output].strip
  @version_info = {}
  if revision_offset = output.rindex('r')
    @version_info[:revision] = output[revision_offset + 1, output.length].to_i
    output.slice! revision_offset..-1
  end
  if edition_offset = output.rindex('_')
    @version_info[:edition] = output[edition_offset + 1, output.length]
    output.slice! edition_offset..-1
  end
  @version_info[:version] = output
  @version_info
end