Class: Vagrant::Driver::VirtualBoxBase

Inherits:
Object
  • Object
show all
Includes:
Util, Util::Retryable
Defined in:
lib/vagrant/driver/virtualbox_base.rb

Overview

Base class for all VirtualBox drivers.

This class provides useful tools for things such as executing VBoxManage and handling SIGINTs and so on.

Instance Method Summary collapse

Methods included from Util::Retryable

#retryable

Constructor Details

#initializeVirtualBoxBase

Returns a new instance of VirtualBoxBase.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vagrant/driver/virtualbox_base.rb', line 19

def initialize
  @logger = Log4r::Logger.new("vagrant::driver::virtualbox_base")

  # This flag is used to keep track of interrupted state (SIGINT)
  @interrupted = false

  # Set the path to VBoxManage
  @vboxmanage_path = "VBoxManage"

  if Util::Platform.windows?
    @logger.debug("Windows. Trying VBOX_INSTALL_PATH for VBoxManage")

    # On Windows, we use the VBOX_INSTALL_PATH environmental
    # variable to find VBoxManage.
    if ENV.has_key?("VBOX_INSTALL_PATH")
      # The path usually ends with a \ but we make sure here
      path = ENV["VBOX_INSTALL_PATH"]
      path += "\\" if !path.end_with?("\\")
      @vboxmanage_path = "#{path}VBoxManage.exe"
    end
  end

  @logger.info("VBoxManage path: #{@vboxmanage_path}")
end

Instance Method Details

#clear_forwarded_portsObject

Clears the forwarded ports that have been set on the virtual machine.



45
46
# File 'lib/vagrant/driver/virtualbox_base.rb', line 45

def clear_forwarded_ports
end

#clear_shared_foldersObject

Clears the shared folders that have been set on the virtual machine.



49
50
# File 'lib/vagrant/driver/virtualbox_base.rb', line 49

def clear_shared_folders
end

#create_dhcp_server(network, options) ⇒ Object

Creates a DHCP server for a host only network.

Parameters:

  • network (String)

    Name of the host-only network.

  • options (Hash)

    Options for the DHCP server.



56
57
# File 'lib/vagrant/driver/virtualbox_base.rb', line 56

def create_dhcp_server(network, options)
end

#create_host_only_network(options) ⇒ Hash

Creates a host only network with the given options.

Parameters:

  • options (Hash)

    Options to create the host only network.

Returns:

  • (Hash)

    The details of the host only network, including keys :name, :ip, and :netmask



64
65
# File 'lib/vagrant/driver/virtualbox_base.rb', line 64

def create_host_only_network(options)
end

#deleteObject

Deletes the virtual machine references by this driver.



68
69
# File 'lib/vagrant/driver/virtualbox_base.rb', line 68

def delete
end

#delete_unused_host_only_networksObject

Deletes any host only networks that aren't being used for anything.



72
73
# File 'lib/vagrant/driver/virtualbox_base.rb', line 72

def delete_unused_host_only_networks
end

#discard_saved_stateObject

Discards any saved state associated with this VM.



76
77
# File 'lib/vagrant/driver/virtualbox_base.rb', line 76

def discard_saved_state
end

#enable_adapters(adapters) ⇒ Object

Enables network adapters on the VM.

The format of each adapter specification should be like so:

{ :type => :hostonly, :hostonly => "vboxnet0", :mac_address => "tubes" }

This must support setting up both host only and bridged networks.

Parameters:

  • adapters (Array<Hash>)

    Array of adapters to enable.



92
93
# File 'lib/vagrant/driver/virtualbox_base.rb', line 92

def enable_adapters(adapters)
end

#execute(*command, &block) ⇒ Object

Execute the given subcommand for VBoxManage and return the output.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/vagrant/driver/virtualbox_base.rb', line 257

