HTTP support for docker

Fix #509
This commit is contained in:
Julien Duponchelle 2016-05-03 16:49:33 +02:00
parent dcbaa62df8
commit 5fd385159c
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
5 changed files with 167 additions and 4 deletions

View File

@ -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)

View File

@ -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,8 @@ 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
if adapters is None:
self.adapters = 1
@ -95,6 +100,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 +140,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
@ -322,6 +345,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()
@ -361,11 +386,22 @@ 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)]
server = AsyncioRawCommandServer(command)
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):

View File

@ -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",

View File

@ -0,0 +1,96 @@
# -*- 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):
self._command = command
@asyncio.coroutine
def run(self, network_reader, network_writer):
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()
@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))
while True:
done, pending = yield from asyncio.wait(
[
network_read,
reader_read
],
return_when=asyncio.FIRST_COMPLETED)
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))
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()

View File

@ -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,