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 (74 lines) | stat: -rw-r--r-- 2,125 bytes parent folder | download | duplicates (6)
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
require 'optparse'

module VagrantPlugins
  module CommandProvider
    class Command < Vagrant.plugin("2", :command)
      def self.synopsis
        "show provider for this environment"
      end

      def execute
        options = {}
        options[:install] = false
        options[:usable] = false

        opts = OptionParser.new do |o|
          o.banner = "Usage: vagrant provider [options] [args]"
          o.separator ""
          o.separator "This command interacts with the provider for this environment."
          o.separator "With no arguments, it'll output the default provider for this"
          o.separator "environment."
          o.separator ""
          o.separator "Options:"
          o.separator ""

          o.on("--install", "Installs the provider if possible") do |f|
            options[:install] = f
          end

          o.on("--usable", "Checks if the named provider is usable") do |f|
            options[:usable] = f
          end
        end

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

        # Get the machine
        machine = nil
        with_target_vms(argv, single_target: true) do |m|
          machine = m
        end

        # Output some machine readable stuff
        @env.ui.machine("provider-name", machine.provider_name, target: machine.name.to_s)

        # Check if we're just doing a usability check
        if options[:usable]
          @env.ui.output(machine.provider_name.to_s)
          return 0 if machine.provider.class.usable?(false)
          return 1
        end

        # Check if we're requesting installation
        if options[:install]
          key = "provider_install_#{machine.provider_name}".to_sym
          if !@env.host.capability?(key)
            raise Vagrant::Errors::ProviderCantInstall,
              provider: machine.provider_name.to_s
          end

          @env.host.capability(key)
          return
        end

        # No subtask, just output the provider name
        @env.ui.output(machine.provider_name.to_s)

        # Success, exit status 0
        0
      end
    end
  end
end