Class: PuppetValidator::Validators::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-validator/validators/syntax.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings, version = nil) ⇒ Syntax

Returns a new instance of Syntax.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppet-validator/validators/syntax.rb', line 3

def initialize(settings, version = nil)
  @logger = settings.logger

  # Set the version of Puppet to load
  if version
    @logger.warn "Loading Puppet version #{version}."
    gem('puppet', version)
  end
  # load in the constructor to avoid polluting the parent process
  require 'puppet'
  require 'puppet/parser'

  Puppet.initialize_settings rescue nil
  Puppet.settings[:app_management] = true if Puppet.settings.include? :app_management

  # 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
end

Instance Method Details

#render!Object



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/puppet-validator/validators/syntax.rb', line 49

def render!
  require 'graphviz'

  begin
    raise 'No Puppet environment found' if Puppet[:code].empty?

    # neuter functions that might run code on the master during compilation
    Puppet::Parser::Functions.newfunction(:generate, :type => :rvalue) { |args|
      true
    }
    Puppet::Parser::Functions.newfunction(:template, :type => :rvalue) { |args|
      args.first
    }
    Puppet::Parser::Functions.newfunction(:inline_template, :type => :rvalue) { |args|
      args.first
    }

    node    = Puppet::Node.indirection.find('validator')
    catalog = Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)

    # These calls are failing due to an internal method not being available in 2 & 3.x. Suspect
    # that it's related to the compiler not being set up fully?
    catalog.remove_resource(catalog.resource("Stage", :main)) rescue nil
    catalog.remove_resource(catalog.resource("Class", :settings)) rescue nil

    graph = catalog.to_ral.relationship_graph.to_dot
    svg   = GraphViz.parse_string(graph) do |graph|
      graph[:label] = 'Resource Relationships'

      graph.each_node do |name, node|
        next unless name.start_with? 'Whit'
        newname = name.dup
        newname.sub!('Admissible_class', 'Starting Class')
        newname.sub!('Completed_class', 'Finishing Class')
        node[:label] = newname[5..-2]
      end
    end.output(:svg => String)

  rescue => detail
    @logger.warn detail.message
    @logger.debug detail.backtrace.join "\n"
    return detail.message
  end

  svg
end

#validate(data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet-validator/validators/syntax.rb', line 26

def validate(data)
  begin
    Puppet[:code] = data

    if Puppet::Node::Environment.respond_to?(:create)
      validation_environment = Puppet::Node::Environment.create(:production, [])
      validation_environment.check_for_reparse
    else
      validation_environment = Puppet::Node::Environment.new(:production)
    end

    validation_environment.known_resource_types.clear

    {:status => true, :message => 'Syntax OK'}
  rescue => detail
    @logger.warn detail.message
    err = {:status => false, :message => detail.message}
    err[:line] = detail.line if detail.methods.include? :line
    err[:pos]  = detail.pos  if detail.methods.include? :pos
    err
  end
end