From d8bffd201f0e452dd28a31b8cb876e41ec86c8fa Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 31 Oct 2017 13:45:56 -0700 Subject: [PATCH] Add delay for parallel destroy without force, restructure state checking This commit adds better messaging to the user if the parallel flag is used without the force flag. It also makes the state checking based on the delta between the initial and final states of the guests due to the fact that there is no guarantee what the "destroyed" state id will be between providers. --- plugins/commands/destroy/command.rb | 40 +++++++++++++++++++---------- templates/locales/en.yml | 3 +++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/plugins/commands/destroy/command.rb b/plugins/commands/destroy/command.rb index 569b3f092..d0b0f7730 100644 --- a/plugins/commands/destroy/command.rb +++ b/plugins/commands/destroy/command.rb @@ -20,7 +20,7 @@ module VagrantPlugins end o.on("--[no-]parallel", - "Enable or disable parallelism if provider supports it") do |p| + "Enable or disable parallelism if provider supports it (automatically enables force)") do |p| options[:parallel] = p end end @@ -29,13 +29,22 @@ module VagrantPlugins argv = parse_options(opts) return if !argv - @logger.debug("'Destroy' each target VM...") - - if options[:parallel] + if options[:parallel] && !options[:force] + @env.ui.warn(I18n.t("vagrant.commands.destroy.warning")) + sleep(5) options[:force] = true end + @logger.debug("'Destroy' each target VM...") + machines = [] + init_states = {} + declined = 0 + + # gather states to be checked after destroy + with_target_vms(argv, reverse: true) do |vm| + init_states[vm.name] = vm.state.id + end @env.batch(options[:parallel]) do |batch| with_target_vms(argv, reverse: true) do |vm| @@ -44,17 +53,20 @@ module VagrantPlugins end end - states = machines.map { |m| m.state.id } - if states.uniq.length == 1 && states.first == :not_created - # Nothing was declined - return 0 - elsif states.uniq.length == 1 && states.first != :not_created - # Everything was declined - return 1 - else - # Some was declined - return 2 + machines.each do |m| + if m.state.id == init_states[m.name] + declined += 1 + end end + + # Nothing was declined + return 0 if declined == 0 + + # Everything was declined, state was not changed + return 1 if declined == machines.length + + # Some was declined + return 2 end end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 98193171e..8136fb86f 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1697,6 +1697,9 @@ en: will_not_destroy: |- The VM '%{name}' will not be destroyed, since the confirmation was declined. + warning: |- + Destroying guests with `--parallel` automatically enables `--force`. + Press ctrl-c to cancel. init: success: |- A `Vagrantfile` has been placed in this directory. You are now