Brian Cain d8bffd201f 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.
2017-10-31 13:52:07 -07:00

74 lines
1.9 KiB
Ruby

module VagrantPlugins
module CommandDestroy
class Command < Vagrant.plugin("2", :command)
def self.synopsis
"stops and deletes all traces of the vagrant machine"
end
def execute
options = {}
options[:force] = false
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant destroy [options] [name|id]"
o.separator ""
o.separator "Options:"
o.separator ""
o.on("-f", "--force", "Destroy without confirmation.") do |f|
options[:force] = f
end
o.on("--[no-]parallel",
"Enable or disable parallelism if provider supports it (automatically enables force)") do |p|
options[:parallel] = p
end
end
# Parse the options
argv = parse_options(opts)
return if !argv
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|
machines << vm
batch.action(vm, :destroy, force_confirm_destroy: options[:force])
end
end
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
end