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.util.sshfuncs import ProcStatus
25 from nepi.util.timefuncs import tnow, tdiffsec
32 class LinuxGRETunnel(LinuxApplication):
33 _rtype = "LinuxGRETunnel"
34 _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
38 def _register_attributes(cls):
39 bwlimit = Attribute("bwLimit",
40 "Specifies the interface's emulated bandwidth in bytes "
45 cls._register_attribute(bwlimit)
47 def __init__(self, ec, guid):
48 super(LinuxGRETunnel, self).__init__(ec, guid)
50 def log_message(self, msg):
51 return " guid %d - GRE tunnel %s - %s - %s " % (self.guid,
52 self.endpoint1.node.get("hostname"),
53 self.endpoint2.node.get("hostname"),
56 def get_endpoints(self):
57 """ Returns the list of RM that are endpoints to the tunnel
60 for guid in self.connections:
61 rm = self.ec.get_resource(guid)
62 if hasattr(rm, "udp_connect_command"):
68 endpoints = self.get_endpoints()
69 if endpoints: return endpoints[0]
74 endpoints = self.get_endpoints()
75 if endpoints and len(endpoints) > 1: return endpoints[1]
78 def app_home(self, endpoint):
79 return os.path.join(endpoint.node.exp_home, self._home)
81 def run_home(self, endpoint):
82 return os.path.join(self.app_home(endpoint), self.ec.run_id)
84 def udp_connect(self, endpoint, remote_ip):
85 # Get udp connect command
86 local_port_file = os.path.join(self.run_home(endpoint),
88 remote_port_file = os.path.join(self.run_home(endpoint),
90 ret_file = os.path.join(self.run_home(endpoint),
92 cipher = self.get("cipher")
93 cipher_key = self.get("cipherKey")
94 bwlimit = self.get("bwLimit")
95 txqueuelen = self.get("txQueueLen")
96 udp_connect_command = endpoint.udp_connect_command(
97 remote_ip, local_port_file, remote_port_file,
98 ret_file, cipher, cipher_key, bwlimit, txqueuelen)
100 # upload command to connect.sh script
101 shfile = os.path.join(self.app_home(endpoint), "udp-connect.sh")
102 endpoint.node.upload(udp_connect_command,
107 # invoke connect script
108 cmd = "bash %s" % shfile
109 (out, err), proc = endpoint.node.run(cmd, self.run_home(endpoint))
111 # check if execution errors occurred
112 msg = " Failed to connect endpoints "
115 self.error(msg, out, err)
116 raise RuntimeError, msg
118 # Wait for pid file to be generated
119 pid, ppid = endpoint.node.wait_pid(self.run_home(endpoint))
121 # If the process is not running, check for error information
122 # on the remote machine
123 if not pid or not ppid:
124 (out, err), proc = endpoint.node.check_errors(self.run_home(endpoint))
125 # Out is what was written in the stderr file
127 msg = " Failed to start command '%s' " % command
128 self.error(msg, out, err)
129 raise RuntimeError, msg
131 # wait until port is written to file
132 port = self.wait_local_port(endpoint)
133 return (port, pid, ppid)
135 def do_provision(self):
136 # create run dir for tunnel on each node
137 self.endpoint1.node.mkdir(self.run_home(self.endpoint1))
138 self.endpoint2.node.mkdir(self.run_home(self.endpoint2))
140 # Invoke connect script in endpoint 1
141 remote_ip1 = socket.gethostbyname(self.endpoint2.node.get("hostname"))
142 (port1, self._pid1, self._ppid1) = self.udp_connect(self.endpoint1,
145 # Invoke connect script in endpoint 2
146 remote_ip2 = socket.gethostbyname(self.endpoint1.node.get("hostname"))
147 (port2, self._pid2, self._ppid2) = self.udp_connect(self.endpoint2,
150 # upload file with port 2 to endpoint 1
151 self.upload_remote_port(self.endpoint1, port2)
153 # upload file with port 1 to endpoint 2
154 self.upload_remote_port(self.endpoint2, port1)
156 # check if connection was successful on both sides
157 self.wait_result(self.endpoint1)
158 self.wait_result(self.endpoint2)
160 self.info("Provisioning finished")
162 self.set_provisioned()
165 if (not self.endpoint1 or self.endpoint1.state < ResourceState.READY) or \
166 (not self.endpoint2 or self.endpoint2.state < ResourceState.READY):
167 self.ec.schedule(reschedule_delay, self.deploy)
175 if self.state == ResourceState.READY:
176 command = self.get("command")
177 self.info("Starting command '%s'" % command)
181 msg = " Failed to execute command '%s'" % command
182 self.error(msg, out, err)
183 raise RuntimeError, msg
186 """ Stops application execution
188 if self.state == ResourceState.STARTED:
189 self.info("Stopping tunnel")
191 # Only try to kill the process if the pid and ppid
193 if self._pid1 and self._ppid1 and self._pid2 and self._ppid2:
194 (out1, err1), proc1 = self.endpoint1.node.kill(self._pid1,
195 self._ppid1, sudo = True)
196 (out2, err2), proc2 = self.endpoint2.node.kill(self._pid2,
197 self._ppid2, sudo = True)
199 if (proc1.poll() and err1) or (proc2.poll() and err2):
200 # check if execution errors occurred
201 msg = " Failed to STOP tunnel"
202 self.error(msg, err1, err2)
203 raise RuntimeError, msg
209 """ Returns the state of the application
211 if self._state == ResourceState.STARTED:
212 # In order to avoid overwhelming the remote host and
213 # the local processor with too many ssh queries, the state is only
214 # requested every 'state_check_delay' seconds.
215 state_check_delay = 0.5
216 if tdiffsec(tnow(), self._last_state_check) > state_check_delay:
217 if self._pid1 and self._ppid1 and self._pid2 and self._ppid2:
218 # Make sure the process is still running in background
219 # No execution errors occurred. Make sure the background
220 # process with the recorded pid is still running.
221 status1 = self.endpoint1.node.status(self._pid1, self._ppid1)
222 status2 = self.endpoint2.node.status(self._pid2, self._ppid2)
224 if status1 == ProcStatus.FINISHED and \
225 status2 == ProcStatus.FINISHED:
227 # check if execution errors occurred
228 (out1, err1), proc1 = self.endpoint1.node.check_errors(
229 self.run_home(self.endpoint1))
231 (out2, err2), proc2 = self.endpoint2.node.check_errors(
232 self.run_home(self.endpoint2))
235 msg = "Error occurred in tunnel"
236 self.error(msg, err1, err2)
241 self._last_state_check = tnow()
245 def wait_local_port(self, endpoint):
246 """ Waits until the local_port file for the endpoint is generated,
247 and returns the port number
250 return self.wait_file(endpoint, "local_port")
252 def wait_result(self, endpoint):
253 """ Waits until the return code file for the endpoint is generated
256 return self.wait_file(endpoint, "ret_file")
258 def wait_file(self, endpoint, filename):
259 """ Waits until file on endpoint is generated """
264 (out, err), proc = endpoint.node.check_output(
265 self.run_home(endpoint), filename)
274 msg = "Couldn't retrieve %s" % filename
275 self.error(msg, out, err)
276 raise RuntimeError, msg
280 def upload_remote_port(self, endpoint, port):
281 # upload remote port number to file
283 endpoint.node.upload(port,
284 os.path.join(self.run_home(endpoint), "remote_port"),
288 def valid_connection(self, guid):