2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 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, \
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.planetlab.node import PlanetlabNode
25 from nepi.resources.planetlab.tap import PlanetlabTap
26 from nepi.util.timefuncs import tnow, tdiffsec
31 PYTHON_VSYS_VERSION = "1.0"
34 class PlanetlabVroute(LinuxApplication):
35 _rtype = "PlanetlabVroute"
36 _help = "Creates a Vroute on a PlanetLab host"
37 _backend = "planetlab"
40 def _register_attributes(cls):
41 action = Attribute("action", "Either add or del",
42 allowed = ["add", "del"],
45 network = Attribute("network", "IPv4 Network Address",
48 cls._register_attribute(action)
49 cls._register_attribute(network)
51 def __init__(self, ec, guid):
52 super(PlanetlabVroute, self).__init__(ec, guid)
53 self._home = "vroute-%s" % self.guid
57 tap = self.get_connected(PlanetlabTap.get_rtype())
63 node = self.tap.get_connected(PlanetlabNode.get_rtype())
64 if node: return node[0]
67 def upload_sources(self):
68 # upload vif-creation python script
69 pl_vroute = os.path.join(os.path.dirname(__file__), "scripts",
72 self.node.upload(pl_vroute,
73 os.path.join(self.node.src_dir, "pl-vroute.py"),
76 # upload stop.sh script
77 stop_command = self.replace_paths(self._stop_command)
78 self.node.upload(stop_command,
79 os.path.join(self.app_home, "stop.sh"),
81 # Overwrite file every time.
82 # The stop.sh has the path to the socket, wich should change
83 # on every experiment run.
86 def upload_start_command(self):
87 # Overwrite file every time.
88 # The stop.sh has the path to the socket, wich should change
89 # on every experiment run.
90 super(PlanetlabVroute, self).upload_start_command(overwrite = True)
92 (out, err), proc = self.execute_command(self.get("command"), blocking = True)
95 if not self.tap or self.tap.state < ResourceState.PROVISIONED:
96 self.ec.schedule(reschedule_delay, self.deploy)
98 if not self.get("command"):
99 self.set("command", self._start_command)
104 self.debug("----- READY ---- ")
108 if self.state == ResourceState.READY:
109 command = self.get("command")
110 self.info("Starting command '%s'" % command)
112 # Vroute started during deployment
115 msg = " Failed to execute command '%s'" % command
116 self.error(msg, out, err)
117 raise RuntimeError, msg
121 command = self.get('command') or ''
123 if self.state == ResourceState.STARTED:
124 self.info("Stopping command '%s'" % self._stop_command)
126 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
127 (out, err), proc = self.execute_command(command,
132 def do_release(self):
133 # Node needs to wait until all associated RMs are released
135 if not self.get("tearDown"):
136 self.set("tearDown", self._tear_down_command())
138 super(PlanetlabVroute, self).do_release()
140 def _tear_down_command(self):
141 if self.get("action") == "del":
144 return self._stop_command
147 def _start_command(self):
148 command = ["sudo -S python ${SRC}/pl-vroute.py"]
149 command.append("-a %s" % self.get("action"))
150 command.append("-n %s" % self.get("network"))
151 command.append("-p %d" % self.tap.get("prefix4"))
152 command.append("-g %s" % self.tap.get("pointopoint"))
153 command.append("-f %s" % self.tap.get("deviceName"))
154 return " ".join(command)
157 def _stop_command(self):
158 command = ["sudo -S python ${SRC}/pl-vroute.py"]
159 command.append("-a %s" % "del")
160 command.append("-n %s" % self.get("network"))
161 command.append("-p %d" % self.tap.get("prefix4"))
162 command.append("-g %s" % self.tap.get("pointopoint"))
163 command.append("-f %s" % self.tap.get("deviceName"))
164 return " ".join(command)
166 def valid_connection(self, guid):