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.
|
|
|
|
"""
|
|
|
|
|
2016-06-23 22:56:06 +00:00
|
|
|
import sys
|
2015-09-08 08:29:30 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2015-06-15 17:30:09 +00:00
|
|
|
import aiohttp
|
2015-10-14 16:10:05 +00:00
|
|
|
import json
|
2016-06-22 13:27:23 +00:00
|
|
|
import sys
|
2016-05-02 15:13:23 +00:00
|
|
|
from gns3server.utils import parse_version
|
2015-09-08 08:29:30 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
from ..base_manager import BaseManager
|
2015-10-14 16:10:05 +00:00
|
|
|
from .docker_vm import DockerVM
|
2016-02-11 14:49:28 +00:00
|
|
|
from .docker_error import *
|
2015-09-08 08:29:30 +00:00
|
|
|
|
2016-02-11 15:01:47 +00:00
|
|
|
DOCKER_MINIMUM_API_VERSION = "1.21"
|
|
|
|
|
2015-09-08 08:29:30 +00:00
|
|
|
|
|
|
|
class Docker(BaseManager):
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
_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'
|
2016-01-06 13:46:45 +00:00
|
|
|
self._connected = False
|
2015-10-14 16:10:05 +00:00
|
|
|
# Allow locking during ubridge operations
|
|
|
|
self.ubridge_lock = asyncio.Lock()
|
2017-03-20 22:50:31 +00:00
|
|
|
self._version_checked = False
|
2017-03-20 19:03:51 +00:00
|
|
|
self._session = None
|
2017-03-20 22:50:31 +00:00
|
|
|
self._connector = None
|
2015-09-08 08:29:30 +00:00
|
|
|
|
2016-01-06 13:46:45 +00:00
|
|
|
@asyncio.coroutine
|
2017-03-20 22:50:31 +00:00
|
|
|
def session(self):
|
|
|
|
if not self._connected or self._session.closed:
|
2016-01-06 13:46:45 +00:00
|
|
|
try:
|
2016-01-11 18:11:25 +00:00
|
|
|
self._connected = True
|
2017-03-20 22:50:31 +00:00
|
|
|
connector = self.connector()
|
|
|
|
self._session = aiohttp.ClientSession(connector=connector)
|
2016-02-11 15:01:47 +00:00
|
|
|
version = yield from self.query("GET", "version")
|
2016-01-06 13:46:45 +00:00
|
|
|
except (aiohttp.errors.ClientOSError, FileNotFoundError):
|
|
|
|
self._connected = False
|
|
|
|
raise DockerError("Can't connect to docker daemon")
|
2016-02-11 15:01:47 +00:00
|
|
|
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))
|
2017-03-20 22:50:31 +00:00
|
|
|
return self._session
|
|
|
|
|
|
|
|
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)
|
|
|
|
except (aiohttp.errors.ClientOSError, FileNotFoundError):
|
|
|
|
raise DockerError("Can't connect to docker daemon")
|
2016-01-06 13:46:45 +00:00
|
|
|
return self._connector
|
|
|
|
|
2016-05-10 10:14:48 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def unload(self):
|
|
|
|
yield from super().unload()
|
2016-01-06 13:46:45 +00:00
|
|
|
if self._connected:
|
2017-03-20 22:50:31 +00:00
|
|
|
if self._session and not self._session.closed:
|
|
|
|
yield from self._session.close()
|
|
|
|
if self._connector and not self._connector.closed:
|
|
|
|
yield from self._connector.close()
|
2015-12-29 11:40:22 +00:00
|
|
|
|
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 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
|
2017-03-20 16:06:00 +00:00
|
|
|
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
|
2017-03-20 16:06:00 +00:00
|
|
|
: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/" + path
|
2016-06-20 09:46:10 +00:00
|
|
|
try:
|
2017-03-20 22:50:31 +00:00
|
|
|
session = yield from self.session()
|
|
|
|
response = yield from session.request(
|
2016-06-20 09:46:10 +00:00
|
|
|
method,
|
|
|
|
url,
|
|
|
|
params=params,
|
|
|
|
data=data,
|
|
|
|
headers={"content-type": "application/json", },
|
2017-03-20 16:06:00 +00:00
|
|
|
timeout=timeout
|
2016-06-20 09:46:10 +00:00
|
|
|
)
|
2016-07-04 12:12:39 +00:00
|
|
|
except (aiohttp.ClientResponseError, aiohttp.ClientOSError) as e:
|
2016-06-20 09:46:10 +00:00
|
|
|
raise DockerError("Docker has returned an error: {}".format(str(e)))
|
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,
|
2017-03-20 22:50:31 +00:00
|
|
|
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
|
|
|
|
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})):
|
2017-01-10 09:09:34 +00:00
|
|
|
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'])
|