1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-26 08:48:11 +00:00

Wait more time for ioucon thread to be completed. Prevent IOS to be

started or stopped if the status isn't right.
This commit is contained in:
grossmj 2014-05-16 12:35:48 -06:00
parent 9b55a8623c
commit 6a839c4b7b
5 changed files with 66 additions and 40 deletions

View File

@ -21,6 +21,7 @@ Useful functions... in the attic ;)
import socket import socket
import errno import errno
import time
def find_unused_port(start_port, end_port, host='127.0.0.1', socket_type="TCP", ignore_ports=[]): def find_unused_port(start_port, end_port, host='127.0.0.1', socket_type="TCP", ignore_ports=[]):
@ -64,3 +65,40 @@ def find_unused_port(start_port, end_port, host='127.0.0.1', socket_type="TCP",
raise Exception("Could not find an unused port: {}".format(e)) raise Exception("Could not find an unused port: {}".format(e))
raise Exception("Could not find a free port between {0} and {1}".format(start_port, end_port)) raise Exception("Could not find a free port between {0} and {1}".format(start_port, end_port))
def wait_socket_is_ready(host, port, wait=2.0, socket_timeout=10):
"""
Waits for a socket to be ready for wait time.
:param host: host/address to connect to
:param port: port to connect to
:param wait: maximum wait time
:param socket_timeout: timeout for the socket
:returns: tuple with boolean indicating if the socket is ready and the last exception
that occurred when connecting to the socket
"""
# 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()
last_exception = None
while (time.time() - begin < wait):
time.sleep(0.01)
try:
with socket.create_connection((host, port), socket_timeout):
pass
except OSError as e:
last_exception = e
continue
connection_success = True
break
return (connection_success, last_exception)

View File

@ -22,10 +22,10 @@ Manages Dynamips hypervisors (load-balancing etc.)
from .hypervisor import Hypervisor from .hypervisor import Hypervisor
from .dynamips_error import DynamipsError from .dynamips_error import DynamipsError
from ..attic import find_unused_port from ..attic import find_unused_port
from ..attic import wait_socket_is_ready
from pkg_resources import parse_version from pkg_resources import parse_version
import os import os
import socket
import time import time
import logging import logging
@ -513,26 +513,9 @@ class HypervisorManager(object):
:param timeout: timeout value (default is 10 seconds) :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() begin = time.time()
# try to connect for 10 seconds # wait for the socket for a maximum of 10 seconds.
while(time.time() - begin < 10.0): connection_success, last_exception = wait_socket_is_ready(host, port, wait=10.0)
time.sleep(0.01)
try:
with socket.create_connection((host, port), timeout):
pass
except OSError as e:
last_exception = e
continue
connection_success = True
break
if not connection_success: if not connection_success:
# FIXME: throw exception here # FIXME: throw exception here

View File

@ -313,9 +313,10 @@ class Router(object):
At least the IOS image must be set before starting it. At least the IOS image must be set before starting it.
""" """
if self.get_status() == "suspended": status = self.get_status()
if status == "suspended":
self.resume() self.resume()
else: elif status == "inactive":
if not os.path.isfile(self._image): if not os.path.isfile(self._image):
raise DynamipsError("IOS image '{}' is not accessible".format(self._image)) raise DynamipsError("IOS image '{}' is not accessible".format(self._image))
@ -340,6 +341,7 @@ class Router(object):
The settings are kept. The settings are kept.
""" """
if self.get_status() != "inactive":
self._hypervisor.send("vm stop {}".format(self._name)) self._hypervisor.send("vm stop {}".format(self._name))
log.info("router {name} [id={id}] has been stopped".format(name=self._name, id=self._id)) log.info("router {name} [id={id}] has been stopped".format(name=self._name, id=self._id))

View File

@ -523,25 +523,11 @@ class IOUDevice(object):
Stops the IOU process. Stops the IOU process.
""" """
# stop the IOU process
if self.is_running():
log.info("stopping IOU instance {} PID={}".format(self._id, self._process.pid))
try:
self._process.terminate()
self._process.wait(1)
except subprocess.TimeoutExpired:
self._process.kill()
if self._process.poll() == None:
log.warn("IOU instance {} PID={} is still running".format(self._id,
self._process.pid))
self._process = None
self._started = False
# stop console support # stop console support
if self._ioucon_thead: if self._ioucon_thead:
self._ioucon_thread_stop_event.set() self._ioucon_thread_stop_event.set()
if self._ioucon_thead.is_alive(): if self._ioucon_thead.is_alive():
self._ioucon_thead.join(timeout=0.10) self._ioucon_thead.join(timeout=3.0) # wait for the thread to free the console port
self._ioucon_thead = None self._ioucon_thead = None
# stop iouyap # stop iouyap
@ -557,6 +543,20 @@ class IOUDevice(object):
self._id)) self._id))
self._iouyap_process = None self._iouyap_process = None
# stop the IOU process
if self.is_running():
log.info("stopping IOU instance {} PID={}".format(self._id, self._process.pid))
try:
self._process.terminate()
self._process.wait(1)
except subprocess.TimeoutExpired:
self._process.kill()
if self._process.poll() == None:
log.warn("IOU instance {} PID={} is still running".format(self._id,
self._process.pid))
self._process = None
self._started = False
def read_iou_stdout(self): def read_iou_stdout(self):
""" """
Reads the standard output of the IOU process. Reads the standard output of the IOU process.

View File

@ -139,7 +139,10 @@ class FileLock:
def unlock(self): def unlock(self):
if self.fd: if self.fd:
# Deleting first prevents a race condition # Deleting first prevents a race condition
try:
os.unlink(self.fd.name) os.unlink(self.fd.name)
except FileNotFoundError as e:
log.debug("{}".format(e))
self.fd.close() self.fd.close()
def __enter__(self): def __enter__(self):