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.2.0"
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
135
136
137
|
# File 'lib/consul/client.rb', line 135
def self.decode(value)
Psych.safe_load(value, [Symbol])
end
|
.decode_r(data) ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/consul/client.rb', line 127
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
139
140
141
142
143
144
145
|
# File 'lib/consul/client.rb', line 139
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
147
148
149
150
151
152
153
|
# File 'lib/consul/client.rb', line 147
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
107
108
109
|
# File 'lib/consul/client.rb', line 107
def self.http
@http ||= Http.new
end
|
.is_online? ⇒ Boolean
119
120
121
122
123
124
125
|
# File 'lib/consul/client.rb', line 119
def self.is_online?
if defined?(Chef)
!::Chef::Config[:solo]
else
false
end
end
|
.local_path ⇒ Object
111
112
113
|
# File 'lib/consul/client.rb', line 111
def self.local_path
ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
end
|
.offline_read(keypath, opts = {}) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/consul/client.rb', line 21
def self.offline_read(keypath, opts={})
files = if opts[:recursive]
Dir.glob(File.join(local_path, path(keypath) + '*'))
else
[File.join(local_path, path(keypath) + ".yaml")]
end
key_data = files.map do |filename|
{}.tap do |data|
data['LockIndex'] = 0
data['Key'] = File.basename(filename).sub(/\.yaml$/, '')
data['Flags'] = 0
data['Value'] = Base64.encode64(File.read(filename))
data['CreateIndex'] = 0
data['ModifyIndex'] = 0
end
end
response = Response.new(FakeNetHttpResponse.new(Psych.dump(key_data)))
end
|
.offline_write(keypath, data = nil) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/consul/client.rb', line 70
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, opts = {}) ⇒ Object
17
18
19
|
# File 'lib/consul/client.rb', line 17
def self.online_read(keypath, opts={})
response = http.get("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : ''))
end
|
.online_write(keypath, data = nil, path_mimic = false) ⇒ Object
65
66
67
68
|
# File 'lib/consul/client.rb', line 65
def self.online_write(keypath,data=nil,path_mimic=false)
data = encode(data)
http.put("/kv" + path(keypath), data)
end
|
.path(keypath) ⇒ Object
115
116
117
|
# File 'lib/consul/client.rb', line 115
def self.path(keypath)
File.join("/" + config.namespace, keypath).gsub(%r{//}, '/')
end
|
.read(keypath, opts = {}) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/consul/client.rb', line 42
def self.read(keypath, opts={})
obj = read_obj(keypath, opts)
if obj.respond_to?(:value)
obj.value
else
Hash[obj.map{|k,v| [k, v.value] }]
end
end
|
.read_obj(keypath, opts = {}) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/consul/client.rb', line 51
def self.read_obj(keypath, opts={})
response = if is_online?
online_read(keypath, opts)
else
offline_read(keypath, opts)
end
keyname = File.basename(keypath)
if response.length == 1 && response.keys.first == keyname
response = response[keyname]
end
response
end
|
.write(keypath, data = nil) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/consul/client.rb', line 82
def self.write(keypath,data=nil)
if is_online?
online_write(keypath,data)
else
offline_write(keypath,data)
end
end
|