Class: Etcd::Client
- Inherits:
-
Object
- Object
- Etcd::Client
- Defined in:
- lib/etcd-tools/mixins/etcd.rb
Instance Attribute Summary collapse
-
#cluster ⇒ Object
readonly
Returns the value of attribute cluster.
Instance Method Summary collapse
- #api_execute(path, method, options = {}) ⇒ Object
- #build_http_object(host, port, options = {}) ⇒ Object
- #cluster_http_request(req, options = {}) ⇒ Object
- #get_hash(path = '') ⇒ Object
- #healthy? ⇒ Boolean
-
#initialize(opts = {}) {|@config| ... } ⇒ Client
constructor
A new instance of Client.
- #members ⇒ Object
- #set_hash(hash, path = '') ⇒ Object
Constructor Details
#initialize(opts = {}) {|@config| ... } ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 9 def initialize(opts = {}) @cluster = opts[:cluster] || [{ host: '127.0.0.1', port: 2379 }] if !opts[:host].nil? || !opts[:port].nil? @cluster = [{ host: opts[:host], port: opts[:port] }] end @config = Config.new @config.read_timeout = opts[:read_timeout] || 10 @config.use_ssl = opts[:use_ssl] || false @config.verify_mode = opts.key?(:verify_mode) ? opts[:verify_mode] : OpenSSL::SSL::VERIFY_PEER @config.user_name = opts[:user_name] || nil @config.password = opts[:password] || nil @config.ca_file = opts.key?(:ca_file) ? opts[:ca_file] : nil @config.ssl_cert = opts.key?(:ssl_cert) ? opts[:ssl_cert] : nil @config.ssl_key = opts.key?(:ssl_key) ? opts[:ssl_key] : nil yield @config if block_given? end |
Instance Attribute Details
#cluster ⇒ Object (readonly)
Returns the value of attribute cluster.
7 8 9 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 7 def cluster @cluster end |
Instance Method Details
#api_execute(path, method, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 26 def api_execute(path, method, = {}) params = [:params] case method when :get req = build_http_request(Net::HTTP::Get, path, params) when :post req = build_http_request(Net::HTTP::Post, path, nil, params) when :put req = build_http_request(Net::HTTP::Put, path, nil, params) when :delete req = build_http_request(Net::HTTP::Delete, path, params) else fail "Unknown http action: #{method}" end req.basic_auth(user_name, password) if [user_name, password].all? cluster_http_request(req, ) end |
#build_http_object(host, port, options = {}) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 67 def build_http_object(host, port, ={}) http = Net::HTTP.new(host, port) http.read_timeout = [:timeout] || read_timeout http.open_timeout = [:timeout] || read_timeout # <- can't modify Config constant with specific option setup_https(http) http end |
#cluster_http_request(req, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 44 def cluster_http_request(req, ={}) cluster.each do |member| http = build_http_object(member[:host], member[:port], ) begin Log.debug("Invoking: '#{req.class}' against '#{member[:host]}:#{member[:port]}' -> '#{req.path}'") res = http.request(req) Log.debug("Response code: #{res.code}") Log.debug("Response body: #{res.body}") return process_http_request(res) rescue Timeout::Error Log.debug("Connection timed out on #{member[:host]}:#{member[:host]}") next rescue Errno::ECONNRESET Log.debug("Connection reset on #{member[:host]}:#{member[:host]}") next rescue Errno::ECONNREFUSED Log.debug("Connection refused on #{member[:host]}:#{member[:host]}") next end end fail "Couldn't connect to the ETCDcluster" end |
#get_hash(path = '') ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 91 def get_hash(path = '') h = {} get(path).children.each do |child| if get(child.key).directory? h[child.key.split('/').last.to_s] = get_hash child.key else value = JSON.parse(child.value) rescue value = child.value h[child.key.split('/').last.to_s] = value end end return Hash[h.sort] rescue Exception => e raise e puts e.backtrace end |
#healthy? ⇒ Boolean
112 113 114 115 116 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 112 def healthy? JSON.parse(api_execute('/health', :get).body)['health'] == 'true' rescue false end |
#members ⇒ Object
107 108 109 110 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 107 def members members = JSON.parse(api_execute(version_prefix + '/members', :get).body)['members'] Hash[members.map{|member| [ member['id'], member.tap { |h| h.delete('id') }]}] end |
#set_hash(hash, path = '') ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/etcd-tools/mixins/etcd.rb', line 75 def set_hash(hash, path = '') hash.each do |key, value| path = "" if path == '/' etcd_key = path + '/' + key.to_s if value.class == Hash set_hash(value, etcd_key) elsif value.class == Array set(etcd_key, value: value.to_json) else set(etcd_key, value: value) end end rescue Exception => e raise e #fixme end |