From 27688a183bd65c5c83a19c35b93aa7fc77649451 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 16 Feb 2014 11:42:20 -0800 Subject: [PATCH] providers/hyperv: destroy --- plugins/providers/hyperv/action.rb | 13 +++-- plugins/providers/hyperv/action/delete_vm.rb | 18 ++++++ .../providers/hyperv/action/stop_instance.rb | 2 +- plugins/providers/hyperv/driver.rb | 55 ++----------------- .../providers/hyperv/scripts/delete_vm.ps1 | 7 +++ .../providers/hyperv/scripts/import_vm.ps1 | 10 ++-- plugins/providers/hyperv/scripts/stop_vm.ps1 | 32 +++-------- 7 files changed, 49 insertions(+), 88 deletions(-) create mode 100644 plugins/providers/hyperv/action/delete_vm.rb create mode 100644 plugins/providers/hyperv/scripts/delete_vm.ps1 diff --git a/plugins/providers/hyperv/action.rb b/plugins/providers/hyperv/action.rb index c5da842b6..d13dc7103 100644 --- a/plugins/providers/hyperv/action.rb +++ b/plugins/providers/hyperv/action.rb @@ -32,19 +32,19 @@ module VagrantPlugins Vagrant::Action::Builder.new.tap do |b| b.use Call, IsCreated do |env1, b1| if !env1[:result] - b2.use MessageNotCreated + b1.use MessageNotCreated next end - b2.use Call, DestroyConfirm do |env2, b3| + b1.use Call, DestroyConfirm do |env2, b2| if !env2[:result] - b3.use MessageWillNotDestroy + b2.use MessageWillNotDestroy next end - b3.use ConfigValidate - b3.use StopInstance - b3.use DeleteVM + b2.use ConfigValidate + b2.use StopInstance + b2.use DeleteVM end end end @@ -128,6 +128,7 @@ module VagrantPlugins # The autoload farm action_root = Pathname.new(File.expand_path("../action", __FILE__)) + autoload :DeleteVM, action_root.join("delete_vm") autoload :IsCreated, action_root.join("is_created") autoload :IsStopped, action_root.join("is_stopped") autoload :ReadState, action_root.join("read_state") diff --git a/plugins/providers/hyperv/action/delete_vm.rb b/plugins/providers/hyperv/action/delete_vm.rb new file mode 100644 index 000000000..afc47dd8f --- /dev/null +++ b/plugins/providers/hyperv/action/delete_vm.rb @@ -0,0 +1,18 @@ +module VagrantPlugins + module HyperV + module Action + class DeleteVM + def initialize(app, env) + @app = app + end + + def call(env) + env[:ui].info('Deleting the Machine') + options = { VmId: env[:machine].id } + env[:machine].provider.driver.execute('delete_vm.ps1', options) + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/hyperv/action/stop_instance.rb b/plugins/providers/hyperv/action/stop_instance.rb index d6693cdc2..fe62693f8 100644 --- a/plugins/providers/hyperv/action/stop_instance.rb +++ b/plugins/providers/hyperv/action/stop_instance.rb @@ -8,7 +8,7 @@ module VagrantPlugins def call(env) env[:ui].info('Stopping the Machine') - options = { vm_id: env[:machine].id } + options = { VmId: env[:machine].id } env[:machine].provider.driver.execute('stop_vm.ps1', options) @app.call(env) end diff --git a/plugins/providers/hyperv/driver.rb b/plugins/providers/hyperv/driver.rb index 7676b1849..0ff8bad4e 100644 --- a/plugins/providers/hyperv/driver.rb +++ b/plugins/providers/hyperv/driver.rb @@ -10,13 +10,6 @@ module VagrantPlugins ERROR_REGEXP = /===Begin-Error===(.+?)===End-Error===/m OUTPUT_REGEXP = /===Begin-Output===(.+?)===End-Output===/m - attr_reader :vmid - - def initialize(id=nil) - @vmid = id - @output = nil - end - def execute(path, options) r = execute_powershell(path, options) if r.exit_code != 0 @@ -48,49 +41,6 @@ module VagrantPlugins protected - def json_output - return @json_output if @json_output - json_success_begin = false - json_error_begin = false - success = [] - error = [] - @output.split("\n").each do |line| - json_error_begin = false if line.include?("===End-Error===") - json_success_begin = false if line.include?("===End-Output===") - message = "" - if json_error_begin || json_success_begin - message = line.gsub("\\'","\"") - end - success << message if json_success_begin - error << message if json_error_begin - json_success_begin = true if line.include?("===Begin-Output===") - json_error_begin = true if line.include?("===Begin-Error===") - end - @json_output = { :success => success, :error => error } - end - - def success? - @error_messages.empty? && json_output[:error].empty? - end - - def process_output(type, data) - if type == :stdout - @output = data.gsub("\r\n", "\n") - end - if type == :stdin - # $stdin.gets.chomp || "" - end - if type == :stderr - @error_messages = data.gsub("\r\n", "\n") - end - end - - def clear_output_buffer - @output = "" - @error_messages = "" - @json_output = nil - end - def execute_powershell(path, options, &block) lib_path = Pathname.new(File.expand_path("../scripts", __FILE__)) path = lib_path.join(path).to_s.gsub("/", "\\") @@ -100,7 +50,10 @@ module VagrantPlugins ps_options << "-#{key}" ps_options << "'#{value}'" end - clear_output_buffer + + # Always have a stop error action for failures + ps_options << "-ErrorAction" << "Stop" + opts = { notify: [:stdout, :stderr, :stdin] } Vagrant::Util::PowerShell.execute(path, *ps_options, **opts, &block) end diff --git a/plugins/providers/hyperv/scripts/delete_vm.ps1 b/plugins/providers/hyperv/scripts/delete_vm.ps1 new file mode 100644 index 000000000..f54d23dc5 --- /dev/null +++ b/plugins/providers/hyperv/scripts/delete_vm.ps1 @@ -0,0 +1,7 @@ +Param( + [Parameter(Mandatory=$true)] + [string]$VmId +) + +$VM = Get-VM -Id $VmId -ErrorAction "Stop" +Remove-VM $VM -Force diff --git a/plugins/providers/hyperv/scripts/import_vm.ps1 b/plugins/providers/hyperv/scripts/import_vm.ps1 index a24351b6f..5d673456f 100644 --- a/plugins/providers/hyperv/scripts/import_vm.ps1 +++ b/plugins/providers/hyperv/scripts/import_vm.ps1 @@ -1,13 +1,13 @@ Param( - [string]$vm_xml_config = $(throw "-vm_xml_config is required."), - [string]$vhdx_path = $(throw "-vhdx_path is required.") + [Parameter(Mandatory=$true)] + [string]$vm_xml_config, + [Parameter(Mandatory=$true)] + [string]$vhdx_path ) -$ErrorActionPreference = "Stop" - # Include the following modules $Dir = Split-Path $script:MyInvocation.MyCommand.Path -. [System.IO.Path]::Combine($Dir, "utils\write_messages.ps1") +. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1")) [xml]$vmconfig = Get-Content -Path $vm_xml_config diff --git a/plugins/providers/hyperv/scripts/stop_vm.ps1 b/plugins/providers/hyperv/scripts/stop_vm.ps1 index 4ac442ab1..5bf4a441d 100644 --- a/plugins/providers/hyperv/scripts/stop_vm.ps1 +++ b/plugins/providers/hyperv/scripts/stop_vm.ps1 @@ -1,26 +1,8 @@ -param ( - [string]$vm_id = $(throw "-vm_id is required.") - ) +Param( + [Parameter(Mandatory=$true)] + [string]$VmId +) -# Include the following modules -$presentDir = Split-Path -parent $PSCommandPath -$modules = @() -$modules += $presentDir + "\utils\write_messages.ps1" -forEach ($module in $modules) { . $module } - -try { - $vm = Get-VM -Id $vm_id -ErrorAction stop - # Shuts down virtual machine regardless of any unsaved application data - Stop-VM $vm -Force - $state = $vm.state - $status = $vm.status - $resultHash = @{ - state = "$state" - status = "$status" - } - $result = ConvertTo-Json $resultHash - Write-Output-Message $result -} -catch { - Write-Error-Message "Failed to stop a VM $_" -} +# Shuts down virtual machine regardless of any unsaved application data +$VM = Get-VM -Id $VmId -ErrorAction "Stop" +Stop-VM $VM -Force