Class: Etcd::Cluster
- Inherits:
-
Object
- Object
- Etcd::Cluster
- Extended by:
- Constants, Loggable, Requestable
- Defined in:
- lib/etcd/cluster.rb
Constant Summary
Constants included from Constants
Etcd::Constants::S_ACTION, Etcd::Constants::S_AND, Etcd::Constants::S_DIR, Etcd::Constants::S_EXPIRATION, Etcd::Constants::S_INDEX, Etcd::Constants::S_KEY, Etcd::Constants::S_KEYS, Etcd::Constants::S_LOCATION, Etcd::Constants::S_NODE, Etcd::Constants::S_NODES, Etcd::Constants::S_PREV_NODE, Etcd::Constants::S_SLASH, Etcd::Constants::S_TTL, Etcd::Constants::S_VALUE
Instance Attribute Summary collapse
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#seed_uri ⇒ Object
Returns the value of attribute seed_uri.
Class Method Summary collapse
-
.cluster_status(uri) ⇒ Array
Of node attributes as [Hash].
-
.init_from_uris(*uris) ⇒ Cluster
creates new cluster with updated status is preferred way to create a cluster instance.
- .nodes_from_attributes(node_attributes) ⇒ Object
-
.nodes_from_uri(uri) ⇒ Array
Of [Node] instances, representing cluster status.
- .parse_cluster_status(cluster_status_response) ⇒ Object
- .status_uri(uri) ⇒ Object
Instance Method Summary collapse
-
#initialize(uri) ⇒ Cluster
constructor
A new instance of Cluster.
-
#leader ⇒ Node
leader instance in cluster.
- #update_status ⇒ Object
Methods included from Loggable
Methods included from Requestable
http_client, request, request_data, reset_http_client!
Constructor Details
#initialize(uri) ⇒ Cluster
Returns a new instance of Cluster.
70 71 72 |
# File 'lib/etcd/cluster.rb', line 70 def initialize(uri) @seed_uri = uri end |
Instance Attribute Details
#nodes ⇒ Object
Returns the value of attribute nodes.
3 4 5 |
# File 'lib/etcd/cluster.rb', line 3 def nodes @nodes end |
#seed_uri ⇒ Object
Returns the value of attribute seed_uri.
4 5 6 |
# File 'lib/etcd/cluster.rb', line 4 def seed_uri @seed_uri end |
Class Method Details
.cluster_status(uri) ⇒ Array
Returns of node attributes as [Hash].
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/etcd/cluster.rb', line 15 def cluster_status(uri) begin logger.debug("cluster_status - from #{uri}") data = request_data(:get, status_uri(uri)) parse_cluster_status(data) rescue Errno::ECONNREFUSED => e logger.debug("cluster_status - error!") nil end end |
.init_from_uris(*uris) ⇒ Cluster
creates new cluster with updated status is preferred way to create a cluster instance
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/etcd/cluster.rb', line 58 def init_from_uris(*uris) Array(uris).each do |uri| if Etcd::Cluster.cluster_status(uri) instance = Etcd::Cluster.new(uri) instance.update_status return instance end end raise AllNodesDownError, "could not initialize a cluster from #{uris.join(", ")}" end |
.nodes_from_attributes(node_attributes) ⇒ Object
46 47 48 49 50 |
# File 'lib/etcd/cluster.rb', line 46 def nodes_from_attributes(node_attributes) res = node_attributes.map do |attrs| Etcd::Node.new(attrs) end end |
.nodes_from_uri(uri) ⇒ Array
Returns of [Node] instances, representing cluster status.
41 42 43 44 |
# File 'lib/etcd/cluster.rb', line 41 def nodes_from_uri(uri) node_attributes = cluster_status(uri) nodes_from_attributes(node_attributes) end |
.parse_cluster_status(cluster_status_response) ⇒ Object
30 31 32 33 34 |
# File 'lib/etcd/cluster.rb', line 30 def parse_cluster_status(cluster_status_response) cluster_status_response['members'].map do |attrs| Etcd::Node.parse_node_data(attrs) end end |
.status_uri(uri) ⇒ Object
26 27 28 |
# File 'lib/etcd/cluster.rb', line 26 def status_uri(uri) "#{uri}/v2/members/" end |
Instance Method Details
#leader ⇒ Node
leader instance in cluster
88 89 90 |
# File 'lib/etcd/cluster.rb', line 88 def leader nodes.select{|x| x.is_leader}.first end |
#update_status ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/etcd/cluster.rb', line 78 def update_status @nodes = begin nodes = Etcd::Cluster.nodes_from_uri(seed_uri) nodes.map{|x| x.update_status} nodes end end |