mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-11-16 16:54:51 +02:00
Add support of ExtraHosts for Docker, Ref. gns3-gui#2482
This commit is contained in:
parent
747814f083
commit
43081152ef
@ -61,11 +61,12 @@ class DockerVM(BaseNode):
|
||||
: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
|
||||
:param extra_hosts: Hosts which will be written into /etc/hosts into docker conainer
|
||||
"""
|
||||
|
||||
def __init__(self, name, node_id, project, manager, image, console=None, aux=None, start_command=None,
|
||||
adapters=None, environment=None, console_type="telnet", console_resolution="1024x768",
|
||||
console_http_port=80, console_http_path="/"):
|
||||
console_http_port=80, console_http_path="/", extra_hosts=None):
|
||||
|
||||
super().__init__(name, node_id, project, manager, console=console, aux=aux, allocate_aux=True, console_type=console_type)
|
||||
|
||||
@ -84,6 +85,8 @@ class DockerVM(BaseNode):
|
||||
self._console_http_path = console_http_path
|
||||
self._console_http_port = console_http_port
|
||||
self._console_websocket = None
|
||||
self._extra_hosts = extra_hosts
|
||||
|
||||
self._volumes = []
|
||||
# Keep a list of created bridge
|
||||
self._bridges = set()
|
||||
@ -114,7 +117,8 @@ class DockerVM(BaseNode):
|
||||
"start_command": self.start_command,
|
||||
"status": self.status,
|
||||
"environment": self.environment,
|
||||
"node_directory": self.working_path
|
||||
"node_directory": self.working_path,
|
||||
"extra_hosts": self.extra_hosts
|
||||
}
|
||||
|
||||
def _get_free_display_port(self):
|
||||
@ -178,6 +182,14 @@ class DockerVM(BaseNode):
|
||||
def environment(self, command):
|
||||
self._environment = command
|
||||
|
||||
@property
|
||||
def extra_hosts(self):
|
||||
return self._extra_hosts
|
||||
|
||||
@extra_hosts.setter
|
||||
def extra_hosts(self, extra_hosts):
|
||||
self._extra_hosts = extra_hosts
|
||||
|
||||
@asyncio.coroutine
|
||||
def _get_container_state(self):
|
||||
"""Returns the container state (e.g. running, paused etc.)
|
||||
@ -288,7 +300,7 @@ class DockerVM(BaseNode):
|
||||
"HostConfig": {
|
||||
"CapAdd": ["ALL"],
|
||||
"Privileged": True,
|
||||
"Binds": self._mount_binds(image_infos)
|
||||
"Binds": self._mount_binds(image_infos),
|
||||
},
|
||||
"Volumes": {},
|
||||
"Env": ["container=docker"], # Systemd compliant: https://github.com/GNS3/gns3-server/issues/573
|
||||
@ -325,6 +337,12 @@ class DockerVM(BaseNode):
|
||||
params["Env"].append("DISPLAY=:{}".format(self._display))
|
||||
params["HostConfig"]["Binds"].append("/tmp/.X11-unix/:/tmp/.X11-unix/")
|
||||
|
||||
if self._extra_hosts is not None and self._extra_hosts.strip() != "":
|
||||
params["HostConfig"]["ExtraHosts"] = [h.strip()
|
||||
for h in self._extra_hosts.split("\n")
|
||||
if h.strip() != "" ]
|
||||
|
||||
|
||||
result = yield from self.manager.query("POST", "containers/create", data=params)
|
||||
self._cid = result['Id']
|
||||
log.info("Docker container '{name}' [{id}] created".format(
|
||||
|
@ -60,7 +60,8 @@ class DockerHandler:
|
||||
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"))
|
||||
aux=request.json.get("aux"),
|
||||
extra_hosts=request.json.get("extra_hosts"))
|
||||
for name, value in request.json.items():
|
||||
if name != "node_id":
|
||||
if hasattr(container, name) and getattr(container, name) != value:
|
||||
@ -312,7 +313,7 @@ class DockerHandler:
|
||||
props = [
|
||||
"name", "console", "aux", "console_type", "console_resolution",
|
||||
"console_http_port", "console_http_path", "start_command",
|
||||
"environment", "adapters"
|
||||
"environment", "adapters", "extra_hosts"
|
||||
]
|
||||
|
||||
changed = False
|
||||
|
@ -87,6 +87,11 @@ DOCKER_CREATE_SCHEMA = {
|
||||
"type": ["string", "null"],
|
||||
"minLength": 0,
|
||||
},
|
||||
"extra_hosts": {
|
||||
"description": "Docker extra hosts (added to /etc/hosts)",
|
||||
"type": ["string", "null"],
|
||||
"minLength": 0,
|
||||
},
|
||||
"container_id": {
|
||||
"description": "Docker container ID Read only",
|
||||
"type": "string",
|
||||
@ -184,6 +189,11 @@ DOCKER_OBJECT_SCHEMA = {
|
||||
"type": ["string", "null"],
|
||||
"minLength": 0,
|
||||
},
|
||||
"extra_hosts": {
|
||||
"description": "Docker extra hosts (added to /etc/hosts)",
|
||||
"type": ["string", "null"],
|
||||
"minLength": 0,
|
||||
},
|
||||
"node_directory": {
|
||||
"description": "Path to the node working directory Read only",
|
||||
"type": "string"
|
||||
|
@ -202,6 +202,28 @@ def test_create_vnc(loop, project, manager):
|
||||
assert vm._console_type == "vnc"
|
||||
|
||||
|
||||
def test_create_with_extra_hosts(loop, project, manager):
|
||||
extra_hosts = "test:199.199.199.1\ntest2:199.199.199.1"
|
||||
|
||||
response = {
|
||||
"Id": "e90e34656806",
|
||||
"Warnings": []
|
||||
}
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
||||
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", extra_hosts=extra_hosts)
|
||||
vm._start_vnc = MagicMock()
|
||||
vm._display = 42
|
||||
loop.run_until_complete(asyncio.async(vm.create()))
|
||||
called_kwargs = mock.call_args[1]
|
||||
assert called_kwargs["data"]["HostConfig"]["ExtraHosts"] == [
|
||||
"test:199.199.199.1",
|
||||
"test2:199.199.199.1"
|
||||
]
|
||||
assert vm._extra_hosts == extra_hosts
|
||||
|
||||
|
||||
def test_create_start_cmd(loop, project, manager):
|
||||
|
||||
response = {
|
||||
|
@ -32,7 +32,7 @@ pytestmark = pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supp
|
||||
@pytest.fixture
|
||||
def base_params():
|
||||
"""Return standard parameters"""
|
||||
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet", "console_resolution": "1280x1024"}
|
||||
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet", "console_resolution": "1280x1024", "extra_hosts": "test:127.0.0.1"}
|
||||
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
@ -69,7 +69,7 @@ def test_docker_create(http_compute, project, base_params):
|
||||
assert response.json["adapters"] == 2
|
||||
assert response.json["environment"] == "YES=1\nNO=0"
|
||||
assert response.json["console_resolution"] == "1280x1024"
|
||||
|
||||
assert response.json["extra_hosts"] == "test:127.0.0.1"
|
||||
|
||||
def test_docker_start(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start", return_value=True) as mock:
|
||||
@ -150,7 +150,8 @@ def test_docker_update(http_compute, vm, tmpdir, free_console_port):
|
||||
response = http_compute.put("/projects/{project_id}/docker/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"name": "test",
|
||||
"console": free_console_port,
|
||||
"start_command": "yes",
|
||||
"environment": "GNS3=1\nGNS4=0"},
|
||||
"environment": "GNS3=1\nGNS4=0",
|
||||
"extra_hosts": "test:127.0.0.1"},
|
||||
example=True)
|
||||
assert mock.called
|
||||
assert response.status == 200
|
||||
@ -158,6 +159,7 @@ def test_docker_update(http_compute, vm, tmpdir, free_console_port):
|
||||
assert response.json["console"] == free_console_port
|
||||
assert response.json["start_command"] == "yes"
|
||||
assert response.json["environment"] == "GNS3=1\nGNS4=0"
|
||||
assert response.json["extra_hosts"] == "test:127.0.0.1"
|
||||
|
||||
|
||||
def test_docker_start_capture(http_compute, vm, tmpdir, project):
|
||||
|
Loading…
Reference in New Issue
Block a user