providers/hyperv: clean up status script

This commit is contained in:
Mitchell Hashimoto 2014-02-16 11:31:33 -08:00
parent 88247797d4
commit 7c0948c81d
8 changed files with 173 additions and 158 deletions

View File

@ -28,6 +28,28 @@ module VagrantPlugins
end
end
def self.action_destroy
Vagrant::Action::Builder.new.tap do |b|
b.use Call, IsCreated do |env1, b1|
if !env1[:result]
b2.use MessageNotCreated
next
end
b2.use Call, DestroyConfirm do |env2, b3|
if !env2[:result]
b3.use MessageWillNotDestroy
next
end
b3.use ConfigValidate
b3.use StopInstance
b3.use DeleteVM
end
end
end
end
def self.action_halt
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate

View File

@ -11,15 +11,9 @@ module VagrantPlugins
def call(env)
if env[:machine].id
begin
options = { vm_id: env[:machine].id }
options = { VmId: env[:machine].id }
response = env[:machine].provider.driver.execute('get_vm_status.ps1', options)
env[:machine_state_id] = response["state"].downcase.to_sym
rescue Error::SubprocessError
env[:machine].id = nil
env[:ui].info "Could not find a machine, assuming it to be deleted or terminated."
env[:machine_state_id] = :not_created
end
else
env[:machine_state_id] = :not_created
end

View File

@ -1,9 +1,3 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Open Technologies, Inc.
# All Rights Reserved. Licensed under the MIT License.
#--------------------------------------------------------------------------
require "log4r"
module VagrantPlugins
module HyperV
module Action
@ -15,7 +9,7 @@ module VagrantPlugins
def call(env)
env[:ui].info('Stopping the Machine')
options = { vm_id: env[:machine].id }
response = env[:machine].provider.driver.execute('stop_vm.ps1', options)
env[:machine].provider.driver.execute('stop_vm.ps1', options)
@app.call(env)
end
end

View File

@ -38,11 +38,16 @@ module VagrantPlugins
end
def state
state_id = nil
state_id = :not_created if !@machine.id
if !state_id
# Run a custom action we define called "read_state" which does
# what it says. It puts the state in the `:machine_state_id`
# key in the environment.
env = @machine.action("read_state")
env = @machine.action(:read_state)
state_id = env[:machine_state_id]
end
# Get the short and long description
short = "Machine's current state is #{state_id}"

View File

@ -1,30 +1,25 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Open Technologies, Inc.
# All Rights Reserved. Licensed under the MIT License.
#--------------------------------------------------------------------------
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 }
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1"))
# Get the VM with the given name
try {
$vm = Get-VM -Id $vm_id -ErrorAction "stop"
$state = $vm.state
$status = $vm.status
$VM = Get-VM -Id $VmId -ErrorAction "Stop"
$State = $VM.state
$Status = $VM.status
} catch [Microsoft.HyperV.PowerShell.VirtualizationOperationFailedException] {
$State = "not_created"
$Status = $State
}
$resultHash = @{
state = "$state"
status = "$status"
state = "$State"
status = "$Status"
}
$result = ConvertTo-Json $resultHash
Write-Output-Message $result
}
catch {
Write-Error-Message $_
}

View File

@ -1,21 +1,20 @@
param (
Param(
[string]$vm_xml_config = $(throw "-vm_xml_config is required."),
[string]$vhdx_path = $(throw "-vhdx_path is required.")
)
# Include the following modules
$presentDir = Split-Path -parent $PSCommandPath
$modules = @()
$modules += $presentDir + "\utils\write_messages.ps1"
forEach ($module in $modules) { . $module }
$ErrorActionPreference = "Stop"
# Include the following modules
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
. [System.IO.Path]::Combine($Dir, "utils\write_messages.ps1")
try {
[xml]$vmconfig = Get-Content -Path $vm_xml_config
$vm_name = $vmconfig.configuration.properties.name.'#text'
$processors = $vmconfig.configuration.settings.processors.count.'#text'
function Get_unique_name($name) {
function GetUniqueName($name) {
Get-VM | ForEach-Object -Process {
if ($name -eq $_.Name) {
$name = $name + "_1"
@ -26,7 +25,7 @@ try {
do {
$name = $vm_name
$vm_name = Get_unique_name $name
$vm_name = GetUniqueName $name
} while ($vm_name -ne $name)
$memory = (Select-Xml -xml $vmconfig -XPath "//memory").node.Bank
@ -137,8 +136,3 @@ try {
}
$result = ConvertTo-Json $resultHash
Write-Output-Message $result
}
catch {
Write-Error-Message $_
return
}

View File

@ -1,8 +1,3 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Open Technologies, Inc.
# All Rights Reserved. Licensed under the MIT License.
#--------------------------------------------------------------------------
param (
[string]$vm_id = $(throw "-vm_id is required.")
)

View File

@ -45,4 +45,20 @@ describe VagrantPlugins::HyperV::Provider do
expect(subject.driver).to be_kind_of(VagrantPlugins::HyperV::Driver)
end
end
describe "#state" do
it "returns not_created if no ID" do
machine.stub(id: nil)
expect(subject.state.id).to eq(:not_created)
end
it "calls an action to determine the ID" do
machine.stub(id: "foo")
machine.should_receive(:action).with(:read_state).
and_return({ machine_state_id: :bar })
expect(subject.state.id).to eq(:bar)
end
end
end