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.
This commit is contained in:
Brian Cain 2017-10-31 13:45:56 -07:00
parent 2d7858ff8a
commit d8bffd201f
2 changed files with 29 additions and 14 deletions

View File

@ -20,7 +20,7 @@ module VagrantPlugins
end end
o.on("--[no-]parallel", 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 options[:parallel] = p
end end
end end
@ -29,13 +29,22 @@ module VagrantPlugins
argv = parse_options(opts) argv = parse_options(opts)
return if !argv return if !argv
@logger.debug("'Destroy' each target VM...") if options[:parallel] && !options[:force]
@env.ui.warn(I18n.t("vagrant.commands.destroy.warning"))
if options[:parallel] sleep(5)
options[:force] = true options[:force] = true
end end
@logger.debug("'Destroy' each target VM...")
machines = [] 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| @env.batch(options[:parallel]) do |batch|
with_target_vms(argv, reverse: true) do |vm| with_target_vms(argv, reverse: true) do |vm|
@ -44,18 +53,21 @@ module VagrantPlugins
end end
end end
states = machines.map { |m| m.state.id } machines.each do |m|
if states.uniq.length == 1 && states.first == :not_created if m.state.id == init_states[m.name]
declined += 1
end
end
# Nothing was declined # Nothing was declined
return 0 return 0 if declined == 0
elsif states.uniq.length == 1 && states.first != :not_created
# Everything was declined # Everything was declined, state was not changed
return 1 return 1 if declined == machines.length
else
# Some was declined # Some was declined
return 2 return 2
end end
end end
end end
end
end end

View File

@ -1697,6 +1697,9 @@ en:
will_not_destroy: |- will_not_destroy: |-
The VM '%{name}' will not be destroyed, since the confirmation The VM '%{name}' will not be destroyed, since the confirmation
was declined. was declined.
warning: |-
Destroying guests with `--parallel` automatically enables `--force`.
Press ctrl-c to cancel.
init: init:
success: |- success: |-
A `Vagrantfile` has been placed in this directory. You are now A `Vagrantfile` has been placed in this directory. You are now