applied the except and raise fixers to the master branch to close the gap with py3
[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 version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.tunnel import LinuxTunnel
22 from nepi.util.sshfuncs import ProcStatus
23 from nepi.util.timefuncs import tnow, tdiffsec
24
25 import re
26 import socket
27 import time
28 import os
29
30 @clsinit_copy
31 class LinuxGRETunnel(LinuxTunnel):
32     _rtype = "linux::GRETunnel"
33     _help = "Constructs a tunnel between two Linux endpoints using a UDP connection "
34
35     def log_message(self, msg):
36         return " guid %d - GRE tunnel %s - %s - %s " % (self.guid, 
37                 self.endpoint1.node.get("hostname"), 
38                 self.endpoint2.node.get("hostname"), 
39                 msg)
40
41     def get_endpoints(self):
42         """ Returns the list of RM that are endpoints to the tunnel 
43         """
44         connected = []
45         for guid in self.connections:
46             rm = self.ec.get_resource(guid)
47             if hasattr(rm, "gre_connect"):
48                 connected.append(rm)
49         return connected
50
51     def initiate_connection(self, endpoint, remote_endpoint):
52         # Return the command to execute to initiate the connection to the
53         # other endpoint
54         connection_run_home = self.run_home(endpoint)
55         connection_app_home = self.app_home(endpoint)
56         data = endpoint.gre_connect(remote_endpoint, 
57                 connection_app_home,
58                 connection_run_home) 
59         return data
60
61     def establish_connection(self, endpoint, remote_endpoint, data):
62         pass
63
64     def verify_connection(self, endpoint, remote_endpoint):
65         remote_ip = socket.gethostbyname(remote_endpoint.node.get("hostname"))
66
67         command = "ping -c 4 %s" % remote_ip
68         (out, err), proc = endpoint.node.execute(command,
69                 blocking = True)
70
71         m = re.search("(\d+)% packet loss", str(out))
72         if not m or int(m.groups()[0]) == 100:
73              msg = " Error establishing GRE Tunnel"
74              self.error(msg, out, err)
75              raise RuntimeError(msg)
76
77     def terminate_connection(self, endpoint, remote_endpoint):
78         pass
79
80     def check_state_connection(self):
81         pass
82
83     def valid_connection(self, guid):
84         # TODO: Validate!
85         return True
86