mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-24 09:18:08 +00:00
Change address/host binding implementation.
This commit is contained in:
parent
862448ada1
commit
3edbdbe0b5
@ -47,8 +47,8 @@ class IModule(multiprocessing.Process):
|
||||
self._context = None
|
||||
self._ioloop = None
|
||||
self._stream = None
|
||||
self._host = args[0]
|
||||
self._port = args[1]
|
||||
self._zmq_host = args[0] # ZeroMQ server address
|
||||
self._zmq_port = args[1] # ZeroMQ server port
|
||||
self._current_session = None
|
||||
self._current_destination = None
|
||||
self._current_call_id = None
|
||||
@ -60,7 +60,7 @@ class IModule(multiprocessing.Process):
|
||||
|
||||
self._context = zmq.Context()
|
||||
self._ioloop = zmq.eventloop.ioloop.IOLoop.instance()
|
||||
self._stream = self._create_stream(self._host, self._port, self._decode_request)
|
||||
self._stream = self._create_stream(self._zmq_host, self._zmq_port, self._decode_request)
|
||||
|
||||
def _create_stream(self, host=None, port=0, callback=None):
|
||||
"""
|
||||
|
@ -115,7 +115,7 @@ class Dynamips(IModule):
|
||||
self._tempdir = kwargs["temp_dir"]
|
||||
self._working_dir = self._projects_dir
|
||||
self._dynamips = ""
|
||||
self._default_host = "127.0.0.1"
|
||||
self._host = kwargs["host"]
|
||||
|
||||
if not sys.platform.startswith("win32"):
|
||||
#FIXME: pickle issues Windows
|
||||
@ -216,7 +216,7 @@ class Dynamips(IModule):
|
||||
raise DynamipsError("Cannot write to working directory {}".format(self._working_dir))
|
||||
|
||||
log.info("starting the hypervisor manager with Dynamips working directory set to '{}'".format(self._working_dir))
|
||||
self._hypervisor_manager = HypervisorManager(self._dynamips, self._working_dir, self._default_host)
|
||||
self._hypervisor_manager = HypervisorManager(self._dynamips, self._working_dir, self._host)
|
||||
|
||||
for name, value in self._hypervisor_manager_settings.items():
|
||||
if hasattr(self._hypervisor_manager, name) and getattr(self._hypervisor_manager, name) != value:
|
||||
|
@ -67,8 +67,17 @@ class DynamipsHypervisor(object):
|
||||
Connects to the hypervisor.
|
||||
"""
|
||||
|
||||
# connect to a local address by default
|
||||
# if listening to all addresses (IPv4 or IPv6)
|
||||
if self._host == "0.0.0.0":
|
||||
host = "127.0.0.1"
|
||||
elif self._host == "::":
|
||||
host = "::1"
|
||||
else:
|
||||
host = self._host
|
||||
|
||||
try:
|
||||
self._socket = socket.create_connection((self._host,
|
||||
self._socket = socket.create_connection((host,
|
||||
self._port),
|
||||
self._timeout)
|
||||
except OSError as e:
|
||||
|
@ -406,6 +406,13 @@ class HypervisorManager(object):
|
||||
:param timeout: timeout value (default is 10 seconds)
|
||||
"""
|
||||
|
||||
# connect to a local address by default
|
||||
# if listening to all addresses (IPv4 or IPv6)
|
||||
if host == "0.0.0.0":
|
||||
host = "127.0.0.1"
|
||||
elif host == "::":
|
||||
host = "::1"
|
||||
|
||||
connection_success = False
|
||||
begin = time.time()
|
||||
# try to connect for 10 seconds
|
||||
@ -423,7 +430,7 @@ class HypervisorManager(object):
|
||||
if not connection_success:
|
||||
# FIXME: throw exception here
|
||||
log.critical("Couldn't connect to hypervisor on {}:{} :{}".format(host, port,
|
||||
last_exception))
|
||||
last_exception))
|
||||
else:
|
||||
log.info("Dynamips server ready after {:.4f} seconds".format(time.time() - begin))
|
||||
|
||||
|
@ -83,7 +83,7 @@ class IOU(IModule):
|
||||
self._udp_start_port_range = 30001
|
||||
self._udp_end_port_range = 40001
|
||||
self._current_udp_port = self._udp_start_port_range
|
||||
self._default_host = "0.0.0.0"
|
||||
self._host = kwargs["host"]
|
||||
self._projects_dir = os.path.join(kwargs["projects_dir"], "iou")
|
||||
self._tempdir = kwargs["temp_dir"]
|
||||
self._working_dir = self._projects_dir
|
||||
@ -252,11 +252,11 @@ class IOU(IModule):
|
||||
except OSError as e:
|
||||
raise IOUError("Could not create working directory {}".format(e))
|
||||
|
||||
iou_instance = IOUDevice(iou_path, self._working_dir, host=self._default_host, name=name)
|
||||
iou_instance = IOUDevice(iou_path, self._working_dir, host=self._host, name=name)
|
||||
# find a console port
|
||||
if self._current_console_port >= self._console_end_port_range:
|
||||
self._current_console_port = self._console_start_port_range
|
||||
iou_instance.console = IOUDevice.find_unused_port(self._current_console_port, self._console_end_port_range, self._default_host)
|
||||
iou_instance.console = IOUDevice.find_unused_port(self._current_console_port, self._console_end_port_range, self._host)
|
||||
self._current_console_port += 1
|
||||
except IOUError as e:
|
||||
self.send_custom_error(str(e))
|
||||
@ -479,13 +479,13 @@ class IOU(IModule):
|
||||
# find a UDP port
|
||||
if self._current_udp_port >= self._udp_end_port_range:
|
||||
self._current_udp_port = self._udp_start_port_range
|
||||
port = IOUDevice.find_unused_port(self._current_udp_port, self._udp_end_port_range, host=self._default_host, socket_type="UDP")
|
||||
port = IOUDevice.find_unused_port(self._current_udp_port, self._udp_end_port_range, host=self._host, socket_type="UDP")
|
||||
self._current_udp_port += 1
|
||||
|
||||
log.info("{} [id={}] has allocated UDP port {} with host {}".format(iou_instance .name,
|
||||
iou_instance .id,
|
||||
port,
|
||||
self._default_host))
|
||||
self._host))
|
||||
response = {"lport": port}
|
||||
|
||||
except IOUError as e:
|
||||
|
@ -30,7 +30,6 @@ import signal
|
||||
import errno
|
||||
import functools
|
||||
import socket
|
||||
import tornado
|
||||
import tornado.ioloop
|
||||
import tornado.web
|
||||
import tornado.autoreload
|
||||
@ -59,14 +58,16 @@ class Server(object):
|
||||
if ipc:
|
||||
self._zmq_port = 0 # this forces to use IPC for communications with the ZeroMQ server
|
||||
else:
|
||||
# communication between the ZeroMQ server and the modules (ZeroMQ dealers)
|
||||
# is IPv4 and local (127.0.0.1)
|
||||
try:
|
||||
# let the OS find an unused port for the ZeroMQ server
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.bind(('127.0.0.1', 0))
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
self._zmq_port = sock.getsockname()[1]
|
||||
except OSError as e:
|
||||
log.warn("could not pick up a random port for the ZeroMQ server: {}".format(e))
|
||||
self._zmq_port = port + 1 # let's try this server port + 1
|
||||
log.critical("server cannot listen to {}: {}".format(self._host, e))
|
||||
raise SystemExit
|
||||
self._ipc = ipc
|
||||
self._modules = []
|
||||
|
||||
@ -111,12 +112,13 @@ class Server(object):
|
||||
#=======================================================================
|
||||
|
||||
# special built-in destination to stop the server
|
||||
JSONRPCWebSocket.register_destination("builtin.stop", self._cleanup)
|
||||
# JSONRPCWebSocket.register_destination("builtin.stop", self._cleanup)
|
||||
|
||||
for module in MODULES:
|
||||
instance = module(module.__name__.lower(),
|
||||
"127.0.0.1", # ZeroMQ server address
|
||||
self._zmq_port, # ZeroMQ server port
|
||||
host=self._host, # server host address
|
||||
projects_dir=self._projects_dir,
|
||||
temp_dir=self._temp_dir)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user