consul_api 0.0.1 → 0.0.2

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: 54c7f6c686636fc5d995b1ff100633b1474ef582
4
- data.tar.gz: 0a2d34bc07ee5f630d9b6decaa8613d5a4b0ab7b
3
+ metadata.gz: 5c41569f58a1d5cfd56e2fa6f1e49aa91cd44c6d
4
+ data.tar.gz: b1d83907083c624c58129e1c32bb20aa3479a9a2
5
5
  SHA512:
6
- metadata.gz: 067509624045f0efa7e1df3809972f2fc8f8bb9b5fe82a32e79f4bf6dc2954d50979fd8137c28e6fb22336a636aef6755ad3eaf142d2bf64ed3e3101953bc851
7
- data.tar.gz: eb9e725177801383916e68250e561fd25c629811d627ecc6243780d825c262c9a7716a0cef9e00c6b84b78da7cb038d20feafd138ed55389fdba977a63fdac87
6
+ metadata.gz: a0bb0fd26ad95722ebbea0fb8abdb0b1ca7c3456cf0149a2b0eb4338ad508691025f5c0acd8570c7f937b65f34f69cc8429570089138681a4e212dc1e05e2e78
7
+ data.tar.gz: 69c4f618c5faaefc1896fafe952417bd3b778d66319e098fc9483ccf967506809767e3adbfde37ea922cc266b5274c20e604f0d7c39c9def3c17ebc8c5433b64
data/consul_api.gemspec CHANGED
@@ -23,4 +23,9 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.6"
25
25
  spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "vcr"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "activesupport"
30
+ spec.add_development_dependency "pry"
26
31
  end
@@ -0,0 +1,61 @@
1
+ module ConsulApi
2
+ class Agent
3
+ include Common
4
+
5
+ def self.checks
6
+ issue_request(path: '/checks')
7
+ end
8
+
9
+ def self.services
10
+ issue_request(path: '/services')
11
+ end
12
+
13
+ def self.members
14
+ issue_request(path: '/members')
15
+ end
16
+
17
+ def self.self
18
+ issue_request(path: '/self')
19
+ end
20
+
21
+ def self.join(address, query={})
22
+ issue_request(path: "/join/#{address}", query: query)
23
+ end
24
+
25
+ def self.force_leave(address)
26
+ issue_request(path: "/force-leave/#{address}")
27
+ end
28
+
29
+ def self.check_register(query)
30
+ issue_request(path: '/check/register/', query: query, method: :put)
31
+ end
32
+
33
+ def self.check_deregister(check_id)
34
+ issue_request(path: "/check/deregister/#{check_id}", method: :put)
35
+ end
36
+
37
+ def self.check_pass(check_id, query={})
38
+ issue_request(path: "/check/pass/#{check_id}", query: query)
39
+ end
40
+
41
+ def self.check_warn(check_id, query={})
42
+ issue_request(path: "/check/warn/#{check_id}", query: query)
43
+ end
44
+
45
+ def self.check_fail(check_id, query={})
46
+ issue_request(path: "/check/fail/#{check_id}", query: query)
47
+ end
48
+
49
+ def self.service_register(query)
50
+ issue_request(path: '/service/register', query: query, method: :put)
51
+ end
52
+
53
+ def self.service_deregister(service_id)
54
+ issue_request(path: "/service/deregister/#{service_id}", method: :put)
55
+ end
56
+
57
+ def self.base_url
58
+ "#{consul_api_url}/v1/agent"
59
+ end
60
+ end
61
+ end
@@ -31,7 +31,7 @@ module ConsulApi
31
31
  end
32
32
 
33
33
  def self.base_url
34
- "#{ENV['CONSUL_SERVER_URL']}/v1/catalog"
34
+ "#{consul_api_url}/v1/catalog"
35
35
  end
36
36
  end
37
37
  end
@@ -2,16 +2,45 @@ module ConsulApi
2
2
  module Common
3
3
  module ClassMethods
4
4
  def issue_request(method: :get, path: '', query: nil)
5
- response = Faraday.send(method, "#{base_url}#{path}", query)
5
+ conn = Faraday.new(:url => "#{base_url}#{path}")
6
+ response = conn.send(method) do |req|
7
+ req.headers['Content-Type'] = 'application/json'
8
+ req.body = query.to_json
9
+ end
10
+
11
+ # gets and puts will fail with an http status code in the 4xx and 5xx range
12
+ fail "http status #{response.status} returned from consul" if response.status >= 400
13
+
14
+ # if the response is empty, return an empty hash
15
+ body = response.body == '' ? '{}' : response.body
16
+
6
17
  begin
7
- parsed_response = JSON.parse(response.body)
18
+ parsed_response = JSON.parse(body)
8
19
  rescue => e
9
20
  fail "unable to parse the json returned by Consul. Returned data: #{response.body}"
10
21
  end
22
+
11
23
  return Hashie::Mash.new(parsed_response) if parsed_response.is_a?(Hash)
12
24
  # assume array otherwise, and return a collection of Mashes
13
25
  parsed_response.map { |node| Hashie::Mash.new(node) }
14
26
  end
27
+
28
+ def consul_ip
29
+ # override with an environment variable, or read from the gateway of docker0 interface
30
+ @@consul_ip ||= ENV['CONSUL_IP'] ? ENV['CONSUL_IP'] : `route -n | grep 'UG[ \t]' | awk '{print $2}'`.strip
31
+ end
32
+
33
+ def consul_api_port
34
+ @@consul_api_port ||= ENV['CONSUL_API_PORT'] ? ENV['CONSUL_API_PORT'] : 8500
35
+ end
36
+
37
+ def consul_dns_port
38
+ @@consul_dns_port ||= ENV['CONSUL_DNS_PORT'] ? ENV['CONSUL_DNS_PORT'] : 8600
39
+ end
40
+
41
+ def consul_api_url
42
+ "http://#{consul_ip}:#{consul_api_port}"
43
+ end
15
44
  end
16
45
 
17
46
  def self.included(base)
@@ -0,0 +1,24 @@
1
+ module ConsulApi
2
+ class DNS
3
+ include Common
4
+ def self.resolve_service(host)
5
+ srv = []
6
+ srv = resolver.getresources(host, Resolv::DNS::Resource::IN::SRV)
7
+ # Sort SRV records: lowest priority first, highest weight first
8
+ srv.sort! { |a,b| (a.priority != b.priority) ? (a.priority <=> b.priority) : (b.weight <=> a.weight) }
9
+ "#{resolve_node(srv.first.target)}:#{srv.first.port}"
10
+ end
11
+
12
+ def self.resolve_node(host)
13
+ resolver.getaddress(host).to_s
14
+ end
15
+
16
+ def self.resolver
17
+ Resolv::DNS.new(
18
+ nameserver_port: [[consul_ip, consul_dns_port]],
19
+ search: [],
20
+ ndots: 1
21
+ )
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module ConsulApi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/consul_api.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require 'faraday'
2
+ require 'hashie'
1
3
  require "consul_api/version"
2
4
  require 'consul_api/common'
3
5
  require 'consul_api/catalog'
6
+ require 'consul_api/agent'
7
+ require 'consul_api/dns'
4
8
  module ConsulApi
5
9
 
6
- # Your code goes here...
7
10
  end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe ConsulApi::Agent, :vcr do
