-
Notifications
You must be signed in to change notification settings - Fork 28
/
consul_services.json.erb
90 lines (86 loc) · 3.74 KB
/
consul_services.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<%
# 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 regexps to exclude (default: lbl7.*,netsvc-probe.*,consul-probed.*)
unless @sort_consul_service_nodes
begin
target_dir = File.split(File.expand_path(template_info['source']))[0]
target = File.join(target_dir, '.preferences.rb')
load "#{target}"
STDERR.puts "Using #{target} file."
@sort_consul_service_nodes = SORT_CONSUL_SERVICE_NODES
rescue LoadError
STDERR.puts 'Couldn\'t find .preferences.rb file ; default configuration will be used.'
@sort_consul_service_nodes = -> (nodes) { nodes.sort {|a,b| a['Node']['Node'] <=> b['Node']['Node'] } }
end
end
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) }
backends = {}
tags_per_service = {}
services(tag: service_tag_filter).each do |service_name, tags|
if !services_blacklist.any? {|r| r.match(service_name)} && (instance_must_tag.nil? || tags.include?(instance_must_tag))
tags_per_service[service_name] = tags.sort
the_backends = []
@sort_consul_service_nodes.call(service(service_name)).each do |snode|
tags_of_instance = snode['Service']['Tags'].sort
if (instance_must_tag.nil? || tags_of_instance.include?(instance_must_tag)) && !tags_of_instance.include?(instance_exclude_tag)
the_backends << snode
end
end
# We add the backend ONLY if at least one valid instance does exists
backends[service_name] = the_backends
end
end
%><%
all_nodes = {}
json_backends = {}
backends.each_pair do |service_name, nodes|
service = {
name: service_name,
count: nodes.count,
tags: tags_per_service[service_name],
instances: [],
}
json_backends[service_name] = service
nodes.each do |snode|
checks = []
snode['Checks'].each do |ncheck|
check = {}
check['checkid'] = ncheck['ID'] || ncheck['CheckID']
check['name'] = ncheck['Name']
check['output'] = ncheck['Output']
check['status'] = ncheck['Status']
check['notes'] = ncheck['Notes']
checks.push(check)
end
meta = snode.service_meta
node_meta = snode.node_meta
node_name = snode['Node']['Node']
all_nodes[node_name] = { meta: node_meta } if node_name
server = { frontend_id: "backend_http__#{service_name}",
id: snode['Service']['ID'],
name: node_name,
sMeta: meta ? meta : {},
connect: snode['Service']['Connect'],
addr: snode.service_address,
port: snode['Service']['Port'],
tags: snode['Service']['Tags'],
checks: checks,
weights: snode['Service']['Weights'],
}
kind_of_service = snode['Service']['Kind']
service['kind'] = kind_of_service if kind_of_service
service[:instances] << server
end
end
json_datacenters = datacenters.to_a
json = { nodes: all_nodes, services: json_backends, datacenters: json_datacenters, generated_at: Time.now}
%><%= JSON.pretty_generate(json) %>