If SIGINT is called during a sequence with an import, VM is properly destroyed

This commit is contained in:
Mitchell Hashimoto 2010-07-18 08:39:31 -07:00
parent 769431ce7c
commit cb730c3410
6 changed files with 36 additions and 6 deletions

View File

@ -52,7 +52,11 @@ module Vagrant
# Run the action chain in a busy block, marking the environment as
# interrupted if a SIGINT occurs, and exiting cleanly once the
# chain has been run.
int_callback = lambda { action_environment.error!(:interrupt) }
int_callback = lambda do
env.logger.info "Waiting for cleanup before exiting..."
action_environment.error!(:interrupt)
end
Busy.busy(int_callback) { callable.call(action_environment) }
exit if action_environment.interrupted?

View File

@ -22,7 +22,10 @@ module Vagrant
end
# Import completed successfully. Continue the chain
@app.call(env)
@app.call(env) if !env.error?
# Interrupted, destroy the VM
env["actions"].run(Destroy) if env.interrupted?
end
end
end

View File

@ -334,7 +334,7 @@ module Vagrant
end
if data.empty?
File.delete(dotfile_path)
File.delete(dotfile_path) if File.exist?(dotfile_path)
else
File.open(dotfile_path, 'w+') do |f|
f.write(data.to_json)

View File

@ -36,4 +36,15 @@ class ImportVMActionTest < Test::Unit::TestCase
assert @env.error?
end
should "run the destroy action if interrupted" do
VirtualBox::VM.stubs(:import).returns(mock("vm"))
@app.expects(:call).once.with() do |env|
assert_equal @env, env
@env.error!(:interrupt)
end
@env.env.actions.expects(:run).with(Vagrant::Action::VM::Destroy).once
@instance.call(@env)
end
end

View File

@ -744,6 +744,7 @@ class EnvironmentTest < Test::Unit::TestCase
@env.stubs(:parent).returns(nil)
@env.stubs(:dotfile_path).returns("foo")
File.stubs(:open)
File.stubs(:exist?).returns(true)
end
def create_vm(created)
@ -771,6 +772,17 @@ class EnvironmentTest < Test::Unit::TestCase
@env.update_dotfile
end
should "not remove the dotfile if it doesn't exist" do
vms = {
:foo => create_vm(false)
}
@env.stubs(:vms).returns(vms)
File.expects(:exist?).with(@env.dotfile_path).returns(false)
File.expects(:delete).never
assert_nothing_raised { @env.update_dotfile }
end
should "write the proper data to dotfile" do
vms = {
:foo => create_vm(false),

View File

@ -71,7 +71,6 @@ Gem::Specification.new do |s|
"lib/vagrant/action/vm/suspend.rb",
"lib/vagrant/active_list.rb",
"lib/vagrant/box.rb",
"lib/vagrant/busy.rb",
"lib/vagrant/command.rb",
"lib/vagrant/commands/base.rb",
"lib/vagrant/commands/box.rb",
@ -107,6 +106,7 @@ Gem::Specification.new do |s|
"lib/vagrant/systems/base.rb",
"lib/vagrant/systems/linux.rb",
"lib/vagrant/util.rb",
"lib/vagrant/util/busy.rb",
"lib/vagrant/util/glob_loader.rb",
"lib/vagrant/util/plain_logger.rb",
"lib/vagrant/util/platform.rb",
@ -162,7 +162,6 @@ Gem::Specification.new do |s|
"test/vagrant/action_test.rb",
"test/vagrant/active_list_test.rb",
"test/vagrant/box_test.rb",
"test/vagrant/busy_test.rb",
"test/vagrant/command_test.rb",
"test/vagrant/commands/base_test.rb",
"test/vagrant/commands/box/add_test.rb",
@ -196,6 +195,7 @@ Gem::Specification.new do |s|
"test/vagrant/ssh_session_test.rb",
"test/vagrant/ssh_test.rb",
"test/vagrant/systems/linux_test.rb",
"test/vagrant/util/busy_test.rb",
"test/vagrant/util/plain_logger_test.rb",
"test/vagrant/util/platform_test.rb",
"test/vagrant/util/stacked_proc_runner_test.rb",
@ -248,7 +248,6 @@ Gem::Specification.new do |s|
"test/vagrant/action_test.rb",
"test/vagrant/active_list_test.rb",
"test/vagrant/box_test.rb",
"test/vagrant/busy_test.rb",
"test/vagrant/command_test.rb",
"test/vagrant/commands/base_test.rb",
"test/vagrant/commands/box/add_test.rb",
@ -282,6 +281,7 @@ Gem::Specification.new do |s|
"test/vagrant/ssh_session_test.rb",
"test/vagrant/ssh_test.rb",
"test/vagrant/systems/linux_test.rb",
"test/vagrant/util/busy_test.rb",
"test/vagrant/util/plain_logger_test.rb",
"test/vagrant/util/platform_test.rb",
"test/vagrant/util/stacked_proc_runner_test.rb",