From 2bae814eb1ec82f62ea2fdee7f9f1caf7fc530ce Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 5 Mar 2015 18:00:17 -0700 Subject: [PATCH] Remove redundant code for Dynamips hypervisor connections. --- gns3server/modules/dynamips/__init__.py | 31 ------------------- .../modules/dynamips/dynamips_hypervisor.py | 30 ++++++++++-------- 2 files changed, 17 insertions(+), 44 deletions(-) diff --git a/gns3server/modules/dynamips/__init__.py b/gns3server/modules/dynamips/__init__.py index 0cf6ae4b..21db9cbd 100644 --- a/gns3server/modules/dynamips/__init__.py +++ b/gns3server/modules/dynamips/__init__.py @@ -294,34 +294,6 @@ class Dynamips(BaseManager): self._dynamips_path = dynamips_path return dynamips_path - @asyncio.coroutine - def _wait_for_hypervisor(self, host, port, timeout=10.0): - """ - Waits for an hypervisor to be started (accepting a socket connection) - - :param host: host/address to connect to the hypervisor - :param port: port to connect to the hypervisor - """ - - begin = time.time() - connection_success = False - last_exception = None - while time.time() - begin < timeout: - yield from asyncio.sleep(0.01) - try: - _, writer = yield from asyncio.open_connection(host, port) - writer.close() - except OSError as e: - last_exception = e - continue - connection_success = True - break - - if not connection_success: - raise DynamipsError("Couldn't connect to hypervisor on {}:{} :{}".format(host, port, last_exception)) - else: - log.info("Dynamips server ready after {:.4f} seconds".format(time.time() - begin)) - @asyncio.coroutine def start_new_hypervisor(self, working_dir=None): """ @@ -356,10 +328,7 @@ class Dynamips(BaseManager): log.info("Creating new hypervisor {}:{} with working directory {}".format(hypervisor.host, hypervisor.port, working_dir)) yield from hypervisor.start() - - yield from self._wait_for_hypervisor(server_host, port) log.info("Hypervisor {}:{} has successfully started".format(hypervisor.host, hypervisor.port)) - yield from hypervisor.connect() if parse_version(hypervisor.version) < parse_version('0.2.11'): raise DynamipsError("Dynamips version must be >= 0.2.11, detected version is {}".format(hypervisor.version)) diff --git a/gns3server/modules/dynamips/dynamips_hypervisor.py b/gns3server/modules/dynamips/dynamips_hypervisor.py index 20ab9764..7c22f011 100644 --- a/gns3server/modules/dynamips/dynamips_hypervisor.py +++ b/gns3server/modules/dynamips/dynamips_hypervisor.py @@ -21,6 +21,7 @@ http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L46 """ import re +import time import logging import asyncio @@ -59,7 +60,7 @@ class DynamipsHypervisor: self._writer = None @asyncio.coroutine - def connect(self): + def connect(self, timeout=10): """ Connects to the hypervisor. """ @@ -73,20 +74,23 @@ class DynamipsHypervisor: else: host = self._host - tries = 3 - while tries > 0: + begin = time.time() + connection_success = False + last_exception = None + while time.time() - begin < timeout: + yield from asyncio.sleep(0.01) try: - self._reader, self._writer = yield from asyncio.wait_for(asyncio.open_connection(host, self._port), timeout=self._timeout) - break + self._reader, self._writer = yield from asyncio.open_connection(host, self._port) except OSError as e: - if tries: - tries -= 1 - log.warn("Could not connect to hypervisor {}:{} {}, retrying...".format(host, self._port, e)) - yield from asyncio.sleep(0.1) - continue - raise DynamipsError("Could not connect to hypervisor {}:{} {}".format(host, self._port, e)) - except asyncio.TimeoutError: - raise DynamipsError("Timeout error while connecting to hypervisor {}:{}".format(host, self._port)) + last_exception = e + continue + connection_success = True + break + + if not connection_success: + raise DynamipsError("Couldn't connect to hypervisor on {}:{} :{}".format(host, self._port, last_exception)) + else: + log.info("Connected to Dynamips hypervisor after {:.4f} seconds".format(time.time() - begin)) try: version = yield from self.send("hypervisor version")