Class: VirtualBox::Appliance

Inherits:
AbstractModel show all
Defined in:
lib/virtualbox/appliance.rb

Overview

Represents an VirtualBox “appliance” which is an exported virtual machine or virtual machines. Appliances typically come with an OVF file and one or more compressed hard disks, and can be used to import directly into other VirtualBox installations. Appliances allow for virtual machine portability.

Instance Method Summary collapse

Methods inherited from AbstractModel

#destroy, #errors, errors_for_relationship, #existing_record!, #inspect, #lazy_attribute?, #lazy_relationship?, #new_record!, #new_record?, #parent_machine, #populate_attributes, #populate_relationship, #populate_relationships, reload!, #reload!, reload?, reloaded!, #save, #save!, #save_attribute, #save_changed_interface_attributes, #save_interface_attribute, #set_relationship, #validate, #write_attribute

Methods included from VirtualBox::AbstractModel::Validatable

#__validates_extract_options, #add_error, #clear_errors, #errors, #errors_on, #full_error_messages, #valid?, #validate, #validates_format_of, #validates_inclusion_of, #validates_numericality_of, #validates_presence_of

Methods included from VirtualBox::AbstractModel::Relatable

#destroy_relationship, #destroy_relationships, #has_relationship?, included, #lazy_relationship?, #loaded_relationship?, #populate_relationship, #populate_relationships, #read_relationship, #relationship_class, #relationship_data, #save_relationship, #save_relationships, #set_relationship

Methods included from VirtualBox::AbstractModel::VersionMatcher

#assert_version_match, #split_version, #version_match?

Methods included from VirtualBox::AbstractModel::Dirty

#changed?, #changes, #clear_dirty!, #ignore_dirty, #method_missing, #set_dirty!

Methods included from VirtualBox::AbstractModel::InterfaceAttributes

#load_interface_attribute, #load_interface_attributes, #save_interface_attribute, #save_interface_attributes, #spec_to_proc

Methods included from VirtualBox::AbstractModel::Attributable

#attributes, #has_attribute?, included, #lazy_attribute?, #loaded_attribute?, #populate_attributes, #read_attribute, #readonly_attribute?, #write_attribute

Methods included from Logger

included, #logger, #logger_output=

Constructor Details

#initialize(*args) ⇒ Appliance

Returns a new instance of Appliance.



11
12
13
14
15
16
17
# File 'lib/virtualbox/appliance.rb', line 11

def initialize(*args)
  write_attribute(:interface, Lib.lib.virtualbox.create_appliance)

  initialize_from_path(*args) if args.length == 1

  clear_dirty!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class VirtualBox::AbstractModel::Dirty

Instance Method Details

#add_machine(vm, options = {}) ⇒ Object

Adds a VM to the appliance



55
56
57
58
59
60
# File 'lib/virtualbox/appliance.rb', line 55

def add_machine(vm, options = {})
  sys_desc = vm.interface.export(interface, path)
  options.each do |key, value|
    sys_desc.add_description(key, value, value)
  end
end

#export(&block) ⇒ Object

Exports the machines to the given path. If a block is given, it will be yielded every percent that the operation progresses. This can be done to check the progress of the export in real-time.



50
51
52
# File 'lib/virtualbox/appliance.rb', line 50

def export(&block)
  interface.write("ovf-1.0", true, path).wait(&block)
end

#import(&block) ⇒ Object

Imports the machines associated with this appliance. If a block is given, it will be yielded every percent that the operation progresses. This can be done to check the progress of the import.



43
44
45
# File 'lib/virtualbox/appliance.rb', line 43

def import(&block)
  interface.import_machines.wait(&block)
end

#initialize_from_path(path) ⇒ Object

Initializes this Appliance instance from a path to an OVF file. This sets up the relationships and so on.

Parameters:

  • path (String)

    Path to the OVF file.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/virtualbox/appliance.rb', line 23

def initialize_from_path(path)
  # Read in the data from the path
  interface.read(path).wait_for_completion(-1)

  # Interpret the data to fill in the interface properties
  interface.interpret

  # Load the interface attributes
  load_interface_attributes(interface)

  # Fill in the virtual systems
  populate_relationship(:virtual_systems, interface.virtual_system_descriptions)

  # Should be an existing record
  existing_record!
end