1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 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 295 296 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
require 'log4r'
require 'vagrant/util/busy'
require 'vagrant/util/platform'
require 'vagrant/util/retryable'
require 'vagrant/util/subprocess'
require 'vagrant/util/which'
module VagrantPlugins
module ProviderVirtualBox
module Driver
# Base class for all VirtualBox drivers.
#
# This class provides useful tools for things such as executing
# VBoxManage and handling SIGINTs and so on.
class Base
# Include this so we can use `Subprocess` more easily.
include Vagrant::Util::Retryable
def initialize
@logger = Log4r::Logger.new("vagrant::provider::virtualbox::base")
# This flag is used to keep track of interrupted state (SIGINT)
@interrupted = false
if Vagrant::Util::Platform.windows? || Vagrant::Util::Platform.cygwin?
@logger.debug("Windows, checking for VBoxManage on PATH first")
@vboxmanage_path = Vagrant::Util::Which.which("VBoxManage")
# On Windows, we use the VBOX_INSTALL_PATH environmental
# variable to find VBoxManage.
if !@vboxmanage_path && (ENV.key?("VBOX_INSTALL_PATH") ||
ENV.key?("VBOX_MSI_INSTALL_PATH"))
@logger.debug("Windows. Trying VBOX_INSTALL_PATH for VBoxManage")
# Get the path.
path = ENV["VBOX_INSTALL_PATH"] || ENV["VBOX_MSI_INSTALL_PATH"]
@logger.debug("VBOX_INSTALL_PATH value: #{path}")
# There can actually be multiple paths in here, so we need to
# split by the separator ";" and see which is a good one.
path.split(";").each do |single|
# Make sure it ends with a \
single += "\\" if !single.end_with?("\\")
# If the executable exists, then set it as the main path
# and break out
vboxmanage = "#{single}VBoxManage.exe"
if File.file?(vboxmanage)
@vboxmanage_path = Vagrant::Util::Platform.cygwin_windows_path(vboxmanage)
break
end
end
end
# If we still don't have one, try to find it using common locations
drive = ENV["SYSTEMDRIVE"] || "C:"
[
"#{drive}/Program Files/Oracle/VirtualBox",
"#{drive}/Program Files (x86)/Oracle/VirtualBox",
"#{ENV["PROGRAMFILES"]}/Oracle/VirtualBox"
].each do |maybe|
path = File.join(maybe, "VBoxManage.exe")
if File.file?(path)
@vboxmanage_path = path
break
end
end
elsif Vagrant::Util::Platform.wsl?
if !Vagrant::Util::Platform.wsl_windows_access?
@logger.error("No user Windows access defined for the Windows Subsystem for Linux. This is required for VirtualBox.")
raise Vagrant::Errors::WSLVirtualBoxWindowsAccessError
end
@logger.debug("Linux platform detected but executing within WSL. Locating VBoxManage.")
@vboxmanage_path = Vagrant::Util::Which.which("VBoxManage") || Vagrant::Util::Which.which("VBoxManage.exe")
if !@vboxmanage_path
# If we still don't have one, try to find it using common locations
drive = "/mnt/c"
[
"#{drive}/Program Files/Oracle/VirtualBox",
"#{drive}/Program Files (x86)/Oracle/VirtualBox"
].each do |maybe|
path = File.join(maybe, "VBoxManage.exe")
if File.file?(path)
@vboxmanage_path = path
break
end
end
end
end
# Fall back to hoping for the PATH to work out
@vboxmanage_path ||= "VBoxManage"
@logger.info("VBoxManage path: #{@vboxmanage_path}")
end
# Clears the forwarded ports that have been set on the virtual machine.
def clear_forwarded_ports
end
# Clears the shared folders that have been set on the virtual machine.
def clear_shared_folders
end
# Creates a DHCP server for a host only network.
#
# @param [String] network Name of the host-only network.
# @param [Hash] options Options for the DHCP server.
def create_dhcp_server(network, options)
end
# Creates a host only network with the given options.
#
# @param [Hash] options Options to create the host only network.
# @return [Hash] The details of the host only network, including
# keys `:name`, `:ip`, and `:netmask`
def create_host_only_network(options)
end
# Deletes the virtual machine references by this driver.
def delete
end
# Deletes any host only networks that aren't being used for anything.
def delete_unused_host_only_networks
end
# Discards any saved state associated with this VM.
def discard_saved_state
end
# 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.
#
# @param [Array<Hash>] adapters Array of adapters to enable.
def enable_adapters(adapters)
end
# Execute a raw command straight through to VBoxManage.
#
# Accepts a retryable: true option if the command should be retried
# upon failure.
#
# Raises a VBoxManage error if it fails.
#
# @param [Array] command Command to execute.
def execute_command(command)
end
# Exports the virtual machine to the given path.
#
# @param [String] path Path to the OVF file.
# @yield [progress] Yields the block with the progress of the export.
def export(path)
end
# 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.
#
# @param [Array<Hash>] ports An array of ports to set. See documentation
# for more information on the format.
def forward_ports(ports)
end
# Halts the virtual machine (pulls the plug).
def halt
end
# Imports the VM from an OVF file.
#
# @param [String] ovf Path to the OVF file.
# @return [String] UUID of the imported VM.
def import(ovf)
end
# Returns the maximum number of network adapters.
def max_network_adapters
8
end
# Returns a list of forwarded ports for a VM.
#
# @param [String] uuid UUID of the VM to read from, or `nil` if this
# VM.
# @param [Boolean] active_only If true, only VMs that are running will
# be checked.
# @return [Array<Array>]
def read_forwarded_ports(uuid=nil, active_only=false)
end
# Returns a list of bridged interfaces.
#
# @return [Hash]
def read_bridged_interfaces
end
# Returns a list of configured DHCP servers
#
# Each DHCP server is represented as a Hash with the following details:
#
# {
# :network => String, # name of the associated network interface as
# # parsed from the NetworkName, e.g. "vboxnet0"
# :ip => String, # IP address of the DHCP server, e.g. "172.28.128.2"
# :lower => String, # lower IP address of the DHCP lease range, e.g. "172.28.128.3"
# :upper => String, # upper IP address of the DHCP lease range, e.g. "172.28.128.254"
# }
#
# @return [Array<Hash>] See comment above for details
def read_dhcp_servers
end
# Returns the guest additions version that is installed on this VM.
#
# @return [String]
def read_guest_additions_version
end
# Returns the value of a guest property on the current VM.
#
# @param [String] property the name of the guest property to read
# @return [String] value of the guest property
# @raise [VirtualBoxGuestPropertyNotFound] if the guest property does not have a value
def read_guest_property(property)
end
# Returns a list of available host only interfaces.
#
# Each interface is represented as a Hash with the following details:
#
# {
# :name => String, # interface name, e.g. "vboxnet0"
# :ip => String, # IP address of the interface, e.g. "172.28.128.1"
# :netmask => String, # netmask associated with the interface, e.g. "255.255.255.0"
# :status => String, # status of the interface, e.g. "Up", "Down"
# }
#
# @return [Array<Hash>] See comment above for details
def read_host_only_interfaces
end
# Returns the MAC address of the first network interface.
#
# @return [String]
def read_mac_address
end
# Returns the folder where VirtualBox places it's VMs.
#
# @return [String]
def read_machine_folder
end
# Returns a list of network interfaces of the VM.
#
# @return [Hash]
def read_network_interfaces
end
# Returns the current state of this VM.
#
# @return [Symbol]
def read_state
end
# Returns a list of all forwarded ports in use by active
# virtual machines.
#
# @return [Array]
def read_used_ports
end
# Returns a list of all UUIDs of virtual machines currently
# known by VirtualBox.
#
# @return [Array<String>]
def read_vms
end
# Reconfigure the hostonly network given by interface (the result
# of read_host_only_networks). This is a sad function that only
# exists to work around VirtualBox bugs.
#
# @return nil
def reconfig_host_only(interface)
end
# Removes the DHCP server identified by the provided network name.
#
# @param [String] network_name The the full network name associated
# with the DHCP server to be removed, e.g. "HostInterfaceNetworking-vboxnet0"
def remove_dhcp_server(network_name)
end
# Sets the MAC address of the first network adapter.
#
# @param [String] mac MAC address without any spaces/hyphens.
def set_mac_address(mac)
end
# Share a set of folders on this VM.
#
# @param [Array<Hash>] folders
def share_folders(folders)
end
# Reads the SSH port of this VM.
#
# @param [Integer] expected Expected guest port of SSH.
def ssh_port(expected)
end
# Starts the virtual machine.
#
# @param [String] mode Mode to boot the VM. Either "headless"
# or "gui"
def start(mode)
end
# Suspend the virtual machine.
def suspend
end
# Unshare folders.
def unshare_folders(names)
end
# Verifies that the driver is ready to accept work.
#
# This should raise a VagrantError if things are not ready.
def verify!
end
# Verifies that an image can be imported properly.
#
# @param [String] path Path to an OVF file.
# @return [Boolean]
def verify_image(path)
end
# Checks if a VM with the given UUID exists.
#
# @return [Boolean]
def vm_exists?(uuid)
end
# Returns a hash of information about a given virtual machine
#
# @param [String] uuid
# @return [Hash] info
def show_vm_info
info = {}
execute('showvminfo', @uuid, '--machinereadable', retryable: true).split("\n").each do |line|
parts = line.partition('=')
key = parts.first.gsub('"', '')
value = parts.last.gsub('"', '')
info[key] = value
end
info
end
# Execute the given subcommand for VBoxManage and return the output.
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: Vagrant::Errors::VBoxManageError, tries: tries, sleep: 1) do
# If there is an error with VBoxManage, this gets set to true
errored = false
# 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.")
elsif r.exit_code == 126
# This exit code happens if VBoxManage is on the PATH,
# but another executable it tries to execute is missing.
# This is usually indicative of a corrupted VirtualBox install.
raise Vagrant::Errors::VBoxManageNotFoundError
else
errored = true
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 =~ /failed to open \/dev\/vboxnetctl/i
# This catches an error message that only shows when kernel
# drivers aren't properly installed.
@logger.error("Error message about unable to open vboxnetctl")
raise Vagrant::Errors::VirtualBoxKernelModuleNotLoaded
end
if r.stderr =~ /VBoxManage([.a-z]+?): error:/
# This catches the generic VBoxManage error case.
@logger.info("VBoxManage error text found, assuming error.")
errored = true
end
end
# If there was an error running VBoxManage, show the error and the
# output.
if errored
raise Vagrant::Errors::VBoxManageError,
command: command.inspect,
stderr: r.stderr,
stdout: r.stdout
end
end
# Return the output, making sure to replace any Windows-style
# newlines with Unix-style.
r.stdout.gsub("\r\n", "\n")
end
# Executes a command and returns the raw result object.
def raw(*command, &block)
int_callback = lambda do
@interrupted = true
# We have to execute this in a thread due to trap contexts
# and locks.
Thread.new { @logger.info("Interrupted.") }.join
end
# Append in the options for subprocess
command << { notify: [:stdout, :stderr] }
Vagrant::Util::Busy.busy(int_callback) do
Vagrant::Util::Subprocess.execute(@vboxmanage_path, *command, &block)
end
rescue Vagrant::Util::Subprocess::LaunchError => e
raise Vagrant::Errors::VBoxManageLaunchError,
message: e.to_s
end
end
end
end
end
|