Class: Etcd::Client

Inherits:
Object
  • Object
show all
Includes:
Keys, Mod::Leader, Mod::Lock, Stats
Defined in:
lib/etcd/client.rb

Overview

This is the central ruby class for Etcd. It provides methods for all etcd api calls. It also provides few additional methods beyond the core etcd api, like Etcd::Client#lock and Etcd::Client#eternal_watch, they are defined in separate modules and included in this class

Constant Summary collapse

HTTP_REDIRECT =
->(r) { r.is_a? Net::HTTPRedirection }
HTTP_SUCCESS =
->(r) { r.is_a? Net::HTTPSuccess }
HTTP_CLIENT_ERROR =
->(r) { r.is_a? Net::HTTPClientError }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mod::Leader

#delete_leader, #get_leader, #mod_leader_endpoint, #set_leader

Methods included from Mod::Lock

#acquire_lock, #delete_lock, #get_lock, #lock, #mod_lock_endpoint, #renew_lock

Methods included from Keys

#compare_and_swap, #create, #create_in_order, #delete, #eternal_watch, #exists?, #get, #key_endpoint, #set, #update, #watch

Methods included from Stats

#stats, #stats_endpoint

Constructor Details

#initialize(opts = {}) ⇒ Client

Creates an Etcd::Client object. It accepts a hash opts as argument

rubocop:disable CyclomaticComplexity

Parameters:

  • opts (Hash) (defaults to: {})

    The options for new Etcd::Client object



41
42
43
44
45
46
47
48
49
50
# File 'lib/etcd/client.rb', line 41

def initialize(opts = {})
  @host = opts[:host] || '127.0.0.1'
  @port = opts[:port] || 4001
  @read_timeout = opts[:read_timeout] || 60
  @allow_redirect = opts.key?(:allow_redirect) ? opts[:allow_redirect] : true
  @use_ssl = opts[:use_ssl] || false
  @verify_mode = opts.key?(:verify_mode) ? opts[:verify_mode] : OpenSSL::SSL::VERIFY_PEER
  @user_name = opts[:user_name] || nil
  @password = opts[:password] || nil
end

Instance Attribute Details

#allow_redirectObject (readonly)

Returns the value of attribute allow_redirect.



29
30
31
# File 'lib/etcd/client.rb', line 29

def allow_redirect
  @allow_redirect
end

#hostObject (readonly)

Returns the value of attribute host.



29
30
31
# File 'lib/etcd/client.rb', line 29

def host
  @host
end

#httpObject (readonly)

Returns the value of attribute http.



29
30
31
# File 'lib/etcd/client.rb', line 29

def http
  @http
end

#passwordObject (readonly)

Returns the value of attribute password.



31
32
33
# File 'lib/etcd/client.rb', line 31

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



29
30
31
# File 'lib/etcd/client.rb', line 29

def port
  @port
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



30
31
32
# File 'lib/etcd/client.rb', line 30

def read_timeout
  @read_timeout
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



30
31
32
# File 'lib/etcd/client.rb', line 30

def use_ssl
  @use_ssl
end

#user_nameObject (readonly)

Returns the value of attribute user_name.



31
32
33
# File 'lib/etcd/client.rb', line 31

def user_name
  @user_name
end

#verify_modeObject (readonly)

Returns the value of attribute verify_mode.



30
31
32
# File 'lib/etcd/client.rb', line 30

def verify_mode
  @verify_mode
end

Instance Method Details

#api_execute(path, method, options = {}) ⇒ Object

This method sends api request to etcd server.

This method has following parameters as argument

  • path - etcd server path (etcd server end point)

  • method - the request method used

  • options - any additional parameters used by request method (optional)

rubocop:disable MethodLength, CyclomaticComplexity



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/etcd/client.rb', line 80

def api_execute(path, method, options = {})
  params = options[: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
  timeout = options[:timeout] || @read_timeout
  http = Net::HTTP.new(host, port)
  http.read_timeout = timeout
  http.use_ssl = use_ssl
  http.verify_mode = verify_mode
  req.basic_auth(user_name, password) if [user_name, password].all?
  Log.debug("Invoking: '#{req.class}' against '#{path}")
  res = http.request(req)
  Log.debug("Response code: #{res.code}")
  process_http_request(res, req, params)
end

#build_http_request(klass, path, params = nil, body = nil) ⇒ Object

rubocop:enable MethodLength



133
134
135
136
137
138
139
# File 'lib/etcd/client.rb', line 133

def build_http_request(klass, path, params = nil, body = nil)
  path += '?' + URI.encode_www_form(params) unless params.nil?
  req = klass.new(path)
  req.body = URI.encode_www_form(body) unless body.nil?
  Etcd::Log.debug("Built #{klass} path:'#{path}'  body:'#{req.body}'")
  req
end

#leaderObject

Get the current leader



69
70
71
# File 'lib/etcd/client.rb', line 69

def leader
  api_execute(version_prefix + '/leader', :get).body.strip
end

#machinesObject

Returns array of all machines in the cluster



64
65
66
# File 'lib/etcd/client.rb', line 64

def machines
  api_execute(version_prefix + '/machines', :get).body.split(',').map(&:strip)
end

#process_http_request(res, req = nil, params = nil) ⇒ Object

need to ahve original request to process the response when it redirects



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/etcd/client.rb', line 107

def process_http_request(res, req = nil, params = nil)
  case res
  when HTTP_SUCCESS
    Log.debug('Http success')
    res
  when HTTP_REDIRECT
    if allow_redirect
      uri = URI(res['location'])
      @host = uri.host
      @port = uri.port
      Log.debug("Http redirect, setting new host to: #{@host}:#{@port}, and retrying")
      api_execute(uri.path, req.method.downcase.to_sym, params: params)
    else
      Log.debug('Http redirect not allowed')
      res.error!
    end
  when HTTP_CLIENT_ERROR
    fail Error.from_http_response(res)
  else
    Log.debug('Http error')
    Log.debug(res.body)
    res.error!
  end
end

#versionObject

Returns the etcd daemon version



59
60
61
# File 'lib/etcd/client.rb', line 59

def version
  api_execute('/version', :get).body
end

#version_prefixObject

Returns the etcd api version that will be used for across API methods



54
55
56
# File 'lib/etcd/client.rb', line 54

def version_prefix
  '/v2'
end