mirror of
https://github.com/GNS3/gns3-server.git
synced 2025-01-29 20:43:48 +02:00
Remove redundant code for Dynamips hypervisor connections.
This commit is contained in:
parent
c012e8ddb3
commit
2bae814eb1
@ -294,34 +294,6 @@ class Dynamips(BaseManager):
|
|||||||
self._dynamips_path = dynamips_path
|
self._dynamips_path = dynamips_path
|
||||||
return 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
|
@asyncio.coroutine
|
||||||
def start_new_hypervisor(self, working_dir=None):
|
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))
|
log.info("Creating new hypervisor {}:{} with working directory {}".format(hypervisor.host, hypervisor.port, working_dir))
|
||||||
yield from hypervisor.start()
|
yield from hypervisor.start()
|
||||||
|
|
||||||
yield from self._wait_for_hypervisor(server_host, port)
|
|
||||||
log.info("Hypervisor {}:{} has successfully started".format(hypervisor.host, hypervisor.port))
|
log.info("Hypervisor {}:{} has successfully started".format(hypervisor.host, hypervisor.port))
|
||||||
|
|
||||||
yield from hypervisor.connect()
|
yield from hypervisor.connect()
|
||||||
if parse_version(hypervisor.version) < parse_version('0.2.11'):
|
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))
|
raise DynamipsError("Dynamips version must be >= 0.2.11, detected version is {}".format(hypervisor.version))
|
||||||
|
@ -21,6 +21,7 @@ http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L46
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import time
|
||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
@ -59,7 +60,7 @@ class DynamipsHypervisor:
|
|||||||
self._writer = None
|
self._writer = None
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def connect(self):
|
def connect(self, timeout=10):
|
||||||
"""
|
"""
|
||||||
Connects to the hypervisor.
|
Connects to the hypervisor.
|
||||||
"""
|
"""
|
||||||
@ -73,20 +74,23 @@ class DynamipsHypervisor:
|
|||||||
else:
|
else:
|
||||||
host = self._host
|
host = self._host
|
||||||
|
|
||||||
tries = 3
|
begin = time.time()
|
||||||
while tries > 0:
|
connection_success = False
|
||||||
|
last_exception = None
|
||||||
|
while time.time() - begin < timeout:
|
||||||
|
yield from asyncio.sleep(0.01)
|
||||||
try:
|
try:
|
||||||
self._reader, self._writer = yield from asyncio.wait_for(asyncio.open_connection(host, self._port), timeout=self._timeout)
|
self._reader, self._writer = yield from asyncio.open_connection(host, self._port)
|
||||||
break
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if tries:
|
last_exception = e
|
||||||
tries -= 1
|
continue
|
||||||
log.warn("Could not connect to hypervisor {}:{} {}, retrying...".format(host, self._port, e))
|
connection_success = True
|
||||||
yield from asyncio.sleep(0.1)
|
break
|
||||||
continue
|
|
||||||
raise DynamipsError("Could not connect to hypervisor {}:{} {}".format(host, self._port, e))
|
if not connection_success:
|
||||||
except asyncio.TimeoutError:
|
raise DynamipsError("Couldn't connect to hypervisor on {}:{} :{}".format(host, self._port, last_exception))
|
||||||
raise DynamipsError("Timeout error while connecting to hypervisor {}:{}".format(host, self._port))
|
else:
|
||||||
|
log.info("Connected to Dynamips hypervisor after {:.4f} seconds".format(time.time() - begin))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
version = yield from self.send("hypervisor version")
|
version = yield from self.send("hypervisor version")
|
||||||
|
Loading…
Reference in New Issue
Block a user