class KafoParsers::PuppetStringsModuleParser
Public Class Methods
available?()
click to toggle source
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 25 def self.available? %x`#{puppet_bin} help strings` if $?.success? return true else raise KafoParsers::ParserNotAvailable.new("#{puppet_bin} does not have strings module installed") end end
new(file)
click to toggle source
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 34 def initialize(file) @file = file raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exists?(file) command = "#{self.class.puppet_bin} strings #{file} --emit-json-stdout" @raw_json = %x`#{command}` unless $?.success? raise KafoParsers::ParseError, "'#{command}' returned error\n#{@raw_json}" end begin @complete_hash = ::JSON.parse(@raw_json) rescue ::JSON::ParserError => e raise KafoParsers::ParseError, "'#{command}' returned invalid json output: #{e.message}\n#{@raw_json}" end self.data_type # we need to determine data_type before any further parsing self 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_strings_module_parser.rb, line 10 def self.parse(file) content = new(file) docs = content.docs # data_type must be called before other validations data = { :object_type => content.data_type, :values => content.values, :validations => content.validations } data[:parameters] = data[:values].keys data.merge!(docs) data end
puppet_bin()
click to toggle source
AIO and system default puppet bins are tested for existence, fallback to just `puppet` otherwise
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 55 def self.puppet_bin @puppet_bin ||= begin found_puppet_path = (::ENV['PATH'].split(File::PATH_SEPARATOR) + ['/opt/puppetlabs/bin']).find do |path| binary = File.join(path, 'puppet') binary if File.executable?(binary) end File.join(found_puppet_path, 'puppet') || 'puppet' end end
Public Instance Methods
data_type()
click to toggle source
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 65 def data_type @data_type ||= begin if (@parsed_hash = @complete_hash['puppet_classes'].find { |klass| klass['file'] == @file }) 'hostclass' elsif (@parsed_hash = @complete_hash['defined_types'].find { |klass| klass['file'] == @file }) 'definition' else raise KafoParsers::ParseError, "unable to find manifest data, syntax error in manifest #{@file}?" end end end
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"'},
}
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 93 def docs data = { :docs => {}, :types => {}, :groups => {}, :conditions => {} } if @parsed_hash.nil? raise KafoParsers::DocParseError, "no documentation found for manifest #{@file}, parsing error?" elsif !@parsed_hash['docstring'].nil? parser = DocParser.new(@parsed_hash['docstring']).parse data[:docs] = parser.docs data[:groups] = parser.groups data[:types] = parser.types data[:conditions] = parser.conditions end data end
validations(param = nil)
click to toggle source
unsupported in puppet strings parser
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 82 def validations(param = nil) [] end
values()
click to toggle source
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 77 def values Hash[@parsed_hash['parameters'].map { |name, value| [ name, sanitize(value) ] }] end
Private Instance Methods
sanitize(value)
click to toggle source
default values using puppet strings includes $ symbol, e.g. “$::foreman::params::ssl” to keep the same API we strip $ if it's present
values are reported as strings which is issue especially for :under strings are double quoted others must be typecast manually according to reported type
# File lib/kafo_parsers/puppet_strings_module_parser.rb, line 115 def sanitize(value) if (value.start_with?("'") && value.end_with?("'")) || (value.start_with?('"') && value.end_with?('"')) value = value[1..-2] end value = value[1..-1] if value.start_with?('$') value = :undef if value == 'undef' value end