providers/hyperv: destroy

This commit is contained in:
Mitchell Hashimoto 2014-02-16 11:42:20 -08:00
parent 7c0948c81d
commit 27688a183b
7 changed files with 49 additions and 88 deletions

View File

@ -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")

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,7 @@
Param(
[Parameter(Mandatory=$true)]
[string]$VmId
)
$VM = Get-VM -Id $VmId -ErrorAction "Stop"
Remove-VM $VM -Force

View File

@ -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

View File

@ -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 $_"
}
$VM = Get-VM -Id $VmId -ErrorAction "Stop"
Stop-VM $VM -Force