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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a1c3d214b88585c7c81c78fd7c2259bb3649f90
4
- data.tar.gz: 90c3338c79704716ff7846f3d82c410645ce4d10
3
+ metadata.gz: 4d4957226da4752a9ea6c7dbad95d11d26a83a6a
4
+ data.tar.gz: 8b162f20735d87d73316e0ed377480b5f3274047
5
5
  SHA512:
6
- metadata.gz: b98f75d271bfb21af5d4bef9eb855c5573c31b41d058e8acd91684adc88e300fb38baf5652764a793d22ab2b4f6588100be56bd98eb7945dfcfebbcae3025413
7
- data.tar.gz: a7b9091b66e273b8b8fc8d1aa743df4f6538fd53b0e712dff2eda242100eef54a7e23ba9bd1822f1ce7ffa95a2386786974800cea8f938f0c7fca3998d091f15
6
+ metadata.gz: cef041656eb904456cbbdfd0556c841f20225ec392ea9c689cc2bc6154d067f9d574b483356005b3c012886b2ada9377e5fc18fdb21ead6fd2b38770e629d528
7
+ data.tar.gz: 85f5dbfb0eae86231554843791453e8400dd915b733a66e2a7ddae18edd3e9738a85d8233a9667146a8fec3efe0d3960734bb9f0c359c13f22a0437a7813934a
data/circle.yml ADDED
@@ -0,0 +1,5 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.1.6
4
+ timezone:
5
+ Etc/GMT
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.10"
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"
@@ -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!
@@ -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
- case response
18
- when Net::HTTPSuccess
19
- ConsulDo.log "get_key", JSON.parse(response.body).first
20
- else
21
- ConsulDo.log "get_key", {}
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
- if response.body == "null"
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, {"name" => ConsulDo.config.session_name})
38
- case response
39
- when Net::HTTPSuccess
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
- JSON.parse(response.body).each do |session_hash|
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
@@ -1,3 +1,3 @@
1
1
  module ConsulDo
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/consul_do.rb CHANGED
@@ -18,6 +18,10 @@ module ConsulDo
18
18
  @elect ||= Elect.new
19
19
  end
20
20
 
21
+ def self.elect!
22
+ @elect = Elect.new
23
+ end
24
+
21
25
  def self.http_put(dest_url, data = nil)
22
26
  uri = URI.parse(dest_url)
23
27
  request = config.http_client.new(uri.host, uri.port)
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.0
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-09 00:00:00.000000000 Z
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.10'
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.10'
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