2012-04-19 20:59:48 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module CommandDestroy
|
2012-11-07 05:05:14 +00:00
|
|
|
class Command < Vagrant.plugin("2", :command)
|
2013-11-23 22:00:42 +00:00
|
|
|
def self.synopsis
|
|
|
|
"stops and deletes all traces of the vagrant machine"
|
|
|
|
end
|
|
|
|
|
2010-08-25 06:05:40 +00:00
|
|
|
def execute
|
2012-02-08 06:58:01 +00:00
|
|
|
options = {}
|
2013-01-29 18:33:40 +00:00
|
|
|
options[:force] = false
|
2012-02-08 06:58:01 +00:00
|
|
|
|
2012-08-14 06:18:50 +00:00
|
|
|
opts = OptionParser.new do |o|
|
2016-05-29 05:06:51 +00:00
|
|
|
o.banner = "Usage: vagrant destroy [options] [name|id]"
|
2014-02-08 08:20:50 +00:00
|
|
|
o.separator ""
|
|
|
|
o.separator "Options:"
|
2012-08-14 06:18:50 +00:00
|
|
|
o.separator ""
|
2012-02-08 06:58:01 +00:00
|
|
|
|
2012-08-14 06:18:50 +00:00
|
|
|
o.on("-f", "--force", "Destroy without confirmation.") do |f|
|
2012-02-08 06:58:01 +00:00
|
|
|
options[:force] = f
|
|
|
|
end
|
2017-10-28 00:05:29 +00:00
|
|
|
|
|
|
|
o.on("--[no-]parallel",
|
2017-10-31 20:45:56 +00:00
|
|
|
"Enable or disable parallelism if provider supports it (automatically enables force)") do |p|
|
2017-10-28 00:05:29 +00:00
|
|
|
options[:parallel] = p
|
|
|
|
end
|
2011-12-17 19:05:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the options
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
|
|
|
|
2017-10-31 20:45:56 +00:00
|
|
|
if options[:parallel] && !options[:force]
|
|
|
|
@env.ui.warn(I18n.t("vagrant.commands.destroy.warning"))
|
|
|
|
sleep(5)
|
2017-10-28 00:05:29 +00:00
|
|
|
options[:force] = true
|
2010-08-25 06:05:40 +00:00
|
|
|
end
|
2012-03-23 15:07:35 +00:00
|
|
|
|
2017-10-31 20:45:56 +00:00
|
|
|
@logger.debug("'Destroy' each target VM...")
|
|
|
|
|
2017-10-28 00:05:29 +00:00
|
|
|
machines = []
|
2017-10-31 20:45:56 +00:00
|
|
|
init_states = {}
|
|
|
|
declined = 0
|
|
|
|
|
2017-10-28 00:05:29 +00:00
|
|
|
@env.batch(options[:parallel]) do |batch|
|
|
|
|
with_target_vms(argv, reverse: true) do |vm|
|
2017-10-31 21:58:29 +00:00
|
|
|
# gather states to be checked after destroy
|
|
|
|
init_states[vm.name] = vm.state.id
|
2017-10-28 00:05:29 +00:00
|
|
|
machines << vm
|
2017-10-31 21:58:29 +00:00
|
|
|
|
2017-10-28 00:05:29 +00:00
|
|
|
batch.action(vm, :destroy, force_confirm_destroy: options[:force])
|
|
|
|
end
|
|
|
|
end
|
2014-04-16 04:29:12 +00:00
|
|
|
|
2017-10-31 20:45:56 +00:00
|
|
|
machines.each do |m|
|
|
|
|
if m.state.id == init_states[m.name]
|
|
|
|
declined += 1
|
|
|
|
end
|
2017-10-28 00:05:29 +00:00
|
|
|
end
|
2017-10-31 20:45:56 +00:00
|
|
|
|
|
|
|
# Nothing was declined
|
|
|
|
return 0 if declined == 0
|
|
|
|
|
2017-12-07 19:02:43 +00:00
|
|
|
# Everything was declined, and all states are `not_created`
|
|
|
|
return 0 if declined == machines.length &&
|
|
|
|
declined == init_states.values.count(:not_created)
|
|
|
|
|
2017-10-31 20:45:56 +00:00
|
|
|
# Everything was declined, state was not changed
|
|
|
|
return 1 if declined == machines.length
|
|
|
|
|
|
|
|
# Some was declined
|
|
|
|
return 2
|
2012-07-28 02:29:40 +00:00
|
|
|
end
|
2010-08-25 06:05:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|