Making UdpTunnel inherite from abstract Tunnel RM
[nepi.git] / src / nepi / resources / linux / gretunnel.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState, \
22         reschedule_delay
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.util.sshfuncs import ProcStatus
25 from nepi.util.timefuncs import tnow, tdiffsec
26
27 import os
28 import socket
29 import time
30
31 @clsinit_copy
32 class LinuxGRETunnel(LinuxApplication):
33     _rtype = "LinuxGRETunnel"
34     _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
35     _backend = "linux"
36
37     @classmethod
38     def _register_attributes(cls):
39         bwlimit = Attribute("bwLimit",
40                 "Specifies the interface's emulated bandwidth in bytes "
41                 "per second.",
42                 type = Types.Integer, 
43                 flags = Flags.Design)
44
45         cls._register_attribute(bwlimit)
46
47     def __init__(self, ec, guid):
48         super(LinuxGRETunnel, self).__init__(ec, guid)
49
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"), 
54                 msg)
55
56     def get_endpoints(self):
57         """ Returns the list of RM that are endpoints to the tunnel 
58         """
59         connected = []
60         for guid in self.connections:
61             rm = self.ec.get_resource(guid)
62             if hasattr(rm, "udp_connect_command"):
63                 connected.append(rm)
64         return connected
65
66     @property
67     def endpoint1(self):
68         endpoints = self.get_endpoints()
69         if endpoints: return endpoints[0]
70         return None
71
72     @property
73     def endpoint2(self):
74         endpoints = self.get_endpoints()
75         if endpoints and len(endpoints) > 1: return endpoints[1]
76         return None
77
78     def app_home(self, endpoint):
79         return os.path.join(endpoint.node.exp_home, self._home)
80
81     def run_home(self, endpoint):
82         return os.path.join(self.app_home(endpoint), self.ec.run_id)
83
84     def udp_connect(self, endpoint, remote_ip):
85         # Get udp connect command
86         local_port_file = os.path.join(self.run_home(endpoint), 
87                 "local_port")
88         remote_port_file = os.path.join(self.run_home(endpoint), 
89                 "remote_port")
90         ret_file = os.path.join(self.run_home(endpoint), 
91                 "ret_file")
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)
99
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,
103                 shfile,
104                 text = True, 
105                 overwrite = False)
106
107         # invoke connect script
108         cmd = "bash %s" % shfile
109         (out, err), proc = endpoint.node.run(cmd, self.run_home(endpoint)) 
110              
111         # check if execution errors occurred
112         msg = " Failed to connect endpoints "
113         
114         if proc.poll():
115             self.error(msg, out, err)
116             raise RuntimeError, msg
117     
118         # Wait for pid file to be generated
119         pid, ppid = endpoint.node.wait_pid(self.run_home(endpoint))
120         
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
126             if err:
127                 msg = " Failed to start command '%s' " % command
128                 self.error(msg, out, err)
129                 raise RuntimeError, msg
130
131         # wait until port is written to file
132         port = self.wait_local_port(endpoint)
133         return (port, pid, ppid)
134
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))
139
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,
143                 remote_ip1)
144
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,
148                 remote_ip2)
149
150         # upload file with port 2 to endpoint 1
151         self.upload_remote_port(self.endpoint1, port2)
152         
153         # upload file with port 1 to endpoint 2
154         self.upload_remote_port(self.endpoint2, port1)
155
156         # check if connection was successful on both sides
157         self.wait_result(self.endpoint1)
158         self.wait_result(self.endpoint2)
159        
160         self.info("Provisioning finished")
161  
162         self.set_provisioned()
163
164     def do_deploy(self):
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)
168         else:
169             self.do_discover()
170             self.do_provision()
171  
172             self.set_ready()
173
174     def do_start(self):
175         if self.state == ResourceState.READY:
176             command = self.get("command")
177             self.info("Starting command '%s'" % command)
178             
179             self.set_started()
180         else:
181             msg = " Failed to execute command '%s'" % command
182             self.error(msg, out, err)
183             raise RuntimeError, msg
184
185     def do_stop(self):
186         """ Stops application execution
187         """
188         if self.state == ResourceState.STARTED:
189             self.info("Stopping tunnel")
190     
191             # Only try to kill the process if the pid and ppid
192             # were retrieved
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) 
198
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
204
205             self.set_stopped()
206
207     @property
208     def state(self):
209         """ Returns the state of the application
210         """
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)
223
224                     if status1 == ProcStatus.FINISHED and \
225                             status2 == ProcStatus.FINISHED:
226
227                         # check if execution errors occurred
228                         (out1, err1), proc1 = self.endpoint1.node.check_errors(
229                                 self.run_home(self.endpoint1))
230
231                         (out2, err2), proc2 = self.endpoint2.node.check_errors(
232                                 self.run_home(self.endpoint2))
233
234                         if err1 or err2: 
235                             msg = "Error occurred in tunnel"
236                             self.error(msg, err1, err2)
237                             self.fail()
238                         else:
239                             self.set_stopped()
240
241                 self._last_state_check = tnow()
242
243         return self._state
244
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 
248         
249         """
250         return self.wait_file(endpoint, "local_port")
251
252     def wait_result(self, endpoint):
253         """ Waits until the return code file for the endpoint is generated 
254         
255         """ 
256         return self.wait_file(endpoint, "ret_file")
257  
258     def wait_file(self, endpoint, filename):
259         """ Waits until file on endpoint is generated """
260         result = None
261         delay = 1.0
262
263         for i in xrange(20):
264             (out, err), proc = endpoint.node.check_output(
265                     self.run_home(endpoint), filename)
266
267             if out:
268                 result = out.strip()
269                 break
270             else:
271                 time.sleep(delay)
272                 delay = delay * 1.5
273         else:
274             msg = "Couldn't retrieve %s" % filename
275             self.error(msg, out, err)
276             raise RuntimeError, msg
277
278         return result
279
280     def upload_remote_port(self, endpoint, port):
281         # upload remote port number to file
282         port = "%s\n" % port
283         endpoint.node.upload(port,
284                 os.path.join(self.run_home(endpoint), "remote_port"),
285                 text = True, 
286                 overwrite = False)
287
288     def valid_connection(self, guid):
289         # TODO: Validate!
290         return True
291