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 "
33 def _register_attributes(cls):
34 network = Attribute("network", "Network address", flags=Flags.Design)
35 prefix = Attribute("prefix", "IP prefix length", flags=Flags.Design)
36 nexthop = Attribute("nexthop", "Nexthop IP", flags=Flags.Design)
38 cls._register_attribute(network)
39 cls._register_attribute(prefix)
40 cls._register_attribute(nexthop)
42 def __init__(self, ec, guid):
43 super(LinuxRoute, self).__init__(ec, guid)
44 self._home = "route-%s" % self.guid
50 from nepi.resources.linux.tap import LinuxTap
51 from nepi.resources.linux.tun import LinuxTun
52 from nepi.resources.linux.interface import LinuxInterface
53 tap = self.get_connected(LinuxTap.get_rtype())
54 tun = self.get_connected(LinuxTun.get_rtype())
55 interface = self.get_connected(LinuxInterface.get_rtype())
56 if tap: self._device = tap[0]
57 elif tun: self._device = tun[0]
58 elif interface: self._device = interface[0]
60 raise RuntimeError, "linux::Routes must be connected to a "\
61 "linux::TAP, linux::TUN, or linux::Interface"
66 return self.device.node
68 def upload_start_command(self):
69 # We want to make sure the route is configured
70 # before the deploy is over, so we execute the
71 # start script now and wait until it finishes.
72 command = self.get("command")
73 command = self.replace_paths(command)
75 shfile = os.path.join(self.app_home, "start.sh")
76 self.node.run_and_wait(command, self.run_home,
80 def upload_sources(self):
81 # upload stop.sh script
82 stop_command = self.replace_paths(self._stop_command)
84 self.node.upload(stop_command,
85 os.path.join(self.app_home, "stop.sh"),
87 # Overwrite file every time.
88 # The stop.sh has the path to the socket, which should change
89 # on every experiment run.
93 if not self.device or self.device.state < ResourceState.PROVISIONED:
94 self.ec.schedule(self.reschedule_delay, self.deploy)
96 if not self.get("command"):
97 self.set("command", self._start_command)
105 if self.state == ResourceState.READY:
106 command = self.get("command")
107 self.info("Starting command '%s'" % command)
111 msg = " Failed to execute command '%s'" % command
112 self.error(msg, out, err)
113 raise RuntimeError, msg
116 command = self.get('command') or ''
118 if self.state == ResourceState.STARTED:
119 self.info("Stopping command '%s'" % command)
121 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
122 (out, err), proc = self.execute_command(command,
126 msg = " Failed to stop command '%s' " % command
127 self.error(msg, out, err)
132 def _start_command(self):
133 network = self.get("network")
134 prefix = self.get("prefix")
135 nexthop = self.get("nexthop")
136 devicename = self.device.get("deviceName")
139 command.append("sudo -S ip route add %s/%s %s dev %s" % (
142 "default" if not nexthop else "via %s" % nexthop,
145 return " ".join(command)
148 def _stop_command(self):
149 network = self.get("network")
150 prefix = self.get("prefix")
151 nexthop = self.get("nexthop")
152 devicename = self.device.get("deviceName")
155 command.append("sudo -S ip route del %s/%s %s dev %s" % (
158 "default" if not nexthop else "via %s" % nexthop,
161 return " ".join(command)