def execute(*command, &block)
  # Get the options hash if it exists
  opts = {}
  opts = command.pop if command.last.is_a?(Hash)

  tries = 0
  tries = 3 if opts[:retryable]

  # Variable to store our execution result
  r = nil

  retryable(:on => Errors::VBoxManageError, :tries => tries, :sleep => 1) do
    # Execute the command
    r = raw(*command, &block)

    # If the command was a failure, then raise an exception that is
    # nicely handled by Vagrant.
    if r.exit_code != 0
      if @interrupted
        @logger.info("Exit code != 0, but interrupted. Ignoring.")
      else
        raise Errors::VBoxManageError, :command => command.inspect
      end
    else
      # Sometimes, VBoxManage fails but doesn't actual return a non-zero
      # exit code. For this we inspect the output and determine if an error
      # occurred.
      if r.stderr =~ /VBoxManage: error:/
        @logger.info("VBoxManage error text found, assuming error.")
        raise Errors::VBoxManageError, :command => command.inspect
      end
    end
  end

  # Return the output, making sure to replace any Windows-style
  # newlines with Unix-style.
  r.stdout.gsub("\r\n", "\n")
end

#execute_command(command) ⇒ Object

Execute a raw command straight through to VBoxManage.

Parameters:

  • command (Array)

    Command to execute.



98
99
# File 'lib/vagrant/driver/virtualbox_base.rb', line 98

def execute_command(command)
end

#export(path) {|progress| ... } ⇒ Object

Exports the virtual machine to the given path.

Parameters:

  • path (String)

    Path to the OVF file.

Yields:

  • (progress)

    Yields the block with the progress of the export.



105
106
# File 'lib/vagrant/driver/virtualbox_base.rb', line 105

def export(path)
end

#forward_ports(ports) ⇒ Object

Forwards a set of ports for a VM.

This will not affect any previously set forwarded ports, so be sure to delete those if you need to.

The format of each port hash should be the following:

{
  :name => "foo",
  :hostport => 8500,
  :guestport => 80,
  :adapter => 1,
  :protocol => "tcp"
}

Note that "adapter" and "protocol" are optional and will default to 1 and "tcp" respectively.

Parameters:

  • ports (Array<Hash>)

    An array of ports to set. See documentation for more information on the format.



128
129
# File 'lib/vagrant/driver/virtualbox_base.rb', line 128

def forward_ports(ports)
end

#haltObject

Halts the virtual machine (pulls the plug).



132
133
# File 'lib/vagrant/driver/virtualbox_base.rb', line 132

def halt
end

#import(ovf) ⇒ String

Imports the VM from an OVF file.

Parameters:

  • ovf (String)

    Path to the OVF file.

Returns:

  • (String)

    UUID of the imported VM.



139
140
# File 'lib/vagrant/driver/virtualbox_base.rb', line 139

def import(ovf)
end

#raw(*command, &block) ⇒ Object

Executes a command and returns the raw result object.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/vagrant/driver/virtualbox_base.rb', line 297

def raw(*command, &block)
  int_callback = lambda do
    @interrupted = true
    @logger.info("Interrupted.")
  end

  # The following is a workaround for a combined VirtualBox and
  # Mac OS X 10.8 bug:
  #
  # Remove the DYLD_LIBRARY_PATH environmental variable on Mac. This
  # is to fix a bug in Mac OS X 10.8 where a warning is printed to the
  # console if this is set when executing certain programs, which
  # can cause some VBoxManage commands to break because they work
  # by just reading stdout and don't expect the OS to just inject
  # garbage into it.
  old_dyld_lib_path = ENV.delete("DYLD_LIBRARY_PATH")
  old_ld_lib_path   = ENV.delete("LD_LIBRARY_PATH")

  Util::Busy.busy(int_callback) do
    Subprocess.execute(@vboxmanage_path, *command, &block)
  end
ensure
  # Reset the library path if it was set before. See above comments
  # for more information on why this was unset in the first place.
  ENV["DYLD_LIBRARY_PATH"] = old_dyld_lib_path if old_dyld_lib_path
  ENV["LD_LIBRARY_PATH"]   = old_ld_lib_path if old_ld_lib_path
end

#read_bridged_interfacesHash

Returns a list of bridged interfaces.

Returns:

  • (Hash)


155
156
# File 'lib/vagrant/driver/virtualbox_base.rb', line 155

def read_bridged_interfaces
end

#read_forwarded_ports(uuid = nil, active_only = false) ⇒ Array<Array>

Returns a list of forwarded ports for a VM.

