class KafoParsers::PuppetModuleParser
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
Public Class Methods
available?()
click to toggle source
# File lib/kafo_parsers/puppet_module_parser.rb, line 11 def self.available? require 'puppet' [2, 3].include?(Puppet::PUPPETVERSION.to_i) rescue LoadError => e raise KafoParsers::ParserNotAvailable.new(e) end
new(file)
click to toggle source
# File lib/kafo_parsers/puppet_module_parser.rb, line 35 def initialize(file) @file = file raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exists?(file) unless @@puppet_initialized require 'puppet' if Puppet::PUPPETVERSION.to_i >= 3 Puppet.initialize_settings else Puppet.parse_config end Encoding.default_external = Encoding::UTF_8 if defined?(Encoding) && Encoding.respond_to?(:default_external=) @@puppet_initialized = true end env = Puppet::Node::Environment.new parser = Puppet::Parser::Parser.new(env) parser.import(@file) # Find object corresponding to class defined in init.pp in list of hostclasses ast_types = parser.environment.known_resource_types.hostclasses.map(&:last) @object = ast_types.find { |ast_type| ast_type.file == file } # Find object in list of definitions if not found among hostclasses if @object.nil? ast_types = parser.environment.known_resource_types.definitions.map(&:last) @object = ast_types.find { |ast_type| ast_type.file == file } end parser end
parse(file)
click to toggle source
You can call this method to get all supported information from a given manifest
@param [ String ] manifest file path to parse @return [ Hash ] hash containing values, validations, documentation, types, groups and conditions
# File lib/kafo_parsers/puppet_module_parser.rb, line 22 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
Public Instance Methods
docs()
click to toggle source
returns data in following form {
:docs => { $param1 => 'documentation without types and conditions'} :types => { $param1 => 'boolean'}, :groups => { $param1 => ['Parameters', 'Advanced']}, :conditions => { $param1 => '$db_type == "mysql"'}, :object_type => 'hostclass' # or definition
}
# File lib/kafo_parsers/puppet_module_parser.rb, line 88 def docs data = { :docs => {}, :types => {}, :groups => {}, :conditions => {}, :object_type => '' } if @object.nil? raise KafoParsers::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 data[:object_type] = @object.type.to_s end data end
validations(param = nil)
click to toggle source
# File lib/kafo_parsers/puppet_module_parser.rb, line 75 def validations(param = nil) return [] if @object.code.nil? @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ } end
values()
click to toggle source
TODO - store parsed object type (Puppet::Parser::AST::Variable must be dumped later)
# File lib/kafo_parsers/puppet_module_parser.rb, line 68 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