Module: Proteus::GlobalCommands::Validate

Includes:
Helpers
Included in:
App
Defined in:
lib/proteus/global_commands/validate.rb

Constant Summary

Constants included from Helpers

Helpers::DEFAULT, Helpers::ERROR

Class Method Summary collapse

Methods included from Helpers

#alert, #confirm, #current_user, #limit, #slack_notification, #slack_webhook, #syscall

Methods included from Helpers::PathHelpers

#config_dir, #config_path, #context_path, #context_temp_directory, #contexts_path, #environments_path, #module_config_path, #module_data_path, #module_hooks_path, #module_io_file, #module_path, #module_templates_path, #modules_path, #plan_file, #root_path, #state_file, #var_file

Class Method Details

.included(thor_class) ⇒ Object



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
# File 'lib/proteus/global_commands/validate.rb', line 10

def self.included(thor_class)
  thor_class.class_eval do
    desc "validate", "Renders templates for all contexts and environments"

    long_desc <<-LONGDESC
      Renders templates for all environments, reporting validation errors.
    LONGDESC
    option :selective, type: :boolean, default: false
    def validate
      # case 1: changes in root => run full validation
      # case 2: no changes in root => run contexts only
      status = `git --no-pager diff origin/master --stat --name-only`.split("\n")

      run_full_validation = if status.map { |l| !l.include?('/') }.any? || !options[:selective]
                              say "Found changes in the root of the repository or --selective is not set. Running full validation.", :green
                              true
                            else
                              say "Running selective validation.", :green
                              false
                            end

      selected_contexts = status.collect do |s|
        parts = s.split("/")
        parts.size > 1 ? parts[1] : nil
      end.reject { |s| s.nil? }.uniq

      self.class.contexts.each do |context|
        unless run_full_validation
          if !selected_contexts.include?(context.name)
            say "Skipping context #{context.name}.", :green
            next
          end
        end

        context.environments.each do |environment|
          module_manager = Proteus::Modules::Manager.new(context: context.name, environment: environment)
          module_manager.render_modules

          terraform = ENV.key?("TERRAFORM_BINARY") ? ENV["TERRAFORM_BINARY"] : "terraform"

          validate_command = <<~VALIDATE_COMMAND
            cd #{context_path(context.name)} \
            && #{terraform} init -backend=false \
            && #{terraform} get \
            && #{terraform} validate -var-file=#{var_file(context.name, environment)} -var 'aws_profile=needs_to_be_set'
          VALIDATE_COMMAND

          `#{validate_command.squeeze(' ')}`

          say "Validated (context: #{context.name}, environment: #{environment}) #{"\u2714".encode('utf-8')}", :green
          exit 1 if $?.exitstatus == 1
          module_manager.clean
        end
      end
    end

  end
end