Replacing _backend for _platform class attribute in ResourceManager
[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 as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState
22 from nepi.resources.linux.application import LinuxApplication
23
24 import os
25
26 @clsinit_copy
27 class LinuxRoute(LinuxApplication):
28     _rtype = "linux::Route"
29     _help = "Adds a route to the host using iptools "
30
31     @classmethod
32     def _register_attributes(cls):
33         network = Attribute("network", "Network address", flags=Flags.Design)
34         prefix = Attribute("prefix", "IP prefix length", flags=Flags.Design)
35         nexthop = Attribute("nexthop", "Nexthop IP", flags=Flags.Design)
36
37         cls._register_attribute(network)
38         cls._register_attribute(prefix)
39         cls._register_attribute(nexthop)
40
41     def __init__(self, ec, guid):
42         super(LinuxRoute, self).__init__(ec, guid)
43         self._home = "route-%s" % self.guid
44         self._device = None
45
46     @property
47     def device(self):
48         if not self._device:
49             from nepi.resources.linux.tap import LinuxTap
50             from nepi.resources.linux.tun import LinuxTun
51             from nepi.resources.linux.interface import LinuxInterface
52             tap = self.get_connected(LinuxTap.get_rtype())
53             tun = self.get_connected(LinuxTun.get_rtype())
54             interface = self.get_connected(LinuxInterface.get_rtype())
55             if tap: self._device = tap[0]
56             elif tun: self._device = tun[0]
57             elif interface: self._device = interface[0]
58             else:
59                 raise RuntimeError, "linux::Routes must be connected to a "\
60                         "linux::TAP, linux::TUN, or linux::Interface"
61         return self._device
62
63     @property
64     def node(self):
65         return self.device.node
66
67     def upload_start_command(self):
68         # We want to make sure the route is configured
69         # before the deploy is over, so we execute the 
70         # start script now and wait until it finishes. 
71         command = self.get("command")
72         command = self.replace_paths(command)
73
74         shfile = os.path.join(self.app_home, "start.sh")
75         self.node.run_and_wait(command, self.run_home,
76             shfile = shfile,
77             overwrite = True)
78
79     def upload_sources(self):
80         # upload stop.sh script
81         stop_command = self.replace_paths(self._stop_command)
82
83         self.node.upload(stop_command,
84                 os.path.join(self.app_home, "stop.sh"),
85                 text = True,
86                 # Overwrite file every time. 
87                 # The stop.sh has the path to the socket, which should change
88                 # on every experiment run.
89                 overwrite = True)
90
91     def do_deploy(self):
92         if not self.device or self.device.state < ResourceState.PROVISIONED:
93             self.ec.schedule(self.reschedule_delay, self.deploy)
94         else:
95             if not self.get("command"):
96                 self.set("command", self._start_command)
97
98             self.do_discover()
99             self.do_provision()
100
101             self.set_ready()
102
103     def do_start(self):
104         if self.state == ResourceState.READY:
105             command = self.get("command")
106             self.info("Starting command '%s'" % command)
107
108             self.set_started()
109         else:
110             msg = " Failed to execute command '%s'" % command
111             self.error(msg, out, err)
112             raise RuntimeError, msg
113
114     def do_stop(self):
115         command = self.get('command') or ''
116         
117         if self.state == ResourceState.STARTED:
118             self.info("Stopping command '%s'" % command)
119
120             command = "bash %s" % os.path.join(self.app_home, "stop.sh")
121             (out, err), proc = self.execute_command(command,
122                     blocking = True)
123
124             if err:
125                 msg = " Failed to stop command '%s' " % command
126                 self.error(msg, out, err)
127
128             self.set_stopped()
129
130     @property
131     def _start_command(self):
132         network = self.get("network")
133         prefix = self.get("prefix")
134         nexthop = self.get("nexthop")
135         devicename = self.device.get("deviceName")
136
137         command = []
138         command.append("sudo -S ip route add %s/%s %s dev %s" % (
139             self.get("network"),
140             self.get("prefix"),
141             "default" if not nexthop else "via %s" % nexthop,
142             devicename))
143
144         return " ".join(command)
145
146     @property
147     def _stop_command(self):
148         network = self.get("network")
149         prefix = self.get("prefix")
150         nexthop = self.get("nexthop")
151         devicename = self.device.get("deviceName")
152
153         command = []
154         command.append("sudo -S ip route del %s/%s %s dev %s" % (
155             self.get("network"),
156             self.get("prefix"),
157             "default" if not nexthop else "via %s" % nexthop,
158             devicename))
159
160         return " ".join(command)
161