Module: Utils::LitaPuppet::PuppetDB

Included in:
Lita::Handlers::Puppet
Defined in:
lib/utils/lita_puppet/puppetdb.rb

Overview

Utility methods for working with PuppetDB

Instance Method Summary collapse

Instance Method Details

#class_nodes(classname) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/utils/lita_puppet/puppetdb.rb', line 16

def class_nodes(classname)
  q = db_connect.request(
    'resources',
    [
      :and,
      [:'=', 'type', 'Class'],
      [:'=', 'title', classname.to_s]
    ]
  )

  q.data.map { |node| node['certname'] }
end

#db_connectObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/utils/lita_puppet/puppetdb.rb', line 5

def db_connect
  ::PuppetDB::Client.new({
                           server: config.puppetdb_url,
                           pem: {
                             'key' => config.puppetdb_key,
                             'cert'    => config.puppetdb_cert,
                             'ca_file' => config.puppetdb_ca_cert
                           }
                         }, config.puppetdb_api_vers)
end

#node_info(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/utils/lita_puppet/puppetdb.rb', line 42

def node_info(node)
  q = db_connect.request(
    'nodes',
    [:'=', 'certname', node]
  )
  begin
    raise 'invalid node' if q.data.empty?
    q.data.last.to_yaml
  rescue
    nil
  end
end

#node_roles_and_profiles(what, nodename) ⇒ Object

rubocop:disable AbcSize



56
57
58
59
60
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
# File 'lib/utils/lita_puppet/puppetdb.rb', line 56

def node_roles_and_profiles(what, nodename)
  # TODO: validate url and nodename
  ::PuppetDB::Client.new({
                           server: config.puppetdb_url,
                           pem: {
                             'key' => config.puppetdb_key,
                             'cert'    => config.puppetdb_cert,
                             'ca_file' => config.puppetdb_ca_cert
                           }
                         }, config.puppetdb_api_vers) # this is weird but required
  d = ::PuppetDB::Client.get("/catalogs/#{nodename}")
  return d['error'] if d['error']

  tags = []
  if d['resources'].is_a?(Array)
    d['resources'].each { |r| tags.concat(r['tags']) }
  else
    d['resources']['data'].each { |r| tags.concat(r['tags']) }
  end

  # return all the tags related to profile:: or role::
  case what
  when 'profiles'
    tags.sort.uniq.select { |t| t.match(/^profile::/) }
  when 'roles'
    tags.sort.uniq.select { |t| t.match(/^role::/) }
  when 'r&p', 'p&r', 'roles and profiles'
    tags.sort.uniq.select { |t| t.match(/^(profile|role)::/) }
  end
end

#query_fact(node, fact) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/utils/lita_puppet/puppetdb.rb', line 29

def query_fact(node, fact)
  q = db_connect.request(
    "facts/#{fact}",
    [:'=', 'certname', node]
  )
  begin
    raise 'invalid query' if q.data.empty?
    q.data.last['value']
  rescue
    nil
  end
end