consul-template-generator 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11a123a605164b3bf85c7d874ae9d731db56cb99
4
- data.tar.gz: 9091f83f039d537c0a1b13fac6a99a7546247881
3
+ metadata.gz: c81f7c798493e55d5de09e72f18774969babccb2
4
+ data.tar.gz: c8b6ed3a09773848a3c02cd4d97edc1d65c8dfbe
5
5
  SHA512:
6
- metadata.gz: 4330d15eb334469bad07257f6e66eaaf29dd9f787a652006e9afd9116e714a9d45072c4dce4fbf3fb80d142012f69e0571ac764f349cb39798ad0ae65ff9ca53
7
- data.tar.gz: 33406c87edc31d2b4534104bcbbe2a5378690346ca834cadbec809797ab3ad5f7792e9650ff7903c6a00ddf0e0f37c29db140c9506fe496a009c7e1dd51f3d0b
6
+ metadata.gz: 0c687b4a6fcc7414422484a01083ef65e9f592a0ba55950df4332d56bd9accdd689b62d23f6fbc2069f908a49580925b2440027bd9262ac7f521dbf3dfac5da3
7
+ data.tar.gz: a30164de62fbaac82dfd7715003fc2db6a5ed1bd8b351e7b31a9aee6ab2569477c05065eede82609914a48cc1e4bdab4523bc2d2f5775abce22b19434aebc352
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- consul-template-generator (0.1.2)
4
+ consul-template-generator (0.3.0)
5
5
  diplomat (~> 0.12.0)
6
6
  popen4 (~> 0.1.2)
7
7
 
@@ -42,7 +42,7 @@ module Consul
42
42
  end
43
43
 
44
44
  def create_session(name)
45
- Diplomat::Session.create({:Node => self.config.node, :Name => name})
45
+ Diplomat::Session.create({:Node => self.config.node, :Name => name, :Behavior => 'release'})
46
46
  end
47
47
 
48
48
  def renew_session(sess_id)
@@ -54,7 +54,7 @@ module Consul
54
54
  raise ConsulSessionExpired
55
55
  rescue Exception => e
56
56
  # Letting this go for the time being, until the above issue is fixed
57
- self.config.logger.error "Unknown error occoured: #{e.message}"
57
+ self.config.logger.error "Unknown error occurred: #{e.message}"
58
58
  end
59
59
  end
60
60
 
@@ -5,74 +5,81 @@ module Consul
5
5
  module Generator
6
6
  module CMD
7
7
  include Consul::Template::Generator
8
+ class << self
8
9
 
9
- def self.configure(consul_host, templates, session_key, log_level, proxy = nil)
10
- Consul::Template::Generator.configure do |config|
11
- config.log_level = log_level
12
- config.templates = templates
13
- config.session_key = session_key
14
- config.consul_host = consul_host
10
+ def configure(consul_host, templates, session_key, log_level, proxy = nil)
11
+ Consul::Template::Generator.configure do |config|
12
+ config.log_level = log_level
13
+ config.templates = templates
14
+ config.session_key = session_key
15
+ config.consul_host = consul_host
16
+ end
17
+ @config = Consul::Template::Generator.config
15
18
  end
16
- end
17
19
 
18
- def self.run(cycle_sleep = nil, lock_sleep = nil)
19
- cycle_sleep ||= 0.5
20
- lock_sleep ||= 1.0
21
- config = Consul::Template::Generator.config
22
- uploaded_hashes = {}
23
- begin
24
- runner = CTRunner.new
25
- runner.acquire_session_lock do
26
- config.logger.info "Session lock acquired..."
27
- begin
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
32
- rescue Interrupt
33
- raise # Re-raise to break this rescue block
34
- rescue ConsulSessionExpired
35
- config.logger.error "The current consul session has expired."
36
- break
37
- rescue Exception => e
38
- config.logger.error "An error occurred while updating template: #{e.message}"
39
- config.logger.debug "Sleeping before attempting to update again..."
40
- sleep lock_sleep
41
- break
42
- end until false
20
+ def signals
21
+ Signal.trap("INT") do
22
+ @config.logger.error "Received INT signal..."
23
+ @interrupted = true
43
24
  end
44
- rescue Interrupt
45
- config.logger.error "Received interrupt signal, exiting..."
46
- break
47
- rescue Exception => e
48
- config.logger.info "Unable to obtain session lock: #{e.message}"
49
- config.logger.debug "Sleeping before attempting lock session again..."
25
+ Signal.trap("TERM") do
26
+ @config.logger.error "Received TERM signal..."
27
+ @terminated = true
28
+ @interrupted = true
29
+ end
30
+ end
31
+
32
+ def run(cycle_sleep = nil, lock_sleep = nil)
33
+ signals
34
+ @terminated = false
35
+ cycle_sleep ||= 0.5
36
+ lock_sleep ||= 1.0
37
+ uploaded_hashes = {}
38
+ runner = CTRunner.new nil, false
50
39
  begin
