2012-07-25 04:32:38 +00:00
|
|
|
require "forwardable"
|
|
|
|
|
|
|
|
require "log4r"
|
|
|
|
|
2014-10-24 16:20:08 +00:00
|
|
|
require "vagrant/util/retryable"
|
|
|
|
|
2012-07-25 04:32:38 +00:00
|
|
|
require File.expand_path("../base", __FILE__)
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
module Driver
|
|
|
|
class Meta < Base
|
|
|
|
# This is raised if the VM is not found when initializing a driver
|
|
|
|
# with a UUID.
|
|
|
|
class VMNotFound < StandardError; end
|
|
|
|
|
|
|
|
# We use forwardable to do all our driver forwarding
|
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
# The UUID of the virtual machine we represent
|
|
|
|
attr_reader :uuid
|
|
|
|
|
|
|
|
# The version of virtualbox that is running.
|
|
|
|
attr_reader :version
|
|
|
|
|
2014-10-24 16:20:08 +00:00
|
|
|
include Vagrant::Util::Retryable
|
|
|
|
|
2012-07-25 04:32:38 +00:00
|
|
|
def initialize(uuid=nil)
|
|
|
|
# Setup the base
|
|
|
|
super()
|
|
|
|
|
|
|
|
@logger = Log4r::Logger.new("vagrant::provider::virtualbox::meta")
|
|
|
|
@uuid = uuid
|
|
|
|
|
|
|
|
# Read and assign the version of VirtualBox we know which
|
|
|
|
# specific driver to instantiate.
|
|
|
|
begin
|
|
|
|
@version = read_version || ""
|
2013-07-12 08:13:10 +00:00
|
|
|
rescue Vagrant::Errors::CommandUnavailable,
|
|
|
|
Vagrant::Errors::CommandUnavailableWindows
|
2012-07-25 04:32:38 +00:00
|
|
|
# This means that VirtualBox was not found, so we raise this
|
|
|
|
# error here.
|
|
|
|
raise Vagrant::Errors::VirtualBoxNotDetected
|
|
|
|
end
|
|
|
|
|
|
|
|
# Instantiate the proper version driver for VirtualBox
|
|
|
|
@logger.debug("Finding driver for VirtualBox version: #{@version}")
|
|
|
|
driver_map = {
|
|
|
|
"4.0" => Version_4_0,
|
2012-09-14 02:11:32 +00:00
|
|
|
"4.1" => Version_4_1,
|
2013-10-15 21:37:45 +00:00
|
|
|
"4.2" => Version_4_2,
|
2015-04-28 15:53:48 +00:00
|
|
|
"4.3" => Version_4_3,
|
2015-05-30 18:01:32 +00:00
|
|
|
"5.0" => Version_5_0,
|
2012-07-25 04:32:38 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 17:54:53 +00:00
|
|
|
if @version.start_with?("4.2.14")
|
2013-07-18 17:56:45 +00:00
|
|
|
# VirtualBox 4.2.14 just doesn't work with Vagrant, so show error
|
2013-07-18 17:54:53 +00:00
|
|
|
raise Vagrant::Errors::VirtualBoxBrokenVersion040214
|
|
|
|
end
|
|
|
|
|
2012-07-25 04:32:38 +00:00
|
|
|
driver_klass = nil
|
|
|
|
driver_map.each do |key, klass|
|
|
|
|
if @version.start_with?(key)
|
|
|
|
driver_klass = klass
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if !driver_klass
|
|
|
|
supported_versions = driver_map.keys.sort.join(", ")
|
2013-11-23 19:20:00 +00:00
|
|
|
raise Vagrant::Errors::VirtualBoxInvalidVersion,
|
|
|
|
supported_versions: supported_versions
|
2012-07-25 04:32:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@logger.info("Using VirtualBox driver: #{driver_klass}")
|
|
|
|
@driver = driver_klass.new(@uuid)
|
|
|
|
|
|
|
|
if @uuid
|
|
|
|
# Verify the VM exists, and if it doesn't, then don't worry
|
|
|
|
# about it (mark the UUID as nil)
|
|
|
|
raise VMNotFound if !@driver.vm_exists?(@uuid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def_delegators :@driver, :clear_forwarded_ports,
|
|
|
|
:clear_shared_folders,
|
2014-09-09 17:17:04 +00:00
|
|
|
:clonevm,
|
2012-07-25 04:32:38 +00:00
|
|
|
:create_dhcp_server,
|
|
|
|
:create_host_only_network,
|
2014-09-09 17:17:04 +00:00
|
|
|
:create_snapshot,
|
2012-07-25 04:32:38 +00:00
|
|
|
:delete,
|
|
|
|
:delete_unused_host_only_networks,
|
|
|
|
:discard_saved_state,
|
|
|
|
:enable_adapters,
|
|
|
|
:execute_command,
|
|
|
|
:export,
|
|
|
|
:forward_ports,
|
|
|
|
:halt,
|
|
|
|
:import,
|
|
|
|
:read_forwarded_ports,
|
|
|
|
:read_bridged_interfaces,
|
2014-11-29 18:11:34 +00:00
|
|
|
:read_dhcp_servers,
|
2012-07-25 04:32:38 +00:00
|
|
|
:read_guest_additions_version,
|
2013-11-22 18:26:36 +00:00
|
|
|
:read_guest_ip,
|
|
|
|
:read_guest_property,
|
2012-07-25 04:32:38 +00:00
|
|
|
:read_host_only_interfaces,
|
|
|
|
:read_mac_address,
|
|
|
|
:read_mac_addresses,
|
|
|
|
:read_machine_folder,
|
|
|
|
:read_network_interfaces,
|
|
|
|
:read_state,
|
|
|
|
:read_used_ports,
|
|
|
|
:read_vms,
|
2015-10-01 01:19:37 +00:00
|
|
|
:reconfig_host_only,
|
2014-11-29 20:24:39 +00:00
|
|
|
:remove_dhcp_server,
|
2013-01-31 06:47:45 +00:00
|
|
|
:resume,
|
2012-07-25 04:32:38 +00:00
|
|
|
:set_mac_address,
|
|
|
|
:set_name,
|
|
|
|
:share_folders,
|
|
|
|
:ssh_port,
|
|
|
|
:start,
|
|
|
|
:suspend,
|
|
|
|
:verify!,
|
|
|
|
:verify_image,
|
|
|
|
:vm_exists?
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# This returns the version of VirtualBox that is running.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def read_version
|
|
|
|
# The version string is usually in one of the following formats:
|
|
|
|
#
|
|
|
|
# * 4.1.8r1234
|
|
|
|
# * 4.1.8r1234_OSE
|
|
|
|
# * 4.1.8_MacPortsr1234
|
|
|
|
#
|
|
|
|
# Below accounts for all of these.
|
|
|
|
|
|
|
|
# Note: We split this into multiple lines because apparently "".split("_")
|
|
|
|
# is [], so we have to check for an empty array in between.
|
2014-10-24 16:20:08 +00:00
|
|
|
output = ""
|
|
|
|
retryable(on: Vagrant::Errors::VirtualBoxVersionEmpty, tries: 3, sleep: 1) do
|
|
|
|
output = execute("--version")
|
|
|
|
if output =~ /vboxdrv kernel module is not loaded/ ||
|
|
|
|
output =~ /VirtualBox kernel modules are not loaded/i
|
|
|
|
raise Vagrant::Errors::VirtualBoxKernelModuleNotLoaded
|
|
|
|
elsif output =~ /Please install/
|
|
|
|
# Check for installation incomplete warnings, for example:
|
|
|
|
# "WARNING: The character device /dev/vboxdrv does not
|
|
|
|
# exist. Please install the virtualbox-ose-dkms package and
|
|
|
|
# the appropriate headers, most likely linux-headers-generic."
|
|
|
|
raise Vagrant::Errors::VirtualBoxInstallIncomplete
|
|
|
|
elsif output.chomp == ""
|
|
|
|
# This seems to happen on Windows for uncertain reasons.
|
|
|
|
# Raise an error otherwise the error is that they have an
|
|
|
|
# incompatible version of VirtualBox which isn't true.
|
2015-02-24 18:48:41 +00:00
|
|
|
raise Vagrant::Errors::VirtualBoxVersionEmpty,
|
|
|
|
vboxmanage: @vboxmanage_path.to_s
|
2014-10-24 16:20:08 +00:00
|
|
|
end
|
2012-07-25 04:32:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
parts = output.split("_")
|
|
|
|
return nil if parts.empty?
|
|
|
|
parts[0].split("r")[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|