applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / route.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 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 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.application import LinuxApplication
22
23 import os
24
25 @clsinit_copy
26 class LinuxRoute(LinuxApplication):
27     _rtype = "linux::Route"
28     _help = "Adds a route to the host using iptools "
29
30     @classmethod
31     def _register_attributes(cls):
32         network = Attribute("network", "Network address", flags=Flags.Design)
33         prefix = Attribute("prefix", "IP prefix length", flags=Flags.Design)
34         nexthop = Attribute("nexthop", "Nexthop IP", flags=Flags.Design)
35
36         cls._register_attribute(network)
37         cls._register_attribute(prefix)
38         cls._register_attribute(nexthop)
39
40     def __init__(self, ec, guid):
41         super(LinuxRoute, self).__init__(ec, guid)
42         self._home = "route-%s" % self.guid
43         self._device = None
44
45     @property
46     def device(self):
47         if not self._device:
48             from nepi.resources.linux.tap import LinuxTap
49             from nepi.resources.linux.tun import LinuxTun
50             from nepi.resources.linux.interface import LinuxInterface
51             tap = self.get_connected(LinuxTap.get_rtype())
52             tun = self.get_connected(LinuxTun.get_rtype())
53             interface = self.get_connected(LinuxInterface.get_rtype())
54             if tap: self._device = tap[0]
55             elif tun: self._device = tun[0]
56             elif interface: self._device = interface[0]
57             else:
58                 raise RuntimeError("linux::Routes must be connected to a "\
59                         "linux::TAP, linux::TUN, or linux::Interface")
60         return self._device
61
62     @property
63     def node(self):
64         return self.device.node
65
66     def upload_start_command(self):
67         # We want to make sure the route is configured
68         # before the deploy is over, so we execute the 
69         # start script now and wait until it finishes. 
70         command = self.get("command")
71         command = self.replace_paths(command)
72
73         shfile = os.path.join(self.app_home, "start.sh")
74         self.node.run_and_wait(command, self.run_home,
75             shfile = shfile,
76             overwrite = True)
77
78     def upload_sources(self):
79         # upload stop.sh script
80         stop_command = self.replace_paths(self._stop_command)
81
82         self.node.upload(stop_command,
83                 os.path.join(self.app_home, "stop.sh"),
84                 text = True,
85                 # Overwrite file every time. 
86                 # The stop.sh has the path to the socket, which should change
87                 # on every experiment run.
88                 overwrite = True)
89
90     def do_deploy(self):
91         if not self.device or self.device.state < ResourceState.PROVISIONED:
92             self.ec.schedule(self.reschedule_delay, self.deploy)
93         else:
94             if not self.get("command"):
95                 self.set("command", self._start_command)
96
97             self.do_discover()
98             self.do_provision()
99
100             self.set_ready()
101
102     def do_start(self):
103         if self.state == ResourceState.READY:
104             command = self.get("command")
105             self.info("Starting command '%s'" % command)
106
107             self.set_started()
108         else:
109             msg = " Failed to execute command '%s'" % command
110             self.error(msg, out, err)
111             raise RuntimeError(msg)
112
113     def do_stop(self):
114         command = self.get('command') or ''
115         
116         if self.state == ResourceState.STARTED:
117             self.info("Stopping command '%s'" % command)
118
119             command = "bash %s" % os.path.join(self.app_home, "stop.sh")
120             (out, err), proc = self.execute_command(command,
121                     blocking = True)
122
123             if err:
124                 msg = " Failed to stop command '%s' " % command
125                 self.error(msg, out, err)
126
127             self.set_stopped()
128
129     @property
130     def _start_command(self):
131         network = self.get("network")
132         prefix = self.get("prefix")
133         nexthop = self.get("nexthop")
134         devicename = self.device.get("deviceName")
135
136         command = []
137         command.append("sudo -S ip route add %s/%s %s dev %s" % (
138             self.get("network"),
139             self.get("prefix"),
140             "default" if not nexthop else "via %s" % nexthop,
141             devicename))
142
143         return " ".join(command)
144
145     @property
146     def _stop_command(self):
147         network = self.get("network")
148         prefix = self.get("prefix")
149         nexthop = self.get("nexthop")
150         devicename = self.device.get("deviceName")
151
152         command = []
153         command.append("sudo -S ip route del %s/%s %s dev %s" % (
154             self.get("network"),
155             self.get("prefix"),
156             "default" if not nexthop else "via %s" % nexthop,
157             devicename))
158
159         return " ".join(command)
160