systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / src / nepi / resources / planetlab / scripts / pl-vif-up.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 import vsys
20
21 from optparse import OptionParser
22
23 def get_options():
24     usage = ("usage: %prog -u <slicename> -N <vif-name> -t <vif-type> -a <ip4-address> "
25                     "-n <net-prefix> -S <snat> -p <pointopoint> -q <txqueuelen> "
26                     "-g <gre_key> -G <gre_remote> -f <vif-name-file> ")
27  
28     parser = OptionParser(usage = usage)
29
30     parser.add_option("-u", "--slicename", dest="slicename",
31         help = "The name of the PlanetLab slice ",
32         type="str")
33
34     parser.add_option("-N", "--vif-name", dest="vif_name",
35         help = "The name of the virtual interface, or a "
36                 "unique numeric identifier to name the interface "
37                 "if GRE mode is used.",
38         type="str")
39
40     parser.add_option("-t", "--vif-type", dest="vif_type",
41             help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
42             "Defaults to IFF_TAP. ", type="str")
43
44     parser.add_option("-a", "--ip4-address", dest="ip4_address",
45             help = "IPv4 address to assign to interface. It must belong to the "
46                 "network segment owned by the slice, given by the vsys_vnet tag. ",
47             type="str")
48
49     parser.add_option("-n", "--net-prefix", dest="net_prefix",
50             help = "IPv4 network prefix for the interface. It must be the one "
51                 "given by the slice's vsys_vnet tag. ",
52             type="int")
53
54     parser.add_option("-s", "--snat", dest="snat", 
55             action="store_true", 
56             default = False,
57             help="Enable SNAT for the interface")
58
59     parser.add_option("-p", "--pointopoint", dest="pointopoint",
60             help = "Peer end point for the interface. ", 
61             default = None,
62             type="str")
63
64     parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
65         help = "Size of transmision queue. Defaults to 0.",
66         default = 0,
67         type="int")
68
69     parser.add_option("-g", "--gre-key", dest="gre_key",
70         help = "When set, enables GRE mode with the corresponding GRE key.", 
71         default = None,
72         type="str")
73
74     parser.add_option("-G", "--gre-remote", dest="gre_remote",
75         help = "Remote endpoint (public IP) for the GRE tunnel.", 
76         default = None,
77         type="str")
78
79     parser.add_option("-f", "--vif-name-file", dest="vif_name_file",
80         help = "File to store the virtual interface name assigned by the OS", 
81         default = "vif_name", type="str")
82
83     (options, args) = parser.parse_args()
84     
85     vif_type = vsys.IFF_TAP
86     if options.vif_type and options.vif_type == "IFF_TUN":
87         vif_type = vsys.IFF_TUN
88
89     return (options.slicename, options.vif_name, vif_type, options.ip4_address, 
90             options.net_prefix, options.snat, options.pointopoint, 
91             options.txqueuelen, options.gre_key, options.gre_remote,
92             options.vif_name_file)
93
94 if __name__ == '__main__':
95
96     (slicename, vif_name, vif_type, ip4_address, net_prefix, snat, pointopoint,
97         txqueuelen, gre_key, gre_remote, vif_name_file) = get_options()
98
99     if (gre_key):
100         import pwd
101         import getpass
102
103         sliceid = pwd.getpwnam(slicename).pw_uid
104
105         if vif_type == vsys.IFF_TAP:
106             vif_prefix = "tap"
107         else:
108             vif_prefix = "tun"
109
110         # if_name should be a unique numeric vif id
111         vif_name = "%s%s-%s" % (vif_prefix, sliceid, vif_name) 
112
113     try:
114         vsys.vif_up(vif_name, ip4_address, net_prefix, snat = snat, 
115             pointopoint = pointopoint, txqueuelen = txqueuelen, 
116             gre_key = gre_key, gre_remote = gre_remote)
117
118     except RuntimeError as e:
119         import sys
120         import traceback
121         traceback.print_exc(file=sys.stderr)
122
123         # Ignore warnings
124         if e.message.find("WARNING:") < 0:
125             sys.exit(1)
126
127     # Saving interface name to vif_name_file
128     with open(vif_name_file, 'w') as f:
129         f.write(vif_name)
130