1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-24 17:28:08 +00:00

Merge branch 'master' into 2.1

This commit is contained in:
Julien Duponchelle 2017-07-11 18:05:11 +02:00
commit 699ecb7c89
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
7 changed files with 338 additions and 10 deletions

View File

@ -36,6 +36,7 @@ log = logging.getLogger(__name__)
# Be carefull to keep it consistent # Be carefull to keep it consistent
DOCKER_MINIMUM_API_VERSION = "1.25" DOCKER_MINIMUM_API_VERSION = "1.25"
DOCKER_MINIMUM_VERSION = "1.13" DOCKER_MINIMUM_VERSION = "1.13"
DOCKER_PREFERRED_API_VERSION = "1.30"
class Docker(BaseManager): class Docker(BaseManager):
@ -50,6 +51,7 @@ class Docker(BaseManager):
self.ubridge_lock = asyncio.Lock() self.ubridge_lock = asyncio.Lock()
self._connector = None self._connector = None
self._session = None self._session = None
self._api_version = DOCKER_MINIMUM_API_VERSION
@asyncio.coroutine @asyncio.coroutine
def _check_connection(self): def _check_connection(self):
@ -61,8 +63,17 @@ class Docker(BaseManager):
except (aiohttp.ClientOSError, FileNotFoundError): except (aiohttp.ClientOSError, FileNotFoundError):
self._connected = False self._connected = False
raise DockerError("Can't connect to docker daemon") raise DockerError("Can't connect to docker daemon")
if parse_version(version["ApiVersion"]) < parse_version(DOCKER_MINIMUM_API_VERSION):
raise DockerError("Docker version is {}. GNS3 requires a minimum version of {}".format(version["Version"], DOCKER_MINIMUM_VERSION)) docker_version = parse_version(version['ApiVersion'])
if docker_version < parse_version(DOCKER_MINIMUM_API_VERSION):
raise DockerError(
"Docker version is {}. GNS3 requires a minimum version of {}".format(
version["Version"], DOCKER_MINIMUM_VERSION))
preferred_api_version = parse_version(DOCKER_PREFERRED_API_VERSION)
if docker_version >= preferred_api_version:
self._api_version = DOCKER_PREFERRED_API_VERSION
def connector(self): def connector(self):
if self._connector is None or self._connector.closed: if self._connector is None or self._connector.closed:
@ -165,8 +176,8 @@ class Docker(BaseManager):
:returns: Websocket :returns: Websocket
""" """
url = "http://docker/v" + DOCKER_MINIMUM_API_VERSION + "/" + path url = "http://docker/v" + self._api_version + "/" + path
connection = yield from self._session.ws_connect(url, connection = yield from aiohttp.ws_connect(url,
origin="http://docker", origin="http://docker",
autoping=True) autoping=True)
return connection return connection

View File

