diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a8b4b122..71969ff3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ [GH-726] - Fix issue where Vagrant would sometimes "lose" a VM if an exception occurred. [GH-725] + - `vagrant destroy` destroys virtual machines in reverse order. [GH-739] ## 0.9.7 (February 9, 2012) diff --git a/lib/vagrant/command/base.rb b/lib/vagrant/command/base.rb index 4ffbd0155..f364bafe9 100644 --- a/lib/vagrant/command/base.rb +++ b/lib/vagrant/command/base.rb @@ -60,10 +60,13 @@ module Vagrant # @param [String] name The name of the VM. Nil if every VM. # @param [Boolean] single_target If true, then an exception will be # raised if more than one target is found. - def with_target_vms(name=nil, single_target=false) + def with_target_vms(name=nil, options=nil) # Using VMs requires a Vagrant environment to be properly setup raise Errors::NoEnvironmentError if !@env.root_path + # Setup the options hash + options ||= {} + # First determine the proper array of VMs. vms = [] if name @@ -89,7 +92,10 @@ module Vagrant end # Make sure we're only working with one VM if single target - raise Errors::MultiVMTargetRequired if single_target && vms.length != 1 + raise Errors::MultiVMTargetRequired if options[:single_target] && vms.length != 1 + + # If we asked for reversed ordering, then reverse it + vms.reverse! if options[:reverse] # Go through each VM and yield it! vms.each do |old_vm| diff --git a/lib/vagrant/command/destroy.rb b/lib/vagrant/command/destroy.rb index ff8c95d6b..d52aadf15 100644 --- a/lib/vagrant/command/destroy.rb +++ b/lib/vagrant/command/destroy.rb @@ -20,7 +20,7 @@ module Vagrant return if !argv @logger.debug("'Destroy' each target VM...") - with_target_vms(argv[0]) do |vm| + with_target_vms(argv[0], :reverse => true) do |vm| if vm.created? # Boolean whether we should actually go through with the destroy # or not. This is true only if the "--force" flag is set or if the diff --git a/lib/vagrant/command/package.rb b/lib/vagrant/command/package.rb index 5662f8a7d..d379913a3 100644 --- a/lib/vagrant/command/package.rb +++ b/lib/vagrant/command/package.rb @@ -51,7 +51,7 @@ module Vagrant end def package_target(name, options) - with_target_vms(name, true) do |vm| + with_target_vms(name, :single_target => true) do |vm| raise Errors::VMNotCreatedError if !vm.created? @logger.debug("Packaging VM: #{vm.name}") package_vm(vm, options) diff --git a/lib/vagrant/command/ssh.rb b/lib/vagrant/command/ssh.rb index e49e5446a..0df1b3007 100644 --- a/lib/vagrant/command/ssh.rb +++ b/lib/vagrant/command/ssh.rb @@ -36,7 +36,7 @@ module Vagrant argv = [] if argv == ssh_args # Execute the actual SSH - with_target_vms(argv[0], true) do |vm| + with_target_vms(argv[0], :single_target => true) do |vm| # Basic checks that are required for proper SSH raise Errors::VMNotCreatedError if !vm.created? raise Errors::VMInaccessible if !vm.state == :inaccessible diff --git a/lib/vagrant/command/ssh_config.rb b/lib/vagrant/command/ssh_config.rb index fa73d1130..5ecb082e5 100644 --- a/lib/vagrant/command/ssh_config.rb +++ b/lib/vagrant/command/ssh_config.rb @@ -19,7 +19,7 @@ module Vagrant argv = parse_options(opts) return if !argv - with_target_vms(argv[0], true) do |vm| + with_target_vms(argv[0], :single_target => true) do |vm| raise Errors::VMNotCreatedError if !vm.created? raise Errors::VMInaccessible if !vm.state == :inaccessible