Example #1
0
 def stop_server(self, port, protocol):
     try:
         server_process = self._server_registry.get_server(
             self._ns, port, protocol)
         self.log.info(
             "Stop %s server on port %s in namespace %s" %
             (protocol, port, self._ns))
         if server_process and server_process.is_running():
             with nsenter.namespace(self._ns_full_path, 'net'):
                 server_process.stop()
                 self._server_registry.remove_server(
                     self._ns, port, protocol)
         elif server_process and not server_process.is_running():
             self.log.warning("%s server is not running on %s" %
                              (protocol, port))
             self._server_registry.remove_server(
                 self.ROOT_NAMESPACE_NAME, port, protocol)
         else:
             self.log.warning("%s server is not running on %s" %
                              (protocol, port))
     except Exception as e:
         self.log.exception(
             "Stopping %s server on port %s failed in namespace %s" %
             (protocol, port, self._ns))
         raise e
Example #2
0
 def _discover_interfaces(self):
     ns_path = '/var/run/netns/%s' % self.name
     manager = multiprocessing.Manager()
     return_dict = manager.dict()
     with nsenter.namespace(ns_path, 'net'):
         process = multiprocessing.Process(
             target=get_interfaces_in_namespace, args=(return_dict,))
         process.start()
         process.join()
     for name, snics in list(return_dict['result'].items()):
         for nic in [snic for snic in snics if snic.family == 2]:
             self._interface_list.append(Interface(
                 name, nic.address, nic.family,
                 nic.netmask, nic.broadcast))
Example #3
0
 def start_client(self, src, clients):
     client = self._client_registry.get_client(self._ns)
     if client and client.is_running():
         self.log.warning("Client is already running on %s" % src)
         return
     try:
         process = WorkerProcess(
             TrafficClient, (src, clients, self._record_queue), {})
         with nsenter.namespace(self._ns_full_path, 'net'):
             process.start()
             self._client_registry.add_client(self._ns, process)
     except Exception as e:
         self.log.exception(
             "Starting client process on interface %s failed" % src)
         raise e
Example #4
0
 def stop_client(self):
     try:
         client = self._client_registry.get_client(self._ns)
         if client and client.is_running():
             with nsenter.namespace(self._ns_full_path, 'net'):
                 client.stop()
                 self._client_registry.remove_client(self._ns)
         elif client and not client.is_running():
             self.log.warning("Client is not running in namespace %s" %
                              self._ns)
             self._client_registry.remove_client(self._ns)
         else:
             self.log.warning("Client is not running in namespace %s" %
                              self._ns)
     except Exception:
         self.log.exception("Stopping client failed in namespace %s" %
                            self._ns)
 def start_server(self, protocol, port, src="0.0.0.0"):
     server_process = self._server_registry.get_server(
         self._ns, port, protocol)
     if server_process and server_process.is_running():
         self.log.warning("%s server on port %s is already running" %
                          (protocol, port))
         return
     self.log.info(
         "Starting %s server on port %s on interface %s in namespace %s" %
         (protocol, port, src, self._ns))
     try:
         server_cls, args, kwargs = create_server_class(protocol, port, src)
         process = WorkerProcess(server_cls, args, kwargs)
         with nsenter.namespace(self._ns_full_path, 'net'):
             process.start()
         self._server_registry.add_server(self._ns, port, protocol, process)
     except Exception as e:
         self.log.exception(
             "Starting %s server on port %s on interface %s failed" %
             (protocol, port, src))
         raise e