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