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