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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
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
27 class LinuxRoute(LinuxApplication):
28 _rtype = "linux::Route"
29 _help = "Adds a route to the host using iptools "
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)
37 cls._register_attribute(network)
38 cls._register_attribute(prefix)
39 cls._register_attribute(nexthop)
41 def __init__(self, ec, guid):
42 super(LinuxRoute, self).__init__(ec, guid)
43 self._home = "route-%s" % self.guid
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]
59 raise RuntimeError, "linux::Routes must be connected to a "\
60 "linux::TAP, linux::TUN, or linux::Interface"
65 return self.device.node
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)
74 shfile = os.path.join(self.app_home, "start.sh")
75 self.node.run_and_wait(command, self.run_home,
79 def upload_sources(self):
80 # upload stop.sh script
81 stop_command = self.replace_paths(self._stop_command)
83 self.node.upload(stop_command,
84 os.path.join(self.app_home, "stop.sh"),
86 # Overwrite file every time.
87 # The stop.sh has the path to the socket, which should change
88 # on every experiment run.
92 if not self.device or self.device.state < ResourceState.PROVISIONED:
93 self.ec.schedule(self.reschedule_delay, self.deploy)
95 if not self.get("command"):
96 self.set("command", self._start_command)
104 if self.state == ResourceState.READY:
105 command = self.get("command")
106 self.info("Starting command '%s'" % command)
110 msg = " Failed to execute command '%s'" % command
111 self.error(msg, out, err)
112 raise RuntimeError, msg
115 command = self.get('command') or ''
117 if self.state == ResourceState.STARTED:
118 self.info("Stopping command '%s'" % command)
120 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
121 (out, err), proc = self.execute_command(command,
125 msg = " Failed to stop command '%s' " % command
126 self.error(msg, out, err)
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")
138 command.append("sudo -S ip route add %s/%s %s dev %s" % (
141 "default" if not nexthop else "via %s" % nexthop,
144 return " ".join(command)
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")
154 command.append("sudo -S ip route del %s/%s %s dev %s" % (
157 "default" if not nexthop else "via %s" % nexthop,
160 return " ".join(command)