Merge pull request #6552 from mitchellh/f-cache-vbox

providers/virtualbox: cache VirtualBox version [GH-6468]
This commit is contained in:
Mitchell Hashimoto 2015-11-19 11:20:09 -08:00
commit 270c0726df
1 changed files with 23 additions and 12 deletions

View File

@ -1,4 +1,5 @@
require "forwardable" require "forwardable"
require "thread"
require "log4r" require "log4r"
@ -17,6 +18,11 @@ 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
@@version_lock = Mutex.new
# The UUID of the virtual machine we represent # The UUID of the virtual machine we represent
attr_reader :uuid attr_reader :uuid
@ -32,19 +38,23 @@ 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 @@version_lock.synchronize do
# specific driver to instantiate. if !@@version
begin # Read and assign the version of VirtualBox we know which
@version = read_version || "" # specific driver to instantiate.
rescue Vagrant::Errors::CommandUnavailable, begin
Vagrant::Errors::CommandUnavailableWindows @@version = read_version
# This means that VirtualBox was not found, so we raise this rescue Vagrant::Errors::CommandUnavailable,
# error here. Vagrant::Errors::CommandUnavailableWindows
raise Vagrant::Errors::VirtualBoxNotDetected # This means that VirtualBox was not found, so we raise this
# error here.
raise Vagrant::Errors::VirtualBoxNotDetected
end
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 +63,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
@ -74,6 +84,7 @@ module VagrantPlugins
@logger.info("Using VirtualBox driver: #{driver_klass}") @logger.info("Using VirtualBox driver: #{driver_klass}")
@driver = driver_klass.new(@uuid) @driver = driver_klass.new(@uuid)
@version = @@version
if @uuid if @uuid
# Verify the VM exists, and if it doesn't, then don't worry # Verify the VM exists, and if it doesn't, then don't worry