1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-25 01:38:08 +00:00
gns3-server/gns3server/compute/docker/__init__.py

227 lines
8.6 KiB
Python
Raw Normal View History

2015-09-08 08:29:30 +00:00
# -*- 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
2015-09-08 08:29:30 +00:00
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
2015-09-08 08:29:30 +00:00
log = logging.getLogger(__name__)
2017-04-24 16:43:12 +00:00
DOCKER_MINIMUM_API_VERSION = "1.25"
2015-09-08 08:29:30 +00:00
class Docker(BaseManager):
_NODE_CLASS = DockerVM
2015-09-08 08:29:30 +00:00
def __init__(self):
super().__init__()
2015-10-14 16:10:05 +00:00
self._server_url = '/var/run/docker.sock'
self._connected = False
2015-10-14 16:10:05 +00:00
# Allow locking during ubridge operations
self.ubridge_lock = asyncio.Lock()
self._connector = None
self._session = None
2015-09-08 08:29:30 +00:00
@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.errors.ClientOSError, FileNotFoundError):
self._connected = False
raise DockerError("Can't connect to docker daemon")
if parse_version(version["ApiVersion"]) < parse_version(DOCKER_MINIMUM_API_VERSION):
2016-05-07 16:39:32 +00:00
raise DockerError("Docker API version is {}. GNS3 requires a minimum API version of {}".format(version["ApiVersion"], DOCKER_MINIMUM_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, conn_timeout=2, limit=None)
except (aiohttp.errors.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:
yield from self._connector.close()
2015-10-14 16:10:05 +00:00
@asyncio.coroutine
def query(self, method, path, data={}, params={}):
2015-09-08 08:29:30 +00:00
"""
2015-10-14 16:10:05 +00:00
Make a query to the docker daemon and decode the request
2015-09-08 08:29:30 +00:00
2015-10-14 16:10:05 +00:00
: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
"""
2016-02-11 14:49:28 +00:00
2015-10-14 16:10:05 +00:00
response = yield from self.http_query(method, path, data=data, params=params)
body = yield from response.read()
if body and len(body):
2016-02-11 14:49:28 +00:00
if response.headers['CONTENT-TYPE'] == 'application/json':
body = json.loads(body.decode("utf-8"))
else:
body = body.decode("utf-8")
2015-10-14 16:10:05 +00:00
log.debug("Query Docker %s %s params=%s data=%s Response: %s", method, path, params, data, body)
return body
2015-09-08 08:29:30 +00:00
@asyncio.coroutine
def http_query(self, method, path, data={}, params={}, timeout=300):
2015-10-14 16:10:05 +00:00
"""
Make a query to the docker daemon
2015-09-08 08:29:30 +00:00
2015-10-14 16:10:05 +00:00
: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
2015-10-14 16:10:05 +00:00
:returns: HTTP response
2015-09-08 08:29:30 +00:00
"""
2015-10-14 16:10:05 +00:00
data = json.dumps(data)
url = "http://docker/v" + DOCKER_MINIMUM_API_VERSION + "/" + path
if timeout is None:
timeout = 60 * 60 * 24 * 31 # One month timeout
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)))
2017-04-18 09:44:20 +00:00
except (asyncio.TimeoutError):
raise DockerError("Docker timeout " + method + " " + path)
2015-10-14 16:10:05 +00:00
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)
2016-02-11 14:49:28 +00:00
if response.status == 304:
2016-05-18 09:23:45 +00:00
raise DockerHttp304Error("Docker has returned an error: {} {}".format(response.status, body))
2016-02-11 14:49:28 +00:00
elif response.status == 404:
2016-05-18 09:23:45 +00:00
raise DockerHttp404Error("Docker has returned an error: {} {}".format(response.status, body))
2016-02-11 14:49:28 +00:00
else:
2016-05-18 09:23:45 +00:00
raise DockerError("Docker has returned an error: {} {}".format(response.status, body))
2015-10-14 16:10:05 +00:00
return response
2015-09-08 08:29:30 +00:00
@asyncio.coroutine
2015-10-14 16:10:05 +00:00
def websocket_query(self, path, params={}):
"""
Open a websocket connection
2015-09-08 08:29:30 +00:00
2015-10-14 16:10:05 +00:00
:param path: Endpoint in API
:param params: Parameters added as a query arg
:returns: Websocket
2015-09-08 08:29:30 +00:00
"""
2015-10-14 16:10:05 +00:00
url = "http://docker/" + path
connection = yield from aiohttp.ws_connect(url,
connector=self.connector(),
2015-10-14 16:10:05 +00:00
origin="http://docker",
autoping=True)
return connection
2015-09-08 08:29:30 +00:00
@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.errors.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))
2015-10-14 16:10:05 +00:00
@asyncio.coroutine
def list_images(self):
"""Gets Docker image list.
2015-09-08 08:29:30 +00:00
2015-10-14 16:10:05 +00:00
:returns: list of dicts
:rtype: list
2015-09-08 08:29:30 +00:00
"""
2015-10-14 16:10:05 +00:00
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})
2015-10-14 16:10:05 +00:00
return sorted(images, key=lambda i: i['image'])