Puppet Function: conftool

Defined in:
puppet/modules/wmflib/lib/puppet/parser/functions/conftool.rb
Function type:
Ruby 3.x API

Overview

conftool()Any

Returns:

  • (Any)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/conftool.rb', line 17

newfunction(:conftool, :type => :rvalue, :arity => -2) do |args|
  case args.length
  when 2
    tags, type = *args
  when 1
    tags = args[0]
    type = 'node'
  end

  # Raise an error if tags is empty or not an hash
  if !tags.is_a?(Hash) || tags.empty?
    raise Puppet::ParseError, "tags should be in hash format"
  end

  # get the data and return them parsed as json
  begin
    selector = tags.map { |k, v| "#{k}=#{v}" }.join ","
    result = []
    data = function_generate(
      [
        '/usr/bin/confctl',
        '--object-type', type,
        'select', selector,
        'get'
      ]
    ).chomp

    # No result returns the empty list
    if data.empty?
      return []
    end

    data.split("\n").each do |line|
      entry = JSON.load(line)
      tags = entry.delete 'tags'
      obj_name = entry.keys.pop
      result.push({'name' => obj_name, 'tags' => tags, 'value' => entry[obj_name]})
    end
    result
  rescue StandardError => e
    raise Puppet::ParseError, "Unable to read data from conftool. Wrapped error is #{e}: #{e.message}"
  end
end