File: command.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (75 lines) | stat: -rw-r--r-- 2,078 bytes parent folder | download
1
2
3
4
5
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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

        @env.batch(options[:parallel]) do |batch|
          with_target_vms(argv, reverse: true) do |vm|
            # gather states to be checked after destroy
            init_states[vm.name] = vm.state.id
            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, and all states are `not_created`
        return 0 if declined == machines.length &&
                    declined == init_states.values.count(:not_created)

        # Everything was declined, state was not changed
        return 1 if declined == machines.length

        # Some was declined
        return 2
      end
    end
  end
end