-
Notifications
You must be signed in to change notification settings - Fork 29
/
consul_nodes.json.erb
70 lines (62 loc) · 2.75 KB
/
consul_nodes.json.erb
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
<%
# This template can be configure the following way with environment variables
# Environment variables to filter services/instances
# SERVICES_TAG_FILTER: basic tag filter for service (default HTTP)
# INSTANCE_MUST_TAG: Second level of filtering (optional, default to SERVICES_TAG_FILTER)
# INSTANCE_EXCLUDE_TAG: Exclude instances having the given tag (default: canary)
# EXCLUDE_SERVICES: comma-separated services of regexps to exclude (default: lbl7.*,netsvc-probe.*,consul-probed.*)
service_tag_filter = ENV['SERVICES_TAG_FILTER'] || nil
instance_must_tag = ENV['INSTANCE_MUST_TAG'] || service_tag_filter
instance_exclude_tag = ENV['INSTANCE_EXCLUDE_TAG']
# Services to hide
services_blacklist_raw = (ENV['EXCLUDE_SERVICES'] || 'lbl7.*,netsvc-probe.*,consul-probed.*').split(',')
services_blacklist = services_blacklist_raw.map { |v| Regexp.new(v) } # Compute the health of a Service
service_per_node = {}
all_coordinates = coordinate.nodes.map { |c| [c['Node'], c['Coord']] }.to_h
services.each do |service_name, tags|
if ! services_blacklist.any? {|r| r.match(service_name)}
service(service_name, tag: service_tag_filter).sort {|a,b| a['Node']['Node'] <=> b['Node']['Node'] }.each do |snode|
node_info = service_per_node[snode['Node']['Node']] || {}
node_node_data = {
Name: snode['Node']['Node'],
Address: snode['Node']['Address'],
Meta: snode.node_meta,
}
node_services_data = node_info[:Service] || []
service_data = {
Service: snode["Service"]["Service"],
Address: snode.service_address,
Tags: snode["Service"]["Tags"],
Port: snode["Service"]["Port"],
}
checks_array = []
snode["Checks"].each do |check|
checks_data = {
name: check["Name"],
status: check["Status"],
notes: check["Notes"],
output: check["Output"],
}
checks_array.push(checks_data)
end
node_service = {
Service: service_data,
Checks: checks_array,
}
node_services_data.push(node_service)
node_data = {
Node: node_node_data,
Service: node_services_data,
# Only put Checks on a Node
checks: snode["Checks"].select{|c| c['ServiceID'].empty? }.map{|c| {output: c['Output'], notes: c['Notes'], name: c['Name'], checkid: c['CheckID'], status: c['Status']}},
Coord: all_coordinates[node_node_data[:Name]],
}
service_per_node[snode['Node']['Node']] = node_data
end
# break
end
end
%><%
json_datacenters = datacenters.to_a
json = { nodes: service_per_node, datacenters: json_datacenters, generated_at: Time.now}
%><%= JSON.pretty_generate(json) %>