Class: PDK::Validate::ControlRepo::EnvironmentConfValidator

Inherits:
InternalRubyValidator show all
Defined in:
lib/pdk/validate/control_repo/environment_conf_validator.rb

Constant Summary collapse

ALLOWED_SETTINGS =
['modulepath', 'manifest', 'config_version', 'environment_timeout'].freeze

Instance Attribute Summary

Attributes inherited from Validator

#context, #options, #prepared

Instance Method Summary collapse

Methods inherited from InternalRubyValidator

#before_validation, #invoke, #prepare_invoke!

Methods inherited from InvokableValidator

#allow_empty_targets?, #fnmatch?, #ignore_dotfiles?, #invoke_style, #parse_targets, #pattern_ignore, #prepare_invoke!, #process_invalid, #process_skipped, #spinner

Methods inherited from Validator

#initialize, #invoke, #prepare_invoke!, #spinner, #spinners_enabled?, #start_spinner, #stop_spinner

Constructor Details

This class inherits a constructor from PDK::Validate::Validator

Instance Method Details

#nameObject



9
10
11
# File 'lib/pdk/validate/control_repo/environment_conf_validator.rb', line 9

def name
  'environment-conf'
end

#patternObject



17
18
19
# File 'lib/pdk/validate/control_repo/environment_conf_validator.rb', line 17

def pattern
  ['environment.conf']
end

#spinner_textObject



21
22
23
# File 'lib/pdk/validate/control_repo/environment_conf_validator.rb', line 21

def spinner_text
  format('Checking Puppet Environment settings (%{patterns}).', patterns: pattern.join(' '))
end

#valid_in_context?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/pdk/validate/control_repo/environment_conf_validator.rb', line 13

def valid_in_context?
  context.is_a?(PDK::Context::ControlRepo)
end

#validate_target(report, target) ⇒ Object



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
# File 'lib/pdk/validate/control_repo/environment_conf_validator.rb', line 25

def validate_target(report, target)
  unless PDK::Util::Filesystem.readable?(target)
    report.add_event(
      file: target,
      source: name,
      state: :failure,
      severity: 'error',
      message: 'Could not be read.'
    )
    return 1
  end

  is_valid = true
  begin
    env_conf = PDK::ControlRepo.environment_conf_as_config(target)

    env_conf.resolve.each do |setting_name, setting_value|
      # Remove the 'environment.' setting_name prefix
      setting_name = setting_name.slice(12..-1)
      next if ALLOWED_SETTINGS.include?(setting_name)

      # A hash indicates that the ini file has a section in it.
      message = if setting_value.is_a?(Hash)
                  format("Invalid section '%{name}'", name: setting_name)
                else
                  format("Invalid setting '%{name}'", name: setting_name)
                end

      report.add_event(
        file: target,
        source: name,
        state: :failure,
        severity: 'error',
        message: message
      )
      is_valid = false
    end

    timeout = env_conf.fetch('environment_timeout', nil)
    unless timeout.nil? || timeout == '0' || timeout == 'unlimited'
      report.add_event(
        file: target,
        source: name,
        state: :failure,
        severity: 'error',
        message: format("environment_timeout is set to '%{timeout}' but should be 0, 'unlimited' or not set.", timeout: timeout)
      )
      is_valid = false
    end

    return 1 unless is_valid

    report.add_event(
      file: target,
      source: name,
      state: :passed,
      severity: 'ok'
    )
    0
  rescue StandardError => e
    report.add_event(
      file: target,
      source: name,
      state: :failure,
      severity: 'error',
      message: e.message
    )
    1
  end
end