consul-templaterb 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5b4bfc2f950af28c0c5db345700d15cdf01076cab18efea784e6ad6d1329e81
4
- data.tar.gz: 725e4f3abcae9d3891b6956547dbb94a1c35beb3c810a34d2e8ade1f59e6f47c
3
+ metadata.gz: d6f546cec480feae29069b3458138d65bdeacc686b5a0e2a0e6762a306ee7cbe
4
+ data.tar.gz: edad0a361f2bfc627994e05ea590cb5c1f62c81a1f25aeaaad9c150110e0ed5c
5
5
  SHA512:
6
- metadata.gz: 10f434a98600c5d2613fd6cfc1c195ed2344a2f9c4929d8fe8625815b9cd9ec086452e10cfbb55d6be9645fc81bd967d9556f40da7e408adb2d52089b260da7a
7
- data.tar.gz: c2ba837aced1ca0db90480b4deb91dffafa0fa5f621ac9f9161e557d25e7ad580fee23f6bbd04fbe2759d2df9f7c75578725d4d50389ab71fd19c246e84dc13b
6
+ metadata.gz: d4882996d68193413e3b9b2cacdb182c14cd6c952b6a53397488689d140e90ce0b6f7cefd898028d85e0cb22114c9a53b1c6333fd06aaa50d73dcc9158560836
7
+ data.tar.gz: 8cbeeda5ae647a91fe9744a5240c0113f216c29613e802f58ec8ffcbb5b49d632192573fe0e4fd1dc357c498d48c81935d38f3f58886bd72c0d37270100efa7f
data/.rubocop.yml CHANGED
@@ -13,7 +13,7 @@ Metrics/BlockNesting:
13
13
  Max: 4
14
14
 
15
15
  Metrics/ClassLength:
16
- Max: 200
16
+ Max: 225
17
17
 
18
18
  Metrics/CyclomaticComplexity:
19
19
  Max: 15
data/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## (UNRELEASED)
4
4
 
5
+ ## 1.8.0 (December 11, 2018)
6
+
7
+ NEW FEATURES:
8
+
9
+ * Added new function `template_info` to get information about the file being rendered and whether
10
+ the first rendering phase is completed.
11
+
5
12
  ## 1.7.0 (December 7, 2018)
6
13
 
7
14
  OPTIMIZATIONS:
data/README.md CHANGED
@@ -290,8 +290,8 @@ Please consult [CHANGELOG.md](CHANGELOG.md) for fixed bugs.
290
290
 
291
291
  * [x] Hashi's Vault support (EXPERIMENTAL)
292
292
  * [ ] Implement automatic dynamic rate limit
293
- * [ ] More samples: apache, nginx, full website displaying consul information...
294
- * [ ] Optimize rendering speed at start-up: an iteration is done very second by default, but it would be possible to speed
293
+ * [x] More samples: apache, nginx, full website displaying consul information...
294
+ * [x] Optimize rendering speed at start-up: an iteration is done very second by default, but it would be possible to speed
295
295
  up rendering by iterating with higher frequency until the first write of result has been performed.
296
296
  * [ ] Allow to tune bandwidth using a simple configuration file (while it should not be necessary for 90% of use-cases)
297
297
 
data/TemplateAPI.md CHANGED
@@ -377,3 +377,48 @@ secret('secret/foo', [force_ttl: intInSecond])
377
377
 
378
378
  </div>
379
379
  </details>
380
+
381
+ ## template_info()
382
+
383
+ It returns information about current template being rendered.
384
+ The information returned has the following structure:
385
+
386
+ ```json
387
+ {
388
+ 'destination': destination_file_for_root_template
389
+ 'source': absolute_path_to_template_file.erb
390
+ 'source_root': absolute_path_to_root_template.erb
391
+ 'was_rendered_once': true|false
392
+ }
393
+ ```
394
+
395
+ - `destination`: absolute path where the file is gonna be written
396
+ - `source`: the template absolute path file, most of the time, if will be
397
+ equal to `source_root`, except when the current template is included
398
+ in another template using `render_file()`
399
+ - `source_root`: the root template absolute file.
400
+ - `was_rendered_once` true if whole template structure has been rendered
401
+ at least once, false if current template data is still incomplete.
402
+
403
+ <details><summary>Examples</summary>
404
+ <div class="samples">
405
+
406
+ ### Display template info
407
+
408
+ ```erb
409
+ I am the file <%= File.basename(template_info['source']) %><%
410
+ if template_info['source'] != template_info['source_root']
411
+ %> included from template <%= File.basename(template_info['source_root']) %><%
412
+ end
413
+ %> rendered as <%= File.basename(template_info['destination']) %>
414
+ ```
415
+
416
+ The command `consul-template template_info.erb` would render:
417
+ `I am the file template_info.erb rendered as template_info`
418
+
419
+ If creating a file called include.erb with contents: `<%= render_file('template_info.erb') %>`,
420
+ the command `consul-templaterb --template include.erb:destination_file.txt` would render:
421
+ `I am the file template_info.erb included from template include.erb rendered as destination_file.txt`
422
+
423
+ </div>
424
+ </details>
@@ -36,6 +36,12 @@ module Consul
36
36
  }
37
37
  @context = {
38
38
  current_erb_path: nil,
39
+ template_info: {
40
+ 'source_root' => nil,
41
+ 'source' => nil,
42
+ 'destination' => nil,
43
+ 'was_rendered_once' => false
44
+ },
39
45
  params: {}
40
46
  }
41
47
 
@@ -104,6 +110,11 @@ module Consul
104
110
  v
105
111
  end
106
112
 
113
+ # Get information about current template
114
+ def template_info
115
+ @context[:template_info]
116
+ end
117
+
107
118
  # https://www.consul.io/api/catalog.html#list-services
108
119
  def services(dc: nil, tag: nil)
109
120
  path = '/v1/catalog/services'
@@ -153,7 +164,7 @@ module Consul
153
164
  def render_file(path, params = {})
154
165
  new_path = File.expand_path(path, File.dirname(@context[:current_erb_path]))
155
166
  raise "render_file ERROR: #{path} is resolved as #{new_path}, but the file does not exists" unless File.exist? new_path
156
- render(File.read(new_path), new_path, params)
167
+ render(File.read(new_path), new_path, params, current_template_info: template_info)
157
168
  end
158
169
 
159
170
  def find_line(e)
@@ -164,12 +175,14 @@ module Consul
164
175
  nil
165
176
  end
166
177
 
167
- def render(tpl, tpl_file_path, params = {})
178
+ def render(tpl, tpl_file_path, params = {}, current_template_info: nil)
168
179
  # Ugly, but allow to use render_file well to support stack of calls
169
180
  old_value = @context
181
+ tpl_info = current_template_info.merge('source' => tpl_file_path.freeze)
170
182
  @context = {
171
183
  current_erb_path: tpl_file_path,
172
- params: params
184
+ params: params,
185
+ template_info: tpl_info
173
186
  }
174
187
  result = ERB.new(tpl, nil, @trim_mode).result(binding)
175
188
  @context = old_value
@@ -182,8 +195,8 @@ module Consul
182
195
  raise e2, "[TEMPLATE SYNTAX ERROR] #{tpl_file_path}#{find_line(e)}: #{e.message}"
183
196
  end
184
197
 
185
- def write(file, tpl, last_result, tpl_file_path, params = {})
186
- data = render(tpl, tpl_file_path, params)
198
+ def write(file, tpl, last_result, tpl_file_path, params = {}, current_template_info: {})
199
+ data = render(tpl, tpl_file_path, params, current_template_info: current_template_info)
187
200
  not_ready = []
188
201
  ready = 0
189
202
  @iteration = Time.now.utc - @start_time
@@ -30,10 +30,11 @@ module Consul
30
30
  @last_result = File.read(output_file) if File.exist? output_file
31
31
  @template = load_template
32
32
  @params = params
33
+ @was_rendered_once = false
33
34
  end
34
35
 
35
- def render(tpl = @template)
36
- @template_manager.render(tpl, template_file, params)
36
+ def render(tpl = @template, current_template_info: _build_default_template_info)
37
+ @template_manager.render(tpl, template_file, params, current_template_info: current_template_info)
37
38
  end
38
39
 
39
40
  def run
@@ -50,17 +51,28 @@ module Consul
50
51
  end
51
52
 
52
53
  # Will throw Consul::Async::InvalidTemplateException if template invalid
53
- def update_template(new_template)
54
+ def update_template(new_template, current_template_info)
54
55
  return false unless new_template != @template
55
56
  # We render to ensure the template is valid
56
- render(new_template)
57
+ render(new_template, current_template_info)
57
58
  @template = new_template.freeze
58
59
  true
59
60
  end
60
61
 
62
+ def _build_default_template_info
63
+ {
64
+ 'destination' => @output_file,
65
+ 'source_root' => template_file.freeze,
66
+ 'source' => template_file.freeze,
67
+ 'was_rendered_once' => @was_rendered_once
68
+ }
69
+ end
70
+
61
71
  def write
62
- success, modified, last_res = @template_manager.write(@output_file, @template, @last_result, template_file, params)
72
+ current_template_info = _build_default_template_info
73
+ success, modified, last_res = @template_manager.write(@output_file, @template, @last_result, template_file, params, current_template_info: current_template_info)
63
74
  @last_result = last_res if last_res
75
+ @was_rendered_once = success unless @was_rendered_once
64
76
  [success, modified, @last_result]
65
77
  end
66
78
 
@@ -68,8 +80,11 @@ module Consul
68
80
  new_time = File.ctime(template_file)
69
81
  if template_file_ctime != new_time
70
82
  begin
83
+ current_template_info = {}
84
+ current_template_info.!merge(current_template_info)
85
+ current_template_info['ready'] = false
71
86
  @template_file_ctime = new_time
72
- return update_template(load_template)
87
+ return update_template(load_template, current_template_info: current_template_info)
73
88
  rescue Consul::Async::InvalidTemplateException => e
74
89
  STDERR.puts "****\n[ERROR] HOT Reload of template #{template_file} did fail due to:\n #{e}\n****\n"
75
90
  raise e unless hot_reload_failure == 'keep'
@@ -1,5 +1,5 @@
1
1
  module Consul
2
2
  module Async
3
- VERSION = '1.7.0'.freeze
3
+ VERSION = '1.8.0'.freeze
4
4
  end
5
5
  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.7.0
4
+ version: 1.8.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: 2018-12-07 00:00:00.000000000 Z
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: em-http-request