Parameters:

  • uuid (String) (defaults to: nil)

    UUID of the VM to read from, or nil if this VM.

  • active_only (Boolean) (defaults to: false)

    If true, only VMs that are running will be checked.

Returns:

  • (Array<Array>)


149
150
# File 'lib/vagrant/driver/virtualbox_base.rb', line 149

def read_forwarded_ports(uuid=nil, active_only=false)
end

#read_guest_additions_versionString

Returns the guest additions version that is installed on this VM.

Returns:

  • (String)


161
162
# File 'lib/vagrant/driver/virtualbox_base.rb', line 161

def read_guest_additions_version
end

#read_host_only_interfacesHash

Returns a list of available host only interfaces.

Returns:

  • (Hash)


167
168
# File 'lib/vagrant/driver/virtualbox_base.rb', line 167

def read_host_only_interfaces
end

#read_mac_addressString

Returns the MAC address of the first network interface.

Returns:

  • (String)


173
174
# File 'lib/vagrant/driver/virtualbox_base.rb', line 173

def read_mac_address
end

#read_machine_folderString

Returns the folder where VirtualBox places it's VMs.

Returns:

  • (String)


179
180
# File 'lib/vagrant/driver/virtualbox_base.rb', line 179

def read_machine_folder
end

#read_network_interfacesHash

Returns a list of network interfaces of the VM.

Returns:

  • (Hash)


185
186
# File 'lib/vagrant/driver/virtualbox_base.rb', line 185

def read_network_interfaces
end

#read_stateSymbol

Returns the current state of this VM.

Returns:

  • (Symbol)


191
192
# File 'lib/vagrant/driver/virtualbox_base.rb', line 191

def read_state
end

#read_used_portsArray

Returns a list of all forwarded ports in use by active virtual machines.

Returns:

  • (Array)


198
199
# File 'lib/vagrant/driver/virtualbox_base.rb', line 198

def read_used_ports
end

#read_vmsArray<String>

Returns a list of all UUIDs of virtual machines currently known by VirtualBox.

Returns:

  • (Array<String>)


205
206
# File 'lib/vagrant/driver/virtualbox_base.rb', line 205

def read_vms
end

#set_mac_address(mac) ⇒ Object

Sets the MAC address of the first network adapter.

Parameters:

  • mac (String)

    MAC address without any spaces/hyphens.



211
212
# File 'lib/vagrant/driver/virtualbox_base.rb', line 211

def set_mac_address(mac)
end

#share_folders(folders) ⇒ Object

Share a set of folders on this VM.

Parameters:

  • folders (Array<Hash>)


217
218
# File 'lib/vagrant/driver/virtualbox_base.rb', line 217

def share_folders(folders)
end

#ssh_port(expected) ⇒ Object

Reads the SSH port of this VM.

Parameters:

  • expected (Integer)

    Expected guest port of SSH.



223
224
# File 'lib/vagrant/driver/virtualbox_base.rb', line 223

def ssh_port(expected)
end

#start(mode) ⇒ Object

Starts the virtual machine.

Parameters:

  • mode (String)

    Mode to boot the VM. Either "headless" or "gui"



230
231
# File 'lib/vagrant/driver/virtualbox_base.rb', line 230

def start(mode)
end

#suspendObject

Suspend the virtual machine.



234
235
# File 'lib/vagrant/driver/virtualbox_base.rb', line 234

def suspend
end

#verify!Object

Verifies that the driver is ready to accept work.

This should raise a VagrantError if things are not ready.



240
241
# File 'lib/vagrant/driver/virtualbox_base.rb', line 240

def verify!
end

#verify_image(path) ⇒ Boolean

Verifies that an image can be imported properly.

Parameters:

  • path (String)

    Path to an OVF file.

Returns:

  • (Boolean)


247
248
# File 'lib/vagrant/driver/virtualbox_base.rb', line 247

def verify_image(path)
end

#vm_exists?(uuid) ⇒ Boolean

Checks if a VM with the given UUID exists.

Returns:

  • (Boolean)


253
254
# File 'lib/vagrant/driver/virtualbox_base.rb', line 253

def vm_exists?(uuid)
end