From dad5962ebb5584ce77c9ef3d055c34aa64d70caf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 4 Nov 2015 15:47:56 -0800 Subject: [PATCH] hosts/darwin: support virtualbox install --- lib/vagrant/errors.rb | 4 ++ lib/vagrant/ui.rb | 3 +- plugins/commands/provider/command.rb | 5 +- .../darwin/cap/provider_install_virtualbox.rb | 51 +++++++++++++++++++ plugins/hosts/darwin/plugin.rb | 5 ++ .../darwin/scripts/install_virtualbox.sh | 15 ++++++ templates/locales/en.yml | 24 +++++++++ 7 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 plugins/hosts/darwin/cap/provider_install_virtualbox.rb create mode 100755 plugins/hosts/darwin/scripts/install_virtualbox.sh diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index f46491604..c76f3cbe9 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -524,6 +524,10 @@ module Vagrant error_key(:provider_cant_install) end + class ProviderInstallFailed < VagrantError + error_key(:provider_install_failed) + end + class ProviderNotFound < VagrantError error_key(:provider_not_found) end diff --git a/lib/vagrant/ui.rb b/lib/vagrant/ui.rb index fb5929f32..c610643a9 100644 --- a/lib/vagrant/ui.rb +++ b/lib/vagrant/ui.rb @@ -319,6 +319,7 @@ module Vagrant target = @prefix target = opts[:target] if opts.key?(:target) + target = "#{target}:" if target != "" # Get the lines. The first default is because if the message # is an empty string, then we want to still use the empty string. @@ -327,7 +328,7 @@ module Vagrant # Otherwise, make sure to prefix every line properly lines.map do |line| - "#{prefix}#{target}: #{line}" + "#{prefix}#{target} #{line}" end.join("\n") end end diff --git a/plugins/commands/provider/command.rb b/plugins/commands/provider/command.rb index 401c8be03..7fd0ef743 100644 --- a/plugins/commands/provider/command.rb +++ b/plugins/commands/provider/command.rb @@ -53,12 +53,13 @@ module VagrantPlugins # Check if we're requesting installation if options[:install] - if !machine.provider.capability?(:install) + key = "provider_install_#{machine.provider_name}".to_sym + if !@env.host.capability?(key) raise Vagrant::Errors::ProviderCantInstall, provider: machine.provider_name.to_s end - machine.provider.capability(:install) + @env.host.capability(key) return end diff --git a/plugins/hosts/darwin/cap/provider_install_virtualbox.rb b/plugins/hosts/darwin/cap/provider_install_virtualbox.rb new file mode 100644 index 000000000..c4748eecb --- /dev/null +++ b/plugins/hosts/darwin/cap/provider_install_virtualbox.rb @@ -0,0 +1,51 @@ +require "pathname" +require "tempfile" + +require "vagrant/util/downloader" +require "vagrant/util/subprocess" + +module VagrantPlugins + module HostDarwin + module Cap + class ProviderInstallVirtualBox + # The URL to download VirtualBox is hardcoded so we can have a + # known-good version to download. + URL = "http://download.virtualbox.org/virtualbox/5.0.8/VirtualBox-5.0.8-103449-OSX.dmg".freeze + VERSION = "5.0.8".freeze + + def self.provider_install_virtualbox(env) + tf = Tempfile.new("vagrant") + tf.close + + # Prefixed UI for prettiness + ui = Vagrant::UI::Prefixed.new(env.ui, "") + + # Start by downloading the file using the standard mechanism + ui.output(I18n.t( + "vagrant.hosts.darwin.virtualbox_install_download", + version: VERSION)) + ui.detail(I18n.t( + "vagrant.hosts.darwin.virtualbox_install_detail")) + dl = Vagrant::Util::Downloader.new(URL, tf.path, ui: ui) + dl.download! + + # Launch it + ui.output(I18n.t( + "vagrant.hosts.darwin.virtualbox_install_install")) + ui.detail(I18n.t( + "vagrant.hosts.darwin.virtualbox_install_install_detail")) + script = File.expand_path("../../scripts/install_virtualbox.sh", __FILE__) + result = Vagrant::Util::Subprocess.execute("bash", script, tf.path) + if result.exit_code != 0 + raise Vagrant::Errors::ProviderInstallFailed, + provider: "virtualbox", + stdout: result.stdout, + stderr: result.stderr + end + + ui.success(I18n.t("vagrant.hosts.darwin.virtualbox_install_success")) + end + end + end + end +end diff --git a/plugins/hosts/darwin/plugin.rb b/plugins/hosts/darwin/plugin.rb index 226a5bae1..f37bcd9dd 100644 --- a/plugins/hosts/darwin/plugin.rb +++ b/plugins/hosts/darwin/plugin.rb @@ -11,6 +11,11 @@ module VagrantPlugins Host end + host_capability("darwin", "provider_install_virtualbox") do + require_relative "cap/provider_install_virtualbox" + Cap::ProviderInstallVirtualBox + end + host_capability("darwin", "rdp_client") do require_relative "cap/rdp" Cap::RDP diff --git a/plugins/hosts/darwin/scripts/install_virtualbox.sh b/plugins/hosts/darwin/scripts/install_virtualbox.sh new file mode 100755 index 000000000..1ac0da126 --- /dev/null +++ b/plugins/hosts/darwin/scripts/install_virtualbox.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -e + +hdiutil attach $1 +cd /Volumes/VirtualBox/ +sudo installer -pkg VirtualBox.pkg -target "/" +cd /tmp +flag=1 +while [ $flag -ne 0 ]; do + sleep 1 + set +e + hdiutil detach /Volumes/VirtualBox/ + flag=$? + set -e +done diff --git a/templates/locales/en.yml b/templates/locales/en.yml index eee943b1c..908af4c4c 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -280,6 +280,7 @@ en: CHANGELOG below: https://github.com/mitchellh/vagrant/blob/v%{version}/CHANGELOG.md + cfengine_config: classes_array: |- The 'classes' configuration must be an array. @@ -1006,6 +1007,14 @@ en: This is a limitation of this provider. Please report this as a feature request to the provider in question. To install this provider, you'll have to do so manually. + provider_install_failed: |- + Installation of the provider '%{provider}' failed! The stdout + and stderr are shown below. Please read the error output, resolve it, + and try again. If problem persists, please install the provider + manually. + + Stdout: %{stdout} + Stderr: %{stderr} provider_not_found: |- The provider '%{provider}' could not be found, but was requested to back the machine '%{machine}'. Please use a provider that exists. @@ -1867,6 +1876,21 @@ en: Preparing to edit /etc/exports. Administrator privileges will be required... nfs_prune: |- Pruning invalid NFS exports. Administrator privileges will be required... + darwin: + virtualbox_install_download: |- + Downloading VirtualBox %{version}... + virtualbox_install_detail: |- + This may not be the latest version of VirtualBox, but it is a version + that is known to work well. Over time, we'll update the version that + is installed. + virtualbox_install_install: |- + Installing VirtualBox. This will take a few minutes... + virtualbox_install_install_detail: |- + You may be asked for your administrator password during this time. + If you're uncomfortable entering your password here, please install + VirtualBox manually. + virtualbox_install_success: |- + VirtualBox has successfully been installed! linux: nfs_export: |- Preparing to edit /etc/exports. Administrator privileges will be required...