2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.resource import clsinit_copy, ResourceState, \
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.util.timefuncs import tnow, tdiffsec
28 state_check_delay = 0.5
31 class LinuxTunnel(LinuxApplication):
32 _rtype = "abstract::LinuxTunnel"
33 _help = "Constructs a tunnel between two Linux endpoints"
36 def __init__(self, ec, guid):
37 super(LinuxTunnel, self).__init__(ec, guid)
38 self._home = "tunnel-%s" % self.guid
40 def log_message(self, msg):
41 return " guid %d - tunnel %s - %s - %s " % (self.guid,
42 self.endpoint1.node.get("hostname"),
43 self.endpoint2.node.get("hostname"),
46 def get_endpoints(self):
47 """ Returns the list of RM that are endpoints to the tunnel
49 raise NotImplementedError
53 endpoints = self.get_endpoints()
54 if endpoints: return endpoints[0]
59 endpoints = self.get_endpoints()
60 if endpoints and len(endpoints) > 1: return endpoints[1]
63 def app_home(self, endpoint):
64 return os.path.join(endpoint.node.exp_home, self._home)
66 def run_home(self, endpoint):
67 return os.path.join(self.app_home(endpoint), self.ec.run_id)
69 def initiate_connection(self, endpoint, remote_endpoint):
70 raise NotImplementedError
72 def establish_connection(self, endpoint, remote_endpoint, data):
73 raise NotImplementedError
75 def verify_connection(self, endpoint, remote_endpoint):
76 raise NotImplementedError
78 def terminate_connection(self, endpoint, remote_endpoint):
79 raise NotImplementedError
81 def check_state_connection(self, endpoint, remote_endpoint):
82 raise NotImplementedError
84 def do_provision(self):
85 # create run dir for tunnel on each node
86 self.endpoint1.node.mkdir(self.run_home(self.endpoint1))
87 self.endpoint2.node.mkdir(self.run_home(self.endpoint2))
89 # Start 2 step connection
90 # Initiate connection from endpoint 1 to endpoint 2
91 data1 = self.initiate_connection(self.endpoint1, self.endpoint2)
93 # Initiate connection from endpoint 2 to endpoint 1
94 data2 = self.initiate_connection(self.endpoint2, self.endpoint1)
96 # Establish connection from endpoint 1 to endpoint 2
97 self.establish_connection(self.endpoint1, self.endpoint2, data2)
99 # Establish connection from endpoint 2 to endpoint 1
100 self.establish_connection(self.endpoint2, self.endpoint1, data1)
102 # check if connection was successful on both sides
103 self.verify_connection(self.endpoint1, self.endpoint2)
104 self.verify_connection(self.endpoint2, self.endpoint1)
106 self.info("Provisioning finished")
108 self.set_provisioned()
111 if (not self.endpoint1 or self.endpoint1.state < ResourceState.READY) or \
112 (not self.endpoint2 or self.endpoint2.state < ResourceState.READY):
113 self.ec.schedule(reschedule_delay, self.deploy)
121 if self.state == ResourceState.READY:
122 command = self.get("command")
123 self.info("Starting command '%s'" % command)
127 msg = " Failed to execute command '%s'" % command
128 self.error(msg, out, err)
129 raise RuntimeError, msg
132 """ Stops application execution
134 if self.state == ResourceState.STARTED:
135 self.info("Stopping tunnel")
137 self.terminate_connection(self.endpoint1, self.endpoint2)
138 self.terminate_connection(self.endpoint2, self.endpoint1)
144 """ Returns the state of the application
146 if self._state == ResourceState.STARTED:
147 # In order to avoid overwhelming the remote host and
148 # the local processor with too many ssh queries, the state is only
149 # requested every 'state_check_delay' seconds.
150 if tdiffsec(tnow(), self._last_state_check) > state_check_delay:
152 self.check_state_connection()
154 self._last_state_check = tnow()
158 def valid_connection(self, guid):