Skip to content

Instantly share code, notes, and snippets.

@shyam-habarakada
Created May 14, 2013 05:49
Show Gist options
  • Save shyam-habarakada/5573971 to your computer and use it in GitHub Desktop.
Save shyam-habarakada/5573971 to your computer and use it in GitHub Desktop.
# Requires the presence of a failover node that will get activated when the site is in maintenance mode, and will be disabled when the site is live.
namespace :deploy do
namespace :web do
desc <<-DESC
Disable via Cloud Load Balancers
DESC
task :lbdisable do
filename = File.expand_path(File.dirname(__FILE__) + "/../config/rackspace_cloudfiles.yml")
creds = YAML.load_file(filename)['development']
lb = CloudLB::Connection.new(:username => creds['username'], :api_key => creds['api_key'], :region => :dfw, :snet => creds['snet'])
balancer_ids = []
lb.load_balancers.each do |load_balancer|
balancer_ids << load_balancer[:id] if [http_load_balancer,https_load_balancer].include?(load_balancer[:name])
end
balancer_ids.each do |balancer_id|
# Enable the failover
balancer = lb.load_balancer(balancer_id)
balancer_nodes = balancer.nodes
failover_node = balancer_nodes.select{|n| n[:address] == failover}.first
if failover_node
balancer.node(failover_node[:id]).condition = "ENABLED"
end
sleep 1
balancer_nodes.delete(failover_node)
# Disable the rest
balancer_nodes.each do |n|
balancer.refresh
until balancer.status == "ACTIVE"
sleep 1
balancer.refresh
end
balancer.node(n[:id]).condition = "DISABLED"
end
sleep 1
end
end
desc <<-DESC
Enable via Cloud Load Balancers
DESC
task :lbenable do
filename = File.expand_path(File.dirname(__FILE__) + "/../config/rackspace_cloudfiles.yml")
creds = YAML.load_file(filename)['development']
lb = CloudLB::Connection.new(:username => creds['username'], :api_key => creds['api_key'], :region => :dfw, :snet => creds['snet'])
balancer_ids = []
lb.load_balancers.each do |load_balancer|
balancer_ids << load_balancer[:id] if [http_load_balancer,https_load_balancer].include?(load_balancer[:name])
end
balancer_ids.each do |balancer_id|
balancer = lb.load_balancer(balancer_id)
balancer_nodes = balancer.nodes
failover_node = balancer_nodes.select{|n| n[:address] == failover}.first
balancer_nodes.delete(failover_node)
sleep 1
# Enable the regular nodes
balancer_nodes.each do |n|
balancer.refresh
until balancer.status == "ACTIVE"
sleep 1
balancer.refresh
end
balancer.node(n[:id]).condition = "ENABLED"
sleep 1
end
# Disable the failover
if failover_node
balancer.refresh
until balancer.status == "ACTIVE"
sleep 1
balancer.refresh
end
balancer.node(failover_node[:id]).condition = "DISABLED"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment