From c02eae2279a7f463b01f235e51f769ec3bda20df Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 9 Apr 2014 09:30:17 -0700 Subject: [PATCH] providers/hyperv: verify cmdlet is available [GH-3398] --- CHANGELOG.md | 1 + plugins/providers/hyperv/action.rb | 2 ++ .../providers/hyperv/action/check_enabled.rb | 23 +++++++++++++++++++ plugins/providers/hyperv/errors.rb | 4 ++++ .../providers/hyperv/scripts/check_hyperv.ps1 | 10 ++++++++ templates/locales/providers_hyperv.yml | 4 ++++ 6 files changed, 44 insertions(+) create mode 100644 plugins/providers/hyperv/action/check_enabled.rb create mode 100644 plugins/providers/hyperv/scripts/check_hyperv.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 790bf47bc..2ff77390c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ BUG FIXES: - guests/fedora: Fix hostname setting. [GH-3382] - guests/fedora: Support predictable network interface names for public/private networks. [GH-3207] + - providers/hyperv: Check for PowerShell features. [GH-3398] - synced\_folders/smb: Only set the chmod properly by default on Windows if it isn't already set. [GH-3394] - synced\_folders/smb: Sharing folders with odd characters like parens diff --git a/plugins/providers/hyperv/action.rb b/plugins/providers/hyperv/action.rb index 301fb50bc..c68930d57 100644 --- a/plugins/providers/hyperv/action.rb +++ b/plugins/providers/hyperv/action.rb @@ -107,6 +107,7 @@ module VagrantPlugins def self.action_up Vagrant::Action::Builder.new.tap do |b| + b.use CheckEnabled b.use HandleBox b.use ConfigValidate b.use Call, IsState, :not_created do |env1, b1| @@ -163,6 +164,7 @@ module VagrantPlugins # The autoload farm action_root = Pathname.new(File.expand_path("../action", __FILE__)) + autoload :CheckEnabled, action_root.join("check_enabled") autoload :DeleteVM, action_root.join("delete_vm") autoload :Import, action_root.join("import") autoload :IsWindows, action_root.join("is_windows") diff --git a/plugins/providers/hyperv/action/check_enabled.rb b/plugins/providers/hyperv/action/check_enabled.rb new file mode 100644 index 000000000..c4df981cd --- /dev/null +++ b/plugins/providers/hyperv/action/check_enabled.rb @@ -0,0 +1,23 @@ +require "fileutils" + +require "log4r" + +module VagrantPlugins + module HyperV + module Action + class CheckEnabled + def initialize(app, env) + @app = app + end + + def call(env) + env[:ui].output("Verifying Hyper-V is enabled...") + result = env[:machine].provider.driver.execute("check_hyperv.ps1", {}) + raise Errors::PowerShellFeaturesDisabled if !result["result"] + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/hyperv/errors.rb b/plugins/providers/hyperv/errors.rb index b82b0325a..ada15cde9 100644 --- a/plugins/providers/hyperv/errors.rb +++ b/plugins/providers/hyperv/errors.rb @@ -22,6 +22,10 @@ module VagrantPlugins error_key(:no_switches) end + class PowerShellFeaturesDisabled < HyperVError + error_key(:powershell_features_disabled) + end + class PowerShellError < HyperVError error_key(:powershell_error) end diff --git a/plugins/providers/hyperv/scripts/check_hyperv.ps1 b/plugins/providers/hyperv/scripts/check_hyperv.ps1 new file mode 100644 index 000000000..f14b5f360 --- /dev/null +++ b/plugins/providers/hyperv/scripts/check_hyperv.ps1 @@ -0,0 +1,10 @@ +# Include the following modules +$Dir = Split-Path $script:MyInvocation.MyCommand.Path +. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1")) + +$check = $(-Not (-Not (Get-Command "Get-VMSwitch" -errorAction SilentlyContinue))) +$result = @{ + result = $check +} + +Write-Output-Message $(ConvertTo-Json $result) diff --git a/templates/locales/providers_hyperv.yml b/templates/locales/providers_hyperv.yml index 6c010cf89..d81875297 100644 --- a/templates/locales/providers_hyperv.yml +++ b/templates/locales/providers_hyperv.yml @@ -51,6 +51,10 @@ en: For more help, please see the documentation on the Vagrant website for Hyper-V. + powershell_features_disabled: |- + The Hyper-V cmdlets for PowerShell are not available! Vagrant + requires these to control Hyper-V. Please enable them in the + "Windows Features" control panel and try again. powershell_error: |- An error occurred while executing a PowerShell script. This error is shown below. Please read the error message and see if this is