ruby-puppetdb 1.4.0 → 1.5.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.
- checksums.yaml +4 -4
- data/lib/puppet/parser/functions/query_resources.rb +38 -0
- data/lib/puppetdb.rb +1 -1
- data/lib/puppetdb/astnode.rb +10 -3
- data/lib/puppetdb/connection.rb +27 -1
- data/lib/puppetdb/{grammar.y → grammar.racc} +0 -0
- data/lib/puppetdb/lexer.rb +83 -80
- data/lib/puppetdb/{lexer.l → lexer.rex} +1 -0
- metadata +24 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9edc345d8dbd3b6b42c2739f3e834da0e3e90497
|
4
|
+
data.tar.gz: a525eb44570248b3c24b580d8219bf56eb2b9bbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2d4eafc8d0a2a5337c4bb48b42652b7a26cdfe4031a991b7c4c6cf2dc3805cd8e12047955ad5454bc6b4354d7497f7e924f6f8c602ee98760eab541a73140bc
|
7
|
+
data.tar.gz: 20a5bbb90a78edd94c4bc9a213504a539e0303b031523a2baf41746e6c07f747331bc294bd7cbf9cc7fabbb9864a9287e1b68c4f9b2378cfb6e057ec05db805c
|
@@ -0,0 +1,38 @@
|
|
1
|
+
Puppet::Parser::Functions.newfunction(:query_resources, :type => :rvalue, :arity => 2, :doc => <<-EOT
|
2
|
+
|
3
|
+
Accepts two arguments: a query used to discover nodes, and a
|
4
|
+
resource query for the resources that should be returned from
|
5
|
+
those hosts.
|
6
|
+
|
7
|
+
The result is a hash that maps the name of the nodes to a list of
|
8
|
+
resource entries. This is a list because there's no single
|
9
|
+
reliable key for resource operations that's of any use to the end user.
|
10
|
+
|
11
|
+
Examples:
|
12
|
+
|
13
|
+
Returns the parameters and such for the ntp class for all CentOS nodes:
|
14
|
+
|
15
|
+
query_resources('operatingsystem=CentOS', 'Class["ntp"]')
|
16
|
+
|
17
|
+
Returns information on the apache user on all nodes that have apache installed on port 443:
|
18
|
+
|
19
|
+
query_resources('Class["apache"]{ port = 443 }', 'User["apache"]')
|
20
|
+
|
21
|
+
Returns the parameters and such for the apache class for all nodes:
|
22
|
+
|
23
|
+
query_resources(false, 'Class["apache"]')
|
24
|
+
|
25
|
+
EOT
|
26
|
+
) do |args|
|
27
|
+
nodequery, resquery = args
|
28
|
+
|
29
|
+
require 'puppet/util/puppetdb'
|
30
|
+
# This is also needed if the puppetdb library isn't pluginsynced to the master
|
31
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..'))
|
32
|
+
require 'puppetdb/connection'
|
33
|
+
|
34
|
+
puppetdb = PuppetDB::Connection.new(Puppet::Util::Puppetdb.server, Puppet::Util::Puppetdb.port)
|
35
|
+
nodequery = puppetdb.parse_query nodequery, :facts if nodequery and nodequery.is_a? String and ! nodequery.empty?
|
36
|
+
resquery = puppetdb.parse_query resquery, :none if resquery and resquery.is_a? String and ! resquery.empty?
|
37
|
+
return puppetdb.resources(nodequery, resquery)
|
38
|
+
end
|
data/lib/puppetdb.rb
CHANGED
data/lib/puppetdb/astnode.rb
CHANGED
@@ -15,14 +15,21 @@ class PuppetDB::ASTNode
|
|
15
15
|
|
16
16
|
# Generate the the query code for a subquery
|
17
17
|
#
|
18
|
+
# As a special case, the from_mode of :none will not wrap the
|
19
|
+
# subquery at all, returning it as is.
|
20
|
+
#
|
18
21
|
# @param from_mode [Symbol] the mode you want to subquery from
|
19
22
|
# @param to_mode [Symbol] the mode you want to subquery to
|
20
23
|
# @param query the query inside the subquery
|
21
24
|
# @return [Array] the resulting subquery
|
22
25
|
def subquery(from_mode, to_mode, query)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
+
if from_mode == :none
|
27
|
+
return query
|
28
|
+
else
|
29
|
+
return ['in', (from_mode == :nodes) ? 'name' : 'certname',
|
30
|
+
['extract', (to_mode == :nodes) ? 'name' : 'certname',
|
31
|
+
["select-#{to_mode.to_s}", query]]]
|
32
|
+
end
|
26
33
|
end
|
27
34
|
|
28
35
|
# Go through the AST and optimize boolean expressions into triplets etc
|
data/lib/puppetdb/connection.rb
CHANGED
@@ -46,12 +46,38 @@ class PuppetDB::Connection
|
|
46
46
|
facts
|
47
47
|
end
|
48
48
|
|
49
|
+
# Get the listed resources for all nodes matching query
|
50
|
+
# return it as a hash of hashes
|
51
|
+
#
|
52
|
+
# @param resquery [Array] a resources query for what resources to fetch
|
53
|
+
# @param nodequery [Array] the query to find the nodes to fetch resources for, optionally empty
|
54
|
+
# @return [Hash] a hash of hashes with resources for each node mathing query
|
55
|
+
def resources(nodequery, resquery, http=nil)
|
56
|
+
if resquery and ! resquery.empty?
|
57
|
+
if nodequery and ! nodequery.empty?
|
58
|
+
q = ['and', resquery, nodequery]
|
59
|
+
else
|
60
|
+
q = resquery
|
61
|
+
end
|
62
|
+
else
|
63
|
+
raise RuntimeError, "PuppetDB resources query error: at least one argument must be non empty; arguments were: nodequery: #{nodequery.inspect} and requery: #{resquery.inspect}"
|
64
|
+
end
|
65
|
+
resources = {}
|
66
|
+
query(:resources, q, http).each do |resource|
|
67
|
+
unless resources.has_key? resource['certname']
|
68
|
+
resources[resource['certname']] = []
|
69
|
+
end
|
70
|
+
resources[resource['certname']] << resource
|
71
|
+
end
|
72
|
+
return resources
|
73
|
+
end
|
74
|
+
|
49
75
|
# Execute a PuppetDB query
|
50
76
|
#
|
51
77
|
# @param endpoint [Symbol] :resources, :facts or :nodes
|
52
78
|
# @param query [Array] query to execute
|
53
79
|
# @return [Array] the results of the query
|
54
|
-
def query(endpoint, query=nil, http=nil, version=:
|
80
|
+
def query(endpoint, query=nil, http=nil, version=:v3)
|
55
81
|
require 'json'
|
56
82
|
|
57
83
|
unless http then
|
File without changes
|
data/lib/puppetdb/lexer.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#--
|
2
2
|
# DO NOT MODIFY!!!!
|
3
|
-
# This file is automatically generated by rex 1.0.
|
3
|
+
# This file is automatically generated by rex 1.0.5
|
4
4
|
# from lexical definition file "lib/puppetdb/lexer.l".
|
5
5
|
#++
|
6
6
|
|
@@ -9,133 +9,136 @@ require 'racc/parser'
|
|
9
9
|
|
10
10
|
require 'yaml'
|
11
11
|
|
12
|
-
|
13
|
-
class Lexer < Racc::Parser
|
12
|
+
class PuppetDB::Lexer < Racc::Parser
|
14
13
|
require 'strscan'
|
15
14
|
|
16
15
|
class ScanError < StandardError ; end
|
17
16
|
|
18
|
-
attr_reader
|
19
|
-
attr_reader
|
17
|
+
attr_reader :lineno
|
18
|
+
attr_reader :filename
|
19
|
+
attr_accessor :state
|
20
20
|
|
21
|
-
def scan_setup
|
21
|
+
def scan_setup(str)
|
22
|
+
@ss = StringScanner.new(str)
|
23
|
+
@lineno = 1
|
24
|
+
@state = nil
|
25
|
+
end
|
22
26
|
|
23
|
-
def action
|
27
|
+
def action
|
24
28
|
yield
|
25
29
|
end
|
26
30
|
|
27
|
-
def scan_str(
|
28
|
-
|
31
|
+
def scan_str(str)
|
32
|
+
scan_setup(str)
|
29
33
|
do_parse
|
30
34
|
end
|
35
|
+
alias :scan :scan_str
|
31
36
|
|
32
37
|
def load_file( filename )
|
33
38
|
@filename = filename
|
34
39
|
open(filename, "r") do |f|
|
35
|
-
|
40
|
+
scan_setup(f.read)
|
36
41
|
end
|
37
42
|
end
|
38
43
|
|
39
44
|
def scan_file( filename )
|
40
|
-
load_file
|
45
|
+
load_file(filename)
|
41
46
|
do_parse
|
42
47
|
end
|
43
48
|
|
49
|
+
|
44
50
|
def next_token
|
45
|
-
@
|
51
|
+
return if @ss.eos?
|
52
|
+
|
53
|
+
# skips empty actions
|
54
|
+
until token = _next_token or @ss.eos?; end
|
55
|
+
token
|
46
56
|
end
|
47
57
|
|
48
|
-
def
|
49
|
-
|
50
|
-
@
|
51
|
-
@
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
@lineno += 1 if text == "\n"
|
57
|
-
case state
|
58
|
-
when nil
|
59
|
-
case
|
60
|
-
when (text = ss.scan(/\s/))
|
61
|
-
;
|
58
|
+
def _next_token
|
59
|
+
text = @ss.peek(1)
|
60
|
+
@lineno += 1 if text == "\n"
|
61
|
+
token = case @state
|
62
|
+
when nil
|
63
|
+
case
|
64
|
+
when (text = @ss.scan(/\s/))
|
65
|
+
;
|
62
66
|
|
63
|
-
|
64
|
-
|
67
|
+
when (text = @ss.scan(/\(/))
|
68
|
+
action { [:LPAREN, text] }
|
65
69
|
|
66
|
-
|
67
|
-
|
70
|
+
when (text = @ss.scan(/\)/))
|
71
|
+
action { [:RPAREN, text] }
|
68
72
|
|
69
|
-
|
70
|
-
|
73
|
+
when (text = @ss.scan(/\[/))
|
74
|
+
action { [:LBRACK, text] }
|
71
75
|
|
72
|
-
|
73
|
-
|
76
|
+
when (text = @ss.scan(/\]/))
|
77
|
+
action { [:RBRACK, text] }
|
74
78
|
|
75
|
-
|
76
|
-
|
79
|
+
when (text = @ss.scan(/\{/))
|
80
|
+
action { [:LBRACE, text] }
|
77
81
|
|
78
|
-
|
79
|
-
|
82
|
+
when (text = @ss.scan(/\}/))
|
83
|
+
action { [:RBRACE, text] }
|
80
84
|
|
81
|
-
|
82
|
-
|
85
|
+
when (text = @ss.scan(/=/))
|
86
|
+
action { [:EQUALS, text] }
|
83
87
|
|
84
|
-
|
85
|
-
|
88
|
+
when (text = @ss.scan(/\!=/))
|
89
|
+
action { [:NOTEQUALS, text] }
|
86
90
|
|
87
|
-
|
88
|
-
|
91
|
+
when (text = @ss.scan(/~/))
|
92
|
+
action { [:MATCH, text] }
|
89
93
|
|
90
|
-
|
91
|
-
|
94
|
+
when (text = @ss.scan(/</))
|
95
|
+
action { [:LESSTHAN, text] }
|
92
96
|
|
93
|
-
|
94
|
-
|
97
|
+
when (text = @ss.scan(/>/))
|
98
|
+
action { [:GREATERTHAN, text] }
|
95
99
|
|
96
|
-
|
97
|
-
|
100
|
+
when (text = @ss.scan(/not(?![\w_:])/))
|
101
|
+
action { [:NOT, text] }
|
98
102
|
|
99
|
-
|
100
|
-
|
103
|
+
when (text = @ss.scan(/and(?![\w_:])/))
|
104
|
+
action { [:AND, text] }
|
101
105
|
|
102
|
-
|
103
|
-
|
106
|
+
when (text = @ss.scan(/or(?![\w_:])/))
|
107
|
+
action { [:OR, text] }
|
104
108
|
|
105
|
-
|
106
|
-
|
109
|
+
when (text = @ss.scan(/true(?![\w_:])/))
|
110
|
+
action { [:BOOLEAN, true]}
|
107
111
|
|
108
|
-
|
109
|
-
|
112
|
+
when (text = @ss.scan(/false(?![\w_:])/))
|
113
|
+
action { [:BOOLEAN, false]}
|
110
114
|
|
111
|
-
|
112
|
-
|
115
|
+
when (text = @ss.scan(/-?\d+\.\d+/))
|
116
|
+
action { [:NUMBER, text.to_f] }
|
113
117
|
|
114
|
-
|
115
|
-
|
118
|
+
when (text = @ss.scan(/-?\d+/))
|
119
|
+
action { [:NUMBER, text.to_i] }
|
116
120
|
|
117
|
-
|
118
|
-
|
121
|
+
when (text = @ss.scan(/\"(\\.|[^\\"])*\"/))
|
122
|
+
action { [:STRING, YAML.load(text)] }
|
119
123
|
|
120
|
-
|
121
|
-
|
124
|
+
when (text = @ss.scan(/\'(\\.|[^\\'])*\'/))
|
125
|
+
action { [:STRING, YAML.load(text)] }
|
122
126
|
|
123
|
-
|
124
|
-
|
127
|
+
when (text = @ss.scan(/[\w_:]+/))
|
128
|
+
action { [:STRING, text] }
|
125
129
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
else
|
130
|
-
text = ss.string[ss.pos .. -1]
|
131
|
-
raise ScanError, "can not match: '" + text + "'"
|
132
|
-
end # if
|
130
|
+
when (text = @ss.scan(/@@/))
|
131
|
+
action { [:EXPORTED, text] }
|
133
132
|
|
134
133
|
else
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
134
|
+
text = @ss.string[@ss.pos .. -1]
|
135
|
+
raise ScanError, "can not match: '" + text + "'"
|
136
|
+
end # if
|
137
|
+
|
138
|
+
else
|
139
|
+
raise ScanError, "undefined state: '" + state.to_s + "'"
|
140
|
+
end # case state
|
141
|
+
token
|
142
|
+
end # def _next_token
|
139
143
|
|
140
144
|
end # class
|
141
|
-
end # module
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-puppetdb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Bode
|
@@ -9,34 +9,34 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-05-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - '>='
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '0'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - '>='
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: chronic
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - '>='
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '0'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - '>='
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
@@ -57,84 +57,84 @@ dependencies:
|
|
57
57
|
name: rspec-expectations
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - '>='
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - '>='
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: rake
|
72
72
|
requirement: !ruby/object:Gem::Requirement
|
73
73
|
requirements:
|
74
|
-
- -
|
74
|
+
- - '>='
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
|
-
- -
|
81
|
+
- - '>='
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
85
|
name: puppetlabs_spec_helper
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- -
|
88
|
+
- - '>='
|
89
89
|
- !ruby/object:Gem::Version
|
90
90
|
version: '0'
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
|
-
- -
|
95
|
+
- - '>='
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '0'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: puppet
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- -
|
102
|
+
- - '>='
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '0'
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- -
|
109
|
+
- - '>='
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
- !ruby/object:Gem::Dependency
|
113
113
|
name: racc
|
114
114
|
requirement: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
|
-
- -
|
116
|
+
- - '>='
|
117
117
|
- !ruby/object:Gem::Version
|
118
118
|
version: '0'
|
119
119
|
type: :development
|
120
120
|
prerelease: false
|
121
121
|
version_requirements: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
123
|
-
- -
|
123
|
+
- - '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
127
|
+
name: rexical
|
128
128
|
requirement: !ruby/object:Gem::Requirement
|
129
129
|
requirements:
|
130
|
-
- -
|
130
|
+
- - '>='
|
131
131
|
- !ruby/object:Gem::Version
|
132
132
|
version: '0'
|
133
133
|
type: :development
|
134
134
|
prerelease: false
|
135
135
|
version_requirements: !ruby/object:Gem::Requirement
|
136
136
|
requirements:
|
137
|
-
- -
|
137
|
+
- - '>='
|
138
138
|
- !ruby/object:Gem::Version
|
139
139
|
version: '0'
|
140
140
|
description: A higher level query language for PuppetDB.
|
@@ -159,11 +159,12 @@ files:
|
|
159
159
|
- lib/puppet/parser/functions/pdbstatusquery.rb
|
160
160
|
- lib/puppet/parser/functions/query_facts.rb
|
161
161
|
- lib/puppet/parser/functions/query_nodes.rb
|
162
|
+
- lib/puppet/parser/functions/query_resources.rb
|
162
163
|
- lib/puppetdb/astnode.rb
|
163
164
|
- lib/puppetdb/connection.rb
|
164
|
-
- lib/puppetdb/grammar.
|
165
|
-
- lib/puppetdb/lexer.l
|
165
|
+
- lib/puppetdb/grammar.racc
|
166
166
|
- lib/puppetdb/lexer.rb
|
167
|
+
- lib/puppetdb/lexer.rex
|
167
168
|
- lib/puppetdb/parser.rb
|
168
169
|
- lib/puppetdb/util.rb
|
169
170
|
- lib/puppetdb.rb
|
@@ -191,12 +192,12 @@ require_paths:
|
|
191
192
|
- lib
|
192
193
|
required_ruby_version: !ruby/object:Gem::Requirement
|
193
194
|
requirements:
|
194
|
-
- -
|
195
|
+
- - '>='
|
195
196
|
- !ruby/object:Gem::Version
|
196
197
|
version: '0'
|
197
198
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
198
199
|
requirements:
|
199
|
-
- -
|
200
|
+
- - '>='
|
200
201
|
- !ruby/object:Gem::Version
|
201
202
|
version: '0'
|
202
203
|
requirements: []
|
@@ -220,4 +221,3 @@ test_files:
|
|
220
221
|
- examples/nova_functions.pp
|
221
222
|
- examples/query_node_examples.pp
|
222
223
|
- examples/query_resource_examples.pp
|
223
|
-
has_rdoc:
|