Class: PuppetValidator

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/puppet-validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ PuppetValidator



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
# File 'lib/puppet-validator.rb', line 34

def initialize(app=nil)
  super(app)

  Puppet.initialize_settings rescue nil
  Puppet.settings[:app_management] = true if Gem::Version.new(Puppet.version) >= Gem::Version.new('4.3.2')

  # set up the base environment
  Puppet.push_context(Puppet.base_context(Puppet.settings), 'Setup for Puppet Validator') rescue nil

  # disable as much disk access as possible
  Puppet::Node::Facts.indirection.terminus_class = :memory
  Puppet::Node.indirection.cache_class = nil

  # make sure that all the settings we expect are defined.
  [:disabled_lint_checks, :puppet_versions, :csrf].each do |name|
    next if settings.respond_to? name

    settings.define_singleton_method(name) { Array.new }

    settings.define_singleton_method("#{name}=") do |arg|
      settings.define_singleton_method(name) { arg }
    end
  end

  # can pass in an array, a filename, or a list of checks
  if settings.disabled_lint_checks.class == String
    path = File.expand_path(settings.disabled_lint_checks)
    if File.file? path
      data = File.readlines(path).map {|line| line.chomp }
      data.reject! {|line| line.empty? or line.start_with? '#' }

      settings.disabled_lint_checks = data
    else
      settings.disabled_lint_checks = settings.disabled_lint_checks.split(',')
    end
  end

  # put our supported versions in reverse semver order
  settings.puppet_versions = settings.puppet_versions.sort_by { |v| Gem::Version.new(v) }.reverse

end