forked from choria-legacy/mcollective-choria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
choria.rb
214 lines (185 loc) · 6.77 KB
/
choria.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require "net/http"
require_relative "../util/choria"
module MCollective
class Discovery
class Choria
def self.discover(filter, timeout, limit=0, client=nil)
Choria.new(filter, timeout, limit, client).discover
end
attr_reader :timeout, :limit, :client, :config
attr_accessor :filter
def initialize(filter, timeout, limit, client)
@filter = filter
@timeout = timeout
@limit = limit
@client = client
@config = Config.instance
end
# Search for nodes
#
# @return [Array<String>] list of certnames found
def discover
queries = []
if choria.proxied_discovery?
Log.debug("Performing discovery against a PuppetDB Proxy")
choria.proxy_discovery_query(proxy_request)
else
Log.debug("Performing direct discovery against PuppetDB")
queries << discover_collective(filter["collective"]) if filter["collective"]
queries << discover_nodes(filter["identity"]) unless filter["identity"].empty?
queries << discover_classes(filter["cf_class"]) unless filter["cf_class"].empty?
queries << discover_facts(filter["fact"]) unless filter["fact"].empty?
queries << discover_agents(filter["agent"]) unless filter["agent"].empty?
choria.pql_query(node_search_string(queries.compact), true)
end
end
# Creates a request hash for the discovery proxy
#
# @return [Hash]
def proxy_request
request = {}
request["collective"] = filter["collective"] if filter["collective"]
request["identities"] = filter["identity"] unless filter["identity"].empty?
request["classes"] = filter["cf_class"] unless filter["cf_class"].empty?
request["facts"] = filter["fact"] unless filter["fact"].empty?
request["agents"] = filter["agent"] unless filter["agent"].empty?
request
end
# Discovers nodes in a specific collective
#
# @param filter [String] a collective name
# @return [String] a query string
def discover_collective(filter)
'certname in inventory[certname] { facts.mcollective.server.collectives.match("\d+") = "%s" }' % filter
end
# Searches for facts
#
# Nodes are searched using an `and` operator via the discover_classes method
#
# When the `rpcutil` agent is required it will look for `Mcollective` class
# otherwise `Mcollective_avent_agentname` thus it will only find plugins
# installed using the `choria/mcollective` AIO plugin packager
#
# @param filter [Array<String>] agent names
# @return [Array<String>] list of nodes found
def discover_agents(filter)
pql = filter.map do |agent|
if agent == "rpcutil"
discover_classes(["mcollective::service"])
elsif agent =~ /^\/(.+)\/$/
'resources {type = "File" and tag ~ "mcollective_agent_.*?%s.*?_server"}' % [string_regexi($1)]
else
'resources {type = "File" and tag = "mcollective_agent_%s_server"}' % [agent]
end
end
pql.join(" and ") unless pql.empty?
end
# Turns a string into a case insensitive regex string
#
# @param value [String]
# @return [String]
def string_regexi(value)
value =~ /^\/(.+)\/$/ ? derived_value = $1 : derived_value = value.dup
derived_value.each_char.map do |char|
if char =~ /[[:alpha:]]/
"[%s%s]" % [char.downcase, char.upcase]
else
char
end
end.join
end
# Capitalize a Puppet Resource
#
# foo::bar => Foo::Bar
#
# @param resource [String] a resource title
# @return [String]
def capitalize_resource(resource)
resource.split("::").map(&:capitalize).join("::")
end
# Searches for facts
#
# Nodes are searched using an `and` operator
#
# @param filter [Array<Hash>] hashes with :fact, :operator and :value
# @return [Array<String>] list of nodes found
def discover_facts(filter)
pql = filter.map do |q|
fact = q[:fact]
operator = q[:operator]
value = q[:value]
case operator
when "=~"
regex = string_regexi(value)
'inventory {facts.%s ~ "%s"}' % [fact, regex]
when "=="
'inventory {facts.%s = "%s"}' % [fact, value]
when "!="
'inventory {!(facts.%s = "%s")}' % [fact, value]
when ">=", ">", "<=", "<"
raise("Do not know how to do string fact comparisons using the '%s' operator with PuppetDB" % operator) unless numeric?(value)
"inventory {facts.%s %s %s}" % [fact, operator, value]
else
raise("Do not know how to do fact comparisons using the '%s' operator with PuppetDB" % operator)
end
end
pql.join(" and ") unless pql.empty?
end
# Searches for classes
#
# Nodes are searched using an `and` operator
#
# @return [Array<String>] list of nodes found
def discover_classes(filter)
pql = filter.map do |klass|
if klass =~ /^\/(.+)\/$/
'resources {type = "Class" and title ~ "%s"}' % [string_regexi($1)]
else
'resources {type = "Class" and title = "%s"}' % [capitalize_resource(klass)]
end
end
pql.join(" and ") unless pql.empty?
end
# Searches for nodes
#
# Nodes are searched using an `or` operator
#
# @return [Array<String>] list of nodes found
def discover_nodes(filter)
if filter.empty?
Log.debug("Empty node filter found, discovering all nodes")
nil
else
pql = filter.map do |ident|
if ident =~ /^pql:\s*(.+)$/
"certname in %s" % $1
elsif ident =~ /^\/(.+)\/$/
'certname ~ "%s"' % string_regexi($1)
else
'certname = "%s"' % ident
end
end
pql.join(" or ") unless pql.empty?
end
end
# Produce a nodes query with the supplied sub query included
#
# @param queries [Array<String>] PQL queries to be used as a sub query
# @return [String] nodes search string
def node_search_string(queries)
filter_queries = queries.map {|q| "(%s)" % q}.join(" and ")
"nodes[certname, deactivated] { %s }" % [filter_queries]
end
# Determines if a string is a number, either float or integer
#
# @param string [String]
# @return [boolean]
def numeric?(string)
true if Float(string) rescue false
end
def choria
@_choria ||= Util::Choria.new(false)
end
end
end
end