From 092202615e8965c663faf70d06893bd1706ebfbb Mon Sep 17 00:00:00 2001 From: YueGuobin Date: Fri, 6 Nov 2026 10:00:17 +0800 Subject: [PATCH] api: add controller endpoint to load Docker images on a compute Add POST /computes/{compute_id}/docker/images/load. It streams an uploaded docker save tar (plain or gzip) to the compute, which loads it into its Docker daemon. Clients can now install image packages such as iol-xe-17-18-02.tar.gz without SSH access to the server. The body is proxied without buffering (async iterable to chunked transfer) and runs with no total timeout, like the existing image sync path between computes. --- gns3server/api/routes/controller/computes.py | 22 +++++++++++++++++++- tests/api/routes/controller/test_computes.py | 20 ++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/gns3server/api/routes/controller/computes.py b/gns3server/api/routes/controller/computes.py index c8de19a55..9dd2c7b54 100644 --- a/gns3server/api/routes/controller/computes.py +++ b/gns3server/api/routes/controller/computes.py @@ -21,7 +21,7 @@ API routes for computes. from typing import Any, List, Optional, Union from uuid import UUID -from fastapi import APIRouter, Body, Depends, Query, status +from fastapi import APIRouter, Body, Depends, Query, Request, status import gns3server.db.models as models from gns3server import schemas @@ -227,6 +227,26 @@ async def docker_pull_image( await compute.forward("POST", "docker", "images/pull", data={"image": image}) +@router.post( + "/{compute_id}/docker/images/load", + status_code=status.HTTP_204_NO_CONTENT, + dependencies=[Depends(has_privilege("Compute.Modify"))], +) +async def docker_load_image(compute_id: Union[str, UUID], request: Request) -> None: + """ + Load a Docker image on a compute from a docker save tar stream. + + The tar archive (plain or gzip compressed) is streamed to the compute + and loaded into its Docker daemon, without buffering the whole file + in the controller memory. + + Required privilege: Compute.Modify + """ + + compute = Controller.instance().get_compute(str(compute_id)) + await compute.http_query("POST", "docker/images/load", data=request.stream(), timeout=None) + + @router.get("/{compute_id}/virtualbox/vms", response_model=List[schemas.ComputeVirtualBoxVM]) async def virtualbox_vms(compute_id: Union[str, UUID]) -> List[schemas.ComputeVirtualBoxVM]: """ diff --git a/tests/api/routes/controller/test_computes.py b/tests/api/routes/controller/test_computes.py index 08e2ec7df..41eb22ae5 100644 --- a/tests/api/routes/controller/test_computes.py +++ b/tests/api/routes/controller/test_computes.py @@ -197,6 +197,26 @@ class TestComputeFeatures: compute.forward.assert_called_with("POST", "docker", "images/pull", data={"image": "nginx:latest"}) assert response.status_code == status.HTTP_204_NO_CONTENT + async def test_compute_load_docker_image(self, app: FastAPI, client: AsyncClient, test_compute: Compute) -> None: + + compute = MagicMock() + compute.http_query = AsyncioMagicMock(return_value={}) + with patch("gns3server.api.routes.controller.computes.Controller.instance") as controller: + controller.return_value.get_compute.return_value = compute + response = await client.post( + app.url_path_for("docker_load_image", compute_id=test_compute.compute_id), + content=b"docker save tar data", + headers={"Content-Type": "application/octet-stream"}, + ) + compute.http_query.assert_called_once() + args, kwargs = compute.http_query.call_args + assert args[0] == "POST" + assert args[1] == "docker/images/load" + assert kwargs["timeout"] is None + # the request body must be streamed, not buffered + assert hasattr(kwargs["data"], "__aiter__") + assert response.status_code == status.HTTP_204_NO_CONTENT + async def test_compute_list_virtualbox_vms(self, app: FastAPI, client: AsyncClient) -> None: params = {"protocol": "http", "host": "localhost", "port": 4242, "user": "julien", "password": "secure"}