New script for port reservation. Use with the vsys_portreservation attribute
[vsys-scripts.git] / exec / claimport
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import string
6
7 vsys_config_dir = "/etc/planetlab/vsys-attributes"
8 slicename=sys.argv[1]
9 port_config=os.path.join(vsys_config_dir,slicename,"vsys_portreservation")
10
11 ports = open(port_config).read().rstrip()
12 (low,high) = map(lambda n:int(n),ports.split('-'))
13
14 arglines = map(string.strip, sys.stdin.readlines())
15 if len(arglines)<1:
16     print >>sys.stderr, "Insufficient argument lines."
17     sys.exit(1)
18
19 port = int(arglines[0]) # interface name
20
21 def do_kill(p):
22    if (p):
23        slice_info = os.popen('chcontext --ctx 1 cat /proc/%s/vinfo'%p).readlines()
24        xid_info = slice_info[0]
25        slice_id = xid_info.split()[1]
26        os.system("vkill %s"%p)
27        slice = os.popen('getent passwd %s'%slice_id).read().split(':')[0]
28        mail_cmd = 'mail -s "PlanetLab notification" %s@slices.planet-lab.org -c %s@slices.planet-lab.org'%(slice,slicename)
29        f = os.popen(mail_cmd,'w')
30        f.write('Hi,\nOne of your processes on PlanetLab was terminated because you were using port %s, which is assigned to %s.'%(port,slicename))
31        f.close()
32
33
34 if (low<=port<=high):
35    fuser_cmd = "ncontext --nid 1 --migrate -- chcontext --ctx 1 fuser -n tcp %d 2> /dev/null"%port
36    process_udp = os.popen(fuser_cmd).read().rstrip()
37
38    fuser_cmd = "ncontext --nid 1 --migrate -- chcontext --ctx 1 fuser -n udp %d 2> /dev/null"%port
39    process_tcp = os.popen(fuser_cmd).read().rstrip()
40
41    do_kill(process_tcp.lstrip())
42    do_kill(process_udp.lstrip())
43    
44 else:
45     print "%d is not in your range"%port