offline_consul-client 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/Gemfile.lock +57 -0
- data/README.md +19 -5
- data/lib/consul/client/connection.rb +167 -0
- data/lib/consul/client/version.rb +1 -1
- data/lib/consul/client.rb +9 -135
- data/lib/consul/data.rb +9 -5
- data/lib/consul/http.rb +4 -0
- data/lib/consul/response.rb +9 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e581d353ceef2e170fae3f52957cc6a04a017ae4
|
4
|
+
data.tar.gz: e3091c3965da1981d52b672555814ca53b5cdcf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55e1354375b9dd7c8bee244d97d2b4345180261c13cba7d769f2a51dca98b2341a2cb59f4834317a967a7a880e24a9ed947ed60e2a9f2c39cb44e1ee08c01d1e
|
7
|
+
data.tar.gz: b70b527b7248849c3212e8b3ce86d97ef47fe9e9c4ed6c559100dcd7f09cc0062311609f770073fad3ae7f43188ec5bee0d63683212e634a8f10786f587def51
|
data/.gitignore
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
offline_consul-client (0.2.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
addressable (2.4.0)
|
10
|
+
coderay (1.1.0)
|
11
|
+
crack (0.4.3)
|
12
|
+
safe_yaml (~> 1.0.0)
|
13
|
+
diff-lcs (1.2.5)
|
14
|
+
hashdiff (0.2.3)
|
15
|
+
method_source (0.8.2)
|
16
|
+
pry (0.10.3)
|
17
|
+
coderay (~> 1.1.0)
|
18
|
+
method_source (~> 0.8.1)
|
19
|
+
slop (~> 3.4)
|
20
|
+
psych (2.0.8)
|
21
|
+
rake (10.5.0)
|
22
|
+
rspec (3.5.0)
|
23
|
+
rspec-core (~> 3.4.2)
|
24
|
+
rspec-expectations (~> 3.5.0)
|
25
|
+
rspec-mocks (~> 3.5.0)
|
26
|
+
rspec-core (3.4.2)
|
27
|
+
rspec-support (~> 3.5.0)
|
28
|
+
rspec-expectations (3.5.0)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.5.0)
|
31
|
+
rspec-mocks (3.5.0)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.5.0)
|
34
|
+
rspec-support (3.5.0)
|
35
|
+
safe_yaml (1.0.4)
|
36
|
+
slop (3.6.0)
|
37
|
+
vcr (2.9.3)
|
38
|
+
webmock (1.22.3)
|
39
|
+
addressable (>= 2.3.6)
|
40
|
+
crack (>= 0.3.2)
|
41
|
+
hashdiff
|
42
|
+
|
43
|
+
PLATFORMS
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
bundler (~> 1.9)
|
48
|
+
offline_consul-client!
|
49
|
+
pry (~> 0.10)
|
50
|
+
psych (~> 2.0)
|
51
|
+
rake (~> 10.0)
|
52
|
+
rspec (~> 3.3)
|
53
|
+
vcr (~> 2.9)
|
54
|
+
webmock (~> 1.9)
|
55
|
+
|
56
|
+
BUNDLED WITH
|
57
|
+
1.13.1
|
data/README.md
CHANGED
@@ -23,11 +23,9 @@ The main methods to use are `Consul::Client.read` and `Consul::Client.write`, th
|
|
23
23
|
```ruby
|
24
24
|
>> Consul::Client.config.namespace = "/bags"
|
25
25
|
>> mydata = {foo:1, bar:[1,2,:three], baz:false, bat:{one:1, two:2, three:3}}
|
26
|
-
|
27
|
-
>>
|
28
|
-
=>
|
29
|
-
>> Consul::Client.read('/some/key')
|
30
|
-
=> {:foo=>1, :bar=>[1, 2, :three], :baz=>false, :bat=>{:one=>1, :two=>2, :three=>3}}
|
26
|
+
>> consul::Client.write('/some/key', mydata)
|
27
|
+
>> puts Consul::Client.read('/some/key')
|
28
|
+
{:foo=>1, :bar=>[1, 2, :three], :baz=>false, :bat=>{:one=>1, :two=>2, :three=>3}}
|
31
29
|
|
32
30
|
```
|
33
31
|
|
@@ -46,6 +44,22 @@ curl "http://consul:8500/v1/kv/bags/some/key?raw"
|
|
46
44
|
:three: 3
|
47
45
|
```
|
48
46
|
|
47
|
+
`Consul::Client` can also be instantiated so that you can have multiple different configs that don't stomp on eachother
|
48
|
+
```ruby
|
49
|
+
>> c = Consul::Client
|
50
|
+
>> f = Consul::Client.new
|
51
|
+
>> b = Consul::Client.new
|
52
|
+
>> f.config.namespace = "/foo"
|
53
|
+
>> b.config.namespace = "/bar"
|
54
|
+
>> puts c.read("key")
|
55
|
+
I am /bags/key
|
56
|
+
>> puts f.read("key)
|
57
|
+
I am /foo/key
|
58
|
+
>> puts b.read("key")
|
59
|
+
I am /bar/key
|
60
|
+
|
61
|
+
```
|
62
|
+
|
49
63
|
###Tweakables
|
50
64
|
|
51
65
|
```ruby
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require "psych"
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
module Consul::Client
|
5
|
+
class Connection
|
6
|
+
|
7
|
+
def config
|
8
|
+
@config ||= Config.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def online_read(keypath, opts={})
|
12
|
+
response = http.get("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : ''))
|
13
|
+
end
|
14
|
+
|
15
|
+
def offline_read(keypath, opts={})
|
16
|
+
files = if opts[:recursive]
|
17
|
+
Dir.glob(File.join(local_path, path(keypath) + '*'))
|
18
|
+
else
|
19
|
+
[File.join(local_path, path(keypath) + ".yaml")]
|
20
|
+
end
|
21
|
+
|
22
|
+
key_data = files.map do |filename|
|
23
|
+
{}.tap do |data|
|
24
|
+
data['LockIndex'] = 0
|
25
|
+
data['Key'] = File.basename(filename).sub(/\.yaml$/, '')
|
26
|
+
data['Flags'] = 0
|
27
|
+
data['Value'] = Base64.encode64(File.read(filename))
|
28
|
+
data['CreateIndex'] = 0
|
29
|
+
data['ModifyIndex'] = 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
response = Consul::Response.new(Consul::Client::FakeNetHttpResponse.new(Psych.dump(key_data)))
|
34
|
+
end
|
35
|
+
|
36
|
+
def read_obj(keypath, opts={})
|
37
|
+
keyname = File.basename(keypath)
|
38
|
+
response = if is_online?
|
39
|
+
online_read(keypath, opts)
|
40
|
+
else
|
41
|
+
offline_read(keypath, opts)
|
42
|
+
end
|
43
|
+
|
44
|
+
if response.length == 1 && response.keys.first == keyname
|
45
|
+
response[keyname]
|
46
|
+
else
|
47
|
+
Hash[response.map{ |k,v| [k, v.value] }]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def read(keypath, opts={})
|
52
|
+
obj = read_obj(keypath, opts)
|
53
|
+
if obj.respond_to?(:value)
|
54
|
+
obj.value
|
55
|
+
else
|
56
|
+
obj
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def online_delete(keypath, opts={})
|
61
|
+
response = http.delete("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : '')).value
|
62
|
+
end
|
63
|
+
|
64
|
+
def offline_delete(keypath, opts={})
|
65
|
+
#do nothing, lets not delete our local files
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
def delete(keypath, opts={})
|
70
|
+
if is_online?
|
71
|
+
online_delete(keypath, opts={})
|
72
|
+
else
|
73
|
+
offline_delete(keypath, opts={})
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def online_write(keypath,data=nil,path_mimic=false)
|
78
|
+
data = encode(data)
|
79
|
+
http.put("/kv" + path(keypath), data)
|
80
|
+
end
|
81
|
+
|
82
|
+
def offline_write(keypath,data=nil)
|
83
|
+
data = encode(data)
|
84
|
+
filename = File.join(local_path, path(keypath) + ".yaml")
|
85
|
+
dirname = File.dirname(filename)
|
86
|
+
|
87
|
+
unless File.directory?(dirname)
|
88
|
+
FileUtils.mkdir_p(dirname)
|
89
|
+
end
|
90
|
+
File.open(filename, 'w') { |f| f.write(data) }
|
91
|
+
Consul::Response.new(Consul::Client::FakeNetHttpResponse.new)
|
92
|
+
end
|
93
|
+
|
94
|
+
def write(keypath,data=nil)
|
95
|
+
if is_online?
|
96
|
+
online_write(keypath,data)
|
97
|
+
else
|
98
|
+
offline_write(keypath,data)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
#def lock(keypath, data=nil)
|
104
|
+
# write(keypath + "?acquire=#{session_id}", data)
|
105
|
+
#end
|
106
|
+
|
107
|
+
#def unlock(keypath, data=nil)
|
108
|
+
# write(keypath + "?release=#{session_id}", data)
|
109
|
+
#end
|
110
|
+
|
111
|
+
#def session_id
|
112
|
+
# @@session_id ||= http.put("/session/create")
|
113
|
+
#end
|
114
|
+
|
115
|
+
#def has_lock?(keypath)
|
116
|
+
# read("/kv/" + keypath).session == session_id
|
117
|
+
#end
|
118
|
+
|
119
|
+
def http
|
120
|
+
@http ||= Consul::Http.new
|
121
|
+
end
|
122
|
+
|
123
|
+
def local_path
|
124
|
+
ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
|
125
|
+
end
|
126
|
+
|
127
|
+
def path(keypath)
|
128
|
+
File.join("/" + config.namespace, keypath).gsub(%r{//}, '/')
|
129
|
+
end
|
130
|
+
|
131
|
+
def is_online?
|
132
|
+
if defined?(Chef)
|
133
|
+
!::Chef::Config[:solo]
|
134
|
+
else
|
135
|
+
false
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def decode_r(data)
|
140
|
+
if data.respond_to?(:keys)
|
141
|
+
Hash[ data.map{ |k,v| [k,decode_r(v)] } ]
|
142
|
+
else
|
143
|
+
decode(data.value)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def decode(value)
|
148
|
+
Psych.safe_load(value, [Symbol])
|
149
|
+
end
|
150
|
+
|
151
|
+
def encode(value)
|
152
|
+
encoded = Psych.dump(value)
|
153
|
+
if decode(encoded) != value
|
154
|
+
raise TypeError, "Unable to relaibly decode #{value.class}"
|
155
|
+
end
|
156
|
+
encoded
|
157
|
+
end
|
158
|
+
|
159
|
+
def encode_r(data)
|
160
|
+
if data.respond_to?(:keys)
|
161
|
+
Hash[ data.map{ |k,v| [k, encode_r(v)] } ]
|
162
|
+
else
|
163
|
+
encode(data)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
data/lib/consul/client.rb
CHANGED
@@ -1,155 +1,29 @@
|
|
1
1
|
require "consul/client/version"
|
2
2
|
require "consul/client/config"
|
3
|
+
require "consul/client/connection"
|
3
4
|
require "consul/client/fake_http_response"
|
4
5
|
require "consul/http"
|
5
6
|
require "consul/response"
|
6
7
|
require "consul/data"
|
7
8
|
|
8
|
-
require "
|
9
|
-
require "fileutils"
|
9
|
+
require "forwardable"
|
10
10
|
|
11
11
|
module Consul
|
12
12
|
module Client
|
13
|
-
def self.config
|
14
|
-
@@config ||= Config.new
|
15
|
-
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
end
|
14
|
+
class << self
|
15
|
+
extend Forwardable
|
20
16
|
|
21
|
-
|
22
|
-
files = if opts[:recursive]
|
23
|
-
Dir.glob(File.join(local_path, path(keypath) + '*'))
|
24
|
-
else
|
25
|
-
[File.join(local_path, path(keypath) + ".yaml")]
|
26
|
-
end
|
17
|
+
def_delegators :connection, *(Connection.instance_methods - Object.instance_methods)
|
27
18
|
|
28
|
-
|
29
|
-
|
30
|
-
data['LockIndex'] = 0
|
31
|
-
data['Key'] = File.basename(filename).sub(/\.yaml$/, '')
|
32
|
-
data['Flags'] = 0
|
33
|
-
data['Value'] = Base64.encode64(File.read(filename))
|
34
|
-
data['CreateIndex'] = 0
|
35
|
-
data['ModifyIndex'] = 0
|
36
|
-
end
|
19
|
+
def new
|
20
|
+
Connection.new
|
37
21
|
end
|
38
22
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
def self.read(keypath, opts={})
|
43
|
-
obj = read_obj(keypath, opts)
|
44
|
-
if obj.respond_to?(:value)
|
45
|
-
obj.value
|
46
|
-
else
|
47
|
-
Hash[obj.map{|k,v| [k, v.value] }]
|
23
|
+
def connection
|
24
|
+
@connection ||= Connection.new
|
48
25
|
end
|
49
26
|
end
|
50
27
|
|
51
|
-
def self.read_obj(keypath, opts={})
|
52
|
-
response = if is_online?
|
53
|
-
online_read(keypath, opts)
|
54
|
-
else
|
55
|
-
offline_read(keypath, opts)
|
56
|
-
end
|
57
|
-
|
58
|
-
keyname = File.basename(keypath)
|
59
|
-
if response.length == 1 && response.keys.first == keyname
|
60
|
-
response = response[keyname]
|
61
|
-
end
|
62
|
-
response
|
63
|
-
end
|
64
|
-
|
65
|
-
def self.online_write(keypath,data=nil,path_mimic=false)
|
66
|
-
data = encode(data)
|
67
|
-
http.put("/kv" + path(keypath), data)
|
68
|
-
end
|
69
|
-
|
70
|
-
def self.offline_write(keypath,data=nil)
|
71
|
-
data = encode(data)
|
72
|
-
filename = File.join(local_path, path(keypath) + ".yaml")
|
73
|
-
dirname = File.dirname(filename)
|
74
|
-
|
75
|
-
unless File.directory?(dirname)
|
76
|
-
FileUtils.mkdir_p(dirname)
|
77
|
-
end
|
78
|
-
File.open(filename, 'w') { |f| f.write(data) }
|
79
|
-
Response.new(FakeNetHttpResponse.new)
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.write(keypath,data=nil)
|
83
|
-
if is_online?
|
84
|
-
online_write(keypath,data)
|
85
|
-
else
|
86
|
-
offline_write(keypath,data)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
|
91
|
-
#def self.lock(keypath, data=nil)
|
92
|
-
# write(keypath + "?acquire=#{session_id}", data)
|
93
|
-
#end
|
94
|
-
|
95
|
-
#def self.unlock(keypath, data=nil)
|
96
|
-
# write(keypath + "?release=#{session_id}", data)
|
97
|
-
#end
|
98
|
-
|
99
|
-
#def self.session_id
|
100
|
-
# @@session_id ||= http.put("/session/create")
|
101
|
-
#end
|
102
|
-
|
103
|
-
#def self.has_lock?(keypath)
|
104
|
-
# read("/kv/" + keypath).session == session_id
|
105
|
-
#end
|
106
|
-
|
107
|
-
def self.http
|
108
|
-
@http ||= Http.new
|
109
|
-
end
|
110
|
-
|
111
|
-
def self.local_path
|
112
|
-
ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
|
113
|
-
end
|
114
|
-
|
115
|
-
def self.path(keypath)
|
116
|
-
File.join("/" + config.namespace, keypath).gsub(%r{//}, '/')
|
117
|
-
end
|
118
|
-
|
119
|
-
def self.is_online?
|
120
|
-
if defined?(Chef)
|
121
|
-
!::Chef::Config[:solo]
|
122
|
-
else
|
123
|
-
false
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def self.decode_r(data)
|
128
|
-
if data.respond_to?(:keys)
|
129
|
-
Hash[ data.map{ |k,v| [k,decode_r(v)] } ]
|
130
|
-
else
|
131
|
-
decode(data.value)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
def self.decode(value)
|
136
|
-
Psych.safe_load(value, [Symbol])
|
137
|
-
end
|
138
|
-
|
139
|
-
def self.encode(value)
|
140
|
-
encoded = Psych.dump(value)
|
141
|
-
if decode(encoded) != value
|
142
|
-
raise TypeError, "Unable to relaibly decode #{value.class}"
|
143
|
-
end
|
144
|
-
encoded
|
145
|
-
end
|
146
|
-
|
147
|
-
def self.encode_r(data)
|
148
|
-
if data.respond_to?(:keys)
|
149
|
-
Hash[ data.map{ |k,v| [k, encode_r(v)] } ]
|
150
|
-
else
|
151
|
-
encode(data)
|
152
|
-
end
|
153
|
-
end
|
154
28
|
end
|
155
29
|
end
|
data/lib/consul/data.rb
CHANGED
@@ -13,11 +13,7 @@ module Consul
|
|
13
13
|
|
14
14
|
def value
|
15
15
|
text = Base64.decode64(hash_map('Value'))
|
16
|
-
|
17
|
-
decoded = Consul::Client.decode(text)
|
18
|
-
rescue Psych::DisallowedClass, Psych::SyntaxError
|
19
|
-
end
|
20
|
-
decoded || text
|
16
|
+
try_decode(text)
|
21
17
|
end
|
22
18
|
|
23
19
|
def flags
|
@@ -30,5 +26,13 @@ module Consul
|
|
30
26
|
@response[key.to_s.split('_').map{ |w| w.capitalize }.join]
|
31
27
|
end
|
32
28
|
|
29
|
+
def try_decode(text)
|
30
|
+
begin
|
31
|
+
decoded = Consul::Client.decode(text)
|
32
|
+
rescue Psych::DisallowedClass, Psych::SyntaxError
|
33
|
+
end
|
34
|
+
decoded || text
|
35
|
+
end
|
36
|
+
|
33
37
|
end
|
34
38
|
end
|
data/lib/consul/http.rb
CHANGED
data/lib/consul/response.rb
CHANGED
@@ -9,8 +9,7 @@ module Consul
|
|
9
9
|
|
10
10
|
attr_reader :response, :data
|
11
11
|
def_delegators :response, :body, :code, :code_type
|
12
|
-
def_delegators :data, :keys, :values, :each, :to_a, :length, :map, :[]
|
13
|
-
def_delegators :to_ruby, :to_h, :to_a
|
12
|
+
def_delegators :data, :keys, :values, :each, :to_a, :to_h, :length, :map, :[]
|
14
13
|
|
15
14
|
class KeyError < RuntimeError; end
|
16
15
|
|
@@ -23,10 +22,18 @@ module Consul
|
|
23
22
|
@data ||= init_data
|
24
23
|
end
|
25
24
|
|
25
|
+
def value
|
26
|
+
@value ||= to_ruby
|
27
|
+
end
|
28
|
+
|
26
29
|
#def locked?
|
27
30
|
# to_h.has_key?("Session")
|
28
31
|
#end
|
29
32
|
|
33
|
+
def success?
|
34
|
+
response.code_type == Net::HTTPOK
|
35
|
+
end
|
36
|
+
|
30
37
|
private
|
31
38
|
|
32
39
|
def init_data
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: offline_consul-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Scholl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- ".rspec"
|
121
121
|
- ".travis.yml"
|
122
122
|
- Gemfile
|
123
|
+
- Gemfile.lock
|
123
124
|
- LICENSE.txt
|
124
125
|
- README.md
|
125
126
|
- Rakefile
|
@@ -129,6 +130,7 @@ files:
|
|
129
130
|
- consul-client.gemspec
|
130
131
|
- lib/consul/client.rb
|
131
132
|
- lib/consul/client/config.rb
|
133
|
+
- lib/consul/client/connection.rb
|
132
134
|
- lib/consul/client/fake_http_response.rb
|
133
135
|
- lib/consul/client/version.rb
|
134
136
|
- lib/consul/data.rb
|