Class: Vagrant::Driver::VirtualBoxBase
- Inherits:
-
Object
- Object
- Vagrant::Driver::VirtualBoxBase
- 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.
Direct Known Subclasses
Instance Method Summary collapse
-
#clear_forwarded_ports ⇒ Object
Clears the forwarded ports that have been set on the virtual machine.
-
#clear_shared_folders ⇒ Object
Clears the shared folders that have been set on the virtual machine.
-
#create_dhcp_server(network, options) ⇒ Object
Creates a DHCP server for a host only network.
-
#create_host_only_network(options) ⇒ Hash
Creates a host only network with the given options.
-
#delete ⇒ Object
Deletes the virtual machine references by this driver.
-
#delete_unused_host_only_networks ⇒ Object
Deletes any host only networks that aren't being used for anything.
-
#discard_saved_state ⇒ Object
Discards any saved state associated with this VM.
-
#enable_adapters(adapters) ⇒ Object
Enables network adapters on the VM.
-
#execute(*command, &block) ⇒ Object
Execute the given subcommand for VBoxManage and return the output.
-
#execute_command(command) ⇒ Object
Execute a raw command straight through to VBoxManage.
-
#export(path) {|progress| ... } ⇒ Object
Exports the virtual machine to the given path.
-
#forward_ports(ports) ⇒ Object
Forwards a set of ports for a VM.
-
#halt ⇒ Object
Halts the virtual machine (pulls the plug).
-
#import(ovf) ⇒ String
Imports the VM from an OVF file.
-
#initialize ⇒ VirtualBoxBase
constructor
A new instance of VirtualBoxBase.
-
#raw(*command, &block) ⇒ Object
Executes a command and returns the raw result object.
-
#read_bridged_interfaces ⇒ Hash
Returns a list of bridged interfaces.
-
#read_forwarded_ports(uuid = nil, active_only = false) ⇒ Array<Array>
Returns a list of forwarded ports for a VM.
-
#read_guest_additions_version ⇒ String
Returns the guest additions version that is installed on this VM.
-
#read_host_only_interfaces ⇒ Hash
Returns a list of available host only interfaces.
-
#read_mac_address ⇒ String
Returns the MAC address of the first network interface.
-
#read_machine_folder ⇒ String
Returns the folder where VirtualBox places it's VMs.
-
#read_network_interfaces ⇒ Hash
Returns a list of network interfaces of the VM.
-
#read_state ⇒ Symbol
Returns the current state of this VM.
-
#read_used_ports ⇒ Array
Returns a list of all forwarded ports in use by active virtual machines.
-
#read_vms ⇒ Array<String>
Returns a list of all UUIDs of virtual machines currently known by VirtualBox.
-
#set_mac_address(mac) ⇒ Object
Sets the MAC address of the first network adapter.
-
#share_folders(folders) ⇒ Object
Share a set of folders on this VM.
-
#ssh_port(expected) ⇒ Object
Reads the SSH port of this VM.
-
#start(mode) ⇒ Object
Starts the virtual machine.
-
#suspend ⇒ Object
Suspend the virtual machine.
-
#verify! ⇒ Object
Verifies that the driver is ready to accept work.
-
#verify_image(path) ⇒ Boolean
Verifies that an image can be imported properly.
-
#vm_exists?(uuid) ⇒ Boolean
Checks if a VM with the given UUID exists.
Methods included from Util::Retryable
Constructor Details
#initialize ⇒ VirtualBoxBase
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_ports ⇒ Object
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_folders ⇒ Object
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.
56 57 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 56 def create_dhcp_server(network, ) end |
#create_host_only_network(options) ⇒ Hash
Creates a host only network with the given options.
64 65 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 64 def create_host_only_network() end |
#delete ⇒ Object
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_networks ⇒ Object
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_state ⇒ Object
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.
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.
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.
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.
128 129 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 128 def forward_ports(ports) end |
#halt ⇒ Object
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.
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 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 297 def raw(*command, &block) int_callback = lambda do @interrupted = true @logger.info("Interrupted.") end Util::Busy.busy(int_callback) do Subprocess.execute(@vboxmanage_path, *command, &block) end end |
#read_bridged_interfaces ⇒ Hash
Returns a list of bridged interfaces.
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.
149 150 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 149 def read_forwarded_ports(uuid=nil, active_only=false) end |
#read_guest_additions_version ⇒ String
Returns the guest additions version that is installed on this VM.
161 162 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 161 def read_guest_additions_version end |
#read_host_only_interfaces ⇒ Hash
Returns a list of available host only interfaces.
167 168 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 167 def read_host_only_interfaces end |
#read_mac_address ⇒ String
Returns the MAC address of the first network interface.
173 174 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 173 def read_mac_address end |
#read_machine_folder ⇒ String
Returns the folder where VirtualBox places it's VMs.
179 180 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 179 def read_machine_folder end |
#read_network_interfaces ⇒ Hash
Returns a list of network interfaces of the VM.
185 186 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 185 def read_network_interfaces end |
#read_state ⇒ Symbol
Returns the current state of this VM.
191 192 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 191 def read_state end |
#read_used_ports ⇒ Array
Returns a list of all forwarded ports in use by active virtual machines.
198 199 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 198 def read_used_ports end |
#read_vms ⇒ Array<String>
Returns a list of all UUIDs of virtual machines currently known by VirtualBox.
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.
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.
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.
223 224 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 223 def ssh_port(expected) end |
#start(mode) ⇒ Object
Starts the virtual machine.
230 231 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 230 def start(mode) end |
#suspend ⇒ Object
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.
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.
253 254 |
# File 'lib/vagrant/driver/virtualbox_base.rb', line 253 def vm_exists?(uuid) end |