40
+ runner.create_session
41
+ @interrupted = false
42
+ runner.acquire_session_lock do
43
+ @config.logger.info "Session lock acquired..."
44
+ begin
45
+ @config.templates.each do |template,template_key|
46
+ uploaded_hashes[template] = runner.run(template, template_key, uploaded_hashes[template]) || uploaded_hashes[template]
47
+ sleep cycle_sleep
48
+ end
49
+ rescue ConsulSessionExpired
50
+ @config.logger.error "The current consul session has expired."
51
+ break
52
+ rescue Exception => e
53
+ @config.logger.error "An error occurred while updating template: #{e.message}"
54
+ @config.logger.debug "Sleeping before attempting to update again..."
55
+ sleep lock_sleep
56
+ break
57
+ end until @interrupted
58
+ end
59
+ rescue Exception => e
60
+ @config.logger.info "Unable to obtain session lock: #{e.message}"
61
+ @config.logger.debug "Sleeping before attempting lock session again..."
51
62
  sleep lock_sleep
52
- rescue Interrupt
53
- config.logger.error "Received interrupt signal, exiting..."
54
- break
55
- end
56
- ensure
57
- runner.destroy_session
58
- end until false
59
- 0
60
- end
63
+ ensure
64
+ runner.destroy_session
65
+ end until @terminated
66
+ 0
67
+ end
61
68
 
62
- def self.run_once
63
- config = Consul::Template::Generator.config
64
- begin
65
- config.templates.each do |template,template_key|
66
- runner = CTRunner.new
67
- result = runner.run template, template_key
69
+ def run_once
70
+ begin
71
+ @config.templates.each do |template,template_key|
72
+ runner = CTRunner.new
73
+ result = runner.run template, template_key
74
+ end
75
+ rescue Exception => e
76
+ @config.logger.error "An unexpected error occurred, unable to process template: #{e.message}"
77
+ 1
78
+ else
79
+ 0
68
80
  end
69
- rescue Exception => e
70
- config.logger.error "An unexpected error occurred, unable to process template: #{e.message}"
71
- 1
72
- else
73
- 0
74
- end
75
- end
81
+ end
82
+ end
76
83
  end
77
84
  end
78
85
  end
@@ -4,8 +4,8 @@ module Consul
4
4
  class CTRunner
5
5
  attr_accessor :session
6
6
 
7
- def initialize(consul_session_id = nil)
8
- if consul_session_id.nil?
7
+ def initialize(consul_session_id = nil, do_create_session = true)
8
+ if (consul_session_id.nil? && do_create_session)
9
9
  create_session
10
10
  else
11
11
  @session = consul_session_id
@@ -17,7 +17,7 @@ module Consul
17
17
  unless @session.nil?
18
18
  destroy_session
19
19
  end
20
- @session = Consul::Template::Generator.create_session 'consul-template-generator'
20
+ @session = Consul::Template::Generator.create_session 'consul-template-generator'
21
21
  end
22
22
 
23
23
  def destroy_session
@@ -1,7 +1,7 @@
1
1
  module Consul
2
2
  module Template
3
3
  module Generator
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
6
6
  end
7
7
  end
data/spec/init_spec.rb CHANGED
@@ -20,7 +20,7 @@ describe 'Consul::Template::Generator::CTRunner' '#initialize' do
20
20
  runner = Consul::Template::Generator::CTRunner.new
21
21
  expect(runner.session).to eql('test-session-id')
22
22
  runner.destroy_session
23
- expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/create').with(:body => '{"Node":"test-node","Name":"consul-template-generator"}')
23
+ expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/create').with(:body => '{"Node":"test-node","Name":"consul-template-generator","Behavior":"release"}')
24
24
  expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/destroy/test-session-id')
25
25
  expect(runner.session).to be_nil
26
26
  end
@@ -30,7 +30,7 @@ describe 'Consul::Template::Generator::CTRunner' '#initialize' do
30
30
  expect(runner.session).to eql('destroyed-session')
31
31
  runner.create_session
32
32
  runner.destroy_session
33
- expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/create').with(:body => '{"Node":"test-node","Name":"consul-template-generator"}')
33
+ expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/create').with(:body => '{"Node":"test-node","Name":"consul-template-generator","Behavior":"release"}')
34
34
  expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/destroy/destroyed-session')
35
35
  expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/destroy/test-session-id')
36
36
  expect(runner.session).to be_nil
@@ -40,7 +40,7 @@ describe 'Consul::Template::Generator::CTRunner' '#initialize' do
40
40
  runner = Consul::Template::Generator::CTRunner.new 'failed-destroyed-session'
41
41
  expect(runner.session).to eql('failed-destroyed-session')
42
42
  runner.create_session
43
- expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/create').with(:body => '{"Node":"test-node","Name":"consul-template-generator"}')
43
+ expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/create').with(:body => '{"Node":"test-node","Name":"consul-template-generator","Behavior":"release"}')
44
44
  expect(WebMock).to have_requested(:put, 'http://127.0.0.1:8500/v1/session/destroy/failed-destroyed-session')
45
45
  expect(runner.session).to eql('test-session-id')
46
46
  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.2.0
4
+ version: 0.3.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-27 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler