Module: PuppetX::EasyType::Validation::CheckImplementation

Defined in:
lib/puppet_x/easy_type/validation/check_implementation.rb

Overview

The implementation for the validatio check

Instance Method Summary collapse

Instance Method Details

#change_to_s(_current, _should) ⇒ Object



9
10
11
# File 'lib/puppet_x/easy_type/validation/check_implementation.rb', line 9

def change_to_s(_current, _should)
  'The check has executed and found issues.'
end

#expected(output) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/puppet_x/easy_type/validation/check_implementation.rb', line 96

def expected(output)
  return true if resource[:expected_output].nil?

  expected_outputs = Array(resource[:expected_output])
  expected_outputs.all? do | expect|
    if expect.is_a? Regexp
      if output.match?(expect)
        Puppet.debug "Regular expression '#{expect}' found in output."
        return true
      else
        Puppet.debug "Regular expression '#{expect}' NOT found in output."
        return false
      end
    else
      if output == expect
        Puppet.debug "String '#{expect}' found in output."
        return true
      else
        Puppet.debug "String '#{expect}' NOT found in output."
        return false
      end
    end
  end
end

#insync?(_to) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/puppet_x/easy_type/validation/check_implementation.rb', line 13

def insync?(_to)
  if resource.noop
    Puppet.debug "#{path}: Skipping check because resource is set to noop."
    return true
  end
  return true if resource[:refreshonly].to_s == 'true'

  if !provider.validate_run?
    message = "Executable #{provider.executable} not found or not in path. Skipping this check..."
    Puppet.warning "#{path}: #{message}"
    resource.class.register_result(resource.control, 'SKIPPED', message)
    return true
  end
  if resource.skip.to_s != 'false'
    message = resource.skip.to_s == 'true' ? "Skipping this check at users request." : resource.skip
    Puppet.debug "#{path}: #{message}"
    resource.class.register_result(resource.control, 'SKIPPED', message)
    return true
  end

  #
  # This is a bit of a hack!! 
  # When running this code through bolt, the fact code is not available anymore when running the puppet code. So
  # running the puppet fact returns an empry string, failing all validations that use this mechanism. The short work-arround
  # is interpreting the check and if it is a "puppet facts..." command, fetch the fact from the current catalog
  #
  if value =~ /^puppet facts (\w+)$/
    output = Facter.value($1).to_s
  else
    output = provider.execute_check
  end
  Puppet.debug output
  result = if resource.expected_value
    value_compare(output)
  else
    expected(output) && not_allowed(output)
  end
  message = result ? resource.title : resource.fail_message
  resource.class.register_result(resource.control, result ? 'PASSED' : "FAILED", message)
  resource.report_as.to_s.downcase == 'none' ? true : result
end

#not_allowed(output) ⇒ Object



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/puppet_x/easy_type/validation/check_implementation.rb', line 71

def not_allowed(output)
  return true if resource[:not_allowed_output].nil?

  not_allowed_outputs = Array(resource[:not_allowed_output])
  not_allowed_outputs.all? do | not_allowed|
    if not_allowed.is_a? Regexp
      if output.match?(not_allowed)
        Puppet.debug "Regular expression '#{not_allowed}' found in output."
        return false
      else
        Puppet.debug "Regular expression '#{not_allowed}' NOT found in output."
        return true
      end
    else
      if output == not_allowed
        Puppet.debug "String '#{not_allowed}' found in output."
        return true
      else
        Puppet.debug "String '#{not_allowed}' NOT found in output."
        return false
      end
    end
  end
end

#value_compare(output) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/puppet_x/easy_type/validation/check_implementation.rb', line 55

def value_compare(output)
  string_value = output.scan(resource.parse_value).flatten
  if string_value.size != 1
    Puppet.debug  "No valid value found after parsing output with #{resource.parse_value}"
    return false
  end
  actual_value = string_value.first.to_f
  Array(resource.expected_value).all?  do |comparison|
    final_comparison = "#{actual_value} #{comparison}"
    final_comparison = final_comparison.gsub('$value', string_value.first)
    value = eval(final_comparison)
    Puppet.debug "Executing compare '#{final_comparison}' return a '#{value}'"
    value
  end
end