@ -0,0 +1,248 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Docker server module.
"""
import sys
import json
import asyncio
import logging
import aiohttp
from gns3server.utils import parse_version
from gns3server.utils.asyncio import locked_coroutine
from gns3server.compute.base_manager import BaseManager
from gns3server.compute.docker.docker_vm import DockerVM
from gns3server.compute.docker.docker_error import DockerError, DockerHttp304Error, DockerHttp404Error
log = logging.getLogger(__name__)
# Be carefull to keep it consistent
DOCKER_MINIMUM_API_VERSION = "1.25"
DOCKER_MINIMUM_VERSION = "1.13"
DOCKER_PREFERRED_API_VERSION = "1.30"
class Docker(BaseManager):
_NODE_CLASS = DockerVM
def __init__(self):
super().__init__()
self._server_url = '/var/run/docker.sock'
self._connected = False
# Allow locking during ubridge operations
self.ubridge_lock = asyncio.Lock()
self._connector = None
self._session = None
self._api_version = DOCKER_MINIMUM_API_VERSION
@asyncio.coroutine
def _check_connection(self):
if not self._connected:
try:
self._connected = True
connector = self.connector()
version = yield from self.query("GET", "version")
except (aiohttp.ClientOSError, FileNotFoundError):
self._connected = False
raise DockerError("Can't connect to docker daemon")
docker_version = parse_version(version['ApiVersion'])
if docker_version < parse_version(DOCKER_MINIMUM_API_VERSION):
raise DockerError(
"Docker version is {}. GNS3 requires a minimum version of {}".format(
version["Version"], DOCKER_MINIMUM_VERSION))
preferred_api_version = parse_version(DOCKER_PREFERRED_API_VERSION)
if docker_version >= preferred_api_version:
self._api_version = DOCKER_PREFERRED_API_VERSION
def connector(self):
if self._connector is None or self._connector.closed:
if not sys.platform.startswith("linux"):
raise DockerError("Docker is supported only on Linux")
try:
self._connector = aiohttp.connector.UnixConnector(self._server_url, limit=None)
except (aiohttp.ClientOSError, FileNotFoundError):
raise DockerError("Can't connect to docker daemon")
return self._connector
@asyncio.coroutine
def unload(self):
yield from super().unload()
if self._connected:
if self._connector and not self._connector.closed:
self._connector.close()
@asyncio.coroutine
def query(self, method, path, data={}, params={}):
"""
Make a query to the docker daemon and decode the request
:param method: HTTP method
:param path: Endpoint in API
:param data: Dictionnary with the body. Will be transformed to a JSON
:param params: Parameters added as a query arg
"""
response = yield from self.http_query(method, path, data=data, params=params)
body = yield from response.read()
if body and len(body):
if response.headers['CONTENT-TYPE'] == 'application/json':
body = json.loads(body.decode("utf-8"))
else:
body = body.decode("utf-8")
log.debug("Query Docker %s %s params=%s data=%s Response: %s", method, path, params, data, body)
return body
@asyncio.coroutine
def http_query(self, method, path, data={}, params={}, timeout=300):
"""
Make a query to the docker daemon
:param method: HTTP method
:param path: Endpoint in API
:param data: Dictionnary with the body. Will be transformed to a JSON
:param params: Parameters added as a query arg
:param timeout: Timeout
:returns: HTTP response
"""
data = json.dumps(data)
if timeout is None:
timeout = 60 * 60 * 24 * 31 # One month timeout
if path == 'version':
url = "http://docker/v1.12/" + path # API of docker v1.0
else:
url = "http://docker/v" + DOCKER_MINIMUM_API_VERSION + "/" + path
try:
if path != "version": # version is use by check connection
yield from self._check_connection()
if self._session is None or self._session.closed:
connector = self.connector()
self._session = aiohttp.ClientSession(connector=connector)
response = yield from self._session.request(
method,
url,
params=params,
data=data,
headers={"content-type": "application/json", },
timeout=timeout
)
except (aiohttp.ClientResponseError, aiohttp.ClientOSError) as e:
raise DockerError("Docker has returned an error: {}".format(str(e)))
except (asyncio.TimeoutError):
raise DockerError("Docker timeout " + method + " " + path)
if response.status >= 300:
body = yield from response.read()
try:
body = json.loads(body.decode("utf-8"))["message"]
except ValueError:
pass
log.debug("Query Docker %s %s params=%s data=%s Response: %s", method, path, params, data, body)
if response.status == 304:
raise DockerHttp304Error("Docker has returned an error: {} {}".format(response.status, body))
elif response.status == 404:
raise DockerHttp404Error("Docker has returned an error: {} {}".format(response.status, body))
else:
raise DockerError("Docker has returned an error: {} {}".format(response.status, body))
return response
@asyncio.coroutine
def websocket_query(self, path, params={}):
"""
Open a websocket connection
:param path: Endpoint in API
:param params: Parameters added as a query arg
:returns: Websocket
"""
<<<<<<< HEAD
url = "http://docker/v" + DOCKER_MINIMUM_API_VERSION + "/" + path
connection = yield from self._session.ws_connect(url,
origin="http://docker",
autoping=True)
=======
url = "http://docker/v" + self._api_version + "/" + path
connection = yield from aiohttp.ws_connect(url,
connector=self.connector(),
origin="http://docker",
autoping=True)
>>>>>>> master
return connection
@locked_coroutine
def pull_image(self, image, progress_callback=None):
"""
Pull image from docker repository
:params image: Image name
:params progress_callback: A function that receive a log message about image download progress
"""
try:
yield from self.query("GET", "images/{}/json".format(image))
return # We already have the image skip the download
except DockerHttp404Error:
pass
if progress_callback:
progress_callback("Pull {} from docker hub".format(image))
response = yield from self.http_query("POST", "images/create", params={"fromImage": image}, timeout=None)
# The pull api will stream status via an HTTP JSON stream
content = ""
while True:
try:
chunk = yield from response.content.read(1024)
except aiohttp.ServerDisconnectedError:
break
if not chunk:
break
content += chunk.decode("utf-8")
try:
while True:
content = content.lstrip(" \r\n\t")
answer, index = json.JSONDecoder().raw_decode(content)
if "progress" in answer and progress_callback:
progress_callback("Pulling image {}:{}: {}".format(image, answer["id"], answer["progress"]))
content = content[index:]
except ValueError: # Partial JSON
pass
response.close()
if progress_callback:
progress_callback("Success pulling image {}".format(image))
@asyncio.coroutine
def list_images(self):
"""Gets Docker image list.
:returns: list of dicts
:rtype: list
"""
images = []
for image in (yield from self.query("GET", "images/json", params={"all": 0})):
if image['RepoTags']:
for tag in image['RepoTags']:
if tag != "<none>:<none>":
images.append({'image': tag})
return sorted(images, key=lambda i: i['image'])

View File

@ -502,6 +502,10 @@ class DockerVM(BaseNode):
msg = yield from ws.receive() msg = yield from ws.receive()
if msg.tp == aiohttp.WSMsgType.text: if msg.tp == aiohttp.WSMsgType.text:
out.feed_data(msg.data.encode()) out.feed_data(msg.data.encode())
elif msg.tp == aiohttp.WSMsgType.BINARY:
out.feed_data(msg.data)
elif msg.tp == aiohttp.WSMsgType.ERROR:
log.critical("Docker WebSocket Error: {}".format(msg.data))
else: else:
out.feed_eof() out.feed_eof()
ws.close() ws.close()

View File

@ -182,9 +182,9 @@ class IOUVM(BaseNode):
except OSError as e: except OSError as e:
raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._path, e)) raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._path, e))
# IOU images must start with the ELF magic number, be 32-bit, little endian # IOU images must start with the ELF magic number, be 32-bit or 64-bit, little endian
# and have an ELF version of 1 normal IOS image are big endian! # and have an ELF version of 1 normal IOS image are big endian!
if elf_header_start != b'\x7fELF\x01\x01\x01': if elf_header_start != b'\x7fELF\x01\x01\x01' and elf_header_start != b'\x7fELF\x02\x01\x01':
raise IOUError("'{}' is not a valid IOU image".format(self._path)) raise IOUError("'{}' is not a valid IOU image".format(self._path))
if not os.access(self._path, os.X_OK): if not os.access(self._path, os.X_OK):

View File

@ -612,6 +612,9 @@ class Project:
:param name: Name of the snapshot :param name: Name of the snapshot
""" """
if name in [snap.name for snap in self.snapshots.values()]:
raise aiohttp.web_exceptions.HTTPConflict(text="The snapshot {} already exist".format(name))
snapshot = Snapshot(self, name=name) snapshot = Snapshot(self, name=name)
try: try:
if os.path.exists(snapshot.path): if os.path.exists(snapshot.path):

View File

@ -17,10 +17,10 @@
import pytest import pytest
import asyncio import asyncio
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
from tests.utils import asyncio_patch, AsyncioMagicMock from tests.utils import asyncio_patch, AsyncioMagicMock
from gns3server.compute.docker import Docker from gns3server.compute.docker import Docker, DOCKER_PREFERRED_API_VERSION, DOCKER_MINIMUM_API_VERSION
from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error
@ -162,3 +162,40 @@ def test_pull_image(loop):
with asyncio_patch("gns3server.compute.docker.Docker.http_query", return_value=mock_query) as mock: with asyncio_patch("gns3server.compute.docker.Docker.http_query", return_value=mock_query) as mock:
images = loop.run_until_complete(asyncio.async(Docker.instance().pull_image("ubuntu"))) images = loop.run_until_complete(asyncio.async(Docker.instance().pull_image("ubuntu")))
mock.assert_called_with("POST", "images/create", params={"fromImage": "ubuntu"}, timeout=None) mock.assert_called_with("POST", "images/create", params={"fromImage": "ubuntu"}, timeout=None)
def test_docker_check_connection_docker_minimum_version(vm, loop):
response = {
'ApiVersion': '1.01',
'Version': '1.12'
}
with patch("gns3server.compute.docker.Docker.connector"), \
asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
vm._connected = False
with pytest.raises(DockerError):
loop.run_until_complete(asyncio.async(vm._check_connection()))
def test_docker_check_connection_docker_preferred_version_against_newer(vm, loop):
response = {
'ApiVersion': '1.31'
}
with patch("gns3server.compute.docker.Docker.connector"), \
asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
vm._connected = False
loop.run_until_complete(asyncio.async(vm._check_connection()))
assert vm._api_version == DOCKER_PREFERRED_API_VERSION
def test_docker_check_connection_docker_preferred_version_against_older(vm, loop):
response = {
'ApiVersion': '1.27',
}
with patch("gns3server.compute.docker.Docker.connector"), \
asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
vm._connected = False
loop.run_until_complete(asyncio.async(vm._check_connection()))
assert vm._api_version == DOCKER_MINIMUM_API_VERSION

View File

@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import aiohttp
import asyncio import asyncio
import pytest import pytest
import uuid import uuid
@ -904,3 +905,27 @@ def test_fix_permission(vm, loop):
loop.run_until_complete(vm._fix_permissions()) loop.run_until_complete(vm._fix_permissions())
mock_exec.assert_called_with('docker', 'exec', 'e90e34656842', '/gns3/bin/busybox', 'sh', '-c', '(/gns3/bin/busybox find "/etc" -depth -print0 | /gns3/bin/busybox xargs -0 /gns3/bin/busybox stat -c \'%a:%u:%g:%n\' > "/etc/.gns3_perms") && /gns3/bin/busybox chmod -R u+rX "/etc" && /gns3/bin/busybox chown {}:{} -R "/etc"'.format(os.getuid(), os.getgid())) mock_exec.assert_called_with('docker', 'exec', 'e90e34656842', '/gns3/bin/busybox', 'sh', '-c', '(/gns3/bin/busybox find "/etc" -depth -print0 | /gns3/bin/busybox xargs -0 /gns3/bin/busybox stat -c \'%a:%u:%g:%n\' > "/etc/.gns3_perms") && /gns3/bin/busybox chmod -R u+rX "/etc" && /gns3/bin/busybox chown {}:{} -R "/etc"'.format(os.getuid(), os.getgid()))
assert process.wait.called assert process.wait.called
def test_read_console_output_with_binary_mode(vm, loop):
class InputStreamMock(object):
def __init__(self):
self.sent = False
@asyncio.coroutine
def receive(self):
if not self.sent:
self.sent = True
return MagicMock(tp=aiohttp.WSMsgType.BINARY, data=b"test")
else:
return MagicMock(tp=aiohttp.WSMsgType.CLOSE)
def close(self):
pass
input_stream = InputStreamMock()
output_stream = MagicMock()
with asyncio_patch('gns3server.compute.docker.docker_vm.DockerVM.stop'):
loop.run_until_complete(asyncio.async(vm._read_console_output(input_stream, output_stream)))
output_stream.feed_data.assert_called_once_with(b"test")