Class: Vagrant::Command::Destroy

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/command/destroy.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Util::SafePuts

#safe_puts

Constructor Details

This class inherits a constructor from Vagrant::Command::Base

Instance Method Details

#executeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant/command/destroy.rb', line 6

def execute
 options = {}

 opts = OptionParser.new do |opts|
   opts.banner = "Usage: vagrant destroy [vm-name]"
   opts.separator ""

   opts.on("-f", "--force", "Destroy without confirmation.") do |f|
     options[:force] = f
   end
 end

 # Parse the options
 argv = parse_options(opts)
 return if !argv

 @logger.debug("'Destroy' each target VM...")
 with_target_vms(argv, :reverse => true) do |vm|
   if vm.created?
     # 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
       choice = nil
       begin
         choice = @env.ui.ask(I18n.t("vagrant.commands.destroy.confirmation",
                                     :name => vm.name))
       rescue Errors::UIExpectsTTY
         # We raise a more specific error but one which basically
         # means the same thing.
         raise Errors::DestroyRequiresForce
       end
       do_destroy = choice.upcase == "Y"
     end

     if do_destroy
       @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
   else
     @logger.info("Not destroying #{vm.name}, since it isn't created.")
     vm.ui.info I18n.t("vagrant.commands.common.vm_not_created")
   end
 end

 # Success, exit status 0
 0
end