-
Notifications
You must be signed in to change notification settings - Fork 28
/
consul_template.xml.erb
70 lines (65 loc) · 2.35 KB
/
consul_template.xml.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
<%
require 'rexml/document'
def build_node(e, name, attributes, text = nil)
xe = REXML::Element.new(name)
attributes.each do |name|
xe.add_attribute name, e[name]
end
xe.text = text if text
xe
end
def create_datacenters
xdatacenters = REXML::Element.new 'datacenters'
datacenters.each do |dc|
xdc = REXML::Element.new 'datacenter'
xdc.add_attribute 'name', dc
xdc.add_attribute 'services', services(dc:dc).keys.count
xdc.add_attribute 'nodes', nodes(dc:dc).count
xdatacenters.add_element xdc
end
xdatacenters
end
def create_services
xservices = REXML::Element.new 'services'
xservices.add_attribute 'count', services.count
services.each do |service_name, tags|
xserv = REXML::Element.new 'service'
xserv.add_attribute 'name', service_name
xserv.add_attribute 'tags', tags.sort.join(',')
xnodes = REXML::Element.new 'instances'
xnodes.add_attribute 'count', service(service_name).count
service(service_name).sort {|a,b| a['Node']['Node'] <=> b['Node']['Node'] }.each do |snode|
xnode = REXML::Element.new 'service-instance'
xnode.add_attribute 'id', snode['Node']['ID'] if snode['Node']['ID']
xnode.add_attribute 'address', snode.service_address
xnode.add_attribute 'name', snode['Node']['Name']
xnode.add_attribute 'port', snode['Service']['Port'].to_i
xnode.add_attribute 'tags', snode['Service']['Tags'].sort.join(',')
xnode.add_attribute 'statuses', (snode['Checks'].map { |c| c['Status'] }.join(' '))
xnodes.add_element xnode
end
xserv.add_element xnodes
xservices.add_element xserv
end
xservices
end
def create_nodes
xnodes = REXML::Element.new 'nodes'
xnodes.add_attribute 'count', nodes.count
nodes.sort {|a,b| a['Node'] <=> b['Node'] }.each do |snode|
xnode = REXML::Element.new 'node'
xnode.add_attribute 'id', snode['ID']
xnode.add_attribute 'address', snode['Address']
xnode.add_attribute 'name', snode['Node']
xnodes.add xnode
end
xnodes
end
doc = REXML::Document.new '<consul-info/>'
doc << REXML::XMLDecl.new(version='1.0', 'UTF-8')
doc.root.add_element create_datacenters
doc.root.add_element create_services
doc.root.add_element create_nodes
result = ''
doc.write(result, 2)
%><%= result %>