Forceful halting with `vagrant halt --force` (or -f for short)
This commit is contained in:
parent
23857242f2
commit
bbb1b70e1d
|
@ -69,7 +69,6 @@ module Vagrant
|
||||||
# to execute a single action on an instance. The syntax for executing a
|
# 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.
|
# single method on an instance is the same as the {execute!} class method.
|
||||||
def execute!(single_action=nil, *args)
|
def execute!(single_action=nil, *args)
|
||||||
|
|
||||||
if single_action
|
if single_action
|
||||||
actions.clear
|
actions.clear
|
||||||
add_action(single_action, *args)
|
add_action(single_action, *args)
|
||||||
|
|
|
@ -2,11 +2,18 @@ module Vagrant
|
||||||
module Actions
|
module Actions
|
||||||
module VM
|
module VM
|
||||||
class Halt < Base
|
class Halt < Base
|
||||||
|
attr_reader :force
|
||||||
|
|
||||||
|
def initialize(vm, force=nil)
|
||||||
|
super
|
||||||
|
@force = force
|
||||||
|
end
|
||||||
|
|
||||||
def execute!
|
def execute!
|
||||||
raise ActionException.new(:vm_not_running) unless @runner.vm.running?
|
raise ActionException.new(:vm_not_running) unless @runner.vm.running?
|
||||||
|
|
||||||
@runner.invoke_around_callback(:halt) do
|
@runner.invoke_around_callback(:halt) do
|
||||||
@runner.system.halt
|
@runner.system.halt if !force
|
||||||
|
|
||||||
if @runner.vm.state(true) != :powered_off
|
if @runner.vm.state(true) != :powered_off
|
||||||
logger.info "Forcing shutdown of VM..."
|
logger.info "Forcing shutdown of VM..."
|
||||||
|
|
|
@ -11,12 +11,21 @@ module Vagrant
|
||||||
description "Halts the currently running vagrant environment"
|
description "Halts the currently running vagrant environment"
|
||||||
|
|
||||||
def execute(args=[])
|
def execute(args=[])
|
||||||
|
parse_options(args)
|
||||||
|
|
||||||
env.require_persisted_vm
|
env.require_persisted_vm
|
||||||
env.vm.execute!(Actions::VM::Halt)
|
env.vm.execute!(Actions::VM::Halt, options[:force])
|
||||||
end
|
end
|
||||||
|
|
||||||
def options_spec(opts)
|
def options_spec(opts)
|
||||||
opts.banner = "Usage: vagrant halt"
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,6 +36,12 @@ class HaltActionTest < Test::Unit::TestCase
|
||||||
@action.execute!
|
@action.execute!
|
||||||
end
|
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
|
should "raise an ActionException if VM is not running" do
|
||||||
@vm.stubs(:running?).returns(false)
|
@vm.stubs(:running?).returns(false)
|
||||||
@vm.expects(:stop).never
|
@vm.expects(:stop).never
|
||||||
|
|
|
@ -21,8 +21,15 @@ class CommandsHaltTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "call the `halt` action on the VM" do
|
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
|
@instance.execute
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue