Module: Consul::Client
- Defined in:
- lib/consul/client.rb,
lib/consul/client/config.rb,
lib/consul/client/version.rb,
lib/consul/client/fake_http_response.rb
Defined Under Namespace
Classes: Config, FakeNetHttpResponse
Constant Summary
collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Attribute Details
#namespace=(value) ⇒ Object
Sets the attribute namespace
13
14
15
|
# File 'lib/consul/client.rb', line 13
def namespace=(value)
@namespace = value
end
|
Class Method Details
.config ⇒ Object
15
16
17
|
# File 'lib/consul/client.rb', line 15
def self.config
@@config ||= Config.new
end
|
.decode(value) ⇒ Object
119
120
121
|
# File 'lib/consul/client.rb', line 119
def self.decode(value)
Psych.safe_load(value, [Symbol])
end
|
.decode_r(data) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/consul/client.rb', line 111
def self.decode_r(data)
if data.respond_to?(:keys)
Hash[ data.map{ |k,v| [k,decode_r(v)] } ]
else
decode(data.value)
end
end
|
.encode(value) ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/consul/client.rb', line 123
def self.encode(value)
encoded = Psych.dump(value)
if decode(encoded) != value
raise TypeError, "Unable to relaibly decode #{value.class}"
end
encoded
end
|
.encode_r(data) ⇒ Object
131
132
133
134
135
136
137
|
# File 'lib/consul/client.rb', line 131
def self.encode_r(data)
if data.respond_to?(:keys)
Hash[ data.map{ |k,v| [k, encode_r(v)] } ]
else
encode(data)
end
end
|
.http ⇒ Object
def self.has_lock?(keypath)
read("/kv/" + keypath).session == session_id
end
95
96
97
|
# File 'lib/consul/client.rb', line 95
def self.http
@http ||= Http.new
end
|
.is_online? ⇒ Boolean
107
108
109
|
# File 'lib/consul/client.rb', line 107
def self.is_online?
!::Chef::Config[:solo]
end
|
.local_path ⇒ Object
99
100
101
|
# File 'lib/consul/client.rb', line 99
def self.local_path
ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
end
|
.offline_read(keypath, recurse = false) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/consul/client.rb', line 28
def self.offline_read(keypath, recurse=false)
files = if recurse
Dir.glob(File.join(local_path, path(keypath) + '*'))
else
[File.join(local_path, path(keypath) + ".yaml")]
end
response = Hash[files.map do |filename|
[File.basename(filename).sub(/\.yaml$/, ''), File.open(filename){ |f| Consul::Data.new('Value' => Base64.encode64(f.read) )}]
end]
keyname = File.basename(keypath)
if response.length == 1 && response.keys.first == keyname
response = response[keyname]
end
decode_r(response)
end
|
.offline_write(keypath, data = nil) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/consul/client.rb', line 58
def self.offline_write(keypath,data=nil)
data = encode(data)
filename = File.join(local_path, path(keypath) + ".yaml")
dirname = File.dirname(filename)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
File.open(filename, 'w') { |f| f.write(data) }
Response.new(FakeNetHttpResponse.new)
end
|
.online_read(keypath, recurse = false) ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/consul/client.rb', line 19
def self.online_read(keypath, recurse=false)
response = http.get("/kv" + path(keypath) + (recurse ? '?recurse' : ''))
keyname = File.basename(keypath)
if response.length == 1 && response.keys.first == keyname
response = response.data[keyname]
end
decode_r(response)
end
|
.online_write(keypath, data = nil, path_mimic = false) ⇒ Object
53
54
55
56
|
# File 'lib/consul/client.rb', line 53
def self.online_write(keypath,data=nil,path_mimic=false)
data = encode(data)
http.put("/kv" + path(keypath), data)
end
|
.path(keypath) ⇒ Object
103
104
105
|
# File 'lib/consul/client.rb', line 103
def self.path(keypath)
File.join("/" + config.namespace, keypath).gsub(%r{//}, '/')
end
|
.read(keypath, recurse = false) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/consul/client.rb', line 45
def self.read(keypath, recurse=false)
if is_online?
online_read(keypath, recurse)
else
offline_read(keypath, recurse)
end
end
|
.write(keypath, data = nil) ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/consul/client.rb', line 70
def self.write(keypath,data=nil)
if is_online?
online_write(keypath,data)
else
offline_write(keypath,data)
end
end
|