4
+ describe '#services' do
5
+ it 'gets services running on an agent' do
6
+ expect(ConsulApi::Agent.services).to be_a(Hashie::Mash)
7
+ end
8
+ end
9
+
10
+ describe '#service_register' do
11
+ it 'registers a service on consul' do
12
+ expect(ConsulApi::Agent.service_register({'ID' => '123', 'Name' => 'abc'})).to be_a(Hashie::Mash)
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,15 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+ require 'webmock/rspec'
3
+ require 'vcr'
4
+ require 'active_support/all'
5
+ require 'pry'
6
+
7
+ require 'consul_api'
8
+
9
+ # Requires supporting ruby files with custom matchers and macros, etc,
10
+ # in spec/support/ and its subdirectories.
11
+ Dir['./spec/support/**/*.rb'].each { |f| require f }
12
+ ENV['CONSUL_IP'] = '127.0.0.1'
13
+ RSpec.configure do |config|
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ VCR.configure do |c|
2
+ c.cassette_library_dir = './spec/vcr'
3
+ c.hook_into :webmock
4
+ end
5
+
6
+ RSpec.configure do |c|
7
+ c.treat_symbols_as_metadata_keys_with_true_values = true
8
+ c.around(:each, :vcr) do |example|
9
+ name = example.metadata[:full_description].split(/\s+/, 2).join('/').underscore.gsub(/[^\w\/]+/, '_')
10
+ options = example.metadata.slice(:record, :match_requests_on).except(:example_group)
11
+ VCR.use_cassette(name, options) { example.call }
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: put
5
+ uri: http://127.0.0.1:8500/v1/agent/service/register
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"ID":"123","Name":"abc"}'
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - '*/*'
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Wed, 17 Sep 2014 22:03:52 GMT
25
+ Content-Length:
26
+ - '0'
27
+ Content-Type:
28
+ - text/plain; charset=utf-8
29
+ body:
30
+ encoding: UTF-8
31
+ string: ''
32
+ http_version:
33
+ recorded_at: Wed, 17 Sep 2014 22:03:52 GMT
34
+ recorded_with: VCR 2.9.2
@@ -0,0 +1,34 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://127.0.0.1:8500/v1/agent/services
6
+ body:
7
+ encoding: UTF-8
8
+ string: 'null'
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - '*/*'
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Content-Type:
24
+ - application/json
25
+ Date:
26
+ - Wed, 17 Sep 2014 22:03:52 GMT
27
+ Content-Length:
28
+ - '162'
29
+ body:
30
+ encoding: UTF-8
31
+ string: '{"123":{"ID":"123","Service":"abc","Tags":null,"Port":0},"jockey_consul_update":{"ID":"jockey_consul_update","Service":"docker_consul_update","Tags":[],"Port":0}}'
32
+ http_version:
33
+ recorded_at: Wed, 17 Sep 2014 22:03:52 GMT
34
+ recorded_with: VCR 2.9.2
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay OConnor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
11
+ date: 2014-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -66,6 +66,76 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
69
139
  description: ''
70
140
  email:
71
141
  - jay@bellycard.com
@@ -80,9 +150,16 @@ files:
80
150
  - Rakefile
81
151
  - consul_api.gemspec
82
152
  - lib/consul_api.rb
153
+ - lib/consul_api/agent.rb
83
154
  - lib/consul_api/catalog.rb
84
155
  - lib/consul_api/common.rb
156
+ - lib/consul_api/dns.rb
85
157
  - lib/consul_api/version.rb
158
+ - spec/agent_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/support/vcr.rb
161
+ - spec/vcr/consul_api/agent_service_register/registers_a_service_on_consul.yml
162
+ - spec/vcr/consul_api/agent_services/gets_services_running_on_an_agent.yml
86
163
  homepage: ''
87
164
  licenses:
88
165
  - MIT
@@ -107,4 +184,9 @@ rubygems_version: 2.2.2
107
184
  signing_key:
108
185
  specification_version: 4
109
186
  summary: Ruby wrapper for Consul V1 API
110
- test_files: []
187
+ test_files:
188
+ - spec/agent_spec.rb
189
+ - spec/spec_helper.rb
190
+ - spec/support/vcr.rb
191
+ - spec/vcr/consul_api/agent_service_register/registers_a_service_on_consul.yml
192
+ - spec/vcr/consul_api/agent_services/gets_services_running_on_an_agent.yml