2012-04-19 20:59:48 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module CommandDestroy
|
2012-06-26 23:18:02 +00:00
|
|
|
class Command < Vagrant.plugin("1", :command)
|
2010-08-25 06:05:40 +00:00
|
|
|
def execute
|
2012-02-08 06:58:01 +00:00
|
|
|
options = {}
|
|
|
|
|
2011-12-17 19:05:49 +00:00
|
|
|
opts = OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: vagrant destroy [vm-name]"
|
2012-02-08 06:58:01 +00:00
|
|
|
opts.separator ""
|
|
|
|
|
|
|
|
opts.on("-f", "--force", "Destroy without confirmation.") do |f|
|
|
|
|
options[:force] = f
|
|
|
|
end
|
2011-12-17 19:05:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the options
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
|
|
|
|
|
|
|
@logger.debug("'Destroy' each target VM...")
|
2012-03-11 22:32:17 +00:00
|
|
|
with_target_vms(argv, :reverse => true) do |vm|
|
2012-07-27 05:39:27 +00:00
|
|
|
vm.action(:destroy)
|
|
|
|
next
|
|
|
|
|
2010-08-25 06:05:40 +00:00
|
|
|
if vm.created?
|
2012-02-08 06:58:01 +00:00
|
|
|
# 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
|
|
|
|
# user confirms it.
|
|
|
|
do_destroy = false
|
|
|
|
|
|
|
|
if options[:force]
|
|
|
|
do_destroy = true
|
|
|
|
else
|
2012-03-08 06:35:40 +00:00
|
|
|
choice = nil
|
2012-07-12 01:47:41 +00:00
|
|
|
|
2012-03-08 06:35:40 +00:00
|
|
|
begin
|
|
|
|
choice = @env.ui.ask(I18n.t("vagrant.commands.destroy.confirmation",
|
|
|
|
:name => vm.name))
|
2012-07-12 01:32:51 +00:00
|
|
|
rescue Interrupt
|
|
|
|
# Ctrl-C was pressed (or SIGINT). We just exit immediately
|
|
|
|
# with a non-zero exit status.
|
|
|
|
return 1
|
2012-04-19 20:59:48 +00:00
|
|
|
rescue Vagrant::Errors::UIExpectsTTY
|
2012-03-08 06:35:40 +00:00
|
|
|
# We raise a more specific error but one which basically
|
|
|
|
# means the same thing.
|
2012-04-19 20:59:48 +00:00
|
|
|
raise Vagrant::Errors::DestroyRequiresForce
|
2012-03-08 06:35:40 +00:00
|
|
|
end
|
2012-07-12 01:47:41 +00:00
|
|
|
|
2012-02-08 06:58:01 +00:00
|
|
|
do_destroy = choice.upcase == "Y"
|
|
|
|
end
|
2012-02-08 06:54:30 +00:00
|
|
|
|
2012-02-08 06:58:01 +00:00
|
|
|
if do_destroy
|
2012-02-08 06:54:30 +00:00
|
|
|
@logger.info("Destroying: #{vm.name}")
|
|
|
|
vm.destroy
|
|
|
|
else
|
|
|
|
@logger.info("Not destroying #{vm.name} since confirmation was declined.")
|
|
|
|
@env.ui.success(I18n.t("vagrant.commands.destroy.will_not_destroy",
|
|
|
|
:name => vm.name), :prefix => false)
|
|
|
|
end
|
2010-08-25 06:05:40 +00:00
|
|
|
else
|
2011-12-17 19:05:49 +00:00
|
|
|
@logger.info("Not destroying #{vm.name}, since it isn't created.")
|
2011-12-27 02:14:09 +00:00
|
|
|
vm.ui.info I18n.t("vagrant.commands.common.vm_not_created")
|
2010-08-25 06:05:40 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-23 15:07:35 +00:00
|
|
|
|
|
|
|
# Success, exit status 0
|
|
|
|
0
|
|
|
|
end
|
2010-08-25 06:05:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|