Class: Kafo::PuppetModuleParser

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/puppet_module_parser.rb

Overview

Based on ideas from puppet-parse by Johan van den Dorpe we don’t build any tree structure since e.g. params from doc does not have to be defined in puppet DSL and vice versa, we just gather all info we can read from the whole manifest

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PuppetModuleParser

Returns a new instance of PuppetModuleParser.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kafo/puppet_module_parser.rb', line 24

def initialize(file)
  @file = file
  raise ModuleName, "File not found #{file}, check you answer file" unless File.exists?(file)
  parser                        = Puppet::Parser::Parser.new('production')
  values                        = Puppet.settings.instance_variable_get('@values')
  values[:production][:confdir] ||= '/' # just some stubbing
  parser.import(@file)

  # Find object corresponding to class defined in init.pp in list of hostclasses
  parser.environment.known_resource_types.hostclasses.each do |ast_objects|
    ast_type = ast_objects.last
    @object = ast_type if ast_type.file == file
  end
  # Find object in list of definitions
  parser.environment.known_resource_types.definitions.each do |ast_objects|
    ast_type = ast_objects.last
    @object = ast_type.last if ast_type.last.file == file
  end
end

Class Method Details

.parse(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kafo/puppet_module_parser.rb', line 11

def self.parse(file)
  content = new(file)
  docs    = content.docs

  data              = {
      :values      => content.values,
      :validations => content.validations
  }
  data[:parameters] = data[:values].keys
  data.merge!(docs)
  data
end

Instance Method Details

#docsObject

returns data in following form

:docs => { $param1 => 'documentation without types and conditions'
:types => { $param1 => 'boolean'},
:groups => { $param1 => ['Parameters', 'Advanced']},
:conditions => { $param1 => '$db_type == "mysql"'},

}



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kafo/puppet_module_parser.rb', line 63

def docs
  data = { :docs => {}, :types => {}, :groups => {}, :conditions => {} }
  if @object.nil?
    raise DocParseError, "no documentation found for manifest #{@file}, parsing error?"
  elsif !@object.doc.nil?
    parser            = DocParser.new(@object.doc).parse
    data[:docs]       = parser.docs
    data[:groups]     = parser.groups
    data[:types]      = parser.types
    data[:conditions] = parser.conditions
  end
  data
end

#validations(param = nil) ⇒ Object



52
53
54
# File 'lib/kafo/puppet_module_parser.rb', line 52

def validations(param = nil)
  @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ }
end

#valuesObject

TODO - store parsed object type (Puppet::Parser::AST::Variable must be dumped later)



45
46
47
48
49
50
# File 'lib/kafo/puppet_module_parser.rb', line 45

def values
  parameters = {}
  arguments  = @object.respond_to?(:arguments) ? @object.arguments : {}
  arguments.each { |k, v| parameters[k] = v.respond_to?(:value) ? v.value : nil }
  parameters
end