Merge branch 'master' of ssh://git.onelab.eu/git/infrastructure
[infrastructure.git] / scripts / vsys_vnet_admin.py
1
2 # vsys_vnet_admin.py - basic command-line client to show assigned network
3 # segments to the vsys_vnet tag in PlanetLab, and to assign new vsys_vnet tags
4 # using available segments
5
6 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
7 #
8
9 import ipaddr
10 from optparse import OptionParser
11 import os
12 import pprint
13 import sys
14 import xmlrpclib
15
16 def plapi(url):
17     """ Returns a XMLRPC proxy to the PLAPI service on the given url """
18     return xmlrpclib.ServerProxy(url, allow_none = True)
19
20 def get_assigned(plapi, auth):
21     """ Returns a dictionary with the assigned network segments as key
22     and the slice name as value"""
23     info = plapi.GetSliceTags(auth, {'tagname': 'vsys_vnet'}, ('name', 'value'))
24     return dict((d['value'], d['name']) for d in info)
25
26 def get_next_available(assigned, base, prefix):
27     """ Returns the next available network segment to be assigned to a 
28     new vsys_vnet tag """
29     net = ipaddr.IPNetwork(base)
30     iter_sub = net.iter_subnets(new_prefix=prefix)
31
32     print assigned
33     try:
34         while True:
35             sub = iter_sub.next()
36             print sub
37             if not [ s for s in map(ipaddr.IPNetwork, assigned.keys()) \
38                     if sub == s or s in sub or sub in s]:
39                return sub.exploded 
40     except StopIteration:
41         pass
42
43     return None
44
45 def assign_vsys_vent(plapi, auth, vsys_vnet, pl_slice):
46     return plapi.AddSliceTag(auth, pl_slice, 'vsys_vnet', vsys_vnet)
47
48 if __name__ == "__main__":
49     pl_user = os.environ.get('PL_USER')
50     pl_pwd = os.environ.get('PL_PASS')
51     pl_url = "https://www.planet-lab.eu:443/PLCAPI/"
52     pl_slice = os.environ.get('PL_SLICE')
53
54     usage = "usage: %prog -u <user> -p <password> -l <url>  --show --assign -b <base_address> -n <prefix>"
55     parser = OptionParser(usage=usage)
56     parser.add_option("-u", "--user", dest="pl_user", help="PlanetLab account user name", default=pl_user)
57     parser.add_option("-p", "--password", dest="pl_pwd", help="PlanetLab account password", default=pl_pwd)
58     parser.add_option("-l", "--url", dest="pl_url", help="PlanetLab XMLRPC url", default=pl_url)
59     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")
60     parser.add_option("-n", "--prefix", dest="prefix", help="Network prefix for segments (defaults to 21)", default = "21")
61     parser.add_option("-s", "--show", action="store_false", dest="show", default=True,
62         help="Show all assigned vsys_vnet tags")
63     parser.add_option("-a", "--assign", action="store_true", dest="assign", default=False,
64         help="Assign next available network segment as a vsys_vnet tag to the user slice")
65
66     (options, args) = parser.parse_args()
67    
68     if not options.pl_user or not options.pl_pwd:
69         print "Error: PlanetLab user and password are required to proceed."
70         sys.exit(1)
71
72     api = plapi(options.pl_url)
73     
74     auth = dict(AuthMethod='password', Username=options.pl_user, AuthString=options.pl_pwd)
75     assigned = get_assigned(api, auth)
76    
77     if options.assign:
78         vsys_vnet = get_next_available(assigned, options.basenet, int(options.prefix))
79         if not vsys_vnet:
80             print "Error: There are no available network segments at the moment to assign"
81             sys.exit(1)
82
83         print "The next available network segment is %s." % vsys_vnet
84         proceed = raw_input ("Do you wish to proceed with the assignment of the vsys_vnet tag? [y/N] ")
85         if proceed.lower() in ['yes', 'y']:
86             if not pl_slice:
87                 pl_slice = raw_input ("Please, enter your slice name \n")
88
89                 if pl_slice in assigned.values():
90                     print "Error: This slice already has a tag vsys_vnet assigned!!!"
91                     sys.exit(1)
92
93             slice_tag_id = assign_vsys_vent(api, auth, vsys_vnet, pl_slice)
94
95             if slice_tag_id and slice_tag_id > 0:
96                 print "Success: the new slice tag id is %d" % slice_tag_id
97             else:
98                 print "Error: Something wrong happened!"
99
100     else:
101         pp = pprint.PrettyPrinter(indent=4)
102         pp.pprint(assigned)
103
104
105