cancel
Showing results for 
Search instead for 
Did you mean: 

HowTo: Drain a node in multiple pools (Python REST API example)

The 'draining' property of a node is configured per-pool.  If the node is used in several pools and you want to prepare it to take out of service, you need to edit each pool to set it to be draining.

With the Stingray UI, you can use the 'Drain a Node' and 'Undrain a Node' wizards.  These checks all pools and ensures that the node is updated in all of them.

This Python script does the same thing:


#!/usr/bin/python



import requests


import json


import sys



def usage():


  print "Syntax: %s drain|undrain nodename:[port]" % sys.argv[0]


  sys.exit( -1 )




if len( sys.argv ) != 3:


  usage()



action = sys.argv[1]


target = sys.argv[2]



if action == "drain":


  print "Draining node " + target + ":"


elif action == "undrain":


  print "Stopping draining node " + target + ":"


else:


  usage()



url = 'https://stingray:9070/api/tm/1.0/config/active/pools/';


client = requests.Session()


client.auth = ('admin', 'admin')


client.verify = 0



response = client.get(url)


pools = json.loads(response.content)



def nodeInList( node, list ):


  for n in list:


    if n.startswith( node ):


      return n


  return ''



for pool in pools['children']:


  print "  checking pool "+pool['name']


  response = client.get(url+pool['name'])


  data = json.loads(response.content)



  matchednode = nodeInList( target, data['properties']['basic']['nodes'] )


  if matchednode == '':


    continue



  if action == 'drain' and nodeInList( matchednode, data['properties']['basic']['draining'] ) == '':


    data['properties']['basic']['draining'].append( matchednode )


    print "    draining node %s in pool %s" % ( matchednode, pool['name'] )


    client.put( url+pool['name'], data = json.dumps( data ), headers = {'content-type': 'application/json'} )



  if action == 'undrain' and nodeInList( matchednode, data['properties']['basic']['draining'] ) != '':


    data['properties']['basic']['draining'].remove( matchednode )


    print "    undraining node %s in pool %s" % ( matchednode, pool['name'] )


    client.put( url+pool['name'], data = json.dumps( data ), headers = {'content-type': 'application/json'} )


Use it as follows:


$ ./setDraining.py drain splash.riverbed.com


Draining node splash.riverbed.com:


  checking pool web site pool


  checking pool Forward Proxy Pool


  checking pool splash


    draining node splash.riverbed.com:443 in pool splash


  checking pool SSH


  checking pool Internal HTTPS loopback


  checking pool WebSocket test


  checking pool splash proxy


    draining node splash.riverbed.com:443 in pool splash proxy


$ ./setDraining.py undrain splash.riverbed.com


Stopping draining node splash.riverbed.com:


  checking pool web site pool


  checking pool Forward Proxy Pool


  checking pool splash


    undraining node splash.riverbed.com:443 in pool splash


  checking pool SSH


  checking pool Internal HTTPS loopback


  checking pool WebSocket test


  checking pool splash proxy


    undraining node splash.riverbed.com:443 in pool splash proxy


Version history
Revision #:
1 of 1
Last update:
‎04-30-2013 12:15:AM
Updated by:
 
Labels (1)