ruby-puppetdb 1.3.3 → 1.4.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/face/query.rb +3 -2
- data/lib/puppetdb.rb +2 -0
- data/lib/puppetdb/astnode.rb +2 -1
- data/lib/puppetdb/connection.rb +5 -2
- data/lib/puppetdb/grammar.y +4 -3
- data/lib/puppetdb/lexer.l +1 -0
- data/lib/puppetdb/lexer.rb +3 -0
- data/lib/puppetdb/parser.rb +101 -90
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/puppetdb/connection_spec.rb +63 -0
- metadata +66 -30
- data/.gitignore +0 -4
- data/.travis.yml +0 -5
- data/COPYING +0 -202
- data/Gemfile +0 -5
- data/Modulefile +0 -7
- data/README.md +0 -267
- data/Rakefile +0 -25
- data/ruby-puppetdb.gemspec +0 -24
data/Gemfile
DELETED
data/Modulefile
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
name 'dalen-puppetdbquery'
|
2
|
-
version %x{git describe --tags}.sub('-','+').chomp
|
3
|
-
author 'Erik Dalen <erik.gustav.dalen@gmail.com>'
|
4
|
-
license 'Apache License V2'
|
5
|
-
summary 'Query functions for the PuppetDB API'
|
6
|
-
project_page 'https://github.com/dalen/puppet-puppetdbquery'
|
7
|
-
|
data/README.md
DELETED
@@ -1,267 +0,0 @@
|
|
1
|
-
[](https://travis-ci.org/dalen/puppet-puppetdbquery)
|
2
|
-
|
3
|
-
PuppetDB query tools
|
4
|
-
====================
|
5
|
-
|
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.
|
8
|
-
|
9
|
-
Query format
|
10
|
-
============
|
11
|
-
|
12
|
-
The query format accepts resource references in the form:
|
13
|
-
|
14
|
-
Type[Name]{attribute1=foo and attribute2=bar}
|
15
|
-
|
16
|
-
Each of the three parts are optional. It will only match non exported resources by default, to match exported resources ad @@ in front.
|
17
|
-
|
18
|
-
Facts can be matched using the operators =, !=, >, < and ~ (regexp match). > and < only work on numbers, ~ only works on strings.
|
19
|
-
|
20
|
-
Any expression can be combined using "not", "and" or "or", in that precedence order. To change precedence you can use parenthesis.
|
21
|
-
|
22
|
-
Alphanumeric strings don't need to be quoted, but can be quoted using double quotes with the same escaping rules as JSON.
|
23
|
-
Numbers are interpreted as numbers and true/false as boolean values, to use them as strings instead simply quote them.
|
24
|
-
|
25
|
-
Installation
|
26
|
-
------------
|
27
|
-
|
28
|
-
Ensure that the lib directory is in Ruby's LOADPATH.
|
29
|
-
|
30
|
-
$ export RUBYLIB=puppet-puppetdbquery/lib:$RUBYLIB
|
31
|
-
|
32
|
-
PuppetDB terminus is required for the Puppet functions, but not the face.
|
33
|
-
|
34
|
-
Usage
|
35
|
-
======
|
36
|
-
|
37
|
-
To get a list of the supported subcommands for the query face, run:
|
38
|
-
|
39
|
-
$ puppet help query
|
40
|
-
|
41
|
-
You can run `puppet help` on the returned subcommands
|
42
|
-
|
43
|
-
$ puppet help query nodes
|
44
|
-
$ puppet help query facts
|
45
|
-
|
46
|
-
CLI
|
47
|
-
---
|
48
|
-
|
49
|
-
Each of the faces uses the following query syntax to return all objects found on a subset of nodes:
|
50
|
-
|
51
|
-
# get all nodes that contain the apache package and are in france, or all nodes in the us
|
52
|
-
$ puppet query nodes (Package[httpd] and country=fr) or country=us)'
|
53
|
-
|
54
|
-
Each of the individual faces returns a different data format:
|
55
|
-
|
56
|
-
nodes - a list of nodes identified by a name
|
57
|
-
|
58
|
-
$ puppet query nodes '(Package["mysql-server"] and architecture=amd64)'
|
59
|
-
["db_node_1", "db_node2"]
|
60
|
-
|
61
|
-
facts - a hash of facts per node
|
62
|
-
|
63
|
-
$ puppet query facts '(Package["mysql-server"] and architecture=amd64)'
|
64
|
-
db_node_1 {"facterversion":"1.6.9","hostname":"controller",...........}
|
65
|
-
db_node_2 {"facterversion":"1.6.9","hostname":"controller",...........}
|
66
|
-
|
67
|
-
events - a list of events on the matched nodes
|
68
|
-
|
69
|
-
$ puppet query events '(Package["mysql-server"] and architecture=amd64)' --since='1 hour ago' --until=now --status=success
|
70
|
-
host.example.com: 2013-06-10T10:58:37.000Z: File[/foo/bar]/content ({md5}5711edf5f5c50bd7845465471d8d39f0 -> {md5}e485e731570b8370f19a2a40489cc24b): content changed '{md5}5711edf5f5c50bd7845465471d8d39f0' to '{md5}e485e731570b8370f19a2a40489cc24b'
|
71
|
-
|
72
|
-
Ruby
|
73
|
-
----
|
74
|
-
|
75
|
-
faces can be called from the ruby in exactly they same way they are called from the command line:
|
76
|
-
|
77
|
-
$ irb> require 'puppet/face'
|
78
|
-
irb> Puppet::Face[:query, :current].nodes(:query => '(Package["mysql-server"] and architecture=amd64)')
|
79
|
-
|
80
|
-
Puppet functions
|
81
|
-
----------------
|
82
|
-
|
83
|
-
There's corresponding functions to query PuppetDB directly from Puppet manifests.
|
84
|
-
|
85
|
-
### query_nodes
|
86
|
-
|
87
|
-
Accepts two arguments, a query used to discover nodes, and a optional
|
88
|
-
fact that should be returned.
|
89
|
-
|
90
|
-
Returns an array of certnames or fact values if a fact is specified.
|
91
|
-
|
92
|
-
#### Examples
|
93
|
-
|
94
|
-
$hosts = query_nodes('manufacturer~"Dell.*" and processorcount=24 and Class[Apache]')
|
95
|
-
|
96
|
-
$hostips = query_nodes('manufacturer~"Dell.*" and processorcount=24 and Class[Apache]', ipaddress)
|
97
|
-
|
98
|
-
### query_facts
|
99
|
-
|
100
|
-
Similar to query_nodes but takes two arguments, the first is a query used to discover nodes, the second is a list of facts to return for those nodes.
|
101
|
-
|
102
|
-
Returns a nested hash where the keys are the certnames of the nodes, each containing a hash with facts and fact values.
|
103
|
-
|
104
|
-
#### Example
|
105
|
-
|
106
|
-
query_facts('Class[Apache]{port=443}', ['osfamily', 'ipaddress'])
|
107
|
-
|
108
|
-
Example return value in JSON format:
|
109
|
-
|
110
|
-
{
|
111
|
-
"foo.example.com": {
|
112
|
-
"ipaddress": "192.168.0.2",
|
113
|
-
"osfamily": "Redhat"
|
114
|
-
},
|
115
|
-
"bar.example.com": {
|
116
|
-
"ipaddress": "192.168.0.3",
|
117
|
-
"osfamily": "Debian"
|
118
|
-
}
|
119
|
-
}
|
120
|
-
|
121
|
-
Hiera backend
|
122
|
-
=============
|
123
|
-
|
124
|
-
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`.
|
125
|
-
|
126
|
-
So instead of writing something like this in for example your `hiera-data/common.yaml`:
|
127
|
-
|
128
|
-
ntp::servers:
|
129
|
-
- 'ntp1.example.com'
|
130
|
-
- 'ntp2.example.com'
|
131
|
-
|
132
|
-
You can now instead write:
|
133
|
-
|
134
|
-
ntp::servers::_nodequery: 'Class[Ntp::Server]'
|
135
|
-
|
136
|
-
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:
|
137
|
-
|
138
|
-
ntp::servers::_nodequery: ['Class[Ntp::Server]', 'ipaddress']
|
139
|
-
|
140
|
-
or a hash:
|
141
|
-
|
142
|
-
ntp::servers::_nodequery:
|
143
|
-
query: 'Class[Ntp::Server]'
|
144
|
-
fact: 'ipaddress'
|
145
|
-
|
146
|
-
When returning facts only nodes that actually have the fact are returned, even if more nodes would in fact match the query itself.
|
147
|
-
|
148
|
-
Deprecated PuppetDB query functions
|
149
|
-
===================================
|
150
|
-
|
151
|
-
This module also contains some older deprecated functions that use the raw
|
152
|
-
version 1 PuppetDB API.
|
153
|
-
They require the json ruby gem and the puppetdb-terminus.
|
154
|
-
|
155
|
-
Only queries over HTTPS are supported atm.
|
156
|
-
|
157
|
-
Usage
|
158
|
-
-----
|
159
|
-
|
160
|
-
### pdbresourcequery
|
161
|
-
|
162
|
-
The first argument is the resource query.
|
163
|
-
Second argument is optional but allows you to specify the item you want
|
164
|
-
from the returned hash.
|
165
|
-
|
166
|
-
It automatically excludes deactivated hosts.
|
167
|
-
|
168
|
-
Returns an array of hashes or array of strings if second argument is provided.
|
169
|
-
|
170
|
-
#### Examples
|
171
|
-
|
172
|
-
# Return an array of hashes describing all files that are owned by root on active hosts.
|
173
|
-
$ret = pdbresourcequery(
|
174
|
-
['and',
|
175
|
-
['=','type','File'],
|
176
|
-
['=',['parameter','owner'],'root']])
|
177
|
-
|
178
|
-
# Return an array of host names having those resources
|
179
|
-
$ret = pdbresourcequery(
|
180
|
-
['and',
|
181
|
-
['=',['node','active'],true],
|
182
|
-
['=','type','File'],
|
183
|
-
['=',['parameter','owner'],'root']], 'certname')
|
184
|
-
|
185
|
-
### pdbresourcequery_all
|
186
|
-
|
187
|
-
Works exactly like pdbresourcequery but also returns deactivated hosts.
|
188
|
-
|
189
|
-
### pdbnodequery
|
190
|
-
|
191
|
-
The first argument is the node query.
|
192
|
-
Second argument is optional but allows you to specify a resource query
|
193
|
-
that the nodes returned also have to match.
|
194
|
-
|
195
|
-
It automatically excludes deactivated hosts.
|
196
|
-
|
197
|
-
Returns a array of strings with the certname of the nodes (fqdn by default).
|
198
|
-
|
199
|
-
#### Examples
|
200
|
-
|
201
|
-
# Return an array of active nodes with an uptime more than 30 days
|
202
|
-
$ret = pdbnodequery(['>',['fact','uptime_days'],30])
|
203
|
-
|
204
|
-
# Return an array of active nodes with an uptime more than 30 days and
|
205
|
-
# having the class 'apache'
|
206
|
-
$ret = pdbnodequery(
|
207
|
-
['>',['fact','uptime_days'],30],
|
208
|
-
['and',
|
209
|
-
['=','type','Class'],
|
210
|
-
['=','title','Apache']])
|
211
|
-
|
212
|
-
### pdbnodequery_all
|
213
|
-
|
214
|
-
Works exactly like pdbnodequery but also returns deactivated hosts.
|
215
|
-
|
216
|
-
### pdbfactquery
|
217
|
-
|
218
|
-
The first argument is the node to get facts for. It can be either a single node
|
219
|
-
or an array of nodes. If it is an array the return value of the function will also
|
220
|
-
be an array.
|
221
|
-
Second argument is optional, if specified only return that specific fact.
|
222
|
-
|
223
|
-
#### Examples
|
224
|
-
|
225
|
-
# Get hash of facts for foo.example.com
|
226
|
-
pdbfactquery('foo.example.com')
|
227
|
-
# Get the uptime fact for foo.example.com
|
228
|
-
pdbfactquery('foo.example.com', 'uptime')
|
229
|
-
# Get the uptime fact for foo.example.com and bar.example.com
|
230
|
-
pdbfactquery(['foo.example.com', 'bar.example.com'], 'uptime')
|
231
|
-
|
232
|
-
### pdbstatusquery
|
233
|
-
|
234
|
-
The first argument is the node to get the status for.
|
235
|
-
Second argument is optional, if specified only return that specific bit of
|
236
|
-
status, one of 'name', 'deactivated', 'catalog_timestamp' and 'facts_timestamp'.
|
237
|
-
|
238
|
-
Returns an array of hashes or a array of strings if second argument is supplied.
|
239
|
-
|
240
|
-
#### Examples
|
241
|
-
|
242
|
-
# Get status for foo.example.com
|
243
|
-
pdbstatusquery('foo.example.com')
|
244
|
-
# Get catalog_timestamp for foo.example.com
|
245
|
-
pdbstatusquery('foo.example.com', 'catalog_timestamp')
|
246
|
-
|
247
|
-
### pdbquery
|
248
|
-
|
249
|
-
This is the generic query function that the others make use of, you probably
|
250
|
-
don't have to use it and can use one of the specific functions below instead.
|
251
|
-
|
252
|
-
The first argument is the URL path that should be queried, for
|
253
|
-
example 'nodes' or 'status/nodes/<nodename>'.
|
254
|
-
The second argument if supplied if the query parameter, if it is
|
255
|
-
a string it is assumed to be JSON formatted and sent as is,
|
256
|
-
anything else is converted to JSON and then sent.
|
257
|
-
|
258
|
-
#### Examples
|
259
|
-
|
260
|
-
# Get list of all active nodes
|
261
|
-
$ret = pdbquery('nodes', ['=', ['node', 'active'], true ])
|
262
|
-
|
263
|
-
# Query status of current node
|
264
|
-
$ret2 = pdbquery("status/nodes/${settings::certname}")
|
265
|
-
|
266
|
-
See [http://docs.puppetlabs.com/puppetdb](http://docs.puppetlabs.com/puppetdb) for
|
267
|
-
more info and examples about the PuppetDB API.
|
data/Rakefile
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
require 'rspec/core/rake_task'
|
4
|
-
require 'puppetlabs_spec_helper/rake_tasks'
|
5
|
-
|
6
|
-
task :default => [:test]
|
7
|
-
|
8
|
-
desc 'Run RSpec'
|
9
|
-
RSpec::Core::RakeTask.new(:test) do |t|
|
10
|
-
t.pattern = 'spec/{unit}/**/*.rb'
|
11
|
-
t.rspec_opts = ['--color']
|
12
|
-
end
|
13
|
-
|
14
|
-
desc "Generate Lexer and Parser"
|
15
|
-
task :generate => [:lexer, :parser]
|
16
|
-
|
17
|
-
desc "Generate Parser"
|
18
|
-
task :parser do
|
19
|
-
`racc lib/puppetdb/grammar.y -o lib/puppetdb/parser.rb --superclass='PuppetDB::Lexer'`
|
20
|
-
end
|
21
|
-
|
22
|
-
desc "Generate Lexer"
|
23
|
-
task :lexer do
|
24
|
-
`rex lib/puppetdb/lexer.l -o lib/puppetdb/lexer.rb`
|
25
|
-
end
|
data/ruby-puppetdb.gemspec
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
Gem::Specification.new do |s|
|
2
|
-
s.name = "ruby-puppetdb"
|
3
|
-
s.version = %x{git describe --tags}.split('-')[0..1].join('.')
|
4
|
-
s.platform = Gem::Platform::RUBY
|
5
|
-
s.authors = ["Dan Bode", "Erik Dalén"]
|
6
|
-
s.email = ["dan@puppetlabs.com", "erik.gustav.dalen@gmail.com"]
|
7
|
-
s.homepage = "https://github.com/dalen/puppet-puppetdbquery"
|
8
|
-
s.summary = %q{Query functions for PuppetDB}
|
9
|
-
s.description = %q{A higher level query language for PuppetDB.}
|
10
|
-
s.license = 'Apache v2'
|
11
|
-
|
12
|
-
|
13
|
-
s.files = `git ls-files`.split("\n")
|
14
|
-
s.test_files = `git ls-files -- {test,spec,features,examples}/*`.split("\n")
|
15
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
s.add_dependency('json')
|
18
|
-
s.add_dependency('chronic')
|
19
|
-
|
20
|
-
s.add_development_dependency 'rspec', '2.13'
|
21
|
-
s.add_development_dependency 'rake'
|
22
|
-
s.add_development_dependency 'puppetlabs_spec_helper'
|
23
|
-
s.add_development_dependency 'puppet'
|
24
|
-
end
|