mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-12-23 14:42:28 +00:00
Merge branch '1.5' into 2.0
This commit is contained in:
commit
9dca7dfe4a
@ -486,14 +486,17 @@ class BaseManager:
|
||||
log.info("Writting image file %s", path)
|
||||
try:
|
||||
remove_checksum(path)
|
||||
# We store the file under his final name only when the upload is finished
|
||||
tmp_path = path + ".tmp"
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, 'wb+') as f:
|
||||
with open(tmp_path, 'wb+') as f:
|
||||
while True:
|
||||
packet = yield from stream.read(512)
|
||||
if not packet:
|
||||
break
|
||||
f.write(packet)
|
||||
os.chmod(path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
|
||||
os.chmod(tmp_path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
|
||||
shutil.move(tmp_path, path)
|
||||
md5sum(path)
|
||||
except OSError as e:
|
||||
raise aiohttp.web.HTTPConflict(text="Could not write image: {} because {}".format(filename, e))
|
||||
|
@ -24,7 +24,7 @@ import tempfile
|
||||
import psutil
|
||||
import platform
|
||||
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
from ..utils.asyncio import wait_run_in_executor
|
||||
from ..ubridge.hypervisor import Hypervisor
|
||||
from .vm_error import VMError
|
||||
|
@ -24,12 +24,11 @@ import logging
|
||||
import aiohttp
|
||||
import urllib
|
||||
import json
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
from ..base_manager import BaseManager
|
||||
from ..project_manager import ProjectManager
|
||||
from .docker_vm import DockerVM
|
||||
from .docker_error import *
|
||||
|
||||
@ -59,10 +58,12 @@ class Docker(BaseManager):
|
||||
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 {}. But GNS3 require a minimum API version {}".format(version["ApiVersion"], DOCKER_MINIMUM_API_VERSION))
|
||||
raise DockerError("Docker API version is {}. GNS3 requires a minimum API version of {}".format(version["ApiVersion"], DOCKER_MINIMUM_API_VERSION))
|
||||
return self._connector
|
||||
|
||||
def __del__(self):
|
||||
@asyncio.coroutine
|
||||
def unload(self):
|
||||
yield from super().unload()
|
||||
if self._connected:
|
||||
self._connector.close()
|
||||
|
||||
|
@ -33,6 +33,7 @@ from ..base_vm import BaseVM
|
||||
from ..adapters.ethernet_adapter import EthernetAdapter
|
||||
from ..nios.nio_udp import NIOUDP
|
||||
from ...utils.asyncio.telnet_server import AsyncioTelnetServer
|
||||
from ...utils.asyncio.raw_command_server import AsyncioRawCommandServer
|
||||
from ...utils.asyncio import wait_for_file_creation
|
||||
from ...utils.get_resource import get_resource
|
||||
from ...ubridge.ubridge_error import UbridgeError, UbridgeNamespaceError
|
||||
@ -54,12 +55,14 @@ class DockerVM(BaseVM):
|
||||
:param console_type: Console type
|
||||
:param aux: TCP aux console port
|
||||
:param console_resolution: Resolution of the VNC display
|
||||
:param console_http_port: Port to redirect HTTP queries
|
||||
:param console_http_path: Url part with the path of the web interface
|
||||
"""
|
||||
|
||||
def __init__(self, name, vm_id, project, manager, image,
|
||||
console=None, aux=None, start_command=None,
|
||||
adapters=None, environment=None, console_type="telnet",
|
||||
console_resolution="1024x768"):
|
||||
console_resolution="1024x768", console_http_port=80, console_http_path="/"):
|
||||
super().__init__(name, vm_id, project, manager, console=console, aux=aux, allocate_aux=True, console_type=console_type)
|
||||
|
||||
self._image = image
|
||||
@ -72,6 +75,9 @@ class DockerVM(BaseVM):
|
||||
self._telnet_servers = []
|
||||
self._x11vnc_process = None
|
||||
self._console_resolution = console_resolution
|
||||
self._console_http_path = console_http_path
|
||||
self._console_http_port = console_http_port
|
||||
self._console_websocket = None
|
||||
|
||||
if adapters is None:
|
||||
self.adapters = 1
|
||||
@ -95,6 +101,8 @@ class DockerVM(BaseVM):
|
||||
"console": self.console,
|
||||
"console_type": self.console_type,
|
||||
"console_resolution": self.console_resolution,
|
||||
"console_http_port": self.console_http_port,
|
||||
"console_http_path": self.console_http_path,
|
||||
"aux": self.aux,
|
||||
"start_command": self.start_command,
|
||||
"environment": self.environment,
|
||||
@ -133,6 +141,22 @@ class DockerVM(BaseVM):
|
||||
def console_resolution(self, resolution):
|
||||
self._console_resolution = resolution
|
||||
|
||||
@property
|
||||
def console_http_path(self):
|
||||
return self._console_http_path
|
||||
|
||||
@console_http_path.setter
|
||||
def console_http_path(self, path):
|
||||
self._console_http_path = path
|
||||
|
||||
@property
|
||||
def console_http_port(self):
|
||||
return self._console_http_port
|
||||
|
||||
@console_http_port.setter
|
||||
def console_http_port(self, port):
|
||||
self._console_http_port = port
|
||||
|
||||
@property
|
||||
def environment(self):
|
||||
return self._environment
|
||||
@ -261,6 +285,9 @@ class DockerVM(BaseVM):
|
||||
params["Cmd"] = ["/bin/sh"]
|
||||
params["Entrypoint"].insert(0, "/gns3/init.sh")
|
||||
|
||||
# Give the information to the container on how many interface should be inside
|
||||
params["Env"].append("GNS3_MAX_ETHERNET=eth{}".format(self.adapters - 1))
|
||||
|
||||
if self._environment:
|
||||
params["Env"] += [e.strip() for e in self._environment.split("\n")]
|
||||
|
||||
@ -300,6 +327,8 @@ class DockerVM(BaseVM):
|
||||
if state == "paused":
|
||||
yield from self.unpause()
|
||||
else:
|
||||
yield from self._clean_servers()
|
||||
|
||||
result = yield from self.manager.query("POST", "containers/{}/start".format(self._cid))
|
||||
|
||||
namespace = yield from self._get_namespace()
|
||||
@ -322,6 +351,8 @@ class DockerVM(BaseVM):
|
||||
|
||||
if self.console_type == "telnet":
|
||||
yield from self._start_console()
|
||||
elif self.console_type == "http" or self.console_type == "https":
|
||||
yield from self._start_http()
|
||||
|
||||
if self.allocate_aux:
|
||||
yield from self._start_aux()
|
||||
@ -338,11 +369,11 @@ class DockerVM(BaseVM):
|
||||
# We can not use the API because docker doesn't expose a websocket api for exec
|
||||
# https://github.com/GNS3/gns3-gui/issues/1039
|
||||
process = yield from asyncio.subprocess.create_subprocess_exec(
|
||||
"docker", "exec", "-i", self._cid, "/gns3/bin/busybox", "sh", "-i",
|
||||
"docker", "exec", "-i", self._cid, "/gns3/bin/busybox", "script", "-qfc", "/gns3/bin/busybox sh", "/dev/null",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.STDOUT,
|
||||
stdin=asyncio.subprocess.PIPE)
|
||||
server = AsyncioTelnetServer(reader=process.stdout, writer=process.stdin, binary=False, echo=False)
|
||||
server = AsyncioTelnetServer(reader=process.stdout, writer=process.stdin, binary=True, echo=True)
|
||||
self._telnet_servers.append((yield from asyncio.start_server(server.run, self._manager.port_manager.console_host, self.aux)))
|
||||
log.debug("Docker container '%s' started listen for auxilary telnet on %d", self.name, self.aux)
|
||||
|
||||
@ -361,11 +392,28 @@ class DockerVM(BaseVM):
|
||||
x11_socket = os.path.join("/tmp/.X11-unix/", "X{}".format(self._display))
|
||||
yield from wait_for_file_creation(x11_socket)
|
||||
|
||||
@asyncio.coroutine
|
||||
def _start_http(self):
|
||||
"""
|
||||
Start an HTTP tunnel to container localhost
|
||||
"""
|
||||
log.debug("Forward HTTP for %s to %d", self.name, self._console_http_port)
|
||||
command = ["docker", "exec", "-i", self._cid, "/gns3/bin/busybox", "nc", "127.0.0.1", str(self._console_http_port)]
|
||||
# We replace the port in the server answer otherwise somelink could be broke
|
||||
server = AsyncioRawCommandServer(command, replaces=[
|
||||
(
|
||||
'{}'.format(self._console_http_port).encode(),
|
||||
'{}'.format(self.console).encode(),
|
||||
)
|
||||
])
|
||||
self._telnet_servers.append((yield from asyncio.start_server(server.run, self._manager.port_manager.console_host, self.console)))
|
||||
|
||||
@asyncio.coroutine
|
||||
def _start_console(self):
|
||||
"""
|
||||
Start streaming the console via telnet
|
||||
"""
|
||||
|
||||
class InputStream:
|
||||
|
||||
def __init__(self):
|
||||
@ -386,12 +434,12 @@ class DockerVM(BaseVM):
|
||||
telnet = AsyncioTelnetServer(reader=output_stream, writer=input_stream, echo=True)
|
||||
self._telnet_servers.append((yield from asyncio.start_server(telnet.run, self._manager.port_manager.console_host, self.console)))
|
||||
|
||||
ws = yield from self.manager.websocket_query("containers/{}/attach/ws?stream=1&stdin=1&stdout=1&stderr=1".format(self._cid))
|
||||
input_stream.ws = ws
|
||||
self._console_websocket = yield from self.manager.websocket_query("containers/{}/attach/ws?stream=1&stdin=1&stdout=1&stderr=1".format(self._cid))
|
||||
input_stream.ws = self._console_websocket
|
||||
|
||||
output_stream.feed_data(self.name.encode() + b" console is now available... Press RETURN to get started.\r\n")
|
||||
|
||||
asyncio.async(self._read_console_output(ws, output_stream))
|
||||
asyncio.async(self._read_console_output(self._console_websocket, output_stream))
|
||||
|
||||
@asyncio.coroutine
|
||||
def _read_console_output(self, ws, out):
|
||||
@ -428,16 +476,23 @@ class DockerVM(BaseVM):
|
||||
log.info("Docker container '{name}' [{image}] restarted".format(
|
||||
name=self._name, image=self._image))
|
||||
|
||||
@asyncio.coroutine
|
||||
def _clean_servers(self):
|
||||
"""
|
||||
Clean the list of running console servers
|
||||
"""
|
||||
if len(self._telnet_servers) > 0:
|
||||
for telnet_server in self._telnet_servers:
|
||||
telnet_server.close()
|
||||
yield from telnet_server.wait_closed()
|
||||
self._telnet_servers = []
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop(self):
|
||||
"""Stops this Docker container."""
|
||||
|
||||
try:
|
||||
if len(self._telnet_servers) > 0:
|
||||
for telnet_server in self._telnet_servers:
|
||||
telnet_server.close()
|
||||
yield from telnet_server.wait_closed()
|
||||
self._telnet_servers = []
|
||||
yield from self._clean_servers()
|
||||
|
||||
if self._ubridge_hypervisor and self._ubridge_hypervisor.is_running():
|
||||
yield from self._ubridge_hypervisor.stop()
|
||||
|
@ -28,9 +28,6 @@ if [ ! -d /tmp/gns3/bin ]; then
|
||||
/gns3/bin/busybox --install -s /tmp/gns3/bin
|
||||
fi
|
||||
|
||||
# Wait 2 seconds to settle the network interfaces
|
||||
sleep 2
|
||||
|
||||
# /etc/hosts
|
||||
[ -s /etc/hosts ] || cat > /etc/hosts << __EOF__
|
||||
127.0.1.1 $HOSTNAME
|
||||
@ -45,6 +42,13 @@ __EOF__
|
||||
# configure loopback interface
|
||||
ip link set dev lo up
|
||||
|
||||
# Wait for all eth available
|
||||
while true
|
||||
do
|
||||
grep $GNS3_MAX_ETHERNET /proc/net/dev > /dev/null && break
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
# activate eth interfaces
|
||||
sed -n 's/^ *\(eth[0-9]*\):.*/\1/p' < /proc/net/dev | while read dev; do
|
||||
ip link set dev $dev up
|
||||
|
@ -34,7 +34,7 @@ log = logging.getLogger(__name__)
|
||||
|
||||
from gns3server.utils.interfaces import interfaces, is_interface_up
|
||||
from gns3server.utils.asyncio import wait_run_in_executor
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
from uuid import UUID, uuid4
|
||||
from ..base_manager import BaseManager
|
||||
from ..project_manager import ProjectManager
|
||||
|
@ -21,7 +21,7 @@ http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L558
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
|
||||
from .device import Device
|
||||
from ..nios.nio_udp import NIOUDP
|
||||
|
@ -24,6 +24,10 @@ import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# This ports are disallowed by Chrome and Firefox to avoid trouble with skip them
|
||||
BANNED_PORTS = set((1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514, 515, 526, 530, 531, 532, 540, 556, 563, 587, 601, 636, 993, 995, 2049, 3659, 4045, 6000, 6665, 6666, 6667, 6668, 6669))
|
||||
|
||||
|
||||
class PortManager:
|
||||
|
||||
"""
|
||||
@ -144,7 +148,7 @@ class PortManager:
|
||||
|
||||
last_exception = None
|
||||
for port in range(start_port, end_port + 1):
|
||||
if port in ignore_ports:
|
||||
if port in ignore_ports or port in BANNED_PORTS:
|
||||
continue
|
||||
|
||||
try:
|
||||
|
@ -562,7 +562,7 @@ class Project:
|
||||
for prop, value in node["properties"].items():
|
||||
if prop.endswith("image"):
|
||||
node["properties"][prop] = os.path.basename(value)
|
||||
if include_images:
|
||||
if include_images is True:
|
||||
self._export_images(value, node["type"], z)
|
||||
z.writestr("project.gns3", json.dumps(topology).encode())
|
||||
|
||||
|
@ -30,7 +30,7 @@ import asyncio
|
||||
import socket
|
||||
import gns3server
|
||||
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
from .qemu_error import QemuError
|
||||
from ..adapters.ethernet_adapter import EthernetAdapter
|
||||
from ..nios.nio_udp import NIOUDP
|
||||
|
@ -28,7 +28,7 @@ import json
|
||||
import socket
|
||||
import asyncio
|
||||
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
from gns3server.utils.telnet_server import TelnetServer
|
||||
from gns3server.utils.asyncio import wait_for_file_creation, wait_for_named_pipe_creation
|
||||
from .virtualbox_error import VirtualBoxError
|
||||
|
@ -31,7 +31,7 @@ import codecs
|
||||
from collections import OrderedDict
|
||||
from gns3server.utils.interfaces import interfaces
|
||||
from gns3server.utils.asyncio import subprocess_check_output
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@ -153,11 +153,11 @@ class VMware(BaseManager):
|
||||
if player_version < 6:
|
||||
raise VMwareError("Using VMware Player requires version 6 or above")
|
||||
elif player_version == 6:
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.13")
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.13.0")
|
||||
elif player_version == 7:
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.14")
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.14.0")
|
||||
elif player_version >= 12:
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.15")
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.15.0")
|
||||
|
||||
@asyncio.coroutine
|
||||
def _check_vmware_workstation_requirements(self, ws_version):
|
||||
@ -175,11 +175,11 @@ class VMware(BaseManager):
|
||||
if ws_version < 10:
|
||||
raise VMwareError("Using VMware Workstation requires version 10 or above")
|
||||
elif ws_version == 10:
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.13")
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.13.0")
|
||||
elif ws_version == 11:
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.14")
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.14.0")
|
||||
elif ws_version >= 12:
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.15")
|
||||
yield from self.check_vmrun_version(minimum_required_version="1.15.0")
|
||||
|
||||
@asyncio.coroutine
|
||||
def check_vmware_version(self):
|
||||
@ -393,7 +393,7 @@ class VMware(BaseManager):
|
||||
return stdout_data.decode("utf-8", errors="ignore").splitlines()
|
||||
|
||||
@asyncio.coroutine
|
||||
def check_vmrun_version(self, minimum_required_version="1.13"):
|
||||
def check_vmrun_version(self, minimum_required_version="1.13.0"):
|
||||
"""
|
||||
Checks the vmrun version.
|
||||
|
||||
@ -416,9 +416,8 @@ class VMware(BaseManager):
|
||||
version = None
|
||||
if match:
|
||||
version = match.group(1)
|
||||
log.debug("VMware vmrun version {} detected".format(version))
|
||||
log.debug("VMware vmrun version {} detected, minimum required: {}".format(version, minimum_required_version))
|
||||
if parse_version(version) < parse_version(minimum_required_version):
|
||||
|
||||
raise VMwareError("VMware vmrun executable version must be >= version {}".format(minimum_required_version))
|
||||
if version is None:
|
||||
log.warning("Could not find VMware vmrun version. Output: {}".format(output))
|
||||
|
@ -31,7 +31,7 @@ import shutil
|
||||
from ...utils.asyncio import wait_for_process_termination
|
||||
from ...utils.asyncio import monitor_process
|
||||
from ...utils.asyncio import subprocess_check_output
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
from .vpcs_error import VPCSError
|
||||
from ..adapters.ethernet_adapter import EthernetAdapter
|
||||
from ..nios.nio_udp import NIOUDP
|
||||
|
@ -74,6 +74,8 @@ class DockerHandler:
|
||||
console=request.json.get("console"),
|
||||
console_type=request.json.get("console_type"),
|
||||
console_resolution=request.json.get("console_resolution", "1024x768"),
|
||||
console_http_port=request.json.get("console_http_port", 80),
|
||||
console_http_path=request.json.get("console_http_path", "/"),
|
||||
aux=request.json.get("aux")
|
||||
)
|
||||
for name, value in request.json.items():
|
||||
@ -279,7 +281,10 @@ class DockerHandler:
|
||||
vm.name = request.json.get("name", vm.name)
|
||||
vm.console = request.json.get("console", vm.console)
|
||||
vm.aux = request.json.get("aux", vm.aux)
|
||||
vm.console_type = request.json.get("console_type", vm.console_type)
|
||||
vm.console_resolution = request.json.get("console_resolution", vm.console_resolution)
|
||||
vm.console_http_port = request.json.get("console_http_port", vm.console_http_port)
|
||||
vm.console_http_path = request.json.get("console_http_path", vm.console_http_path)
|
||||
vm.start_command = request.json.get("start_command", vm.start_command)
|
||||
vm.environment = request.json.get("environment", vm.environment)
|
||||
vm.adapters = request.json.get("adapters", vm.adapters)
|
||||
|
@ -412,7 +412,7 @@ class ProjectHandler:
|
||||
response.content_length = None
|
||||
response.start(request)
|
||||
|
||||
for data in project.export(include_images=bool(request.GET.get("images", "0"))):
|
||||
for data in project.export(include_images=bool(request.GET.get("include_images", "0"))):
|
||||
response.write(data)
|
||||
yield from response.drain()
|
||||
|
||||
|
@ -41,13 +41,21 @@ DOCKER_CREATE_SCHEMA = {
|
||||
},
|
||||
"console_type": {
|
||||
"description": "console type",
|
||||
"enum": ["telnet", "vnc"]
|
||||
"enum": ["telnet", "vnc", "http", "https"]
|
||||
},
|
||||
"console_resolution": {
|
||||
"description": "console resolution for VNC",
|
||||
"type": ["string", "null"],
|
||||
"pattern": "^[0-9]+x[0-9]+$"
|
||||
},
|
||||
"console_http_port": {
|
||||
"description": "Internal port in the container of the HTTP server",
|
||||
"type": "integer",
|
||||
},
|
||||
"console_http_path": {
|
||||
"description": "Path of the web interface",
|
||||
"type": "string",
|
||||
},
|
||||
"aux": {
|
||||
"description": "auxilary TCP port",
|
||||
"minimum": 1,
|
||||
@ -104,7 +112,15 @@ DOCKER_UPDATE_SCHEMA = {
|
||||
},
|
||||
"console_type": {
|
||||
"description": "console type",
|
||||
"enum": ["telnet", "vnc"]
|
||||
"enum": ["telnet", "vnc", "http", "https"]
|
||||
},
|
||||
"console_http_port": {
|
||||
"description": "Internal port in the container of the HTTP server",
|
||||
"type": "integer",
|
||||
},
|
||||
"console_http_path": {
|
||||
"description": "Path of the web interface",
|
||||
"type": "string",
|
||||
},
|
||||
"aux": {
|
||||
"description": "auxilary TCP port",
|
||||
@ -168,7 +184,15 @@ DOCKER_OBJECT_SCHEMA = {
|
||||
},
|
||||
"console_type": {
|
||||
"description": "console type",
|
||||
"enum": ["telnet", "vnc"]
|
||||
"enum": ["telnet", "vnc", "http", "https"]
|
||||
},
|
||||
"console_http_port": {
|
||||
"description": "Internal port in the container of the HTTP server",
|
||||
"type": "integer",
|
||||
},
|
||||
"console_http_path": {
|
||||
"description": "Path of the web interface",
|
||||
"type": "string",
|
||||
},
|
||||
"container_id": {
|
||||
"description": "Docker container ID",
|
||||
|
@ -25,7 +25,7 @@ import asyncio
|
||||
import socket
|
||||
import re
|
||||
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
from gns3server.utils.asyncio import wait_for_process_termination
|
||||
from gns3server.utils.asyncio import subprocess_check_output
|
||||
from .ubridge_hypervisor import UBridgeHypervisor
|
||||
|
@ -16,6 +16,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
import re
|
||||
import textwrap
|
||||
import posixpath
|
||||
|
||||
@ -44,3 +45,44 @@ def int_to_macaddress(integer):
|
||||
Convert an integer to a macaddress
|
||||
"""
|
||||
return ":".join(textwrap.wrap("%012x" % (integer), width=2))
|
||||
|
||||
|
||||
def parse_version(version):
|
||||
"""
|
||||
Return a comparable tuple from a version string. We try to force tuple to semver with version like 1.2.0
|
||||
|
||||
Replace pkg_resources.parse_version which now display a warning when use for comparing version with tuple
|
||||
|
||||
:returns: Version string as comparable tuple
|
||||
"""
|
||||
|
||||
release_type_found = False
|
||||
version_infos = re.split('(\.|[a-z]+)', version)
|
||||
version = []
|
||||
for info in version_infos:
|
||||
if info == '.' or len(info) == 0:
|
||||
continue
|
||||
try:
|
||||
info = int(info)
|
||||
# We pad with zero to compare only on string
|
||||
# This avoid issue when comparing version with different length
|
||||
version.append("%06d" % (info,))
|
||||
except ValueError:
|
||||
# Force to a version with three number
|
||||
if len(version) == 1:
|
||||
version.append("00000")
|
||||
if len(version) == 2:
|
||||
version.append("000000")
|
||||
# We want rc to be at lower level than dev version
|
||||
if info == 'rc':
|
||||
info = 'c'
|
||||
version.append(info)
|
||||
release_type_found = True
|
||||
if release_type_found is False:
|
||||
# Force to a version with three number
|
||||
if len(version) == 1:
|
||||
version.append("00000")
|
||||
if len(version) == 2:
|
||||
version.append("000000")
|
||||
version.append("final")
|
||||
return tuple(version)
|
||||
|
116
gns3server/utils/asyncio/raw_command_server.py
Normal file
116
gns3server/utils/asyncio/raw_command_server.py
Normal file
@ -0,0 +1,116 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2014 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/>.
|
||||
|
||||
import re
|
||||
import asyncio
|
||||
import asyncio.subprocess
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
READ_SIZE = 4096
|
||||
|
||||
|
||||
class AsyncioRawCommandServer:
|
||||
"""
|
||||
Expose a process on the network his stdoud and stdin will be forward
|
||||
on network
|
||||
"""
|
||||
|
||||
def __init__(self, command, replaces=[]):
|
||||
"""
|
||||
:param command: Command to run
|
||||
:param replaces: List of tuple to replace in the output ex: [(b":8080", b":6000")]
|
||||
"""
|
||||
self._command = command
|
||||
self._replaces = replaces
|
||||
# We limit number of process
|
||||
self._lock = asyncio.Semaphore(value=4)
|
||||
|
||||
@asyncio.coroutine
|
||||
def run(self, network_reader, network_writer):
|
||||
yield from self._lock.acquire()
|
||||
process = yield from asyncio.subprocess.create_subprocess_exec(*self._command,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.STDOUT,
|
||||
stdin=asyncio.subprocess.PIPE)
|
||||
try:
|
||||
yield from self._process(network_reader, network_writer, process.stdout, process.stdin)
|
||||
except ConnectionResetError:
|
||||
network_writer.close()
|
||||
if process.returncode is None:
|
||||
process.kill()
|
||||
yield from process.wait()
|
||||
self._lock.release()
|
||||
|
||||
@asyncio.coroutine
|
||||
def _process(self, network_reader, network_writer, process_reader, process_writer):
|
||||
network_read = asyncio.async(network_reader.read(READ_SIZE))
|
||||
reader_read = asyncio.async(process_reader.read(READ_SIZE))
|
||||
timeout = 30
|
||||
|
||||
while True:
|
||||
done, pending = yield from asyncio.wait(
|
||||
[
|
||||
network_read,
|
||||
reader_read
|
||||
],
|
||||
timeout=timeout,
|
||||
return_when=asyncio.FIRST_COMPLETED)
|
||||
if len(done) == 0:
|
||||
raise ConnectionResetError()
|
||||
for coro in done:
|
||||
data = coro.result()
|
||||
if coro == network_read:
|
||||
if network_reader.at_eof():
|
||||
raise ConnectionResetError()
|
||||
|
||||
network_read = asyncio.async(network_reader.read(READ_SIZE))
|
||||
|
||||
process_writer.write(data)
|
||||
yield from process_writer.drain()
|
||||
elif coro == reader_read:
|
||||
if process_reader.at_eof():
|
||||
raise ConnectionResetError()
|
||||
|
||||
reader_read = asyncio.async(process_reader.read(READ_SIZE))
|
||||
|
||||
for replace in self._replaces:
|
||||
data = data.replace(replace[0], replace[1])
|
||||
timeout = 2 # We reduce the timeout when the process start to return stuff to avoid problem with server not closing the connection
|
||||
|
||||
network_writer.write(data)
|
||||
yield from network_writer.drain()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
command = ["nc", "localhost", "80"]
|
||||
server = AsyncioRawCommandServer(command)
|
||||
coro = asyncio.start_server(server.run, '127.0.0.1', 4444, loop=loop)
|
||||
s = loop.run_until_complete(coro)
|
||||
|
||||
try:
|
||||
loop.run_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
# Close the server
|
||||
s.close()
|
||||
loop.run_until_complete(s.wait_closed())
|
||||
loop.close()
|
@ -15,6 +15,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import re
|
||||
import asyncio
|
||||
import asyncio.subprocess
|
||||
|
||||
|
@ -58,6 +58,8 @@ def test_json(vm, project):
|
||||
'console': vm.console,
|
||||
'console_type': 'telnet',
|
||||
'console_resolution': '1024x768',
|
||||
'console_http_port': 80,
|
||||
'console_http_path': '/',
|
||||
'aux': vm.aux,
|
||||
'start_command': vm.start_command,
|
||||
'environment': vm.environment,
|
||||
@ -101,7 +103,9 @@ def test_create(loop, project, manager):
|
||||
"Name": "test",
|
||||
"Hostname": "test",
|
||||
"Image": "ubuntu",
|
||||
"Env": [],
|
||||
"Env": [
|
||||
"GNS3_MAX_ETHERNET=eth0"
|
||||
],
|
||||
"Entrypoint": ["/gns3/init.sh"],
|
||||
"Cmd": ["/bin/sh"]
|
||||
})
|
||||
@ -140,7 +144,10 @@ def test_create_vnc(loop, project, manager):
|
||||
"Name": "test",
|
||||
"Hostname": "test",
|
||||
"Image": "ubuntu",
|
||||
"Env": ['DISPLAY=:42'],
|
||||
"Env": [
|
||||
"GNS3_MAX_ETHERNET=eth0",
|
||||
"DISPLAY=:42"
|
||||
],
|
||||
"Entrypoint": ["/gns3/init.sh"],
|
||||
"Cmd": ["/bin/sh"]
|
||||
})
|
||||
@ -180,7 +187,9 @@ def test_create_start_cmd(loop, project, manager):
|
||||
"Name": "test",
|
||||
"Hostname": "test",
|
||||
"Image": "ubuntu",
|
||||
"Env": []
|
||||
"Env": [
|
||||
"GNS3_MAX_ETHERNET=eth0"
|
||||
]
|
||||
})
|
||||
assert vm._cid == "e90e34656806"
|
||||
|
||||
@ -209,7 +218,11 @@ def test_create_environment(loop, project, manager):
|
||||
],
|
||||
"Privileged": True
|
||||
},
|
||||
"Env": ["YES=1", "NO=0"],
|
||||
"Env": [
|
||||
"GNS3_MAX_ETHERNET=eth0",
|
||||
"YES=1",
|
||||
"NO=0"
|
||||
],
|
||||
"Volumes": {},
|
||||
"NetworkDisabled": True,
|
||||
"Name": "test",
|
||||
@ -264,7 +277,9 @@ def test_create_image_not_available(loop, project, manager):
|
||||
"Name": "test",
|
||||
"Hostname": "test",
|
||||
"Image": "ubuntu",
|
||||
"Env": [],
|
||||
"Env": [
|
||||
"GNS3_MAX_ETHERNET=eth0"
|
||||
],
|
||||
"Entrypoint": ["/gns3/init.sh"],
|
||||
"Cmd": ["/bin/sh"]
|
||||
})
|
||||
@ -479,7 +494,9 @@ def test_update(loop, vm):
|
||||
"Name": "test",
|
||||
"Hostname": "test",
|
||||
"Image": "ubuntu",
|
||||
"Env": [],
|
||||
"Env": [
|
||||
"GNS3_MAX_ETHERNET=eth0"
|
||||
],
|
||||
"Entrypoint": ["/gns3/init.sh"],
|
||||
"Cmd": ["/bin/sh"]
|
||||
})
|
||||
@ -544,7 +561,9 @@ def test_update_running(loop, vm):
|
||||
"Name": "test",
|
||||
"Hostname": "test",
|
||||
"Image": "ubuntu",
|
||||
"Env": [],
|
||||
"Env": [
|
||||
"GNS3_MAX_ETHERNET=eth0"
|
||||
],
|
||||
"Entrypoint": ["/gns3/init.sh"],
|
||||
"Cmd": ["/bin/sh"]
|
||||
})
|
||||
|
@ -22,7 +22,7 @@ import os
|
||||
import sys
|
||||
|
||||
from tests.utils import asyncio_patch
|
||||
from pkg_resources import parse_version
|
||||
from gns3server.utils import parse_version
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from gns3server.compute.vpcs.vpcs_vm import VPCSVM
|
||||
|
@ -173,9 +173,9 @@ def test_upload_vm(http_compute, tmpdir):
|
||||
|
||||
|
||||
def test_upload_vm_permission_denied(http_compute, tmpdir):
|
||||
with open(str(tmpdir / "test2"), "w+") as f:
|
||||
with open(str(tmpdir / "test2.tmp"), "w+") as f:
|
||||
f.write("")
|
||||
os.chmod(str(tmpdir / "test2"), 0)
|
||||
os.chmod(str(tmpdir / "test2.tmp"), 0)
|
||||
|
||||
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/dynamips/vms/test2", body="TEST", raw=True)
|
||||
|
@ -347,13 +347,3 @@ def test_upload_vm(http_compute, tmpdir):
|
||||
with open(str(tmpdir / "test2.md5sum")) as f:
|
||||
checksum = f.read()
|
||||
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
|
||||
|
||||
|
||||
def test_upload_vm_permission_denied(http_compute, tmpdir):
|
||||
with open(str(tmpdir / "test2"), "w+") as f:
|
||||
f.write("")
|
||||
os.chmod(str(tmpdir / "test2"), 0)
|
||||
|
||||
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/iou/vms/test2", body="TEST", raw=True)
|
||||
assert response.status == 409
|
||||
|
@ -264,9 +264,9 @@ def test_upload_vm_forbiden_location(http_compute, tmpdir):
|
||||
|
||||
|
||||
def test_upload_vm_permission_denied(http_compute, tmpdir):
|
||||
with open(str(tmpdir / "test2"), "w+") as f:
|
||||
with open(str(tmpdir / "test2.tmp"), "w+") as f:
|
||||
f.write("")
|
||||
os.chmod(str(tmpdir / "test2"), 0)
|
||||
os.chmod(str(tmpdir / "test2.tmp"), 0)
|
||||
|
||||
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/qemu/vms/test2", body="TEST", raw=True)
|
||||
|
@ -16,7 +16,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from gns3server.utils import force_unix_path, macaddress_to_int, int_to_macaddress
|
||||
from gns3server.utils import *
|
||||
|
||||
|
||||
def test_force_unix_path():
|
||||
@ -31,3 +31,19 @@ def test_macaddress_to_int():
|
||||
|
||||
def test_int_to_macaddress():
|
||||
assert int_to_macaddress(52228632586) == "00:0c:29:11:b0:0a"
|
||||
|
||||
|
||||
def test_parse_version():
|
||||
assert parse_version('1') == ('000001', '00000', '000000', 'final')
|
||||
assert parse_version('1.3') == ('000001', '000003', '000000', 'final')
|
||||
assert parse_version('1.3.dev3') == ('000001', '000003', '000000', 'dev', '000003')
|
||||
assert parse_version('1.3a1') == ('000001', '000003', '000000', 'a', '000001')
|
||||
assert parse_version('1.3rc1') == ('000001', '000003', '000000', 'c', '000001')
|
||||
|
||||
assert parse_version('1.2.3') > parse_version('1.2.2')
|
||||
assert parse_version('1.3') > parse_version('1.2.2')
|
||||
assert parse_version('1.3') > parse_version('1.3alpha1')
|
||||
assert parse_version('1.3') > parse_version('1.3rc1')
|
||||
assert parse_version('1.3rc1') > parse_version('1.3alpha3')
|
||||
assert parse_version('1.3dev1') > parse_version('1.3rc1')
|
||||
assert parse_version('1.2.3') > parse_version('1.2')
|
||||
|
Loading…
Reference in New Issue
Block a user