cancel
Showing results for 
Search instead for 
Did you mean: 

Problem this snippet solves:

Summary: This script will auto-register a virtual or physical machine into a pool on startup, or de-register on shutdown. This is for pycontrol v1, it will need to be ported to v2 to work properly.

How to use this snippet:

This script should go into rc.local or equivalent and expects 'start' or 'stop' as an argument. On startup, the system will automatically add itself to a pool you specify, and on shutdown it will pull itself out. This script requires netifaces, available at http://pypi.python.org/.

Script

register_self.py

Code :

#!/bin/env python

'''
----------------------------------------------------------------------------
 The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
 Software Development Kit for iControl"; you may not use this file except in
 compliance with the License. The License is included in the iControl
 Software Development Kit.

 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and limitations
 under the License.

 The Original Code is iControl Code and related documentation
 distributed by F5.

 The Initial Developer of the Original Code is F5 Networks,
 Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2004 F5 Networks,
 Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.

 Alternatively, the contents of this file may be used under the terms
 of the GNU General Public License (the "GPL"), in which case the
 provisions of GPL are applicable instead of those above.  If you wish
 to allow use of your version of this file only under the terms of the
 GPL and not to allow others to use your version of this file under the
 License, indicate your decision by deleting the provisions above and
 replace them with the notice and other provisions required by the GPL.
 If you do not delete the provisions above, a recipient may use your
 version of this file under either the License or the GPL.
----------------------------------------------------------------------------
'''

'''
This script intends to be run with arguments of 'start' or 'stop', with the obvious
intent to be run in an rc.local or whichever *NIX equivalent you're dealing with. 
Run it last in the sequence, after all your services spin up.
'''

import netifaces
import pycontrol.pyControl as pc
import sys

'''
This is a rough cut at a vm instance that will auto-register. Requires netifaces
'''

### Setup your constants here. Change these to reflect your environment. 
LTM  = '192.168.1.245'
USER = 'admin'
PASS= 'admin'
IFACE   = 'eth0'

# Script constants for the pool. Change these to reflect your environment.
POOL   = 'foo'
PORT    = 80

# Load the Pool WSDL
WSDLS= 'LocalLB.Pool'

# bail if we don't have an argument
if not len(sys.argv) == 2:
print "Usage: %s [start|stop]" % sys.argv[0]
sys.exit()

# Get the appropriate interface address that you want to register.
address = netifaces.ifaddresses(IFACE)[2][0]['addr']

# 
# Register yourself with a pool.
#

# Build your LocalLB_Pool object
b = pc.BIGIP(
hostname = LTM,
username = USER,
password = PASS,
wsdl_files = [WSDLS]
)

# Laziness is a virtue, as Larry Wall says.
p = b.LocalLB_Pool



# Let's add ourselves to the pool after we check if we're already there,
# which could happen in the event of a system crash 

if sys.argv[1] == 'start':

mems = p.get_member(pool_names = [ POOL ])['return']

for m in mems[0]:
if address in m.values():
print "Already registered with the LTM. Exiting"
sys.exit()

print "Adding %s address %s to the pool" % (IFACE, address)
p.add_member(
pool_names = [POOL],
members    = [[ { 'address': address, 'port': PORT } ]]
)

# If we're shutting down, remove ourselves.
if sys.argv[1] == 'stop':

print "Removing %s %s from the pool" % (IFACE, address)
p.remove_member(
pool_names = [POOL],
members    = [[ { 'address': address, 'port': PORT } ]]
)
Version history
Last update:
‎09-Mar-2015 12:29
Updated by:
Contributors