consul-template-generator 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/consul-template-generator +10 -9
- data/lib/consul/template/generator.rb +1 -5
- data/lib/consul/template/generator/cmd.rb +12 -8
- data/lib/consul/template/generator/configuration.rb +6 -6
- data/lib/consul/template/generator/ct.rb +2 -2
- data/lib/consul/template/generator/key_value.rb +5 -5
- data/lib/consul/template/generator/run.rb +6 -6
- data/lib/consul/template/generator/version.rb +1 -1
- data/spec/ct_spec.rb +2 -3
- data/spec/init_spec.rb +1 -2
- data/spec/key_value_spec.rb +12 -12
- data/spec/run_spec.rb +12 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11a123a605164b3bf85c7d874ae9d731db56cb99
|
4
|
+
data.tar.gz: 9091f83f039d537c0a1b13fac6a99a7546247881
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4330d15eb334469bad07257f6e66eaaf29dd9f787a652006e9afd9116e714a9d45072c4dce4fbf3fb80d142012f69e0571ac764f349cb39798ad0ae65ff9ca53
|
7
|
+
data.tar.gz: 33406c87edc31d2b4534104bcbbe2a5378690346ca834cadbec809797ab3ad5f7792e9650ff7903c6a00ddf0e0f37c29db140c9506fe496a009c7e1dd51f3d0b
|
@@ -5,7 +5,7 @@ require 'consul/template/generator/cmd'
|
|
5
5
|
include Consul::Template::Generator
|
6
6
|
|
7
7
|
def verify_opts(opts)
|
8
|
-
!opts[:
|
8
|
+
!(opts[:templates].empty? || opts[:templates].any? { |k,v| v.nil? })
|
9
9
|
end
|
10
10
|
|
11
11
|
|
@@ -26,14 +26,14 @@ EOC
|
|
26
26
|
options[:consul] = h
|
27
27
|
end
|
28
28
|
|
29
|
-
options[:
|
30
|
-
opts.on('-t
|
31
|
-
options[:
|
29
|
+
options[:templates] = {}
|
30
|
+
opts.on('-t TEMPLATES', '--templates TEMPLATES', 'Comma separated list of consul-template ctmpl file and keys to monitor, tmple.ctmpl:tmple-key,tmple2.ctmpl:templ-key2 (required)') do |t|
|
31
|
+
options[:templates] = Hash[t.split(',').collect { |v| v.split(':') } ]
|
32
32
|
end
|
33
33
|
|
34
|
-
options[:
|
35
|
-
opts.on('-
|
36
|
-
options[:
|
34
|
+
options[:session_key] = 'consul-template-generator'
|
35
|
+
opts.on('-s SESSION_KEY', '--session-key SESSION_KEY', 'Key used to lock template generation session [default: consul-template-generator') do |l|
|
36
|
+
options[:session_key] = l
|
37
37
|
end
|
38
38
|
|
39
39
|
options[:proxy] = nil
|
@@ -64,8 +64,9 @@ end
|
|
64
64
|
|
65
65
|
opt_parser.parse!
|
66
66
|
|
67
|
+
|
67
68
|
unless verify_opts(options)
|
68
|
-
STDOUT.puts "
|
69
|
+
STDOUT.puts "'--templates' must be provided with <template>:<key> pairs\n\n"
|
69
70
|
puts opt_parser
|
70
71
|
exit(1)
|
71
72
|
end
|
@@ -74,7 +75,7 @@ if options[:unset_proxy]
|
|
74
75
|
ENV['http_proxy'] = nil
|
75
76
|
end
|
76
77
|
|
77
|
-
CMD.configure(options[:consul], options[:
|
78
|
+
CMD.configure(options[:consul], options[:templates], options[:session_key], options[:log_level], options[:proxy])
|
78
79
|
|
79
80
|
ec = 1
|
80
81
|
cmd = ARGV[0]
|
@@ -31,14 +31,10 @@ module Consul
|
|
31
31
|
self.config.consul_template_binary = ct_binary
|
32
32
|
end
|
33
33
|
|
34
|
-
if self.config.
|
34
|
+
if self.config.templates.empty? || self.config.templates.any? { |k,v| v.nil? }
|
35
35
|
raise "template must be defined in configuration"
|
36
36
|
end
|
37
37
|
|
38
|
-
if self.config.template_key.nil?
|
39
|
-
raise "template_key must be defined in configuration"
|
40
|
-
end
|
41
|
-
|
42
38
|
Diplomat.configure do |config|
|
43
39
|
config.url = "http://#{self.config.consul_host}"
|
44
40
|
config.options = self.config.client_options
|
@@ -6,11 +6,11 @@ module Consul
|
|
6
6
|
module CMD
|
7
7
|
include Consul::Template::Generator
|
8
8
|
|
9
|
-
def self.configure(consul_host,
|
9
|
+
def self.configure(consul_host, templates, session_key, log_level, proxy = nil)
|
10
10
|
Consul::Template::Generator.configure do |config|
|
11
11
|
config.log_level = log_level
|
12
|
-
config.
|
13
|
-
config.
|
12
|
+
config.templates = templates
|
13
|
+
config.session_key = session_key
|
14
14
|
config.consul_host = consul_host
|
15
15
|
end
|
16
16
|
end
|
@@ -19,14 +19,16 @@ module Consul
|
|
19
19
|
cycle_sleep ||= 0.5
|
20
20
|
lock_sleep ||= 1.0
|
21
21
|
config = Consul::Template::Generator.config
|
22
|
-
|
22
|
+
uploaded_hashes = {}
|
23
23
|
begin
|
24
24
|
runner = CTRunner.new
|
25
25
|
runner.acquire_session_lock do
|
26
26
|
config.logger.info "Session lock acquired..."
|
27
27
|
begin
|
28
|
-
|
29
|
-
|
28
|
+
config.templates.each do |template,template_key|
|
29
|
+
uploaded_hashes[template] = runner.run(template, template_key, uploaded_hashes[template]) || uploaded_hashes[template]
|
30
|
+
sleep cycle_sleep
|
31
|
+
end
|
30
32
|
rescue Interrupt
|
31
33
|
raise # Re-raise to break this rescue block
|
32
34
|
rescue ConsulSessionExpired
|
@@ -60,8 +62,10 @@ module Consul
|
|
60
62
|
def self.run_once
|
61
63
|
config = Consul::Template::Generator.config
|
62
64
|
begin
|
63
|
-
|
64
|
-
|
65
|
+
config.templates.each do |template,template_key|
|
66
|
+
runner = CTRunner.new
|
67
|
+
result = runner.run template, template_key
|
68
|
+
end
|
65
69
|
rescue Exception => e
|
66
70
|
config.logger.error "An unexpected error occurred, unable to process template: #{e.message}"
|
67
71
|
1
|
@@ -39,25 +39,25 @@ module Consul
|
|
39
39
|
end
|
40
40
|
|
41
41
|
class Configuration
|
42
|
-
attr_accessor :
|
42
|
+
attr_accessor :templates, :session_key, :consul_template_binary, :logger, :log_level
|
43
43
|
attr_accessor :consul_host, :node, :client_options
|
44
44
|
|
45
45
|
def initialize
|
46
46
|
@log_level = :debug
|
47
47
|
@node = nil
|
48
48
|
@consul_host = nil
|
49
|
-
@
|
50
|
-
@
|
49
|
+
@templates = {}
|
50
|
+
@session_key = 'consul-template-generator'
|
51
51
|
@client_options = {}
|
52
52
|
@logger = Consul::Template::Generator::STDLogger
|
53
53
|
end
|
54
54
|
|
55
|
-
def lock_key
|
56
|
-
"/lock/#{
|
55
|
+
def lock_key(key)
|
56
|
+
"/lock/#{key.sub(/^\//, '')}"
|
57
57
|
end
|
58
58
|
|
59
59
|
def session_lock_key
|
60
|
-
|
60
|
+
@session_key
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
@@ -5,9 +5,9 @@ module Consul
|
|
5
5
|
module Template
|
6
6
|
module Generator
|
7
7
|
class CTRunner
|
8
|
-
def render_template
|
8
|
+
def render_template(template)
|
9
9
|
body = nil
|
10
|
-
cmd = %{#{@config.consul_template_binary} -dry -once -template #{
|
10
|
+
cmd = %{#{@config.consul_template_binary} -dry -once -template #{template}}
|
11
11
|
procs = ::Open4.popen4(*cmd) do |pid, stdin, stdout, stderr|
|
12
12
|
body = stdout.read.strip
|
13
13
|
# consul-template -dry inserts '> \n' at the top of stdout, remove it
|
@@ -5,8 +5,8 @@ module Consul
|
|
5
5
|
module Generator
|
6
6
|
class CTRunner
|
7
7
|
|
8
|
-
def acquire_lock(
|
9
|
-
lock_key
|
8
|
+
def acquire_lock(target_key)
|
9
|
+
lock_key = @config.lock_key target_key
|
10
10
|
@config.logger.debug "Attempting to acquire lock on key: #{lock_key}"
|
11
11
|
Consul::Template::Generator.renew_session @session
|
12
12
|
unless Diplomat::Lock.acquire(lock_key, @session)
|
@@ -27,10 +27,10 @@ module Consul
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
def upload_template(raw_template)
|
31
|
-
@config.logger.info "Uploading key: #{
|
30
|
+
def upload_template(template_key, raw_template)
|
31
|
+
@config.logger.info "Uploading key: #{template_key}"
|
32
32
|
begin
|
33
|
-
Diplomat::Kv.put(
|
33
|
+
Diplomat::Kv.put(template_key, raw_template)
|
34
34
|
rescue Exception => e
|
35
35
|
raise TemplateUploadError, "Encountered an unexpected error while uploading template: #{e.message}"
|
36
36
|
end
|
@@ -4,11 +4,11 @@ module Consul
|
|
4
4
|
module Template
|
5
5
|
module Generator
|
6
6
|
class CTRunner
|
7
|
-
def run(comp_hash = nil)
|
7
|
+
def run(template, template_key, comp_hash = nil)
|
8
8
|
status, body, hash, uploaded_hash = nil, nil, nil, nil
|
9
|
-
acquire_lock do
|
10
|
-
@config.logger.debug "Attempting to render template: #{
|
11
|
-
status, body, hash = render_template
|
9
|
+
acquire_lock template_key do
|
10
|
+
@config.logger.debug "Attempting to render template: #{template}"
|
11
|
+
status, body, hash = render_template template
|
12
12
|
unless status == 0
|
13
13
|
raise TemplateRenderError, "consul-template exited with on-zero exit status"
|
14
14
|
end
|
@@ -17,9 +17,9 @@ module Consul
|
|
17
17
|
end
|
18
18
|
@config.logger.debug "Template rendered..."
|
19
19
|
if comp_hash.nil? || comp_hash != hash
|
20
|
-
@config.logger.info "Change in template discovered, attempting to upload to key #{
|
20
|
+
@config.logger.info "Change in template discovered, attempting to upload to key #{template_key}"
|
21
21
|
@config.logger.debug "Existing hash: #{comp_hash || 'nil'}, new hash: #{hash}"
|
22
|
-
uploaded = upload_template(body)
|
22
|
+
uploaded = upload_template(template_key, body)
|
23
23
|
if uploaded
|
24
24
|
@config.logger.info "New template uploaded..."
|
25
25
|
uploaded_hash = hash
|
data/spec/ct_spec.rb
CHANGED
@@ -24,8 +24,7 @@ end
|
|
24
24
|
describe 'Consul::Template::Generator::CTRunner' '#render_template' do
|
25
25
|
before do
|
26
26
|
Consul::Template::Generator.configure do |config|
|
27
|
-
config.
|
28
|
-
config.template_key = '/test-template'
|
27
|
+
config.templates = {'/etc/test-template.ctmpl' => '/test-template'}
|
29
28
|
config.consul_template_binary = 'consul-template'
|
30
29
|
end
|
31
30
|
end
|
@@ -36,7 +35,7 @@ describe 'Consul::Template::Generator::CTRunner' '#render_template' do
|
|
36
35
|
exp_hash = '54b0c58c7ce9f2a8b551351102ee0938'
|
37
36
|
setup_open4(exp_out, 0, 101)
|
38
37
|
runner = CTRunner.new('test-session')
|
39
|
-
exit_code, body, hash = runner.render_template
|
38
|
+
exit_code, body, hash = runner.render_template Consul::Template::Generator.config.templates['/etc/test-template.ctmpl']
|
40
39
|
expect(body).to eql(exp_out)
|
41
40
|
expect(hash).to eql(exp_hash)
|
42
41
|
expect(exit_code).to eql(@status)
|
data/spec/init_spec.rb
CHANGED
@@ -8,8 +8,7 @@ include Consul::Template::Generator
|
|
8
8
|
describe 'Consul::Template::Generator::CTRunner' '#initialize' do
|
9
9
|
before(:each) do
|
10
10
|
Consul::Template::Generator.configure do |config|
|
11
|
-
config.
|
12
|
-
config.template_key = 'test-session-template'
|
11
|
+
config.templates = {'test-session-template.ctmpl' => 'test-session-template' }
|
13
12
|
config.node = 'test-node'
|
14
13
|
config.log_level = :off
|
15
14
|
config.consul_template_binary = 'consul-template'
|
data/spec/key_value_spec.rb
CHANGED
@@ -10,18 +10,18 @@ include Consul::Template::Generator
|
|
10
10
|
describe 'Consul::Template::Generator::CTRunner' '#acquire_lock' do
|
11
11
|
before do
|
12
12
|
Consul::Template::Generator.configure do |config|
|
13
|
-
config.
|
14
|
-
config.template_key = '/test-template'
|
13
|
+
config.templates = { 'test-template.ctmpl' => '/test-template' }
|
15
14
|
config.consul_host = '127.0.0.1:8500'
|
16
15
|
config.consul_template_binary = 'consul-template'
|
17
16
|
config.log_level = :off
|
18
17
|
end
|
18
|
+
@config = Consul::Template::Generator.config
|
19
19
|
end
|
20
20
|
|
21
21
|
context 'acquires lock' do
|
22
22
|
it 'clean lock acquisition' do
|
23
23
|
runner = CTRunner.new('test-session')
|
24
|
-
runner.acquire_lock do
|
24
|
+
runner.acquire_lock @config.templates['test-template.ctmpl'] do
|
25
25
|
expect(true).to be_truthy ## This is simply to assert we are able to execute code in the block
|
26
26
|
end
|
27
27
|
expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/kv/lock/test-template').with(:query => {:acquire => 'test-session'})
|
@@ -30,7 +30,7 @@ describe 'Consul::Template::Generator::CTRunner' '#acquire_lock' do
|
|
30
30
|
|
31
31
|
it 'handles being unable to acquire lock' do
|
32
32
|
runner = CTRunner.new('test-session-lock-fail')
|
33
|
-
expect { runner.acquire_lock { puts 'hi' } }.to raise_error(KeyNotLockedError)
|
33
|
+
expect { runner.acquire_lock @config.templates['test-template.ctmpl'] { puts 'hi' } }.to raise_error(KeyNotLockedError)
|
34
34
|
expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/kv/lock/test-template').with(:query => {:acquire => 'test-session-lock-fail'})
|
35
35
|
expect(WebMock).to_not have_requested(:put, 'http://127.0.0.1:8500/v1/kv/lock/test-template').with(:query => {:release => 'test-session-lock-fail'})
|
36
36
|
end
|
@@ -40,8 +40,8 @@ end
|
|
40
40
|
describe 'Consul::Template::Generator::CTRunner' '#acquire_session_lock' do
|
41
41
|
before do
|
42
42
|
Consul::Template::Generator.configure do |config|
|
43
|
-
config.
|
44
|
-
config.
|
43
|
+
config.templates = { 'test-template.ctmpl' => '/test-template' }
|
44
|
+
config.session_key = '/session/test-template'
|
45
45
|
config.consul_host = '127.0.0.1:8500'
|
46
46
|
config.consul_template_binary = 'consul-template'
|
47
47
|
config.log_level = :off
|
@@ -71,17 +71,17 @@ describe 'Consul::Template::Generator::CTRunner' '#upload_template' do
|
|
71
71
|
context 'uploads template' do
|
72
72
|
before do
|
73
73
|
Consul::Template::Generator.configure do |config|
|
74
|
-
config.
|
75
|
-
config.template_key = '/test-template'
|
74
|
+
config.templates = { 'test-template.ctmpl' => '/test-template' }
|
76
75
|
config.consul_host = '127.0.0.1:8500'
|
77
76
|
config.consul_template_binary = 'consul-template'
|
78
77
|
config.log_level = :off
|
79
78
|
end
|
79
|
+
@config = Consul::Template::Generator.config
|
80
80
|
end
|
81
81
|
|
82
82
|
it 'does a clean upload' do
|
83
83
|
runner = CTRunner.new('test-session')
|
84
|
-
success = runner.upload_template('this is a test')
|
84
|
+
success = runner.upload_template(@config.templates['test-template.ctmpl'], 'this is a test')
|
85
85
|
expect(success).to be_truthy
|
86
86
|
expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/kv/test-template').with(:body => 'this is a test')
|
87
87
|
end
|
@@ -90,17 +90,17 @@ describe 'Consul::Template::Generator::CTRunner' '#upload_template' do
|
|
90
90
|
context 'handles template upload failure' do
|
91
91
|
before do
|
92
92
|
Consul::Template::Generator.configure do |config|
|
93
|
-
config.
|
94
|
-
config.template_key = '/test-template-failure'
|
93
|
+
config.templates = { 'test-template.ctmpl' => '/test-template-failure' }
|
95
94
|
config.consul_host = '127.0.0.1:8500'
|
96
95
|
config.consul_template_binary = 'consul-template'
|
97
96
|
config.log_level = :off
|
98
97
|
end
|
98
|
+
@config = Consul::Template::Generator.config
|
99
99
|
end
|
100
100
|
|
101
101
|
it 'does a clean upload' do
|
102
102
|
runner = CTRunner.new('test-session')
|
103
|
-
expect { runner.upload_template('this is a fail test') }.to raise_error(TemplateUploadError)
|
103
|
+
expect { runner.upload_template(@config.templates['test-template.ctmpl'], 'this is a fail test') }.to raise_error(TemplateUploadError)
|
104
104
|
expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/kv/test-template-failure').with(:body => 'this is a fail test')
|
105
105
|
end
|
106
106
|
end
|
data/spec/run_spec.rb
CHANGED
@@ -13,37 +13,37 @@ describe 'Consul::Template::Generator::CTRunner' '#run' do
|
|
13
13
|
context 'run' do
|
14
14
|
it 'handles a clean run' do
|
15
15
|
exp_hash = 'bbf9afe7431caf5f89a608bc31e8d822'
|
16
|
-
expect(@runner).to receive(:render_template).with(
|
17
|
-
expect(@runner).to receive(:upload_template).with('test body').and_return(true)
|
18
|
-
hash = @runner.run
|
16
|
+
expect(@runner).to receive(:render_template).with('test-template').and_return([0, 'test body', exp_hash])
|
17
|
+
expect(@runner).to receive(:upload_template).with('/test-template', 'test body').and_return(true)
|
18
|
+
hash = @runner.run 'test-template', '/test-template'
|
19
19
|
expect(hash).to eql(exp_hash)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "doesn't upload unchanged template" do
|
23
23
|
exp_hash = 'bbf9afe7431caf5f89a608bc31e8d822'
|
24
|
-
expect(@runner).to receive(:render_template).with(
|
24
|
+
expect(@runner).to receive(:render_template).with('test-template').and_return([0, 'test body', exp_hash])
|
25
25
|
expect(@runner).not_to receive(:upload_template)
|
26
|
-
hash = @runner.run exp_hash
|
26
|
+
hash = @runner.run 'test-template', '/test-template', exp_hash
|
27
27
|
expect(hash).to be_nil
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'handles non-zero exit status' do
|
31
|
-
expect(@runner).to receive(:render_template).with(
|
31
|
+
expect(@runner).to receive(:render_template).with('test-template').and_return([1, 'not used', 'not used'])
|
32
32
|
expect(@runner).not_to receive(:upload_template)
|
33
|
-
expect { @runner.run }.to raise_exception(TemplateRenderError)
|
33
|
+
expect { @runner.run 'test-template', '/test-template' }.to raise_exception(TemplateRenderError)
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'handles empty rendered template' do
|
37
|
-
expect(@runner).to receive(:render_template).with(
|
37
|
+
expect(@runner).to receive(:render_template).with('test-template').and_return([0, '', 'not used'])
|
38
38
|
expect(@runner).not_to receive(:upload_template)
|
39
|
-
expect { @runner.run }.to raise_exception(TemplateRenderError)
|
39
|
+
expect { @runner.run 'test-template', '/test-template' }.to raise_exception(TemplateRenderError)
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'handles bad return from upload_template' do
|
43
43
|
exp_hash = 'bbf9afe7431caf5f89a608bc31e8d822'
|
44
|
-
expect(@runner).to receive(:render_template).with(
|
45
|
-
expect(@runner).to receive(:upload_template).with('test body').and_return(false)
|
46
|
-
expect { @runner.run }.to raise_exception(TemplateUploadError)
|
44
|
+
expect(@runner).to receive(:render_template).with('test-template').and_return([0, 'test body', exp_hash])
|
45
|
+
expect(@runner).to receive(:upload_template).with('/test-template', 'test body').and_return(false)
|
46
|
+
expect { @runner.run 'test-template', '/test-template' }.to raise_exception(TemplateUploadError)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consul-template-generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Oldfield
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|