Forceful halting with `vagrant halt --force` (or -f for short)

This commit is contained in:
Mitchell Hashimoto 2010-04-25 16:42:12 -07:00
parent 23857242f2
commit bbb1b70e1d
5 changed files with 32 additions and 4 deletions

View File

@ -69,7 +69,6 @@ module Vagrant
# to execute a single action on an instance. The syntax for executing a
# single method on an instance is the same as the {execute!} class method.
def execute!(single_action=nil, *args)
if single_action
actions.clear
add_action(single_action, *args)

View File

@ -2,11 +2,18 @@ module Vagrant
module Actions
module VM
class Halt < Base
attr_reader :force
def initialize(vm, force=nil)
super
@force = force
end
def execute!
raise ActionException.new(:vm_not_running) unless @runner.vm.running?
@runner.invoke_around_callback(:halt) do
@runner.system.halt
@runner.system.halt if !force
if @runner.vm.state(true) != :powered_off
logger.info "Forcing shutdown of VM..."

View File

@ -11,12 +11,21 @@ module Vagrant
description "Halts the currently running vagrant environment"
def execute(args=[])
parse_options(args)
env.require_persisted_vm
env.vm.execute!(Actions::VM::Halt)
env.vm.execute!(Actions::VM::Halt, options[:force])
end
def options_spec(opts)
opts.banner = "Usage: vagrant halt"
# Defaults
options[:force] = false
opts.on("-f", "--force", "Forceful shutdown of virtual machine.") do |v|
options[:force] = true
end
end
end
end

View File

@ -36,6 +36,12 @@ class HaltActionTest < Test::Unit::TestCase
@action.execute!
end
should "not call halt on the system if forcing" do
@action.stubs(:force).returns(true)
@runner.system.expects(:halt).never
@action.execute!
end
should "raise an ActionException if VM is not running" do
@vm.stubs(:running?).returns(false)
@vm.expects(:stop).never

View File

@ -21,8 +21,15 @@ class CommandsHaltTest < Test::Unit::TestCase
end
should "call the `halt` action on the VM" do
@persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt).once
@persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, false).once
@instance.execute
end
should "be forceful if -f flag is sent" do
%w{--force -f}.each do |flag|
@persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, true).once
@instance.execute([flag])
end
end
end
end