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.2"
Class Method Summary
collapse
Class Method Details
.config ⇒ Object
13
14
15
|
# File 'lib/consul/client.rb', line 13
def self.config
@@config ||= Config.new
end
|
.decode(value) ⇒ Object
121
122
123
|
# File 'lib/consul/client.rb', line 121
def self.decode(value)
Psych.safe_load(value, [Symbol])
end
|
.decode_r(data) ⇒ Object
113
114
115
116
117
118
119
|
# File 'lib/consul/client.rb', line 113
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
125
126
127
128
129
130
131
|
# File 'lib/consul/client.rb', line 125
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
133
134
135
136
137
138
139
|
# File 'lib/consul/client.rb', line 133
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
93
94
95
|
# File 'lib/consul/client.rb', line 93
def self.http
@http ||= Http.new
end
|
.is_online? ⇒ Boolean
105
106
107
108
109
110
111
|
# File 'lib/consul/client.rb', line 105
def self.is_online?
if defined?(Chef)
!::Chef::Config[:solo]
else
false
end
end
|
.local_path ⇒ Object
97
98
99
|
# File 'lib/consul/client.rb', line 97
def self.local_path
ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
end
|
.offline_read(keypath, recurse = false) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/consul/client.rb', line 26
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
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/consul/client.rb', line 56
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
17
18
19
20
21
22
23
24
|
# File 'lib/consul/client.rb', line 17
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
51
52
53
54
|
# File 'lib/consul/client.rb', line 51
def self.online_write(keypath,data=nil,path_mimic=false)
data = encode(data)
http.put("/kv" + path(keypath), data)
end
|
.path(keypath) ⇒ Object
101
102
103
|
# File 'lib/consul/client.rb', line 101
def self.path(keypath)
File.join("/" + config.namespace, keypath).gsub(%r{//}, '/')
end
|
.read(keypath, recurse = false) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/consul/client.rb', line 43
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
68
69
70
71
72
73
74
|
# File 'lib/consul/client.rb', line 68
def self.write(keypath,data=nil)
if is_online?
online_write(keypath,data)
else
offline_write(keypath,data)
end
end
|