Remove redundant code for Dynamips hypervisor connections.

This commit is contained in:
Jeremy 2015-03-05 18:00:17 -07:00
parent c012e8ddb3
commit 2bae814eb1
2 changed files with 17 additions and 44 deletions

View File

@ -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))

View File

@ -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")