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 (104 lines) | stat: -rw-r--r-- 3,144 bytes parent folder | download | duplicates (5)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'optparse'

module VagrantPlugins
  module CommandValidate
    class Command < Vagrant.plugin("2", :command)
      def self.synopsis
        "validates the Vagrantfile"
      end

      def execute
        options = {}

        opts = OptionParser.new do |o|
          o.banner = "Usage: vagrant validate [options]"
          o.separator ""
          o.separator "Validates a Vagrantfile config"
          o.separator ""
          o.separator "Options:"
          o.separator ""

          o.on("-p", "--ignore-provider", "Ignores provider config options") do |p|
            options[:ignore_provider] = p
          end
        end

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

        action_env = {}
        if options[:ignore_provider]
          action_env[:ignore_provider] = true
          tmp_data_dir = mockup_providers!
        end

        # Validate the configuration of all machines
        with_target_vms() do |machine|
          machine.action_raw(:config_validate, Vagrant::Action::Builtin::ConfigValidate, action_env)
        end

        @env.ui.info(I18n.t("vagrant.commands.validate.success"))

        # Success, exit status 0
        0
      ensure
        FileUtils.remove_entry tmp_data_dir if tmp_data_dir
      end

      protected

      # This method is required to bypass some of the provider checks that would
      # otherwise raise exceptions before Vagrant could load and validate a config.
      # It essentially ignores that there are no installed or usable prodivers so
      # that Vagrant can go along and validate the rest of the Vagrantfile and ignore
      # any provider blocks.
      #
      # return [String] tmp_data_dir - Temporary dir used to store guest metadata during validation
      def mockup_providers!
        require 'log4r'
        logger = Log4r::Logger.new("vagrant::validate")
        logger.debug("Overriding all registered provider classes for validate")

        # Without setting up a tmp Environment, Vagrant will completely
        # erase the local data dotfile and you can lose state after the
        # validate command completes.
        tmp_data_dir = Dir.mktmpdir("vagrant-validate-")
        @env = Vagrant::Environment.new(
          cwd: @env.cwd,
          home_path: @env.home_path,
          ui_class: @env.ui_class,
          vagrantfile_name: @env.vagrantfile_name,
          local_data_path: tmp_data_dir,
          data_dir: tmp_data_dir
        )

        Vagrant.plugin("2").manager.providers.each do |key, data|
          data[0].class_eval do
            def initialize(machine)
            end

            def machine_id_changed
            end

            def self.installed?
              true
            end

            def self.usable?(raise_error=false)
              true
            end

            def state
              state_id = Vagrant::MachineState::NOT_CREATED_ID
              short = :not_created
              long = :not_created
              Vagrant::MachineState.new(state_id, short, long)
            end
          end
        end
        tmp_data_dir
      end
    end
  end
end