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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 38

def initialize(file)
  @file = file = File.expand_path(file)
  raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exist?(file)

  command = ['strings', 'generate', '--format', 'json', file]
  @raw_json, stderr, status = self.class.run_puppet(command)

  unless status.success?
    raise KafoParsers::ParseError, "'#{command}' returned error:\n  #{@raw_json}\n  #{stderr}"
  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

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 29

def self.available?
  _stdout, _stderr, status = run_puppet(['help', 'strings'])
  if status.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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 14

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



60
61
62
63
64
65
66
67
68
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 60

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
    found_puppet_path.nil? ? 'puppet' : File.join(found_puppet_path, 'puppet')
  end
end

Instance Method Details

#data_typeObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 70

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

}



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 102

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? && !@parsed_hash['docstring']['text'].nil?
    # Lowest precedence: types given in the strings hash from class definition
    tag_params.each do |param|
      data[:types][param['name']] = param['types'][0] unless param['types'].nil?
    end

    # Next: types and other data from RDoc parser
    rdoc_parser = DocParser.new(@parsed_hash['docstring']['text']).parse
    data[:docs] = rdoc_parser.docs
    data[:groups] = rdoc_parser.groups
    data[:conditions] = rdoc_parser.conditions
    data[:types].merge! rdoc_parser.types

    # Highest precedence: data in YARD @param stored in the 'text' field
    tag_params.each do |param|
      param_name = param['name']
      unless param['text'].nil? || param['text'].empty?
        param_parser = ParamDocParser.new(param_name, param['text'].split($/))
        data[:docs][param_name] = param_parser.doc if param_parser.doc
        data[:groups][param_name] = param_parser.group if param_parser.group
        data[:conditions][param_name] = param_parser.condition if param_parser.condition
        data[:types][param_name] = param_parser.type if param_parser.type
      end
    end
  end
  data
end

#validations(param = nil) ⇒ Object

unsupported in puppet strings parser



91
92
93
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 91

def validations(param = nil)
  []
end

#valuesObject



82
83
84
85
86
87
88
# File 'lib/kafo_parsers/puppet_strings_module_parser.rb', line 82

def values
  Hash[
    # combine known parameters (from tags) with any defaults provided
    tag_params.select { |param| !param['types'].nil? }.map { |param| [ param['name'], nil ] } +
      @parsed_hash.fetch('defaults', {}).map { |name, value| [ name, value.nil? ? nil : sanitize(value) ] }
  ]
end