Class: PuppetValidator

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

Defined Under Namespace

Classes: Validators

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ PuppetValidator

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

def initialize(app=nil)
  super(app)

  # 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 all installed Puppet versions in reverse semver order
  #settings.puppet_versions = settings.puppet_versions.sort_by { |v| Gem::Version.new(v) }.reverse
  settings.puppet_versions = Gem::Specification.all.select {|g| g.name == 'puppet' }.collect {|g| g.version.to_s }

  settings.logger.error "Please gem install one or more Puppet versions." if settings.puppet_versions.empty?

end

Class Method Details

.run_in_processObject

rspec defines a crapton of global information and doesn’t clean up well between runs. This means that there are global objects that leak and chew up memory. To counter that, we fork a process to run the spec test.

This also allows us to load different versions of Puppet so users can select the version they want to validate against.



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

def self.run_in_process
  raise "Please define a block to run in a new process." unless block_given?

  reader, writer = IO.pipe
  output = nil

  if fork
    writer.close
    output = reader.read
    reader.close
    Process.wait
  else
    # Attempt to drop privileges for safety.
    Process.euid = Etc.getpwnam('nobody').uid if Process.uid == 0
    
    begin
      reader.close
      writer.write(yield)
      writer.close
    rescue StandardError, LoadError => e
      settings.logger.error e.message
      settings.logger.debug e.backtrace.join "\n"
      writer.write e.message
    end

    # if we fire any at_exit hooks, Sinatra has a kitten
    exit!
  end

  output
end