mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-06 15:21:13 +02:00
Merge branch '2.0' into 2.1
This commit is contained in:
275 files changed
+5384
-272
No files matched your search
@@ -28,7 +28,7 @@ import platform
|
||||
|
||||
from gns3server.utils.interfaces import interfaces
|
||||
from ..compute.port_manager import PortManager
|
||||
from ..utils.asyncio import wait_run_in_executor
|
||||
from ..utils.asyncio import wait_run_in_executor, locked_coroutine
|
||||
from ..utils.asyncio.telnet_server import AsyncioTelnetServer
|
||||
from ..ubridge.hypervisor import Hypervisor
|
||||
from ..ubridge.ubridge_error import UbridgeError
|
||||
@@ -519,12 +519,16 @@ class BaseNode:
|
||||
except UbridgeError as e:
|
||||
raise UbridgeError("{}: {}".format(e, self._ubridge_hypervisor.read_stdout()))
|
||||
|
||||
@asyncio.coroutine
|
||||
@locked_coroutine
|
||||
def _start_ubridge(self):
|
||||
"""
|
||||
Starts uBridge (handles connections to and from this node).
|
||||
"""
|
||||
|
||||
# Prevent us to start multiple ubridge
|
||||
if self._ubridge_hypervisor and self._ubridge_hypervisor.is_running():
|
||||
return
|
||||
|
||||
if self.ubridge_path is None:
|
||||
raise NodeError("uBridge is not available, path doesn't exist, or you just installed GNS3 and need to restart your user session to refresh user permissions.")
|
||||
|
||||
|
||||
@@ -24,14 +24,14 @@ import asyncio
|
||||
import logging
|
||||
import aiohttp
|
||||
import json
|
||||
import sys
|
||||
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__)
|
||||
|
||||
from ..base_manager import BaseManager
|
||||
from .docker_vm import DockerVM
|
||||
from .docker_error import *
|
||||
|
||||
DOCKER_MINIMUM_API_VERSION = "1.21"
|
||||
|
||||
@@ -46,30 +46,38 @@ class Docker(BaseManager):
|
||||
self._connected = False
|
||||
# Allow locking during ubridge operations
|
||||
self.ubridge_lock = asyncio.Lock()
|
||||
self._connector = None
|
||||
self._session = None
|
||||
|
||||
@asyncio.coroutine
|
||||
def connector(self):
|
||||
if not self._connected or self._connector.closed:
|
||||
if not sys.platform.startswith("linux"):
|
||||
raise DockerError("Docker is supported only on Linux")
|
||||
|
||||
def _check_connection(self):
|
||||
if not self._connected:
|
||||
try:
|
||||
self._connector = aiohttp.connector.UnixConnector(self._server_url, conn_timeout=2)
|
||||
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):
|
||||
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:
|
||||
self._connector.close()
|
||||
if self._connector and not self._connector.closed:
|
||||
yield from self._connector.close()
|
||||
|
||||
@asyncio.coroutine
|
||||
def query(self, method, path, data={}, params={}):
|
||||
@@ -84,7 +92,7 @@ class Docker(BaseManager):
|
||||
|
||||
response = yield from self.http_query(method, path, data=data, params=params)
|
||||
body = yield from response.read()
|
||||
if len(body):
|
||||
if body and len(body):
|
||||
if response.headers['CONTENT-TYPE'] == 'application/json':
|
||||
body = json.loads(body.decode("utf-8"))
|
||||
else:
|
||||
@@ -93,7 +101,7 @@ class Docker(BaseManager):
|
||||
return body
|
||||
|
||||
@asyncio.coroutine
|
||||
def http_query(self, method, path, data={}, params={}):
|
||||
def http_query(self, method, path, data={}, params={}, timeout=300):
|
||||
"""
|
||||
Make a query to the docker daemon
|
||||
|
||||
@@ -101,18 +109,28 @@ class Docker(BaseManager):
|
||||
: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)
|
||||
url = "http://docker/" + path
|
||||
|
||||
if timeout is None:
|
||||
timeout = 60 * 60 * 24 * 31 # One month timeout
|
||||
|
||||
try:
|
||||
response = yield from aiohttp.request(
|
||||
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,
|
||||
connector=(yield from self.connector()),
|
||||
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)))
|
||||
@@ -143,11 +161,50 @@ class Docker(BaseManager):
|
||||
|
||||
url = "http://docker/" + path
|
||||
connection = yield from aiohttp.ws_connect(url,
|
||||
connector=(yield from self.connector()),
|
||||
connector=self.connector(),
|
||||
origin="http://docker",
|
||||
autoping=True)
|
||||
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:
|
||||
chunk = yield from response.content.read(1024)
|
||||
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.
|
||||
|
||||
@@ -24,7 +24,6 @@ import shutil
|
||||
import psutil
|
||||
import shlex
|
||||
import aiohttp
|
||||
import json
|
||||
import os
|
||||
|
||||
from gns3server.utils.asyncio.telnet_server import AsyncioTelnetServer
|
||||
@@ -767,26 +766,9 @@ class DockerVM(BaseNode):
|
||||
"""
|
||||
Pull image from docker repository
|
||||
"""
|
||||
log.info("Pull %s from docker hub", image)
|
||||
response = yield from self.manager.http_query("POST", "images/create", params={"fromImage": image})
|
||||
# The pull api will stream status via an HTTP JSON stream
|
||||
content = ""
|
||||
while True:
|
||||
chunk = yield from response.content.read(1024)
|
||||
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:
|
||||
self.project.emit("log.info", {"message": "Pulling image {}:{}: {}".format(self._image, answer["id"], answer["progress"])})
|
||||
content = content[index:]
|
||||
except ValueError: # Partial JSON
|
||||
pass
|
||||
self.project.emit("log.info", {"message": "Success pulling image {}".format(self._image)})
|
||||
def callback(msg):
|
||||
self.project.emit("log.info", {"message": msg})
|
||||
yield from self.manager.pull_image(image, progress_callback=callback)
|
||||
|
||||
@asyncio.coroutine
|
||||
def _start_ubridge_capture(self, adapter_number, output_file):
|
||||
|
||||
@@ -88,7 +88,7 @@ class EthernetSwitch(Device):
|
||||
:param ports: ports info
|
||||
"""
|
||||
if ports != self._ports:
|
||||
if len(self._nios) > 0:
|
||||
if len(self._nios) > 0 and len(ports) != len(self._ports):
|
||||
raise NodeError("Can't modify a switch already connected.")
|
||||
|
||||
port_number = 0
|
||||
@@ -99,6 +99,13 @@ class EthernetSwitch(Device):
|
||||
|
||||
self._ports = ports
|
||||
|
||||
@asyncio.coroutine
|
||||
def update_port_settings(self):
|
||||
for port_settings in self._ports:
|
||||
port_number = port_settings["port_number"]
|
||||
if port_number in self._nios and self._nios[port_number] is not None:
|
||||
yield from self.set_port_settings(port_number, port_settings)
|
||||
|
||||
@asyncio.coroutine
|
||||
def create(self):
|
||||
|
||||
|
||||
@@ -239,10 +239,10 @@ class FrameRelaySwitch(Device):
|
||||
"""
|
||||
|
||||
if port1 not in self._nios:
|
||||
raise DynamipsError("Port {} is not allocated".format(port1))
|
||||
return
|
||||
|
||||
if port2 not in self._nios:
|
||||
raise DynamipsError("Port {} is not allocated".format(port2))
|
||||
return
|
||||
|
||||
nio1 = self._nios[port1]
|
||||
nio2 = self._nios[port2]
|
||||
|
||||
@@ -566,7 +566,7 @@ class IOUVM(BaseNode):
|
||||
self._terminate_process_iou()
|
||||
|
||||
if returncode != 0:
|
||||
if returncode == 11:
|
||||
if returncode == -11:
|
||||
message = "{} process has stopped, return code: {}. This could be an issue with the image using a different image can fix the issue.\n{}".format(process_name, returncode, self.read_iou_stdout())
|
||||
else:
|
||||
message = "{} process has stopped, return code: {}\n{}".format(process_name, returncode, self.read_iou_stdout())
|
||||
|
||||
@@ -74,7 +74,7 @@ class Project:
|
||||
try:
|
||||
if os.path.exists(self.tmp_working_directory()):
|
||||
shutil.rmtree(self.tmp_working_directory())
|
||||
except OSError:
|
||||
except OSError as e:
|
||||
raise aiohttp.web.HTTPInternalServerError(text="Could not clean project directory: {}".format(e))
|
||||
|
||||
log.info("Project {id} with path '{path}' created".format(path=self._path, id=self._id))
|
||||
|
||||
@@ -1014,7 +1014,9 @@ class VirtualBoxVM(BaseNode):
|
||||
|
||||
if self.ubridge:
|
||||
yield from self._ubridge_send("bridge delete {name}".format(name="VBOX-{}-{}".format(self._id, adapter_number)))
|
||||
yield from self._control_vm("setlinkstate{} off".format(adapter_number + 1))
|
||||
vm_state = yield from self._get_vm_state()
|
||||
if vm_state == "running":
|
||||
yield from self._control_vm("setlinkstate{} off".format(adapter_number + 1))
|
||||
else:
|
||||
vm_state = yield from self._get_vm_state()
|
||||
if vm_state == "running":
|
||||
|
||||
@@ -396,7 +396,7 @@ class VMware(BaseManager):
|
||||
try:
|
||||
stdout_data, _ = yield from asyncio.wait_for(process.communicate(), timeout=timeout)
|
||||
except asyncio.TimeoutError:
|
||||
raise VMwareError("vmrun has timed out after {} seconds!\nTry to run {} in a terminal to see more informations.\nAnd make sure GNS3 and VMware run under the same user.".format(timeout, command_string))
|
||||
raise VMwareError("vmrun has timed out after {} seconds!\nTry to run {} in a terminal to see more informations.\n\nMake sure GNS3 and VMware run under the same user and whitelist vmrun.exe in your antivirus.".format(timeout, command_string))
|
||||
|
||||
if process.returncode:
|
||||
# vmrun print errors on stdout
|
||||
|
||||
Reference in new issue
Block a user