Class: Consul::Client::Connection
- Inherits:
-
Object
- Object
- Consul::Client::Connection
- Defined in:
- lib/consul/client/connection.rb
Instance Method Summary collapse
- #config ⇒ Object
- #decode(value) ⇒ Object
- #decode_r(data) ⇒ Object
- #delete(keypath, opts = {}) ⇒ Object
- #encode(value) ⇒ Object
- #encode_r(data) ⇒ Object
-
#http ⇒ Object
def has_lock?(keypath) read(“/kv/” + keypath).session == session_id end.
- #is_online? ⇒ Boolean
- #local_path ⇒ Object
- #offline_delete(keypath, opts = {}) ⇒ Object
- #offline_read(keypath, opts = {}) ⇒ Object
- #offline_write(keypath, data = nil) ⇒ Object
- #online_delete(keypath, opts = {}) ⇒ Object
- #online_read(keypath, opts = {}) ⇒ Object
- #online_write(keypath, data = nil, path_mimic = false) ⇒ Object
- #path(keypath) ⇒ Object
- #read(keypath, opts = {}) ⇒ Object
- #read_obj(keypath, opts = {}) ⇒ Object
- #write(keypath, data = nil) ⇒ Object
Instance Method Details
#config ⇒ Object
7 8 9 |
# File 'lib/consul/client/connection.rb', line 7 def config @config ||= Config.new end |
#decode(value) ⇒ Object
147 148 149 |
# File 'lib/consul/client/connection.rb', line 147 def decode(value) Psych.safe_load(value, [Symbol]) end |
#decode_r(data) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/consul/client/connection.rb', line 139 def decode_r(data) if data.respond_to?(:keys) Hash[ data.map{ |k,v| [k,decode_r(v)] } ] else decode(data.value) end end |
#delete(keypath, opts = {}) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/consul/client/connection.rb', line 69 def delete(keypath, opts={}) if is_online? online_delete(keypath, opts={}) else offline_delete(keypath, opts={}) end end |
#encode(value) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/consul/client/connection.rb', line 151 def 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
159 160 161 162 163 164 165 |
# File 'lib/consul/client/connection.rb', line 159 def 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 has_lock?(keypath)
read("/kv/" + keypath).session == session_id
end
119 120 121 |
# File 'lib/consul/client/connection.rb', line 119 def http @http ||= Consul::Http.new end |
#is_online? ⇒ Boolean
131 132 133 134 135 136 137 |
# File 'lib/consul/client/connection.rb', line 131 def is_online? if defined?(Chef) !::Chef::Config[:solo] else false end end |
#local_path ⇒ Object
123 124 125 |
# File 'lib/consul/client/connection.rb', line 123 def local_path ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client" end |
#offline_delete(keypath, opts = {}) ⇒ Object
64 65 66 67 |
# File 'lib/consul/client/connection.rb', line 64 def offline_delete(keypath, opts={}) #do nothing, lets not delete our local files true end |
#offline_read(keypath, opts = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/consul/client/connection.rb', line 15 def 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 = Consul::Response.new(Consul::Client::FakeNetHttpResponse.new(Psych.dump(key_data))) end |
#offline_write(keypath, data = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/consul/client/connection.rb', line 82 def 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) } Consul::Response.new(Consul::Client::FakeNetHttpResponse.new) end |
#online_delete(keypath, opts = {}) ⇒ Object
60 61 62 |
# File 'lib/consul/client/connection.rb', line 60 def online_delete(keypath, opts={}) response = http.delete("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : '')).value end |
#online_read(keypath, opts = {}) ⇒ Object
11 12 13 |
# File 'lib/consul/client/connection.rb', line 11 def online_read(keypath, opts={}) response = http.get("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : '')) end |
#online_write(keypath, data = nil, path_mimic = false) ⇒ Object
77 78 79 80 |
# File 'lib/consul/client/connection.rb', line 77 def online_write(keypath,data=nil,path_mimic=false) data = encode(data) http.put("/kv" + path(keypath), data) end |
#path(keypath) ⇒ Object
127 128 129 |
# File 'lib/consul/client/connection.rb', line 127 def path(keypath) File.join("/" + config.namespace, keypath).gsub(%r{//}, '/') end |
#read(keypath, opts = {}) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/consul/client/connection.rb', line 51 def read(keypath, opts={}) obj = read_obj(keypath, opts) if obj.respond_to?(:value) obj.value else obj end end |
#read_obj(keypath, opts = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/consul/client/connection.rb', line 36 def read_obj(keypath, opts={}) keyname = File.basename(keypath) response = if is_online? online_read(keypath, opts) else offline_read(keypath, opts) end if response.length == 1 && response.keys.first == keyname response[keyname] else Hash[response.map{ |k,v| [k, v.value] }] end end |
#write(keypath, data = nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/consul/client/connection.rb', line 94 def write(keypath,data=nil) if is_online? online_write(keypath,data) else offline_write(keypath,data) end end |