From cb730c34106081786c095ee02d17ae025f4a6c8e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 18 Jul 2010 08:39:31 -0700 Subject: [PATCH] If SIGINT is called during a sequence with an import, VM is properly destroyed --- lib/vagrant/action.rb | 6 +++++- lib/vagrant/action/vm/import.rb | 5 ++++- lib/vagrant/environment.rb | 2 +- test/vagrant/action/vm/import_test.rb | 11 +++++++++++ test/vagrant/environment_test.rb | 12 ++++++++++++ vagrant.gemspec | 6 +++--- 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 2f461f958..fdf12915b 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -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? diff --git a/lib/vagrant/action/vm/import.rb b/lib/vagrant/action/vm/import.rb index b544819cf..9348631c3 100644 --- a/lib/vagrant/action/vm/import.rb +++ b/lib/vagrant/action/vm/import.rb @@ -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 diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index e691a9da7..c001756ed 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -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) diff --git a/test/vagrant/action/vm/import_test.rb b/test/vagrant/action/vm/import_test.rb index 6467e885e..47d385695 100644 --- a/test/vagrant/action/vm/import_test.rb +++ b/test/vagrant/action/vm/import_test.rb @@ -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 diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 53ab5ad96..78f2cc0af 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -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), diff --git a/vagrant.gemspec b/vagrant.gemspec index 8ce30253a..4076a0738 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -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",