Replacing _backend for _platform class attribute in ResourceManager
[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 from nepi.resources.linux.tunnel import LinuxTunnel
23 from nepi.util.sshfuncs import ProcStatus
24 from nepi.util.timefuncs import tnow, tdiffsec
25
26 import re
27 import socket
28 import time
29 import os
30
31 @clsinit_copy
32 class LinuxGRETunnel(LinuxTunnel):
33     _rtype = "linux::GRETunnel"
34     _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
35
36     def log_message(self, msg):
37         return " guid %d - GRE tunnel %s - %s - %s " % (self.guid, 
38                 self.endpoint1.node.get("hostname"), 
39                 self.endpoint2.node.get("hostname"), 
40                 msg)
41
42     def get_endpoints(self):
43         """ Returns the list of RM that are endpoints to the tunnel 
44         """
45         connected = []
46         for guid in self.connections:
47             rm = self.ec.get_resource(guid)
48             if hasattr(rm, "gre_connect"):
49                 connected.append(rm)
50         return connected
51
52     def initiate_connection(self, endpoint, remote_endpoint):
53         # Return the command to execute to initiate the connection to the
54         # other endpoint
55         connection_run_home = self.run_home(endpoint)
56         connection_app_home = self.app_home(endpoint)
57         data = endpoint.gre_connect(remote_endpoint, 
58                 connection_app_home,
59                 connection_run_home) 
60         return data
61
62     def establish_connection(self, endpoint, remote_endpoint, data):
63         pass
64
65     def verify_connection(self, endpoint, remote_endpoint):
66         remote_ip = socket.gethostbyname(remote_endpoint.node.get("hostname"))
67
68         command = "ping -c 4 %s" % remote_ip
69         (out, err), proc = endpoint.node.execute(command,
70                 blocking = True)
71
72         m = re.search("(\d+)% packet loss", str(out))
73         if not m or int(m.groups()[0]) == 100:
74              msg = " Error establishing GRE Tunnel"
75              self.error(msg, out, err)
76              raise RuntimeError, msg
77
78     def terminate_connection(self, endpoint, remote_endpoint):
79         pass
80
81     def check_state_connection(self):
82         pass
83
84     def valid_connection(self, guid):
85         # TODO: Validate!
86         return True
87