Created
August 16, 2013 15:34
-
-
Save maxmanders/6250934 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use Zookeeper to add/remove from the cluster as required. | |
require 'zk' | |
zookeeper_node = search(:node, "role:zookeeper AND chef_environment:#{node.chef_environment}")[0] | |
listen_ip = zookeeper_node[:ipaddress] | |
zk = ZK.new("#{listen_ip}:2181") | |
# Cluster members reported by Zookeeper. | |
active_cluster_members = zk.children(node[:zk_watcher][:zk_node_path]).collect { |x| x.split(':')[0] } | |
# Cluster members reported by the node. | |
node_cluster_members = eval %x[rabbitmqctl cluster_status -q | grep running_nodes | grep -o "\[.*\]"] | |
# This node is in a cluster by itself, it needs to join | |
# one of the cluster nodes. | |
if node_cluster_members.length == 1 | |
bash "join_cluster" do | |
user "root" | |
code <<-EOH | |
rabbitmqctl stop_app | |
rabbitmqctl reset | |
rabbitmqctl join_cluster #{active_cluster_members.to_set.first} | |
rabbitmqctl start_app | |
EOH | |
returns [0,1,2,3] | |
end | |
else | |
# At last one node needs to add any missing nodes to the cluster. All nodes | |
# can try, and we'll ignore those that fail because the node is already a member. | |
nodes_to_add = active_cluster_members.to_set - node_cluster_members.to_set | |
nodes_to_add.each do |new_node| | |
bash "add_node_to_cluster" do | |
user "root" | |
code <<-EOH | |
rabbitmqctl stop_app | |
rabbitmqctl reset | |
rabbitmqctl join_cluster #{new_node} | |
rabbitmqctl start_app | |
EOH | |
returns [0,1,2,3] | |
end | |
end | |
# At least one node needs to forget any stale nodes on behalf of the cluster. | |
# All nodes can try, and we'll ignore those that fail because the node has left. | |
nodes_to_remove = node_cluster_members.to_set - active_cluster_members.to_set | |
nodes_to_remove.each do |dead_node| | |
bash "remove_node_from_cluster" do | |
user "root" | |
code <<-EOH | |
rabbitmqctl join_cluster forget_cluster_node #{dead_node} | |
EOH | |
returns [0,1,2,3] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment