Class: KafoParsers::PuppetStringsModuleParser

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo_parsers/puppet_strings_module_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PuppetStringsModuleParser

Returns a new instance of PuppetStringsModuleParser.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kafo_parsers/puppet_strings_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)

  command = "#{self.class.puppet_bin} strings #{file} --emit-json-stdout"
  @raw_json = `#{command}`
  unless $?.success?
    raise KafoParsers::ParseError, "'#{command}' returned error\n#{@raw_json}"
  end

  Encoding.default_external = Encoding::UTF_8 if defined?(Encoding)
  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

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 26

def self.available?
  `#{puppet_bin} help strings 2>&1`
  if $?.success?
    return true
  else
    raise KafoParsers::ParserNotAvailable.new("#{puppet_bin} does not have strings module installed")
  end
end

.parse(file) ⇒ Hash

You can call this method to get all supported information from a given manifest

Parameters:

  • manifest (String)

    file path to parse

Returns:

  • (Hash)

    hash containing values, validations, documentation, types, groups and conditions



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 11

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_binObject

AIO and system default puppet bins are tested for existence, fallback to just ‘puppet` otherwise



57
58
59
60
61
62
63
64
65
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 57

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

Instance Method Details

#data_typeObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 67

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

#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"'},

}



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 95

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]       = merge_doc_types(parser.types)
    data[:conditions]  = parser.conditions
  end
  data
end

#validations(param = nil) ⇒ Object

unsupported in puppet strings parser



84
85
86
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 84

def validations(param = nil)
  []
end

#valuesObject



79
80
81
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 79

def values
  Hash[@parsed_hash['parameters'].map { |name, value| [ name, value.nil? ? nil : sanitize(value) ] }]
end