From 3c2fab9d0d24d8988142064ff966201a668b7e22 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 20 Nov 2015 15:56:14 -0800 Subject: [PATCH] providers/virtualbox: more robust lookup for VBoxManage on Win --- .../cap/provider_install_virtualbox.rb | 3 +- plugins/providers/virtualbox/driver/base.rb | 29 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/plugins/hosts/windows/cap/provider_install_virtualbox.rb b/plugins/hosts/windows/cap/provider_install_virtualbox.rb index 160a6bf5d..c2e3325f8 100644 --- a/plugins/hosts/windows/cap/provider_install_virtualbox.rb +++ b/plugins/hosts/windows/cap/provider_install_virtualbox.rb @@ -2,6 +2,7 @@ require "pathname" require "tempfile" require "vagrant/util/downloader" +require "vagrant/util/powershell" require "vagrant/util/subprocess" module VagrantPlugins @@ -35,7 +36,7 @@ module VagrantPlugins ui.detail(I18n.t( "vagrant.hosts.windows.virtualbox_install_install_detail")) script = File.expand_path("../../scripts/install_virtualbox.ps1", __FILE__) - result = Vagrant::Util::Powershell.execute(script, tf.path) + result = Vagrant::Util::PowerShell.execute(script, tf.path) if result.exit_code != 0 raise Vagrant::Errors::ProviderInstallFailed, provider: "virtualbox", diff --git a/plugins/providers/virtualbox/driver/base.rb b/plugins/providers/virtualbox/driver/base.rb index 8dc0ba994..c304d3efb 100644 --- a/plugins/providers/virtualbox/driver/base.rb +++ b/plugins/providers/virtualbox/driver/base.rb @@ -4,6 +4,7 @@ require 'vagrant/util/busy' require 'vagrant/util/platform' require 'vagrant/util/retryable' require 'vagrant/util/subprocess' +require 'vagrant/util/which' module VagrantPlugins module ProviderVirtualBox @@ -22,16 +23,16 @@ module VagrantPlugins # This flag is used to keep track of interrupted state (SIGINT) @interrupted = false - # Set the path to VBoxManage - @vboxmanage_path = "VBoxManage" - if Vagrant::Util::Platform.windows? || Vagrant::Util::Platform.cygwin? - @logger.debug("Windows. Trying VBOX_INSTALL_PATH for VBoxManage") + @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 ENV.key?("VBOX_INSTALL_PATH") || - ENV.key?("VBOX_MSI_INSTALL_PATH") + 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}") @@ -51,8 +52,24 @@ module VagrantPlugins 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 end + # Fall back to hoping for the PATH to work out + @vboxmanage_path ||= "VBoxManage" @logger.info("VBoxManage path: #{@vboxmanage_path}") end