consul_do 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/circle.yml +5 -0
- data/consul_do.gemspec +1 -1
- data/lib/consul_do/config.rb +2 -1
- data/lib/consul_do/elect.rb +23 -20
- data/lib/consul_do/version.rb +1 -1
- data/lib/consul_do.rb +4 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d4957226da4752a9ea6c7dbad95d11d26a83a6a
|
4
|
+
data.tar.gz: 8b162f20735d87d73316e0ed377480b5f3274047
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cef041656eb904456cbbdfd0556c841f20225ec392ea9c689cc2bc6154d067f9d574b483356005b3c012886b2ada9377e5fc18fdb21ead6fd2b38770e629d528
|
7
|
+
data.tar.gz: 85f5dbfb0eae86231554843791453e8400dd915b733a66e2a7ddae18edd3e9738a85d8233a9667146a8fec3efe0d3960734bb9f0c359c13f22a0437a7813934a
|
data/circle.yml
ADDED
data/consul_do.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler", "~> 1
|
22
|
+
spec.add_development_dependency "bundler", "~> 1"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.3"
|
25
25
|
spec.add_development_dependency "pry", "~> 0.10"
|
data/lib/consul_do/config.rb
CHANGED
@@ -3,7 +3,7 @@ require 'optparse'
|
|
3
3
|
module ConsulDo
|
4
4
|
class Config
|
5
5
|
|
6
|
-
attr_accessor :key, :host, :port, :http_proxy, :verbose
|
6
|
+
attr_accessor :key, :host, :port, :http_proxy, :verbose, :token
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@host = 'localhost'
|
@@ -19,6 +19,7 @@ module ConsulDo
|
|
19
19
|
opts.on("-k", "--key KEY=consul_do", "Coordination key"){ |v| self.key = v }
|
20
20
|
opts.on("-h", "--consul-host HOST=localhost", "Consul hostname"){ |v| self.host = v }
|
21
21
|
opts.on("-p", "--consul-port PORT=8500", "Consul port"){ |v| self.port = v }
|
22
|
+
opts.on("-t", "--token TOKEN", "ACL Token"){ |v| self.token = v }
|
22
23
|
opts.on("--http_proxy http://HOST:PORT", "Use supplied proxy instead of ENV "){ |v| self.http_proxy = v }
|
23
24
|
opts.on("-v", "--verbose", "Consul port"){ |v| self.verbose = v }
|
24
25
|
end.parse!
|
data/lib/consul_do/elect.rb
CHANGED
@@ -12,35 +12,30 @@ module ConsulDo
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def get_key
|
15
|
-
url = "#{base_url}/v1/kv/service/#{ConsulDo.config.key}/leader"
|
15
|
+
url = "#{base_url}/v1/kv/service/#{ConsulDo.config.key}/leader?" + token_str
|
16
16
|
response = ConsulDo.http_get(url)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
ConsulDo.log "get_key", parse_json(response.body, [{}]).first
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse_json(json_blob, default_value)
|
21
|
+
begin
|
22
|
+
JSON.parse(json_blob)
|
23
|
+
rescue JSON::ParserError, TypeError
|
24
|
+
default_value
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
28
|
def get_session_info(session_id)
|
26
29
|
url = "#{base_url}/v1/session/info/#{session_id}"
|
27
30
|
response = ConsulDo.http_get(url)
|
28
|
-
|
29
|
-
raise "Invalid Session"
|
30
|
-
else
|
31
|
-
ConsulDo.log "get_session_info", JSON.parse(response.body).first
|
32
|
-
end
|
31
|
+
ConsulDo.log "get_session_info", parse_json(response.body, []).first or raise "Invalid Session"
|
33
32
|
end
|
34
33
|
|
35
34
|
def create_session
|
36
35
|
url = "#{base_url}/v1/session/create"
|
37
|
-
response = ConsulDo.http_put(url, {
|
38
|
-
|
39
|
-
|
40
|
-
ConsulDo.log "create_session", JSON.parse(response.body)['ID']
|
41
|
-
else
|
42
|
-
raise "Could not create session: #{response.code} - #{response.message} (#{response.body})"
|
43
|
-
end
|
36
|
+
response = ConsulDo.http_put(url, {'name' => ConsulDo.config.session_name})
|
37
|
+
|
38
|
+
ConsulDo.log "create_session", parse_json(response.body, {})['ID'] or raise "Could not create session: #{response.code} - #{response.message} (#{response.body})"
|
44
39
|
end
|
45
40
|
|
46
41
|
def session
|
@@ -63,7 +58,7 @@ module ConsulDo
|
|
63
58
|
def delete_sessions
|
64
59
|
url = "#{base_url}/v1/session/node/#{get_session_info(session)['Node']}"
|
65
60
|
response = ConsulDo.http_get(url)
|
66
|
-
|
61
|
+
parse_json(response.body, []).each do |session_hash|
|
67
62
|
if block_given?
|
68
63
|
delete_session(session_hash['ID']) if yield session_hash['Name']
|
69
64
|
else
|
@@ -72,8 +67,16 @@ module ConsulDo
|
|
72
67
|
end
|
73
68
|
end
|
74
69
|
|
70
|
+
def token_str
|
71
|
+
if ConsulDo.config.token
|
72
|
+
"&token=#{ConsulDo.config.token}"
|
73
|
+
else
|
74
|
+
""
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
75
78
|
def get_lock
|
76
|
-
url = "#{base_url}/v1/kv/service/#{ConsulDo.config.key}/leader?acquire=#{session}"
|
79
|
+
url = "#{base_url}/v1/kv/service/#{ConsulDo.config.key}/leader?acquire=#{session}" + token_str
|
77
80
|
response = ConsulDo.http_put(url, {'updated' => Time.now})
|
78
81
|
@session_has_lock = true if response.body == "true"
|
79
82
|
end
|
data/lib/consul_do/version.rb
CHANGED
data/lib/consul_do.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consul_do
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Scholl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1
|
19
|
+
version: '1'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1
|
26
|
+
version: '1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- Rakefile
|
126
126
|
- bin/console
|
127
127
|
- bin/setup
|
128
|
+
- circle.yml
|
128
129
|
- consul_do.gemspec
|
129
130
|
- exe/consul-do
|
130
131
|
- lib/consul_do.rb
|