mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-28 08:24:34 +02:00
feat: report Docker pull progress and add an idle timeout
A pull now reports docker.image.pull notifications on the controller stream: started, throttled downloading events with the layer counts and the latest daemon message, then completed or failed. An optional progress_id is echoed in every event so a client can correlate them with its own request. Image operations belong to a compute, not to a project, so the events go to the controller stream and reach clients that have no project open. The pull also has an idle socket read timeout of 60 seconds. Without it an unreachable registry left the pull, the compute request and the client request hanging forever with no feedback at all. The timeout is idle time, not total time: the daemon streams progress during the whole pull, so a minute without data means the registry is gone. When it fires, the daemon connection closes and Docker cancels the pull. Docker.http_query now accepts an aiohttp ClientTimeout object so a caller can ask for a policy finer than a total timeout.
This commit is contained in:
9 files changed
+266
-27
No files matched your search
@@ -19,6 +19,7 @@ API routes for images.
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import urllib.parse
|
||||
from typing import List, Optional
|
||||
|
||||
@@ -26,10 +27,16 @@ from fastapi import APIRouter, Body, HTTPException, Query, Request, status
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
from gns3server.compute.docker import Docker
|
||||
from gns3server.compute.docker.docker_error import DockerError
|
||||
from gns3server.compute.dynamips import Dynamips
|
||||
from gns3server.compute.iou import IOU
|
||||
from gns3server.compute.notification_manager import NotificationManager
|
||||
from gns3server.compute.qemu import Qemu
|
||||
|
||||
# Daemon progress lines arrive dozens of times per second per layer: emit at
|
||||
# most one downloading event per second per pull.
|
||||
PULL_PROGRESS_INTERVAL = 1.0
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@@ -44,13 +51,51 @@ async def get_docker_images() -> List[dict]:
|
||||
|
||||
|
||||
@router.post("/docker/images/pull", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def pull_docker_image(image: str = Body(..., embed=True, min_length=1, pattern=r"^\S+$")) -> None:
|
||||
async def pull_docker_image(
|
||||
image: str = Body(..., embed=True, min_length=1, pattern=r"^\S+$"),
|
||||
progress_id: Optional[str] = Query(
|
||||
None, pattern=r"^\S+$", description="Client supplied id to correlate progress notifications"
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Pull or update a Docker image.
|
||||
|
||||
The pull progress is reported as docker.image.pull notifications: a
|
||||
started event, throttled downloading events and a terminal completed or
|
||||
failed event. The optional progress_id is echoed in every event so a
|
||||
client can correlate them with its own request.
|
||||
"""
|
||||
|
||||
docker_manager = Docker.instance()
|
||||
await docker_manager.pull_image(image, force=True)
|
||||
|
||||
def emit(state: str, message: Optional[str] = None, progress: Optional[dict] = None) -> None:
|
||||
event = {"image": image, "state": state}
|
||||
if message is not None:
|
||||
event["message"] = message
|
||||
if progress is not None:
|
||||
event["layers_total"] = progress["layers_total"]
|
||||
event["layers_done"] = progress["layers_done"]
|
||||
if progress_id is not None:
|
||||
event["progress_id"] = progress_id
|
||||
NotificationManager.instance().emit("docker.image.pull", event)
|
||||
|
||||
last_emit = 0.0
|
||||
|
||||
def progress_callback(progress: dict) -> None:
|
||||
nonlocal last_emit
|
||||
now = time.monotonic()
|
||||
if now - last_emit < PULL_PROGRESS_INTERVAL:
|
||||
return
|
||||
last_emit = now
|
||||
emit("downloading", progress["message"], progress)
|
||||
|
||||
emit("started", f"Pulling '{image}' from Docker repository")
|
||||
try:
|
||||
await docker_manager.pull_image(image, progress_callback=progress_callback, force=True)
|
||||
except DockerError as e:
|
||||
emit("failed", str(e))
|
||||
raise
|
||||
emit("completed", f"Success pulling image {image}")
|
||||
|
||||
|
||||
@router.post("/docker/images/load", status_code=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@@ -215,16 +215,26 @@ async def docker_get_images(compute_id: Union[str, UUID]) -> List[schemas.Comput
|
||||
dependencies=[Depends(has_privilege("Compute.Modify"))],
|
||||
)
|
||||
async def docker_pull_image(
|
||||
compute_id: Union[str, UUID], image: str = Body(..., embed=True, min_length=1, pattern=r"^\S+$")
|
||||
compute_id: Union[str, UUID],
|
||||
image: str = Body(..., embed=True, min_length=1, pattern=r"^\S+$"),
|
||||
progress_id: Optional[str] = Query(
|
||||
None, pattern=r"^\S+$", description="Client supplied id to correlate progress notifications"
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Pull or update a Docker image on a compute.
|
||||
|
||||
The pull progress is reported on the controller notification stream as
|
||||
docker.image.pull events (started, downloading, completed or failed). The
|
||||
optional progress_id is echoed in every event so a client can correlate
|
||||
them with its own request.
|
||||
|
||||
Required privilege: Compute.Modify
|
||||
"""
|
||||
|
||||
compute = Controller.instance().get_compute(str(compute_id))
|
||||
await compute.forward("POST", "docker", "images/pull", data={"image": image})
|
||||
params = {"progress_id": progress_id} if progress_id is not None else None
|
||||
await compute.forward("POST", "docker", "images/pull", data={"image": image}, params=params)
|
||||
|
||||
|
||||
@router.post(
|
||||
|
||||
Reference in new issue
Block a user