2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 INRIA
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;
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.
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/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
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
26 class LinuxRoute(LinuxApplication):
27 _rtype = "linux::Route"
28 _help = "Adds a route to the host using iptools "
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)
36 cls._register_attribute(network)
37 cls._register_attribute(prefix)
38 cls._register_attribute(nexthop)
40 def __init__(self, ec, guid):
41 super(LinuxRoute, self).__init__(ec, guid)
42 self._home = "route-%s" % self.guid
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]
58 raise RuntimeError("linux::Routes must be connected to a "\
59 "linux::TAP, linux::TUN, or linux::Interface")
64 return self.device.node
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)
73 shfile = os.path.join(self.app_home, "start.sh")
74 self.node.run_and_wait(command, self.run_home,
78 def upload_sources(self):
79 # upload stop.sh script
80 stop_command = self.replace_paths(self._stop_command)
82 self.node.upload(stop_command,
83 os.path.join(self.app_home, "stop.sh"),
85 # Overwrite file every time.
86 # The stop.sh has the path to the socket, which should change
87 # on every experiment run.
91 if not self.device or self.device.state < ResourceState.PROVISIONED:
92 self.ec.schedule(self.reschedule_delay, self.deploy)
94 if not self.get("command"):
95 self.set("command", self._start_command)
103 if self.state == ResourceState.READY:
104 command = self.get("command")
105 self.info("Starting command '%s'" % command)
109 msg = " Failed to execute command '%s'" % command
110 self.error(msg, out, err)
111 raise RuntimeError(msg)
114 command = self.get('command') or ''
116 if self.state == ResourceState.STARTED:
117 self.info("Stopping command '%s'" % command)
119 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
120 (out, err), proc = self.execute_command(command,
124 msg = " Failed to stop command '%s' " % command
125 self.error(msg, out, err)
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")
137 command.append("sudo -S ip route add %s/%s %s dev %s" % (
140 "default" if not nexthop else "via %s" % nexthop,
143 return " ".join(command)
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")
153 command.append("sudo -S ip route del %s/%s %s dev %s" % (
156 "default" if not nexthop else "via %s" % nexthop,
159 return " ".join(command)