961d50c4cc3b25c1413b764b7e6eb1e5ccaa4d7a
[nepi.git] / src / nepi / resources / linux / tunnel.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.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.application import LinuxApplication
22 from nepi.util.timefuncs import tnow, tdiffsec
23
24 import os
25 import time
26
27 state_check_delay = 0.5
28
29 @clsinit_copy
30 class LinuxTunnel(LinuxApplication):
31     _rtype = "abstract::linux::Tunnel"
32     _help = "Constructs a tunnel between two Linux endpoints"
33     _backend = "linux"
34
35     def __init__(self, ec, guid):
36         super(LinuxTunnel, self).__init__(ec, guid)
37         self._home = "tunnel-%s" % self.guid
38
39     def log_message(self, msg):
40         return " guid %d - tunnel %s - %s - %s " % (self.guid, 
41                 self.endpoint1.node.get("hostname"), 
42                 self.endpoint2.node.get("hostname"), 
43                 msg)
44
45     def get_endpoints(self):
46         """ Returns the list of RM that are endpoints to the tunnel 
47         """
48         raise NotImplementedError
49
50     @property
51     def endpoint1(self):
52         endpoints = self.get_endpoints()
53         if endpoints: return endpoints[0]
54         return None
55
56     @property
57     def endpoint2(self):
58         endpoints = self.get_endpoints()
59         if endpoints and len(endpoints) > 1: return endpoints[1]
60         return None
61
62     def app_home(self, endpoint):
63         return os.path.join(endpoint.node.exp_home, self._home)
64
65     def run_home(self, endpoint):
66         return os.path.join(self.app_home(endpoint), self.ec.run_id)
67
68     def initiate_connection(self, endpoint, remote_endpoint):
69         raise NotImplementedError
70
71     def establish_connection(self, endpoint, remote_endpoint, data):
72         raise NotImplementedError
73
74     def verify_connection(self, endpoint, remote_endpoint):
75         raise NotImplementedError
76
77     def terminate_connection(self, endpoint, remote_endpoint):
78         raise NotImplementedError
79
80     def check_state_connection(self, endpoint, remote_endpoint):
81         raise NotImplementedError
82
83     def do_provision(self):
84         # create run dir for tunnel on each node 
85         self.endpoint1.node.mkdir(self.run_home(self.endpoint1))
86         self.endpoint2.node.mkdir(self.run_home(self.endpoint2))
87
88         self.debug("Initiate the connection")
89         # Start 2 step connection
90         # Initiate connection from endpoint 1 to endpoint 2
91         data1 = self.initiate_connection(self.endpoint1, self.endpoint2)
92
93         # Initiate connection from endpoint 2 to endpoint 1
94         data2 = self.initiate_connection(self.endpoint2, self.endpoint1)
95
96         self.debug("Establish the connection")
97         # Establish connection from endpoint 1 to endpoint 2
98         self.establish_connection(self.endpoint1, self.endpoint2, data2)
99         
100         # Establish connection from endpoint 2 to endpoint 1
101         self.establish_connection(self.endpoint2, self.endpoint1, data1)
102
103         self.debug("Verify the connection")
104         # check if connection was successful on both sides
105         self.verify_connection(self.endpoint1, self.endpoint2)
106         self.verify_connection(self.endpoint2, self.endpoint1)
107        
108         self.info("Provisioning finished")
109  
110         self.set_provisioned()
111
112     def do_deploy(self):
113         if (not self.endpoint1 or self.endpoint1.state < ResourceState.READY) or \
114             (not self.endpoint2 or self.endpoint2.state < ResourceState.READY):
115             self.ec.schedule(self.reschedule_delay, self.deploy)
116         else:
117             self.do_discover()
118             self.do_provision()
119  
120             self.set_ready()
121
122     def do_start(self):
123         if self.state == ResourceState.READY:
124             command = self.get("command")
125             self.info("Starting command '%s'" % command)
126             
127             self.set_started()
128         else:
129             msg = " Failed to execute command '%s'" % command
130             self.error(msg, out, err)
131             raise RuntimeError, msg
132
133     def do_stop(self):
134         """ Stops application execution
135         """
136
137         if self.state == ResourceState.STARTED:
138             self.info("Stopping tunnel")
139
140             self.terminate_connection(self.endpoint1, self.endpoint2)
141             self.terminate_connection(self.endpoint2, self.endpoint1)
142
143             self.set_stopped()
144
145     @property
146     def state(self):
147         """ Returns the state of the application
148         """
149         if self._state == ResourceState.STARTED:
150             # In order to avoid overwhelming the remote host and
151             # the local processor with too many ssh queries, the state is only
152             # requested every 'state_check_delay' seconds.
153             if tdiffsec(tnow(), self._last_state_check) > state_check_delay:
154                 
155                 self.check_state_connection()
156
157                 self._last_state_check = tnow()
158
159         return self._state
160
161     def valid_connection(self, guid):
162         # TODO: Validate!
163         return True
164