/
refresh_parser.rb
346 lines (306 loc) · 13.9 KB
/
refresh_parser.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
module ManageIQ::Providers
module Hawkular
class MiddlewareManager::RefreshParser
include ::Hawkular::ClientUtils
def self.ems_inv_to_hashes(ems, options = nil)
new(ems, options).ems_inv_to_hashes
end
def initialize(ems, _options = nil)
@ems = ems
@eaps = []
@data = {}
@data_index = {}
end
def ems_inv_to_hashes
# the order of the method calls is important here, because they make use of @eaps and @data_index
fetch_middleware_servers
fetch_domains_with_servers
fetch_server_entities
fetch_availability
@data
end
def fetch_middleware_servers
@data[:middleware_servers] = []
@ems.feeds.each do |feed|
@ems.eaps(feed).each do |eap|
@eaps << eap
server = parse_middleware_server(eap)
if server.fetch_path(:properties, 'In Container') == 'true'
container_id = @ems.container_id(eap.feed)
if container_id
backing_ref = 'docker://' + container_id
container = Container.find_by(:backing_ref => backing_ref)
set_lives_on(server, container) if container
end
else
machine_id = @ems.machine_id(eap.feed)
host_instance = find_host_by_bios_uuid(machine_id) ||
find_host_by_bios_uuid(alternate_machine_id(machine_id))
set_lives_on(server, host_instance) if host_instance
end
@data[:middleware_servers] << server
@data_index.store_path(:middleware_servers, :by_ems_ref, server[:ems_ref], server)
end
end
end
def set_lives_on(server, lives_on)
server[:lives_on_id] = lives_on.id
server[:lives_on_type] = lives_on.type
end
def fetch_domains_with_servers
@data[:middleware_domains] = []
@ems.feeds.each do |feed|
@ems.domains(feed).each do |domain|
parsed_domain = parse_middleware_domain(feed, domain)
# add the server groups to the domain
parsed_domain[:middleware_server_groups] = fetch_server_groups(feed)
@data[:middleware_domains] << parsed_domain
@data_index.store_path(:middleware_domains, :by_ems_ref, parsed_domain[:ems_ref], parsed_domain)
# now it's safe to fetch the domain servers (it assumes the server groups to be already fetched)
fetch_domain_servers(feed)
end
end
end
def fetch_server_groups(feed)
@ems.server_groups(feed).map do |group|
parsed_group = parse_middleware_server_group(group)
@data_index.store_path(:middleware_server_groups, :by_name, parsed_group[:name], parsed_group)
parsed_group
end
end
def fetch_domain_servers(feed)
path = ::Hawkular::Inventory::CanonicalPath.new(:feed_id => hawk_escape_id(feed),
:resource_type_id => hawk_escape_id('Domain WildFly Server'))
domain_servers = @ems.inventory_client.list_resources_for_type(path.to_s, :fetch_properties => true)
domain_servers.each do |domain_server|
@eaps << domain_server
server_name = parse_domain_server_name(domain_server.id)
server = parse_middleware_server(domain_server, true, server_name)
# Add the association to server group. The information about what server is in which server group is under
# the server-config resource's configuration
config_path = domain_server.path.to_s.sub(/%2Fserver%3D/, '%2Fserver-config%3D')
config = @ems.inventory_client.get_config_data_for_resource(config_path)
server_group_name = config['value']['Server Group']
server_group = @data_index.fetch_path(:middleware_server_groups, :by_name, server_group_name)
server[:middleware_server_group] = server_group
@data[:middleware_servers] << server
@data_index.store_path(:middleware_servers, :by_ems_ref, server[:ems_ref], server)
end
end
def alternate_machine_id(machine_id)
return if machine_id.nil?
# See the BZ #1294461 [1] for a more complete background.
# Here, we'll try to adjust the machine ID to the format from that bug. We expect to get a string like
# this: 2f68d133a4bc4c4bb19ecb47d344746c . For such string, we should return
# this: 33d1682f-bca4-4b4c-b19e-cb47d344746c .The actual BIOS UUID is probably returned in upcase, but other
# providers store it in downcase, so, we let the upcase/downcase logic to other methods with more
# business knowledge.
# 1 - https://bugzilla.redhat.com/show_bug.cgi?id=1294461
alternate = []
alternate << swap_part(machine_id[0, 8])
alternate << swap_part(machine_id[8, 4])
alternate << swap_part(machine_id[12, 4])
alternate << machine_id[16, 4]
alternate << machine_id[20, 12]
alternate.join('-')
end
def swap_part(part)
# here, we receive parts of an UUID, split into an array with 2 chars each element, and reverse the invidual
# elements, joining and reversing the final outcome
# for instance:
# 2f68d133 -> ["2f", "68", "d1", "33"] -> ["f2", "86", "1d", "33"] -> f2861d33 -> 33d1682f
part.scan(/../).collect(&:reverse).join.reverse
end
def find_host_by_bios_uuid(machine_id)
return if machine_id.nil?
identity_system = machine_id.downcase
Vm.find_by(:uid_ems => identity_system,
:type => uuid_provider_types) if identity_system
end
def uuid_provider_types
# after the PoC, we might want to test/support these extra providers:
# ManageIQ::Providers::Openstack::CloudManager::Vm
# ManageIQ::Providers::Vmware::InfraManager::Vm
'ManageIQ::Providers::Redhat::InfraManager::Vm'
end
def fetch_server_entities
@data[:middleware_deployments] = []
@data[:middleware_datasources] = []
@data[:middleware_messagings] = []
@eaps.map do |eap|
@ems.child_resources(eap.path, true).map do |child|
next unless child.type_path.end_with?('Deployment', 'Datasource', 'JMS%20Topic', 'JMS%20Queue')
server = @data_index.fetch_path(:middleware_servers, :by_ems_ref, eap.path)
process_server_entity(server, child)
end
end
end
def fetch_availability
resources_by_metric_id = {}
metric_id_by_resource_path = {}
feeds_of_interest = @eaps.map(&:feed).uniq
feeds_of_interest.each do |feed|
deployment_status_mt_path = ::Hawkular::Inventory::CanonicalPath.new(
:metric_type_id => 'Deployment%20Status~Deployment%20Status', :feed_id => feed
)
deployment_status_metrics = @ems.inventory_client.list_metrics_for_metric_type(deployment_status_mt_path)
deployment_status_metrics.each do |deployment_status_metric|
deployment_status_metric_path = ::Hawkular::Inventory::CanonicalPath.parse(deployment_status_metric.path)
# By dropping metric_id from the canonical path we end up with the resource path
resource_path = ::Hawkular::Inventory::CanonicalPath.new(
:tenant_id => deployment_status_metric_path.tenant_id,
:feed_id => deployment_status_metric_path.feed_id,
:resource_ids => deployment_status_metric_path.resource_ids
)
metric_id_by_resource_path[URI.decode(resource_path.to_s)] = deployment_status_metric.hawkular_metric_id
end
end
@data[:middleware_deployments].each do |deployment|
# Mark default status for all deployments
deployment[:status] = process_availability
path = ::Hawkular::Inventory::CanonicalPath.parse(deployment[:ems_ref])
# for subdeployments use it's parent deployment availability.
path = path.up if URI.decode(path.resource_ids.last).include? '/subdeployment='
# Ensure consistency on keys (resource_path) used on metric_id_by_resource_path
path = ::Hawkular::Inventory::CanonicalPath.new(:tenant_id => path.tenant_id,
:feed_id => path.feed_id,
:resource_ids => path.resource_ids)
path = URI.decode(path.to_s)
next unless metric_id_by_resource_path.key? path
metric_id = metric_id_by_resource_path[path]
resources_by_metric_id[metric_id] = [] unless resources_by_metric_id.key? metric_id
resources_by_metric_id[metric_id] << deployment
end
unless resources_by_metric_id.empty?
availabilities = @ems.metrics_client.avail.raw_data(resources_by_metric_id.keys,
:limit => 1, :order => 'DESC')
parse_availability availabilities, resources_by_metric_id
end
end
def process_entity_with_config(server, entity, continuation)
entity_id = hawk_escape_id entity.id
server_path = ::Hawkular::Inventory::CanonicalPath.parse(server[:ems_ref])
resource_ids = server_path.resource_ids << entity_id
resource_path = ::Hawkular::Inventory::CanonicalPath.new(:feed_id => server_path.feed_id,
:resource_ids => resource_ids)
config = @ems.inventory_client.get_config_data_for_resource(resource_path.to_s)
send(continuation, server, entity, config)
end
def process_server_entity(server, entity)
if entity.type_path.end_with?('Deployment')
@data[:middleware_deployments] << parse_deployment(server, entity)
elsif entity.type_path.end_with?('Datasource')
@data[:middleware_datasources] << process_entity_with_config(server, entity, :parse_datasource)
else
@data[:middleware_messagings] << process_entity_with_config(server, entity, :parse_messaging)
end
end
def process_availability(availability = nil)
case
when availability.blank?, availability['value'].casecmp('unknown').zero?
'Unknown'
when availability['value'].casecmp('up').zero?
'Enabled'
when availability['value'].casecmp('down').zero?
'Disabled'
else
'Unknown'
end
end
def parse_availability(availabilities, resources_by_metric_id)
availabilities.each do |availability|
availability_status = process_availability(availability['data'].first)
resources_by_metric_id[availability['id']].each do |resource|
resource[:status] = availability_status
end
end
resources_by_metric_id
end
def parse_deployment(server, deployment)
specific = {
:name => parse_deployment_name(deployment.id),
:middleware_server => server,
}
parse_base_item(deployment).merge(specific)
end
def parse_messaging(server, messaging, config)
type_path = ::Hawkular::Inventory::CanonicalPath.parse(messaging.type_path)
specific = {
:name => messaging.name,
:middleware_server => server,
:messaging_type => URI.decode(type_path.resource_type_id)
}
if !config.empty? && !config['value'].empty? && config['value'].respond_to?(:except)
specific[:properties] = config['value'].except('Username', 'Password')
end
parse_base_item(messaging).merge(specific)
end
def parse_datasource(server, datasource, config)
specific = {
:name => datasource.name,
:middleware_server => server,
}
if !config.empty? && !config['value'].empty? && config['value'].respond_to?(:except)
specific[:properties] = config['value'].except('Username', 'Password')
end
parse_base_item(datasource).merge(specific)
end
def parse_middleware_domain(feed, domain)
specific = {
:name => parse_domain_name(feed),
:type_path => domain.type_path,
}
parse_base_item(domain).merge(specific)
end
def parse_middleware_server_group(group)
specific = {
:name => parse_server_group_name(group.name),
:type_path => group.type_path,
:profile => group.properties['Profile'],
}
parse_base_item(group).merge(specific)
end
def parse_middleware_server(eap, domain = false, name = nil)
not_started = domain && eap.properties['Server State'] == 'STOPPED'
hostname, product = ['Hostname', 'Product Name'].map do |x|
not_started && eap.properties[x].nil? ? _('not yet available') : eap.properties[x]
end
specific = {
:name => name || parse_standalone_server_name(eap.id),
:type_path => eap.type_path,
:hostname => hostname,
:product => product,
}
parse_base_item(eap).merge(specific)
end
private
def parse_base_item(item)
data = {
:ems_ref => item.path,
:nativeid => item.id,
}
[:properties, :feed].each do |field|
if item.respond_to? field
data.merge!(field => item.send(field))
end
end
data
end
def parse_deployment_name(name)
name.sub(/^.*deployment=/, '')
end
def parse_server_group_name(name)
name.sub(/^Domain Server Group \[/, '').chomp(']')
end
def parse_domain_server_name(name)
name.sub(%r{^.*\/server=}, '')
end
def parse_domain_name(name)
name.sub(/^[^\.]+\./, '')
end
def parse_standalone_server_name(name)
name.sub(/~~$/, '').sub(/^.*?~/, '')
end
end
end
end