diff --git a/bin/vagrant b/bin/vagrant index 0cd730b68..a51aaf8be 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -45,7 +45,13 @@ begin env.ui.warn(I18n.t("vagrant.general.not_in_installer")) if !Vagrant.in_installer? # Execute the CLI interface, and exit with the proper error code - exit(env.cli(ARGV)) + exit_status = env.cli(ARGV) + + # Unload the environment so cleanup can be done + env.unload + + # Exit with the exit status from our CLI command + exit(exit_status) rescue Vagrant::Errors::VagrantError => e logger.error("Vagrant experienced an error! Details:") logger.error(e.inspect) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c88f245af..8cbe2d642 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -379,6 +379,18 @@ module Vagrant nil end + # Unload the environment, running completion hooks. The environment + # should not be used after this (but CAN be, technically). It is + # recommended to always immediately set the variable to `nil` after + # running this so you can't accidentally run any more methods. Example: + # + # env.unload + # env = nil + # + def unload + hook(:environment_unload) + end + # Makes a call to the CLI with the given arguments as if they # came from the real command line (sometimes they do!). An example: # diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 9de9d531c..b455f62e0 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -300,6 +300,13 @@ VF end end + describe "#unload" do + it "should run the unload hook" do + instance.should_receive(:hook).with(:environment_unload).once + instance.unload + end + end + describe "getting a machine" do # A helper to register a provider for use in tests. def register_provider(name, config_class=nil)