ruby-puppetdb 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Modulefile +1 -1
- data/README.md +27 -0
- data/lib/hiera/backend/puppetdb_backend.rb +52 -0
- data/lib/puppetdb/lexer.l +8 -6
- data/lib/puppetdb/lexer.rb +5 -5
- data/lib/puppetdb/parser.rb +36 -36
- metadata +93 -86
- checksums.yaml +0 -7
data/Modulefile
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,7 @@ PuppetDB query tools
|
|
4
4
|
====================
|
5
5
|
|
6
6
|
This module implements command line tools and Puppet functions that can be used to query puppetdb.
|
7
|
+
There's also a hiera backend that can be used to return query results from puppetdb.
|
7
8
|
|
8
9
|
Query format
|
9
10
|
============
|
@@ -112,6 +113,32 @@ Example return value in JSON format:
|
|
112
113
|
}
|
113
114
|
}
|
114
115
|
|
116
|
+
Hiera backend
|
117
|
+
=============
|
118
|
+
|
119
|
+
The hiera backend can be used to return an array with results from a puppetdb query. It requires another hiera backend to be active at the same time, and that will be used to define the actual puppetdb query to be used. It does not matter which backend that is, there can even be several of them. To enable add the backend `puppetdb`to the backends list in `hiera.yaml`.
|
120
|
+
|
121
|
+
So instead of writing something like this in for example your `hiera-data/common.yaml`:
|
122
|
+
|
123
|
+
ntp::servers:
|
124
|
+
- 'ntp1.example.com'
|
125
|
+
- 'ntp2.example.com'
|
126
|
+
|
127
|
+
You can now instead write:
|
128
|
+
|
129
|
+
ntp::servers::_nodequery: 'Class[Ntp::Server]'
|
130
|
+
|
131
|
+
It will then find all nodes with the class ntp::server and return an array containing their certname. If you instead want to return the value of a fact, for example the `ipaddress`, the nodequery can be a tuple, like:
|
132
|
+
|
133
|
+
ntp::servers::_nodequery: ['Class[Ntp::Server]', 'ipaddress']
|
134
|
+
|
135
|
+
or a hash:
|
136
|
+
|
137
|
+
ntp::servers::_nodequery:
|
138
|
+
query: 'Class[Ntp::Server]'
|
139
|
+
fact: 'ipaddress'
|
140
|
+
|
141
|
+
When returning facts only nodes that actually have the fact are returned, even if more nodes would in fact match the query itself.
|
115
142
|
|
116
143
|
Deprecated PuppetDB query functions
|
117
144
|
===================================
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class Hiera
|
2
|
+
module Backend
|
3
|
+
class Puppetdb_backend
|
4
|
+
def initialize
|
5
|
+
require 'puppetdb/connection'
|
6
|
+
begin
|
7
|
+
require 'puppet'
|
8
|
+
# This is needed when we run from hiera cli
|
9
|
+
Puppet.initialize_settings unless Puppet[:confdir]
|
10
|
+
require 'puppet/util/puppetdb'
|
11
|
+
server = Puppet::Util::Puppetdb.server
|
12
|
+
port = Puppet::Util::Puppetdb.port
|
13
|
+
rescue
|
14
|
+
server = 'puppetdb'
|
15
|
+
port = 443
|
16
|
+
end
|
17
|
+
|
18
|
+
Hiera.debug("Hiera PuppetDB backend starting")
|
19
|
+
|
20
|
+
@puppetdb = PuppetDB::Connection.new(server, port)
|
21
|
+
end
|
22
|
+
|
23
|
+
def lookup(key, scope, order_override, resolution_type)
|
24
|
+
return nil if key.end_with? "::_nodequery"
|
25
|
+
|
26
|
+
Hiera.debug("Looking up #{key} in PuppetDB backend")
|
27
|
+
|
28
|
+
if nodequery = Backend.lookup(key + "::_nodequery", nil, scope, order_override, :priority)
|
29
|
+
Hiera.debug("Found nodequery #{nodequery.inspect}")
|
30
|
+
|
31
|
+
# Support specifying the query in a few different ways
|
32
|
+
if nodequery.is_a? Hash
|
33
|
+
query = nodequery['query']
|
34
|
+
fact = nodequery['fact']
|
35
|
+
elsif nodequery.is_a? Array
|
36
|
+
query, fact = *nodequery
|
37
|
+
else
|
38
|
+
query = nodequery.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
if fact then
|
42
|
+
query = @puppetdb.parse_query query, :facts if query.is_a? String
|
43
|
+
@puppetdb.facts([fact], query).each_value.collect { |facts| facts[fact] }
|
44
|
+
else
|
45
|
+
query = @puppetdb.parse_query query, :nodes if query.is_a? String
|
46
|
+
@puppetdb.query(:nodes, query).collect { |n| n['name'] }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/puppetdb/lexer.l
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require 'json'
|
4
4
|
|
5
5
|
class PuppetDB::Lexer
|
6
|
+
macro
|
7
|
+
STR [\w_:]
|
6
8
|
rule
|
7
9
|
\s # whitespace no action
|
8
10
|
\( { [:LPAREN, text] }
|
@@ -16,14 +18,14 @@ rule
|
|
16
18
|
~ { [:MATCH, text] }
|
17
19
|
< { [:LESSTHAN, text] }
|
18
20
|
> { [:GREATERTHAN, text] }
|
19
|
-
not
|
20
|
-
and
|
21
|
-
or
|
22
|
-
true
|
23
|
-
false
|
21
|
+
not(?!{STR}) { [:NOT, text] }
|
22
|
+
and(?!{STR}) { [:AND, text] }
|
23
|
+
or(?!{STR}) { [:OR, text] }
|
24
|
+
true(?!{STR}) { [:BOOLEAN, true]}
|
25
|
+
false(?!{STR}) { [:BOOLEAN, false]}
|
24
26
|
-?\d+\.\d+ { [:NUMBER, text.to_f] }
|
25
27
|
-?\d+ { [:NUMBER, text.to_i] }
|
26
28
|
\"(\\.|[^\\"])*\" { [:STRING, JSON.load(text)] }
|
27
|
-
|
29
|
+
{STR}+ { [:STRING, text] }
|
28
30
|
@@ { [:EXPORTED, text] }
|
29
31
|
end
|
data/lib/puppetdb/lexer.rb
CHANGED
@@ -93,19 +93,19 @@ class Lexer < Racc::Parser
|
|
93
93
|
when (text = ss.scan(/>/))
|
94
94
|
@rex_tokens.push action { [:GREATERTHAN, text] }
|
95
95
|
|
96
|
-
when (text = ss.scan(/not/))
|
96
|
+
when (text = ss.scan(/not(?![\w_:])/))
|
97
97
|
@rex_tokens.push action { [:NOT, text] }
|
98
98
|
|
99
|
-
when (text = ss.scan(/and/))
|
99
|
+
when (text = ss.scan(/and(?![\w_:])/))
|
100
100
|
@rex_tokens.push action { [:AND, text] }
|
101
101
|
|
102
|
-
when (text = ss.scan(/or/))
|
102
|
+
when (text = ss.scan(/or(?![\w_:])/))
|
103
103
|
@rex_tokens.push action { [:OR, text] }
|
104
104
|
|
105
|
-
when (text = ss.scan(/true/))
|
105
|
+
when (text = ss.scan(/true(?![\w_:])/))
|
106
106
|
@rex_tokens.push action { [:BOOLEAN, true]}
|
107
107
|
|
108
|
-
when (text = ss.scan(/false/))
|
108
|
+
when (text = ss.scan(/false(?![\w_:])/))
|
109
109
|
@rex_tokens.push action { [:BOOLEAN, false]}
|
110
110
|
|
111
111
|
when (text = ss.scan(/-?\d+\.\d+/))
|
data/lib/puppetdb/parser.rb
CHANGED
@@ -14,58 +14,58 @@ module PuppetDB
|
|
14
14
|
##### State transition tables begin ###
|
15
15
|
|
16
16
|
racc_action_table = [
|
17
|
-
|
18
|
-
-29,
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
21,
|
24
|
-
|
17
|
+
4, 4, 5, 5, 8, 8, -29, -29, -29, -29,
|
18
|
+
-29, 3, 3, 36, 47, 9, 9, 12, 12, 4,
|
19
|
+
4, 5, 5, 8, 8, 16, 17, 5, 36, 8,
|
20
|
+
3, 3, 16, 17, 9, 9, 12, 12, 4, 4,
|
21
|
+
5, 5, 8, 8, 5, 32, 8, 41, 8, 3,
|
22
|
+
3, 49, 48, 9, 9, 12, 12, 28, 16, 17,
|
23
|
+
20, 21, 22, 18, 19, 36, 41, 42, 36, 41,
|
24
|
+
42, 25, 8, 51, 16 ]
|
25
25
|
|
26
26
|
racc_action_check = [
|
27
|
-
0, 17, 0, 17, 0, 17,
|
28
|
-
|
29
|
-
3,
|
30
|
-
|
31
|
-
4,
|
32
|
-
|
33
|
-
|
34
|
-
|
27
|
+
0, 17, 0, 17, 0, 17, 9, 9, 9, 9,
|
28
|
+
9, 0, 17, 19, 24, 0, 17, 0, 17, 8,
|
29
|
+
3, 8, 3, 8, 3, 24, 24, 11, 18, 11,
|
30
|
+
8, 3, 1, 1, 8, 3, 8, 3, 4, 16,
|
31
|
+
4, 16, 4, 16, 10, 15, 10, 22, 13, 4,
|
32
|
+
16, 26, 25, 4, 16, 4, 16, 10, 26, 26,
|
33
|
+
2, 2, 2, 2, 2, 21, 21, 21, 20, 20,
|
34
|
+
20, 5, 29, 32, 34 ]
|
35
35
|
|
36
36
|
racc_action_pointer = [
|
37
|
-
-2,
|
38
|
-
|
39
|
-
|
40
|
-
nil,
|
37
|
+
-2, 18, 52, 18, 36, 54, nil, nil, 17, -2,
|
38
|
+
40, 23, nil, 42, nil, 45, 37, -1, 12, -3,
|
39
|
+
52, 49, 30, nil, 11, 47, 44, nil, nil, 66,
|
40
|
+
nil, nil, 73, nil, 60, nil, nil, nil, nil, nil,
|
41
41
|
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
|
42
42
|
nil, nil ]
|
43
43
|
|
44
44
|
racc_action_default = [
|
45
|
-
-32,
|
46
|
-
-
|
47
|
-
|
48
|
-
-
|
49
|
-
|
50
|
-
-
|
45
|
+
-32, -1, -32, -32, -32, -32, -15, -16, -32, -26,
|
46
|
+
-32, -18, -25, -19, -20, -32, -32, -32, -32, -32,
|
47
|
+
-32, -32, -32, -3, -32, -32, -32, -17, -26, -21,
|
48
|
+
-23, -22, -32, -4, -5, -10, -30, -9, -6, -7,
|
49
|
+
-8, -29, -31, -14, -13, -12, -11, -2, -27, -28,
|
50
|
+
-24, 52 ]
|
51
51
|
|
52
52
|
racc_goto_table = [
|
53
|
-
|
54
|
-
|
55
|
-
34
|
53
|
+
30, 27, 31, 1, 39, 44, 23, 24, 38, 43,
|
54
|
+
46, 26, 35, 37, 40, 45, 29, 15, 50, 33,
|
55
|
+
34 ]
|
56
56
|
|
57
57
|
racc_goto_check = [
|
58
|
-
11, 11,
|
59
|
-
|
60
|
-
2
|
58
|
+
11, 7, 11, 2, 4, 4, 2, 2, 3, 3,
|
59
|
+
3, 2, 5, 5, 5, 5, 10, 1, 11, 2,
|
60
|
+
2 ]
|
61
61
|
|
62
62
|
racc_goto_pointer = [
|
63
|
-
nil,
|
64
|
-
|
63
|
+
nil, 17, 3, -12, -16, -6, nil, -9, nil, nil,
|
64
|
+
5, -11 ]
|
65
65
|
|
66
66
|
racc_goto_default = [
|
67
|
-
nil, nil, nil,
|
68
|
-
|
67
|
+
nil, nil, nil, 2, nil, nil, 6, 7, 10, 11,
|
68
|
+
13, 14 ]
|
69
69
|
|
70
70
|
racc_reduce_table = [
|
71
71
|
0, 0, :racc_error,
|
metadata
CHANGED
@@ -1,95 +1,95 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-puppetdb
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 1.1.0
|
5
10
|
platform: ruby
|
6
|
-
authors:
|
11
|
+
authors:
|
7
12
|
- Dan Bode
|
8
|
-
- Erik
|
13
|
+
- "Erik Dal\xC3\xA9n"
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2013-05-27 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
15
22
|
name: json
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
requirements:
|
18
|
-
- - '>='
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '0'
|
21
|
-
type: :runtime
|
22
23
|
prerelease: false
|
23
|
-
|
24
|
-
requirements:
|
25
|
-
- -
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
|
28
|
-
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
29
34
|
name: rspec
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
31
|
-
requirements:
|
32
|
-
- - '='
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: '2.13'
|
35
|
-
type: :development
|
36
35
|
prerelease: false
|
37
|
-
|
38
|
-
requirements:
|
39
|
-
- -
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
|
42
|
-
-
|
43
|
-
|
44
|
-
|
45
|
-
requirements:
|
46
|
-
- - '>='
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version: '0'
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 2
|
42
|
+
- 13
|
43
|
+
version: "2.13"
|
49
44
|
type: :development
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
50
48
|
prerelease: false
|
51
|
-
|
52
|
-
requirements:
|
53
|
-
- -
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
|
56
|
-
-
|
57
|
-
|
58
|
-
requirement: !ruby/object:Gem::Requirement
|
59
|
-
requirements:
|
60
|
-
- - '>='
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
version: '0'
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
63
56
|
type: :development
|
57
|
+
version_requirements: *id003
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: puppetlabs_spec_helper
|
64
60
|
prerelease: false
|
65
|
-
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
|
70
|
-
-
|
71
|
-
|
72
|
-
requirement: !ruby/object:Gem::Requirement
|
73
|
-
requirements:
|
74
|
-
- - '>='
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
version: '0'
|
61
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
77
68
|
type: :development
|
69
|
+
version_requirements: *id004
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: puppet
|
78
72
|
prerelease: false
|
79
|
-
|
80
|
-
requirements:
|
81
|
-
- -
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
|
73
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id005
|
84
82
|
description: A higher level query language for PuppetDB.
|
85
|
-
email:
|
83
|
+
email:
|
86
84
|
- dan@puppetlabs.com
|
87
85
|
- erik.gustav.dalen@gmail.com
|
88
|
-
executables:
|
86
|
+
executables:
|
89
87
|
- find-nodes
|
90
88
|
extensions: []
|
89
|
+
|
91
90
|
extra_rdoc_files: []
|
92
|
-
|
91
|
+
|
92
|
+
files:
|
93
93
|
- .gitignore
|
94
94
|
- .travis.yml
|
95
95
|
- COPYING
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- examples/nova_functions.pp
|
102
102
|
- examples/query_node_examples.pp
|
103
103
|
- examples/query_resource_examples.pp
|
104
|
+
- lib/hiera/backend/puppetdb_backend.rb
|
104
105
|
- lib/puppet/application/query.rb
|
105
106
|
- lib/puppet/face/query.rb
|
106
107
|
- lib/puppet/parser/functions/pdbfactquery.rb
|
@@ -131,31 +132,37 @@ files:
|
|
131
132
|
- spec/unit/puppet/parser/functions/pdbstatusquery_spec.rb
|
132
133
|
- spec/unit/puppet/parser/functions/query_facts_spec.rb
|
133
134
|
- spec/unit/puppet/parser/functions/query_nodes_spec.rb
|
135
|
+
has_rdoc: true
|
134
136
|
homepage: https://github.com/dalen/puppet-puppetdbquery
|
135
|
-
licenses:
|
137
|
+
licenses:
|
136
138
|
- Apache v2
|
137
|
-
metadata: {}
|
138
139
|
post_install_message:
|
139
140
|
rdoc_options: []
|
140
|
-
|
141
|
+
|
142
|
+
require_paths:
|
141
143
|
- lib
|
142
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
-
requirements:
|
144
|
-
- -
|
145
|
-
- !ruby/object:Gem::Version
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
144
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
segments:
|
149
|
+
- 0
|
150
|
+
version: "0"
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
segments:
|
156
|
+
- 0
|
157
|
+
version: "0"
|
152
158
|
requirements: []
|
159
|
+
|
153
160
|
rubyforge_project:
|
154
|
-
rubygems_version:
|
161
|
+
rubygems_version: 1.3.6
|
155
162
|
signing_key:
|
156
|
-
specification_version:
|
163
|
+
specification_version: 3
|
157
164
|
summary: Query functions for PuppetDB
|
158
|
-
test_files:
|
165
|
+
test_files:
|
159
166
|
- examples/nova_functions.pp
|
160
167
|
- examples/query_node_examples.pp
|
161
168
|
- examples/query_resource_examples.pp
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 81a535df46240ab33eb4c43ca94752259bd4cf8b
|
4
|
-
data.tar.gz: 4d548e94d084b68f492a171b41ac61736d46bdc6
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: f4062916d1f84f812d5c5dfc1d605707b42bbe221ee0ebd3f24c61862525c16a229267fc82b94ceb8a2dba788c3bac614b825402e4ecc24ee56631a3f6a4d493
|
7
|
-
data.tar.gz: 741ca1846fdebc9816ee6a06fa702dac1eabfa03f7c82276a21ddc897680b07bd305684d5d698e943d8f6947fb7727faa569b1fa7b6adaa9b35e91d50fa3ce20
|