Class: Bolt::Plugin::Puppetdb

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/plugin/puppetdb.rb

Defined Under Namespace

Classes: FactLookupError

Constant Summary collapse

TEMPLATE_OPTS =
%w[alias config facts features name uri vars].freeze
PLUGIN_OPTS =
%w[_plugin _cache query target_mapping].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, context:) ⇒ Puppetdb

Returns a new instance of Puppetdb.



19
20
21
22
23
# File 'lib/bolt/plugin/puppetdb.rb', line 19

def initialize(config:, context:)
  pdb_config = Bolt::PuppetDB::Config.load_config(config, context.boltdir)
  @puppetdb_client = Bolt::PuppetDB::Client.new(pdb_config)
  @logger = Bolt::Logger.logger(self)
end

Instance Attribute Details

#puppetdb_clientObject (readonly)

Returns the value of attribute puppetdb_client.



17
18
19
# File 'lib/bolt/plugin/puppetdb.rb', line 17

def puppetdb_client
  @puppetdb_client
end

Instance Method Details

#fact_path(raw_fact) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bolt/plugin/puppetdb.rb', line 43

def fact_path(raw_fact)
  fact_path = raw_fact.split(".")
  fact_path = fact_path.map do |segment|
    # Turn it into an integer if we can
    Integer(segment)
  rescue ArgumentError
    # Otherwise return the value
    segment
  end
  if fact_path[0] == 'facts'
    fact_path.drop(1)
  elsif fact_path == ['certname']
    fact_path
  else
    raise FactLookupError.new(raw_fact, "fact lookups must start with 'facts.'")
  end
end

#hook_descriptionsObject



33
34
35
36
37
# File 'lib/bolt/plugin/puppetdb.rb', line 33

def hook_descriptions
  {
    resolve_reference: 'Query PuppetDB for a group of targets.'
  }
end

#hooksObject



29
30
31
# File 'lib/bolt/plugin/puppetdb.rb', line 29

def hooks
  hook_descriptions.keys
end

#nameObject



25
26
27
# File 'lib/bolt/plugin/puppetdb.rb', line 25

def name
  'puppetdb'
end

#resolve_facts(config, certname, target_data) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bolt/plugin/puppetdb.rb', line 99

def resolve_facts(config, certname, target_data)
  Bolt::Util.walk_vals(config) do |value|
    case value
    when String
      if value == 'certname'
        certname
      else
        data = target_data&.detect { |d| d['path'] == fact_path(value) }
        warn_missing_fact(certname, value) if data.nil?
        # If there's no fact data this will be nil
        data&.fetch('value', nil)
      end
    when Array, Hash
      value
    else
      raise FactLookupError.new(value, "fact lookups must be a string")
    end
  end
end

#resolve_reference(opts) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bolt/plugin/puppetdb.rb', line 61

def resolve_reference(opts)
  targets = @puppetdb_client.query_certnames(opts['query'])
  facts = []

  template = opts.delete('target_mapping') || {}

  keys = Set.new(TEMPLATE_OPTS) & opts.keys
  unless keys.empty?
    raise Bolt::ValidationError, "PuppetDB plugin expects keys #{keys.to_a} to be set under 'target_mapping'"
  end

  keys = Set.new(opts.keys) - PLUGIN_OPTS
  unless keys.empty?
    raise Bolt::ValidationError, "Unknown keys in PuppetDB plugin: #{keys.to_a}"
  end

  Bolt::Util.walk_vals(template) do |value|
    # This is done in parts instead of in place so that we only need to
    # make one puppetDB query
    if value.is_a?(String)
      facts << fact_path(value)
    end
    value
  end

  facts.uniq!
  # Returns {'mycertname' => [{'path' => ['nested', 'fact'], 'value' => val'}], ... }
  fact_values = @puppetdb_client.fact_values(targets, facts)

  targets.map do |certname|
    target_data = fact_values[certname]
    target = resolve_facts(template, certname, target_data) || {}
    target['uri'] = certname unless target['uri'] || target['name']

    target
  end
end

#warn_missing_fact(certname, fact) ⇒ Object



39
40
41
# File 'lib/bolt/plugin/puppetdb.rb', line 39

def warn_missing_fact(certname, fact)
  Bolt::Logger.warn("puppetdb_missing_fact", "Could not find fact #{fact} for node #{certname}")
end