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
21 from nepi.resources.linux.application import LinuxApplication
22 from nepi.util.timefuncs import tnow, tdiffsec
27 state_check_delay = 0.5
30 class LinuxTunnel(LinuxApplication):
31 _rtype = "abstract::linux::Tunnel"
32 _help = "Constructs a tunnel between two Linux endpoints"
34 def __init__(self, ec, guid):
35 super(LinuxTunnel, self).__init__(ec, guid)
36 self._home = "tunnel-%s" % self.guid
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"),
44 def get_endpoints(self):
45 """ Returns the list of RM that are endpoints to the tunnel
47 raise NotImplementedError
51 endpoints = self.get_endpoints()
52 if endpoints: return endpoints[0]
57 endpoints = self.get_endpoints()
58 if endpoints and len(endpoints) > 1: return endpoints[1]
61 def app_home(self, endpoint):
62 return os.path.join(endpoint.node.exp_home, self._home)
64 def run_home(self, endpoint):
65 return os.path.join(self.app_home(endpoint), self.ec.run_id)
67 def endpoint_mkdir(self, endpoint):
68 endpoint.node.mkdir(self.run_home(endpoint))
70 def initiate_connection(self, endpoint, remote_endpoint):
71 raise NotImplementedError
73 def establish_connection(self, endpoint, remote_endpoint, data):
74 raise NotImplementedError
76 def verify_connection(self, endpoint, remote_endpoint):
77 raise NotImplementedError
79 def terminate_connection(self, endpoint, remote_endpoint):
80 raise NotImplementedError
82 def check_state_connection(self, endpoint, remote_endpoint):
83 raise NotImplementedError
85 def do_provision(self):
86 # create run dir for tunnel on each node
87 self.endpoint_mkdir(self.endpoint1)
88 self.endpoint_mkdir(self.endpoint2)
90 self.debug("Initiate the connection")
91 # Start 2 step connection
92 # Initiate connection from endpoint 1 to endpoint 2
93 data1 = self.initiate_connection(self.endpoint1, self.endpoint2)
95 # Initiate connection from endpoint 2 to endpoint 1
96 data2 = self.initiate_connection(self.endpoint2, self.endpoint1)
98 self.debug("Establish the connection")
99 # Establish connection from endpoint 1 to endpoint 2
100 self.establish_connection(self.endpoint1, self.endpoint2, data2)
102 # Establish connection from endpoint 2 to endpoint 1
103 self.establish_connection(self.endpoint2, self.endpoint1, data1)
105 self.debug("Verify the connection")
106 # check if connection was successful on both sides
107 self.verify_connection(self.endpoint1, self.endpoint2)
108 self.verify_connection(self.endpoint2, self.endpoint1)
110 self.info("Provisioning finished")
112 self.set_provisioned()
115 if (not self.endpoint1 or self.endpoint1.state < ResourceState.READY) or \
116 (not self.endpoint2 or self.endpoint2.state < ResourceState.READY):
117 self.ec.schedule(self.reschedule_delay, self.deploy)
125 if self.state == ResourceState.READY:
126 command = self.get("command")
127 self.info("Starting command '%s'" % command)
131 msg = " Failed to execute command '%s'" % command
132 self.error(msg, out, err)
133 raise RuntimeError, msg
136 """ Stops application execution
139 if self.state == ResourceState.STARTED:
140 self.info("Stopping tunnel")
142 self.terminate_connection(self.endpoint1, self.endpoint2)
143 self.terminate_connection(self.endpoint2, self.endpoint1)
149 """ Returns the state of the application
151 if self._state == ResourceState.STARTED:
152 # In order to avoid overwhelming the remote host and
153 # the local processor with too many ssh queries, the state is only
154 # requested every 'state_check_delay' seconds.
155 if tdiffsec(tnow(), self._last_state_check) > state_check_delay:
157 self.check_state_connection()
159 self._last_state_check = tnow()
163 def valid_connection(self, guid):