providers/virtualbox: cache VirtualBox version [GH-6468]

This commit is contained in:
Mitchell Hashimoto 2015-11-19 11:00:02 -08:00
parent 2ffbe4e6e1
commit d56c8fda18
1 changed files with 18 additions and 12 deletions

View File

@ -17,6 +17,10 @@ module VagrantPlugins
# We use forwardable to do all our driver forwarding # We use forwardable to do all our driver forwarding
extend Forwardable extend Forwardable
# We cache the read VirtualBox version here once we have one,
# since during the execution of Vagrant, it likely doesn't change.
@@version = nil
# The UUID of the virtual machine we represent # The UUID of the virtual machine we represent
attr_reader :uuid attr_reader :uuid
@ -32,19 +36,21 @@ module VagrantPlugins
@logger = Log4r::Logger.new("vagrant::provider::virtualbox::meta") @logger = Log4r::Logger.new("vagrant::provider::virtualbox::meta")
@uuid = uuid @uuid = uuid
# Read and assign the version of VirtualBox we know which if !@@version
# specific driver to instantiate. # Read and assign the version of VirtualBox we know which
begin # specific driver to instantiate.
@version = read_version || "" begin
rescue Vagrant::Errors::CommandUnavailable, @@version = read_version
Vagrant::Errors::CommandUnavailableWindows rescue Vagrant::Errors::CommandUnavailable,
# This means that VirtualBox was not found, so we raise this Vagrant::Errors::CommandUnavailableWindows
# error here. # This means that VirtualBox was not found, so we raise this
raise Vagrant::Errors::VirtualBoxNotDetected # error here.
raise Vagrant::Errors::VirtualBoxNotDetected
end
end end
# Instantiate the proper version driver for VirtualBox # Instantiate the proper version driver for VirtualBox
@logger.debug("Finding driver for VirtualBox version: #{@version}") @logger.debug("Finding driver for VirtualBox version: #{@@version}")
driver_map = { driver_map = {
"4.0" => Version_4_0, "4.0" => Version_4_0,
"4.1" => Version_4_1, "4.1" => Version_4_1,
@ -53,14 +59,14 @@ module VagrantPlugins
"5.0" => Version_5_0, "5.0" => Version_5_0,
} }
if @version.start_with?("4.2.14") if @@version.start_with?("4.2.14")
# VirtualBox 4.2.14 just doesn't work with Vagrant, so show error # VirtualBox 4.2.14 just doesn't work with Vagrant, so show error
raise Vagrant::Errors::VirtualBoxBrokenVersion040214 raise Vagrant::Errors::VirtualBoxBrokenVersion040214
end end
driver_klass = nil driver_klass = nil
driver_map.each do |key, klass| driver_map.each do |key, klass|
if @version.start_with?(key) if @@version.start_with?(key)
driver_klass = klass driver_klass = klass
break break
end end