consul-templaterb 1.24.1 → 1.25.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +40 -34
- data/TemplateAPI.md +19 -0
- data/bin/consul-templaterb +3 -0
- data/lib/consul/async/consul_template.rb +46 -0
- data/lib/consul/async/version.rb +1 -1
- data/samples/consul_template.json.erb +10 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb96ca2831db9d4320c6363153b38f83273d8d4f5f9b1505f581fbf868a825e5
|
4
|
+
data.tar.gz: a5549d23034f13b2cc683694bf2f4e93c339dde7a1b146e1b282a250e4ea1728
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ce7de28e8e8596abfadb913b14a9423782f97920842122ce8fae30d902f48f1c2238d3189107da097f69ce9cf24286c297fe192a8a66cd4ea4411a765f55e65
|
7
|
+
data.tar.gz: fab0163e3b4ba25fdc0ab0a447a74accafdf7f8dff889fabfad4a82baab787a9f7d5f73372e6abe4468be8a247c88f744adb0790250c40f39f013e4a1bc59e38
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
|
3
3
|
## (UNRELEASED)
|
4
4
|
|
5
|
+
## 1.25.0 (February 27, 2020)
|
6
|
+
|
7
|
+
NEW FEATURES:
|
8
|
+
|
9
|
+
* Added support for `agent_members()` aka https://www.consul.io/api/agent.html#list-members
|
10
|
+
* Added [samples/members.json.erb](samples/members.json.erb) demonstrating its usage
|
11
|
+
|
5
12
|
## 1.24.1 (February 19, 2020)
|
6
13
|
|
7
14
|
BUGFIX:
|
data/README.md
CHANGED
@@ -5,30 +5,31 @@
|
|
5
5
|
[![License](https://img.shields.io/badge/license-ApacheV2-yellowgreen.svg)](#license)
|
6
6
|
|
7
7
|
The ruby GEM [consul-templaterb](https://rubygems.org/gems/consul-templaterb)
|
8
|
-
is both a library and an executable that allows
|
8
|
+
is both a library and an executable that allows generating files
|
9
9
|
using data from Consul (Discovery and Key/Value Store) easily using ruby's
|
10
|
-
|
10
|
+
[ERB](https://en.wikipedia.org/wiki/ERuby) templates.
|
11
|
+
It also supports launching programs and babysitting processes
|
11
12
|
when rendering the files, thus notifying programs when data do change.
|
12
13
|
|
13
|
-
It is intended for
|
14
|
+
It is intended for the users accustomed to expressiveness or Ruby templating,
|
14
15
|
allowing for more flexibility and features than Go templates.
|
15
16
|
|
16
|
-
It also allows
|
17
|
+
It also allows using all of the Ruby language, especially useful for generating
|
17
18
|
files in several formats ([JSON](samples/consul_template.json.erb),
|
18
19
|
[XML](samples/consul_template.xml.erb)) for which text substitutions are hard
|
19
20
|
to get right (escaping, attributes encoding...).
|
20
21
|
|
21
|
-
It also focuses on good performance and lightweight usage of bandwidth,
|
22
|
+
It also focuses on providing good performance and lightweight usage of bandwidth,
|
22
23
|
especially for very large clusters and watching lots of services.
|
23
24
|
|
24
|
-
For
|
25
|
+
For the rendering of complex templates in large Consul Clusters, it usually
|
25
26
|
renders faster with a more predictable way the template than the original
|
26
27
|
consul-template.
|
27
28
|
|
28
|
-
It provides a very [simple API](TemplateAPI.md) to write your
|
29
|
+
It provides a very [simple API](TemplateAPI.md) to write your templates
|
29
30
|
with fully [working examples](samples/).
|
30
31
|
|
31
|
-
It also
|
32
|
+
It also allows displaying a very nice and hi-performance HTML5 UI for Consul,
|
32
33
|
see [consul-ui](samples/consul-ui) for details.
|
33
34
|
|
34
35
|
## Video introduction to consul-templaterb
|
@@ -42,19 +43,15 @@ inspired strongly the creation of this GEM and this executable wants
|
|
42
43
|
to achieve better results in some use cases, especially for very large
|
43
44
|
Consul clusters with many nodes and servers.
|
44
45
|
|
45
|
-
consul-template has more features regarding Consul support (for instance, it
|
46
|
-
has support for Hashicorp's Vault), but consul-templaterb focuses on getting
|
47
|
-
more power with the generation of templates and more performance.
|
48
|
-
|
49
46
|
Consul Template uses Go templates which is very limited in its set of
|
50
47
|
features: it is complicated to sort, apply real transformations
|
51
48
|
using code and even interact with the OS (ex: get the current date, format
|
52
49
|
timestamps...).
|
53
50
|
|
54
|
-
The sort feature for instances allow you to create predictable output (i.e: meaning
|
51
|
+
The sort feature for instances allow you to create a predictable output (i.e: meaning
|
55
52
|
that the order of nodes is predictable), thus it might offer better performance
|
56
53
|
since the reload of processes if happening ONLY when the files are binary
|
57
|
-
different. Thus, if using consul-templaterb, you will reload less your
|
54
|
+
different. Thus, if using consul-templaterb, you will reload less your HAProxy or
|
58
55
|
load-balancer than you would do with consul-template.
|
59
56
|
|
60
57
|
Compared to consul-template, consul-templaterb offers the following features:
|
@@ -66,7 +63,7 @@ Compared to consul-template, consul-templaterb offers the following features:
|
|
66
63
|
* Supports all Ruby features (ex: base64, real JSON/XML generation...)
|
67
64
|
* Information about bandwidth
|
68
65
|
|
69
|
-
The executable supports semantics and command
|
66
|
+
The executable supports semantics and command-line flags and options similar to
|
70
67
|
HashiCorp's Consul-template, so many flags you might use in consul-template will
|
71
68
|
work in a similar way. It also supports the same environment variable
|
72
69
|
`CONSUL_HTTP_ADDR` to find the Consul Agent to query and `CONSUL_HTTP_TOKEN` to
|
@@ -96,7 +93,7 @@ $ gem install consul-templaterb
|
|
96
93
|
```
|
97
94
|
|
98
95
|
If you simply want to use the executable on your preferred Linux distribution, you
|
99
|
-
have to install first: ruby and ruby
|
96
|
+
have to install first: `ruby` and `ruby-dev`.
|
100
97
|
|
101
98
|
### Quick install on Ubuntu-Linux
|
102
99
|
|
@@ -110,7 +107,7 @@ You can now use it directly using the binary `consul-templaterb` in your path.
|
|
110
107
|
|
111
108
|
On Windows, a bug exists with Ruby greater than 2.4.
|
112
109
|
|
113
|
-
|
110
|
+
To make it work, use this command as explained in
|
114
111
|
https://github.com/oneclick/rubyinstaller2/issues/96#issuecomment-434619796
|
115
112
|
|
116
113
|
```
|
@@ -119,10 +116,10 @@ gem install eventmachine consul-templaterb --platform ruby
|
|
119
116
|
|
120
117
|
### Run it with Docker
|
121
118
|
|
122
|
-
|
119
|
+
A Docker image is also available https://hub.docker.com/r/discoverycriteo/consul-templaterb
|
123
120
|
and allows to quickly have a working
|
124
121
|
[Consul-UI](https://github.com/criteo/consul-templaterb/blob/master/samples/consul-ui/README.md)
|
125
|
-
that will
|
122
|
+
that will serve the UI to explore your Consul Cluster.
|
126
123
|
|
127
124
|
### Playing with the samples templates
|
128
125
|
|
@@ -145,7 +142,7 @@ Using samples/checks.html output for samples/checks.html.erb
|
|
145
142
|
[...]
|
146
143
|
```
|
147
144
|
|
148
|
-
It will render a full web site you may browse to look in real
|
145
|
+
It will render a full web site you may browse to look in real-time the status of your
|
149
146
|
Consul Cluster.
|
150
147
|
|
151
148
|
You can now have a look to the [API Documentation](TemplateAPI.md) to modify existing
|
@@ -202,10 +199,10 @@ In the same way as consul-template, consul-templaterb supports multiple template
|
|
202
199
|
commands when the files do change. The parameter `--template <ERB>:<DEST>:[reload_command]:params_file` works
|
203
200
|
in the following way:
|
204
201
|
|
205
|
-
* ERB
|
202
|
+
* ERB: the ERB file to use as a template
|
206
203
|
* DEST: the destination file
|
207
204
|
* reload_command: optional shell command executed whenever the file has been modified
|
208
|
-
* params_file: JSON or YAML file to load and to use as parameter for template (see
|
205
|
+
* params_file: JSON or YAML file to load and to use as parameter for the template (see
|
209
206
|
[param() function](TemplateAPI.md#paramparameter_name-default_value-nil) to retrieve
|
210
207
|
the values)
|
211
208
|
|
@@ -219,7 +216,7 @@ $ consul-templaterb \\
|
|
219
216
|
--template "samples/consul_template.erb:consul-summary.txt"
|
220
217
|
```
|
221
218
|
|
222
|
-
### Process management and
|
219
|
+
### Process management and signalization of configuration files
|
223
220
|
|
224
221
|
With the `--exec` argument (can be specified multiple times), consul-templaterb will launch
|
225
222
|
the process specified when all templates have been generated. When generated file(s) do change,
|
@@ -247,7 +244,7 @@ By design, the GEM supports limiting the number of requests per endpoints (see c
|
|
247
244
|
`bin/consul-templaterb` file). It avoids using too much network to fetch data from Consul
|
248
245
|
in large Consul Clusters (especially when watching lots of files).
|
249
246
|
|
250
|
-
The limitation is static for now, but fair dynamic bandwidth allocation will allow
|
247
|
+
The limitation is static for now, but fair dynamic bandwidth allocation will allow limiting
|
251
248
|
the bandwidth used to get information for all services by capping the global bandwidth used
|
252
249
|
by consul-templaterb.
|
253
250
|
|
@@ -260,13 +257,13 @@ examples:
|
|
260
257
|
2. [Show all services in Cluster](samples/consul-ui/consul_services.json.erb)
|
261
258
|
3. [Show all Key/Values nicely](samples/consul-ui/consul_keys.json.erb)
|
262
259
|
4. [Services in XML](samples/consul_template.xml.erb)
|
263
|
-
5. [Services in JSON](samples/consul_template.json.erb)
|
264
|
-
6. [Generate
|
265
|
-
7. [Export Consul Statistics to Prometheus](samples/metrics.erb)
|
266
|
-
datacenters and nodes and export it to
|
260
|
+
5. [Services in JSON or YAML](samples/consul_template.json.erb)
|
261
|
+
6. [Generate HAProxy Configuration](samples/ha_proxy.cfg.erb)
|
262
|
+
7. [Export Consul Statistics to Prometheus](samples/metrics.erb): count all services, their state,
|
263
|
+
datacenters and nodes and export it to Prometheus easily to trigger alerts.
|
267
264
|
8. [List all services/Nodes with their statuses for all datacenters](samples/all_services.txt.erb)
|
268
265
|
9. [Show all services/instances not passing on all DCs](samples/tools/find_all_failing_services.txt.erb)
|
269
|
-
10. [List all
|
266
|
+
10. [List all RubyGems consul versions from remote server JSON](samples/list_ruby_versions_from_rubygems.txt.erb)
|
270
267
|
|
271
268
|
If you want to test it quickly, you might try with (assuming your consul agent is listening on
|
272
269
|
`http://localhost:8500`):
|
@@ -282,10 +279,19 @@ use (website updated automagically when values to change).
|
|
282
279
|
All templates are validated using the Travis CI, so all should be working for your Consul
|
283
280
|
Configuration.
|
284
281
|
|
282
|
+
### Structured text generation (YAML, JSON, XML...)
|
283
|
+
|
284
|
+
Since ERB supports real language features, we recommend you to use Hashes or Arrays in Ruby and
|
285
|
+
at the end to generate the output. It allows changing very quickly from JSON to YAML or XML and
|
286
|
+
avoiding all the pitfalls of structured language serialization (such as escaping attributes or
|
287
|
+
indentation).
|
288
|
+
|
289
|
+
See [Services in JSON or YAML](samples/consul_template.json.erb) to look at a working example.
|
290
|
+
|
285
291
|
## Template development
|
286
292
|
|
287
293
|
Please look at [the template API](TemplateAPI.md) to have a list of all functions you might use for your
|
288
|
-
templates.
|
294
|
+
templates. Don't forget to have a look at the [samples/](samples/) directory to have full working examples.
|
289
295
|
|
290
296
|
## Development
|
291
297
|
|
@@ -325,10 +331,10 @@ Please consult [CHANGELOG.md](CHANGELOG.md) for fixed bugs.
|
|
325
331
|
|
326
332
|
* [x] Hashi's Vault support (EXPERIMENTAL)
|
327
333
|
* [ ] Implement automatic dynamic rate limit
|
328
|
-
* [x] More samples: apache, nginx, full website displaying consul information...
|
329
|
-
* [x] Optimize rendering speed at start-up: an iteration is done
|
334
|
+
* [x] More samples: apache, nginx, a full website displaying consul information...
|
335
|
+
* [x] Optimize rendering speed at start-up: an iteration is done every second by default, but it would be possible to speed
|
330
336
|
up rendering by iterating with higher frequency until the first write of result has been performed.
|
331
|
-
* [ ] Allow
|
337
|
+
* [ ] Allow tuning bandwidth using a simple configuration file (while it should not be necessary for 90% of use-cases)
|
332
338
|
|
333
339
|
## Contributing
|
334
340
|
|
@@ -339,4 +345,4 @@ This project is intended to be a safe, welcoming space for collaboration, and co
|
|
339
345
|
|
340
346
|
## License
|
341
347
|
|
342
|
-
The gem is available as
|
348
|
+
The gem is available as Open-Source under the terms of the Apache v2 license. See [LICENSE.txt](LICENSE.txt) file.
|
data/TemplateAPI.md
CHANGED
@@ -451,6 +451,25 @@ value123 : <%= result.get_decoded('/my/multiple/values/value123') %>
|
|
451
451
|
Since `kv('/my/multiple/values', recurse: true)` will retrieve all values at once, it might be more
|
452
452
|
efficient in some cases than retrieving all values one by one.
|
453
453
|
|
454
|
+
## agent_members(wan: false)
|
455
|
+
|
456
|
+
[Get the Serf information](https://www.consul.io/api/agent.html#list-members) from Consul Agent point of view.
|
457
|
+
This is a list of Serf information containing serf information. This information is not consistent and should be used with care, most of the time, you should prefer `nodes()`.
|
458
|
+
|
459
|
+
If you are using `consul-templaterb` with a Consul server directly, you might use `wan:true` to have the list of all
|
460
|
+
consul servers connected thru WAN (aka all consul servers from all datacenters).
|
461
|
+
|
462
|
+
The returned value is an array containing the following objects containing the following attributes (accessed as Hash elements):
|
463
|
+
* "Name": Name of node
|
464
|
+
* "Addr": IP Address of node as seen in serf
|
465
|
+
* "Port": Serf port
|
466
|
+
* "Tags": Hash of properties, including version, dc, VSN info...
|
467
|
+
* "Status": Serf code from 0 to 5 giving Health information
|
468
|
+
|
469
|
+
Another property is available as `status()`, that translates the Hash property "Status" into something human redable ('none', 'alive', 'leaving', 'left', 'failed').
|
470
|
+
|
471
|
+
See [samples/members.json.erb](samples/members.json.erb) for example of usage.
|
472
|
+
|
454
473
|
## agent_metrics()
|
455
474
|
|
456
475
|
[Get the metrics of Consul Agent](https://www.consul.io/api/agent.html#view-metrics). Since this endpoint does
|
data/bin/consul-templaterb
CHANGED
@@ -74,6 +74,9 @@ options = {
|
|
74
74
|
'/v1/agent/metrics': {
|
75
75
|
min_duration: 60 # Refresh metrics only minute max
|
76
76
|
},
|
77
|
+
'/v1/agent/members': {
|
78
|
+
min_duration: 60 # Refresh self info every minute max
|
79
|
+
},
|
77
80
|
'/v1/agent/self': {
|
78
81
|
min_duration: 60 # Refresh self info every minute max
|
79
82
|
}
|
@@ -189,6 +189,15 @@ module Consul
|
|
189
189
|
create_if_missing(path, query_params) { ConsulAgentMetrics.new(ConsulEndpoint.new(consul_conf, path, true, query_params, default_value)) }
|
190
190
|
end
|
191
191
|
|
192
|
+
# https://www.consul.io/api/agent.html#list-members
|
193
|
+
def agent_members(wan: false)
|
194
|
+
path = '/v1/agent/members'
|
195
|
+
query_params = {}
|
196
|
+
query_params['wan'] = true if wan
|
197
|
+
default_value = '[]'
|
198
|
+
create_if_missing(path, query_params) { ConsulTemplateMembers.new(ConsulEndpoint.new(consul_conf, path, true, query_params, default_value)) }
|
199
|
+
end
|
200
|
+
|
192
201
|
# Return a param of template
|
193
202
|
def param(key, default_value = nil)
|
194
203
|
v = @context[:params][key]
|
@@ -598,6 +607,43 @@ module Consul
|
|
598
607
|
end
|
599
608
|
end
|
600
609
|
|
610
|
+
# The ServiceInstance has shortcuts (such as service_address method), but is
|
611
|
+
# basically a Hash.
|
612
|
+
class SerfMember < Hash
|
613
|
+
def initialize(obj)
|
614
|
+
merge!(obj)
|
615
|
+
end
|
616
|
+
|
617
|
+
# List the possible Serf statuses as text, indexed by self['Status']
|
618
|
+
def serf_statuses
|
619
|
+
%w[none alive leaving left failed].freeze
|
620
|
+
end
|
621
|
+
|
622
|
+
# Return status as text
|
623
|
+
def status
|
624
|
+
serf_statuses[self['Status']] || "unknownStatus:#{self['Status']}"
|
625
|
+
end
|
626
|
+
end
|
627
|
+
|
628
|
+
# List of serf members of the whole cluster
|
629
|
+
class ConsulTemplateMembers < ConsulTemplateAbstractArray
|
630
|
+
def initialize(consul_endpoint)
|
631
|
+
super(consul_endpoint)
|
632
|
+
end
|
633
|
+
|
634
|
+
def result_delegate
|
635
|
+
return @cached_result if @cached_json == result.json
|
636
|
+
|
637
|
+
new_res = []
|
638
|
+
result.json.each do |v|
|
639
|
+
new_res << SerfMember.new(v)
|
640
|
+
end
|
641
|
+
@cached_result = new_res
|
642
|
+
@cached_json = result.json
|
643
|
+
new_res
|
644
|
+
end
|
645
|
+
end
|
646
|
+
|
601
647
|
# Key/Values representations
|
602
648
|
# This is an array as it might contain several values
|
603
649
|
# Several helpers exist to handle nicely transformations
|
data/lib/consul/async/version.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
<%
|
2
2
|
# This template can be configure the following way with environment variables
|
3
3
|
# Environment variables to filter services/instances
|
4
|
+
# OUTPUT_FORMAT: `JSON | YAML` to output to YAML or JSON
|
4
5
|
# SERVICES_TAG_FILTER: basic tag filter for service (default HTTP)
|
5
6
|
# INSTANCE_MUST_TAG: Second level of filtering (optional, default to SERVICES_TAG_FILTER)
|
6
7
|
# INSTANCE_EXCLUDE_TAG: Exclude instances having the given tag (default: canary)
|
@@ -15,20 +16,6 @@
|
|
15
16
|
services_blacklist = services_blacklist_raw.map { |v| Regexp.new(v) } # Compute the health of a Service
|
16
17
|
|
17
18
|
# Compute the health of a Service
|
18
|
-
def compute_attributes(snode)
|
19
|
-
w = 100
|
20
|
-
snode['Service']['Tags'].each do |tag|
|
21
|
-
match = /^weight-([1-9][0-9])*$/.match(tag)
|
22
|
-
w = match[1].to_i if match
|
23
|
-
end
|
24
|
-
attributes = ""
|
25
|
-
node_status = snode.status
|
26
|
-
attributes = "#{attributes} disabled" if node_status == 'critical'
|
27
|
-
if node_status == 'warning'
|
28
|
-
w = w / 8
|
29
|
-
end
|
30
|
-
attributes = "#{attributes} weight #{w}" if w.positive?
|
31
|
-
end
|
32
19
|
backends = {}
|
33
20
|
services(tag: service_tag_filter).each do |service_name, tags|
|
34
21
|
if !services_blacklist.any? {|r| r.match(service_name)} && tags.include?(instance_must_tag)
|
@@ -58,9 +45,17 @@
|
|
58
45
|
addr: snode.service_address,
|
59
46
|
port: snode['Service']['Port'],
|
60
47
|
tags: snode['Service']['Tags'],
|
48
|
+
weights:snode.weights,
|
61
49
|
}
|
62
50
|
service[:instances] << server
|
63
51
|
end
|
64
52
|
end
|
65
53
|
json = { services: json_backends}
|
66
|
-
%><%= JSON.
|
54
|
+
%><%= case (ENV['OUTPUT_FORMAT'] || 'JSON').upcase
|
55
|
+
when 'JSON'
|
56
|
+
JSON.pretty_generate(json)
|
57
|
+
when 'YAML', 'YML'
|
58
|
+
YAML.dump(json)
|
59
|
+
else
|
60
|
+
raise "Cannot serialize to #{ENV['OUTPUT_FORMAT']}"
|
61
|
+
end %>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consul-templaterb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.25.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- SRE Core Services
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: em-http-request
|