Class: Elasticsearch::Manager::ESManager
- Inherits:
-
Object
- Object
- Elasticsearch::Manager::ESManager
- Includes:
- Elasticsearch::Model
- Defined in:
- lib/elasticsearch/manager/manager.rb,
lib/elasticsearch/manager/rollingrestart.rb
Instance Attribute Summary collapse
-
#leader ⇒ Object
Returns the value of attribute leader.
-
#members ⇒ Object
Returns the value of attribute members.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
Instance Method Summary collapse
- #cluster_green? ⇒ Boolean
- #cluster_health ⇒ Object
- #cluster_members! ⇒ Object
- #cluster_stable? ⇒ Boolean
- #cluster_state ⇒ Object
- #cluster_status ⇒ Object
- #disable_routing ⇒ Object
- #enable_routing ⇒ Object
-
#initialize(cluster_host = 'localhost', port = 9200) ⇒ ESManager
constructor
A new instance of ESManager.
- #restart_node(node_ip, timeout, sleep_interval) ⇒ Object
- #rolling_restart(timeout = 600, sleep_interval = 30) ⇒ Object
Constructor Details
#initialize(cluster_host = 'localhost', port = 9200) ⇒ ESManager
11 12 13 14 15 16 |
# File 'lib/elasticsearch/manager/manager.rb', line 11 def initialize(cluster_host = 'localhost', port = 9200) @client = Elasticsearch::Client::ESClient.new(cluster_host, port) @leader = nil @nodes = nil @members = nil end |
Instance Attribute Details
#leader ⇒ Object
Returns the value of attribute leader.
9 10 11 |
# File 'lib/elasticsearch/manager/manager.rb', line 9 def leader @leader end |
#members ⇒ Object
Returns the value of attribute members.
9 10 11 |
# File 'lib/elasticsearch/manager/manager.rb', line 9 def members @members end |
#nodes ⇒ Object
Returns the value of attribute nodes.
9 10 11 |
# File 'lib/elasticsearch/manager/manager.rb', line 9 def nodes @nodes end |
Instance Method Details
#cluster_green? ⇒ Boolean
18 19 20 |
# File 'lib/elasticsearch/manager/manager.rb', line 18 def cluster_green? @client.green? end |
#cluster_health ⇒ Object
39 40 41 42 |
# File 'lib/elasticsearch/manager/manager.rb', line 39 def cluster_health health = @client.health Health.new.extend(Health::Representer).from_hash(health) end |
#cluster_members! ⇒ Object
32 33 34 35 36 37 |
# File 'lib/elasticsearch/manager/manager.rb', line 32 def cluster_members! state = cluster_state @nodes = state.nodes @leader = @nodes.select { |n| n.master }[0].ip @members = @nodes.map { |n| n.ip } end |
#cluster_stable? ⇒ Boolean
26 27 28 29 30 |
# File 'lib/elasticsearch/manager/manager.rb', line 26 def cluster_stable? health = cluster_health moving = [health.relocating_shards, health.initializing_shards, health.unassigned_shards] cluster_green? && moving.all? { |x| x == 0 } end |
#cluster_state ⇒ Object
44 45 46 47 |
# File 'lib/elasticsearch/manager/manager.rb', line 44 def cluster_state state = @client.state ClusterState.new.extend(ClusterState::Representer).from_hash(state) end |
#cluster_status ⇒ Object
22 23 24 |
# File 'lib/elasticsearch/manager/manager.rb', line 22 def cluster_status @client.status end |
#disable_routing ⇒ Object
49 50 51 52 |
# File 'lib/elasticsearch/manager/manager.rb', line 49 def disable_routing ret = @client.routing(true) ret['transient']['cluster']['routing']['allocation']['enable'] == 'none' end |
#enable_routing ⇒ Object
54 55 56 57 |
# File 'lib/elasticsearch/manager/manager.rb', line 54 def enable_routing ret = @client.routing(false) ret['transient']['cluster']['routing']['allocation']['enable'] == 'all' end |
#restart_node(node_ip, timeout, sleep_interval) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/elasticsearch/manager/rollingrestart.rb', line 30 def restart_node(node_ip, timeout, sleep_interval) puts "\nRestarting Elasticsearch on node: #{node_ip}" raise "Could not disable shard routing prior to restarting node: #{node_ip}".colorize(:red) unless disable_routing Net::SSH.start(node_ip, ENV['USER']) do |ssh| ssh.exec 'sudo service elasticsearch restart' end puts "Elasticsearch restarted on node: #{node_ip}" begin wait_for_node_available(node_ip, timeout, sleep_interval) puts "Node back up!".colorize(:green) rescue Timeout::Error raise NodeAvailableTimeout, "Node did not become available after waiting #{timeout} seconds...".colorize(:red) end raise "Could not re-enable shard routing following restart of node: #{node_ip}".colorize(:red) unless enable_routing begin wait_for_stable(timeout, sleep_interval) puts "Cluster stabilized!".colorize(:green) rescue Timeout::Error raise StabalizationTimeout, "Cluster not re-stabilize after waiting #{timeout} seconds...".colorize(:red) end end |
#rolling_restart(timeout = 600, sleep_interval = 30) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/elasticsearch/manager/rollingrestart.rb', line 14 def rolling_restart(timeout = 600, sleep_interval = 30) highline = HighLine.new @members.each do |m| unless m == @leader unless highline.agree('Continue with rolling restart of cluster? (y/n) ') raise UserRequestedStop, "Stopping rolling restart at user request!".colorize(:red) end restart_node(m, timeout, sleep_interval) end end unless highline.agree("\nRestarting current cluster master, continue? (y/n) ") raise UserRequestedStop, "Stopping rolling restart at user request before restarting master node!".colorize(:red) end restart_node(@leader, timeout, sleep_interval) end |