Added GRE functionallity to PlanetLabTAP
[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.tunnel import LinuxTunnel
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(LinuxTunnel):
33     _rtype = "LinuxGRETunnel"
34     _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
35     _backend = "linux"
36
37     def log_message(self, msg):
38         return " guid %d - GRE tunnel %s - %s - %s " % (self.guid, 
39                 self.endpoint1.node.get("hostname"), 
40                 self.endpoint2.node.get("hostname"), 
41                 msg)
42
43     def get_endpoints(self):
44         """ Returns the list of RM that are endpoints to the tunnel 
45         """
46         connected = []
47         for guid in self.connections:
48             rm = self.ec.get_resource(guid)
49             if hasattr(rm, "gre_connect_command"):
50                 connected.append(rm)
51         return connected
52
53     def initiate_connection(self, endpoint, remote_endpoint):
54         # Return the command to execute to initiate the connection to the
55         # other endpoint
56         connection_run_home = self.run_home(endpoint)
57         gre_connect_command = endpoint.gre_connect_command(
58                 remote_endpoint, connection_run_home)
59
60         # upload command to connect.sh script
61         shfile = os.path.join(self.app_home(endpoint), "gre-connect.sh")
62         endpoint.node.upload(udp_connect_command,
63                 shfile,
64                 text = True, 
65                 overwrite = False)
66
67         # invoke connect script
68         cmd = "bash %s" % shfile
69         (out, err), proc = endpoint.node.run(cmd, self.run_home(endpoint)) 
70              
71         # check if execution errors occurred
72         msg = " Failed to connect endpoints "
73         
74         if proc.poll():
75             self.error(msg, out, err)
76             raise RuntimeError, msg
77     
78         # Wait for pid file to be generated
79         pid, ppid = endpoint.node.wait_pid(self.run_home(endpoint))
80         
81         # If the process is not running, check for error information
82         # on the remote machine
83         if not pid or not ppid:
84             (out, err), proc = endpoint.node.check_errors(self.run_home(endpoint))
85             # Out is what was written in the stderr file
86             if err:
87                 msg = " Failed to start command '%s' " % command
88                 self.error(msg, out, err)
89                 raise RuntimeError, msg
90
91         # Wait if name
92         return True
93
94     def establish_connection(self, endpoint, remote_endpoint, data):
95         pass
96
97     def verify_connection(self, endpoint, remote_endpoint):
98         # Execute a ping from both sides to verify that the tunnel works
99         pass
100
101     def terminate_connection(self, endpoint, remote_endpoint):
102         pass
103
104     def check_state_connection(self, endpoint, remote_endpoint):
105         raise NotImplementedError
106
107     def valid_connection(self, guid):
108         # TODO: Validate!
109         return True
110