From: Thierry Parmentelat Date: Wed, 11 Jul 2012 22:05:32 +0000 (+0200) Subject: added on behalf of Alina X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=e172fbb69b5d2debe7418a184a5c9ac649103908;p=infrastructure.git added on behalf of Alina --- diff --git a/scripts/vsys_vnet_admin.py b/scripts/vsys_vnet_admin.py new file mode 100755 index 0000000..509e15d --- /dev/null +++ b/scripts/vsys_vnet_admin.py @@ -0,0 +1,105 @@ +# +# vsys_vnet_admin.py - basic command-line client to show assigned network +# segments to the vsys_vnet tag in PlanetLab, and to assign new vsys_vnet tags +# using available segments +# +# Author: Alina Quereilhac +# + +import ipaddr +from optparse import OptionParser +import os +import pprint +import sys +import xmlrpclib + +def plapi(url): + """ Returns a XMLRPC proxy to the PLAPI service on the given url """ + return xmlrpclib.ServerProxy(url, allow_none = True) + +def get_assigned(plapi, auth): + """ Returns a dictionary with the assigned network segments as key + and the slice name as value""" + info = plapi.GetSliceTags(auth, {'tagname': 'vsys_vnet'}, ('name', 'value')) + return dict((d['value'], d['name']) for d in info) + +def get_next_available(assigned, base, prefix): + """ Returns the next available network segment to be assigned to a + new vsys_vnet tag """ + net = ipaddr.IPNetwork(base) + iter_sub = net.iter_subnets(new_prefix=prefix) + + print assigned + try: + while True: + sub = iter_sub.next() + print sub + if not [ s for s in map(ipaddr.IPNetwork, assigned.keys()) \ + if sub == s or s in sub or sub in s]: + return sub.exploded + except StopIteration: + pass + + return None + +def assign_vsys_vent(plapi, auth, vsys_vnet, pl_slice): + return plapi.AddSliceTag(auth, pl_slice, 'vsys_vnet', vsys_vnet) + +if __name__ == "__main__": + pl_user = os.environ.get('PL_USER') + pl_pwd = os.environ.get('PL_PASS') + pl_url = "https://www.planet-lab.eu:443/PLCAPI/" + pl_slice = os.environ.get('PL_SLICE') + + usage = "usage: %prog -u -p -l --show --assign -b -n " + parser = OptionParser(usage=usage) + parser.add_option("-u", "--user", dest="pl_user", help="PlanetLab account user name", default=pl_user) + parser.add_option("-p", "--password", dest="pl_pwd", help="PlanetLab account password", default=pl_pwd) + parser.add_option("-l", "--url", dest="pl_url", help="PlanetLab XMLRPC url", default=pl_url) + parser.add_option("-b", "--base", dest="basenet", help="Base network address to perform assignment (defaults to 10.0.0.0/8)", default="10.0.0.0/8") + parser.add_option("-n", "--prefix", dest="prefix", help="Network prefix for segments (defaults to 21)", default = "21") + parser.add_option("-s", "--show", action="store_false", dest="show", default=True, + help="Show all assigned vsys_vnet tags") + parser.add_option("-a", "--assign", action="store_true", dest="assign", default=False, + help="Assign next available network segment as a vsys_vnet tag to the user slice") + + (options, args) = parser.parse_args() + + if not options.pl_user or not options.pl_pwd: + print "Error: PlanetLab user and password are required to proceed." + sys.exit(1) + + api = plapi(options.pl_url) + + auth = dict(AuthMethod='password', Username=options.pl_user, AuthString=options.pl_pwd) + assigned = get_assigned(api, auth) + + if options.assign: + vsys_vnet = get_next_available(assigned, options.basenet, int(options.prefix)) + if not vsys_vnet: + print "Error: There are no available network segments at the moment to assign" + sys.exit(1) + + print "The next available network segment is %s." % vsys_vnet + proceed = raw_input ("Do you wish to proceed with the assignment of the vsys_vnet tag? [y/N] ") + if proceed.lower() in ['yes', 'y']: + if not pl_slice: + pl_slice = raw_input ("Please, enter your slice name \n") + + if pl_slice in assigned.values(): + print "Error: This slice already has a tag vsys_vnet assigned!!!" + sys.exit(1) + + slice_tag_id = assign_vsys_vent(api, auth, vsys_vnet, pl_slice) + + if slice_tag_id and slice_tag_id > 0: + print "Success: the new slice tag id is %d" % slice_tag_id + else: + print "Error: Something wrong happened!" + + else: + pp = pprint.PrettyPrinter(indent=4) + pp.pprint(assigned) + + +