mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-21 13:45:13 +02:00
The pull started event was emitted by the route before the pull took its per compute lock, so a request queued behind another pull claimed to be running while it was still waiting. The pull now reports the started state itself, once it owns the lock. Deleting an image interpolates the name into the daemon API path. The pattern on the path parameter limits it to the characters of an image reference and validate_docker_image_name also rejects "..", so a name cannot escape that path. Applied on both the controller and compute routes.
258 lines
8.4 KiB
Python
258 lines
8.4 KiB
Python
#
|
|
# Copyright (C) 2020 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/>.
|
|
|
|
"""
|
|
API routes for images.
|
|
"""
|
|
|
|
import os
|
|
import time
|
|
import urllib.parse
|
|
from typing import List, Optional
|
|
|
|
from fastapi import APIRouter, Body, HTTPException, Path, 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
|
|
from gns3server.utils.images import DOCKER_IMAGE_NAME_PATTERN, validate_docker_image_name
|
|
|
|
# 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()
|
|
|
|
|
|
@router.get("/docker/images")
|
|
async def get_docker_images() -> List[dict]:
|
|
"""
|
|
Get all Docker images.
|
|
"""
|
|
|
|
docker_manager = Docker.instance()
|
|
return await docker_manager.list_images()
|
|
|
|
|
|
@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+$"),
|
|
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()
|
|
|
|
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
|
|
# the started event comes from the pull itself, once it owns the lock,
|
|
# and is never throttled
|
|
if progress.get("state") == "started":
|
|
emit("started", progress["message"])
|
|
return
|
|
now = time.monotonic()
|
|
if now - last_emit < PULL_PROGRESS_INTERVAL:
|
|
return
|
|
last_emit = now
|
|
emit("downloading", progress["message"], progress)
|
|
|
|
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)
|
|
async def load_docker_image(
|
|
request: Request,
|
|
image: Optional[str] = Query(None, pattern=r"^\S+$", description="Tag of the image in the archive"),
|
|
digest: Optional[str] = Query(
|
|
None, pattern=r"^\S+$", description="Image id reported by the controller host daemon"
|
|
),
|
|
) -> None:
|
|
"""
|
|
Load a Docker image into the Docker daemon from a docker save tar stream.
|
|
|
|
The optional image tag and digest let the compute remember which image the
|
|
controller pushed and how the local daemon identifies it (see
|
|
Docker.load_image).
|
|
"""
|
|
|
|
docker_manager = Docker.instance()
|
|
await docker_manager.load_image(request.stream(), image=image, digest=digest)
|
|
|
|
|
|
@router.delete("/docker/images/{image:path}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_docker_image(
|
|
image: str = Path(..., pattern=DOCKER_IMAGE_NAME_PATTERN),
|
|
force: bool = Query(False, description="Remove the image even if a container uses it"),
|
|
) -> None:
|
|
"""
|
|
Delete a Docker image from the Docker daemon.
|
|
|
|
The daemon refuses to delete an image used by a container (even a stopped
|
|
one) and answers a conflict, unless force is set.
|
|
"""
|
|
|
|
image = urllib.parse.unquote(image)
|
|
try:
|
|
validate_docker_image_name(image)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail=str(e)) from e
|
|
docker_manager = Docker.instance()
|
|
await docker_manager.delete_image(image, force=force)
|
|
|
|
|
|
@router.get("/dynamips/images")
|
|
async def get_dynamips_images() -> List[dict]:
|
|
"""
|
|
Get all Dynamips images.
|
|
"""
|
|
|
|
dynamips_manager = Dynamips.instance()
|
|
return await dynamips_manager.list_images()
|
|
|
|
|
|
@router.post("/dynamips/images/{filename:path}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def upload_dynamips_image(filename: str, request: Request) -> None:
|
|
"""
|
|
Upload a Dynamips IOS image.
|
|
"""
|
|
|
|
dynamips_manager = Dynamips.instance()
|
|
await dynamips_manager.write_image(urllib.parse.unquote(filename), request.stream())
|
|
|
|
|
|
@router.get("/dynamips/images/{filename:path}")
|
|
async def download_dynamips_image(filename: str) -> FileResponse:
|
|
"""
|
|
Download a Dynamips IOS image.
|
|
"""
|
|
|
|
filename = urllib.parse.unquote(filename)
|
|
|
|
# Raise error if user try to escape
|
|
if filename[0] == "." or os.path.sep in filename:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
dynamips_manager = Dynamips.instance()
|
|
image_path = dynamips_manager.get_abs_image_path(filename)
|
|
|
|
if not os.path.exists(image_path):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
return FileResponse(image_path, media_type="application/octet-stream")
|
|
|
|
|
|
@router.get("/iou/images")
|
|
async def get_iou_images() -> List[dict]:
|
|
"""
|
|
Get all IOU images.
|
|
"""
|
|
|
|
iou_manager = IOU.instance()
|
|
return await iou_manager.list_images()
|
|
|
|
|
|
@router.post("/iou/images/{filename:path}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def upload_iou_image(filename: str, request: Request) -> None:
|
|
"""
|
|
Upload an IOU image.
|
|
"""
|
|
|
|
iou_manager = IOU.instance()
|
|
await iou_manager.write_image(urllib.parse.unquote(filename), request.stream())
|
|
|
|
|
|
@router.get("/iou/images/{filename:path}")
|
|
async def download_iou_image(filename: str) -> FileResponse:
|
|
"""
|
|
Download an IOU image.
|
|
"""
|
|
|
|
filename = urllib.parse.unquote(filename)
|
|
|
|
# Raise error if user try to escape
|
|
if filename[0] == "." or os.path.sep in filename:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
iou_manager = IOU.instance()
|
|
image_path = iou_manager.get_abs_image_path(filename)
|
|
if not os.path.exists(image_path):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
return FileResponse(image_path, media_type="application/octet-stream")
|
|
|
|
|
|
@router.get("/qemu/images")
|
|
async def get_qemu_images() -> List[dict]:
|
|
|
|
qemu_manager = Qemu.instance()
|
|
return await qemu_manager.list_images()
|
|
|
|
|
|
@router.post("/qemu/images/{filename:path}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def upload_qemu_image(filename: str, request: Request) -> None:
|
|
|
|
qemu_manager = Qemu.instance()
|
|
await qemu_manager.write_image(urllib.parse.unquote(filename), request.stream())
|
|
|
|
|
|
@router.get("/qemu/images/{filename:path}")
|
|
async def download_qemu_image(filename: str) -> FileResponse:
|
|
|
|
filename = urllib.parse.unquote(filename)
|
|
|
|
# Raise error if user try to escape
|
|
if filename[0] == "." or os.path.sep in filename:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
qemu_manager = Qemu.instance()
|
|
image_path = qemu_manager.get_abs_image_path(filename)
|
|
|
|
if not os.path.exists(image_path):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
return FileResponse(image_path, media_type="application/octet-stream")
|