mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-22 22:10:57 +00:00
parent
1f756c0d4f
commit
5a28b9409a
@ -40,12 +40,25 @@ class Docker(BaseManager):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._server_url = '/var/run/docker.sock'
|
||||
self._connector = aiohttp.connector.UnixConnector(self._server_url)
|
||||
self._connected = False
|
||||
# Allow locking during ubridge operations
|
||||
self.ubridge_lock = asyncio.Lock()
|
||||
|
||||
@asyncio.coroutine
|
||||
def connector(self):
|
||||
if not self._connected:
|
||||
try:
|
||||
self._connector = aiohttp.connector.UnixConnector(self._server_url)
|
||||
self._connected = True
|
||||
yield from self.query("GET", "info")
|
||||
except (aiohttp.errors.ClientOSError, FileNotFoundError):
|
||||
self._connected = False
|
||||
raise DockerError("Can't connect to docker daemon")
|
||||
return self._connector
|
||||
|
||||
def __del__(self):
|
||||
self._connector.close()
|
||||
if self._connected:
|
||||
self._connector.close()
|
||||
|
||||
@asyncio.coroutine
|
||||
def query(self, method, path, data={}, params={}):
|
||||
@ -80,7 +93,7 @@ class Docker(BaseManager):
|
||||
response = yield from aiohttp.request(
|
||||
method,
|
||||
url,
|
||||
connector=self._connector,
|
||||
connector=(yield from self.connector()),
|
||||
params=params,
|
||||
data=data,
|
||||
headers={"content-type": "application/json", },
|
||||
@ -107,7 +120,7 @@ class Docker(BaseManager):
|
||||
|
||||
url = "http://docker/" + path
|
||||
connection = yield from aiohttp.ws_connect(url,
|
||||
connector=self._connector,
|
||||
connector=(yield from self.connector()),
|
||||
origin="http://docker",
|
||||
autoping=True)
|
||||
return connection
|
||||
|
@ -249,9 +249,6 @@ class Server:
|
||||
if server_config.getboolean("shell"):
|
||||
asyncio.async(self.start_shell())
|
||||
|
||||
from gns3server.modules.docker import Docker
|
||||
asyncio.async(Docker.instance().query("GET", "info"))
|
||||
|
||||
try:
|
||||
self._loop.run_forever()
|
||||
except TypeError as e:
|
||||
|
@ -27,6 +27,7 @@ from gns3server.modules.docker.docker_error import DockerError
|
||||
def test_query_success(loop):
|
||||
|
||||
vm = Docker()
|
||||
vm._connector = MagicMock()
|
||||
response = MagicMock()
|
||||
response.status = 200
|
||||
|
||||
@ -50,6 +51,8 @@ def test_query_success(loop):
|
||||
def test_query_error(loop):
|
||||
|
||||
vm = Docker()
|
||||
vm._connected = True
|
||||
vm._connector = MagicMock()
|
||||
response = MagicMock()
|
||||
response.status = 404
|
||||
|
||||
@ -72,6 +75,8 @@ def test_query_error(loop):
|
||||
def test_query_error_json(loop):
|
||||
|
||||
vm = Docker()
|
||||
vm._connected = True
|
||||
vm._connector = MagicMock()
|
||||
response = MagicMock()
|
||||
response.status = 404
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user