Skip to content

Instantly share code, notes, and snippets.

@smashwilson
Created September 2, 2015 13:34
Show Gist options
  • Save smashwilson/c8b5addd511221db3002 to your computer and use it in GitHub Desktop.
Save smashwilson/c8b5addd511221db3002 to your computer and use it in GitHub Desktop.
Update the condition of a load balancer node with libcloud
import os
import time
from libcloud.loadbalancer.types import State, Provider, MemberCondition
from libcloud.loadbalancer.providers import get_driver
username = os.environ['RACKSPACE_USERNAME']
apikey = os.environ['RACKSPACE_APIKEY']
region = os.environ['RACKSPACE_REGION']
clb_name = os.environ['CLB_NAME']
server_addr = os.environ['SERVER_ADDR']
server_port = os.environ['SERVER_PORT']
driver = get_driver(Provider.RACKSPACE_US)(username, apikey, region=region)
# Find the load balancer.
lb = None
lbs = driver.list_balancers()
for each in lbs:
if each.name == clb_name:
lb = each
if not lb:
raise RuntimeError("Unable to find the load balancer")
print "Updating load balancer:", lb.name
# Find the member we want to update.
member = None
members = lb.list_members()
for each in members:
if each.ip == server_addr and each.port == server_port:
member = each
if not member:
raise RuntimeError("Unable to find the member to update")
print "Updating member:", member.ip
driver.ex_balancer_update_member(lb, member,
condition=MemberCondition.DRAINING)
while True:
balancer = driver.get_balancer(balancer_id=lb.id)
if balancer.state == State.RUNNING:
break
time.sleep(5)
print "Member is now draining connections."
@smashwilson
Copy link
Author

Documentation for the Rackspace load balancer driver, including Rackspace-specific extension methods like ex_balancer_update_member.

@jantzen05
Copy link

Thanks for your solution.
Do you know if it's possible to get the member condition too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment