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