mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-19 20:55:14 +02:00
By default uvicorn waits for open connections forever. A long-lived streaming response keeps the server waiting after Ctrl+C. One common case is the MCP Streamable HTTP transport: the client opens a standalone GET request and the server answers with an SSE stream (not the old SSE transport). A second Ctrl+C sets force_exit. Uvicorn then skips the lifespan shutdown, so project and controller cleanup does not run. Add timeout_graceful_shutdown. After the timeout uvicorn cancels the remaining tasks and runs the lifespan shutdown. Also make the four notification WebSocket handlers stop when app.state.exiting is set. The HTTP stream handlers already do this.
222 lines
7.1 KiB
Python
222 lines
7.1 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/>.
|
|
|
|
|
|
import logging
|
|
|
|
from fastapi import Depends, FastAPI, Request
|
|
from fastapi.responses import JSONResponse
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
from gns3server.api.operation_ids import generate_operation_id
|
|
from gns3server.compute.compute_error import (
|
|
ComputeError,
|
|
ComputeForbiddenError,
|
|
ComputeNotFoundError,
|
|
ComputeTimeoutError,
|
|
ComputeUnauthorizedError,
|
|
)
|
|
from gns3server.compute.error import ImageMissingError, NodeError
|
|
from gns3server.compute.ubridge.ubridge_error import UbridgeError
|
|
from gns3server.version import __api_version__
|
|
|
|
from . import (
|
|
atm_switch_nodes,
|
|
capabilities,
|
|
cloud_nodes,
|
|
compute,
|
|
docker_nodes,
|
|
dynamips_nodes,
|
|
ethernet_hub_nodes,
|
|
ethernet_switch_nodes,
|
|
frame_relay_switch_nodes,
|
|
images,
|
|
iou_nodes,
|
|
nat_nodes,
|
|
notifications,
|
|
projects,
|
|
qemu_nodes,
|
|
virtualbox_nodes,
|
|
vmware_nodes,
|
|
vpcs_nodes,
|
|
)
|
|
from .dependencies.authentication import compute_authentication
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
compute_api = FastAPI(
|
|
title="GNS3 compute API",
|
|
description="This page describes the private compute API for GNS3. PLEASE DO NOT USE DIRECTLY!",
|
|
generate_unique_id_function=generate_operation_id,
|
|
version=__api_version__,
|
|
servers=[{"url": "/v3/compute"}],
|
|
)
|
|
|
|
compute_api.state.controller_host = None
|
|
compute_api.state.exiting = False
|
|
|
|
|
|
@compute_api.exception_handler(ComputeError)
|
|
async def compute_error_handler(request: Request, exc: ComputeError):
|
|
log.error(f"Compute error: {exc}")
|
|
return JSONResponse(
|
|
status_code=409,
|
|
content={"message": str(exc)},
|
|
)
|
|
|
|
|
|
@compute_api.exception_handler(ComputeTimeoutError)
|
|
async def compute_timeout_error_handler(request: Request, exc: ComputeTimeoutError):
|
|
log.error(f"Compute timeout error: {exc}")
|
|
return JSONResponse(
|
|
status_code=408,
|
|
content={"message": str(exc)},
|
|
)
|
|
|
|
|
|
@compute_api.exception_handler(ComputeUnauthorizedError)
|
|
async def compute_unauthorized_error_handler(request: Request, exc: ComputeUnauthorizedError):
|
|
log.error(f"Compute unauthorized error: {exc}")
|
|
return JSONResponse(
|
|
status_code=401,
|
|
content={"message": str(exc)},
|
|
)
|
|
|
|
|
|
@compute_api.exception_handler(ComputeForbiddenError)
|
|
async def compute_forbidden_error_handler(request: Request, exc: ComputeForbiddenError):
|
|
log.error(f"Compute forbidden error: {exc}")
|
|
return JSONResponse(
|
|
status_code=403,
|
|
content={"message": str(exc)},
|
|
)
|
|
|
|
|
|
@compute_api.exception_handler(ComputeNotFoundError)
|
|
async def compute_not_found_error_handler(request: Request, exc: ComputeNotFoundError):
|
|
log.error(f"Compute not found error: {exc}")
|
|
return JSONResponse(
|
|
status_code=404,
|
|
content={"message": str(exc)},
|
|
)
|
|
|
|
|
|
@compute_api.exception_handler(ImageMissingError)
|
|
async def image_missing_error_handler(request: Request, exc: ImageMissingError):
|
|
log.error(f"Compute image missing error: {exc}")
|
|
return JSONResponse(
|
|
status_code=409,
|
|
content={"message": str(exc), "image": exc.image, "exception": exc.__class__.__name__},
|
|
)
|
|
|
|
|
|
@compute_api.exception_handler(NodeError)
|
|
async def node_error_handler(request: Request, exc: NodeError):
|
|
log.error(f"Compute node error: {exc}")
|
|
return JSONResponse(
|
|
status_code=409,
|
|
content={"message": str(exc), "exception": exc.__class__.__name__},
|
|
)
|
|
|
|
|
|
@compute_api.exception_handler(UbridgeError)
|
|
async def ubridge_error_handler(request: Request, exc: UbridgeError):
|
|
log.error(f"Compute uBridge error: {exc}")
|
|
return JSONResponse(
|
|
status_code=409,
|
|
content={"message": str(exc), "exception": exc.__class__.__name__},
|
|
)
|
|
|
|
|
|
# make sure the content key is "message", not "detail" per default
|
|
@compute_api.exception_handler(StarletteHTTPException)
|
|
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
|
return JSONResponse(
|
|
status_code=exc.status_code,
|
|
content={"message": exc.detail},
|
|
)
|
|
|
|
|
|
compute_api.include_router(capabilities.router, dependencies=[Depends(compute_authentication)], tags=["Capabilities"])
|
|
|
|
compute_api.include_router(compute.router, dependencies=[Depends(compute_authentication)], tags=["Compute"])
|
|
|
|
compute_api.include_router(notifications.router, tags=["Notifications"])
|
|
|
|
compute_api.include_router(projects.router, dependencies=[Depends(compute_authentication)], tags=["Projects"])
|
|
|
|
compute_api.include_router(images.router, dependencies=[Depends(compute_authentication)], tags=["Images"])
|
|
|
|
compute_api.include_router(
|
|
atm_switch_nodes.router,
|
|
dependencies=[Depends(compute_authentication)],
|
|
prefix="/projects/{project_id}/atm_switch/nodes",
|
|
tags=["ATM switch"],
|
|
)
|
|
compute_api.include_router(
|
|
cloud_nodes.router,
|
|
dependencies=[Depends(compute_authentication)],
|
|
prefix="/projects/{project_id}/cloud/nodes",
|
|
tags=["Cloud nodes"],
|
|
)
|
|
|
|
compute_api.include_router(docker_nodes.router, prefix="/projects/{project_id}/docker/nodes", tags=["Docker nodes"])
|
|
|
|
compute_api.include_router(
|
|
dynamips_nodes.router, prefix="/projects/{project_id}/dynamips/nodes", tags=["Dynamips nodes"]
|
|
)
|
|
|
|
compute_api.include_router(
|
|
ethernet_hub_nodes.router,
|
|
dependencies=[Depends(compute_authentication)],
|
|
prefix="/projects/{project_id}/ethernet_hub/nodes",
|
|
tags=["Ethernet hub nodes"],
|
|
)
|
|
|
|
compute_api.include_router(
|
|
ethernet_switch_nodes.router,
|
|
dependencies=[Depends(compute_authentication)],
|
|
prefix="/projects/{project_id}/ethernet_switch/nodes",
|
|
tags=["Ethernet switch nodes"],
|
|
)
|
|
|
|
compute_api.include_router(
|
|
frame_relay_switch_nodes.router,
|
|
dependencies=[Depends(compute_authentication)],
|
|
prefix="/projects/{project_id}/frame_relay_switch/nodes",
|
|
tags=["Frame Relay switch nodes"],
|
|
)
|
|
|
|
compute_api.include_router(iou_nodes.router, prefix="/projects/{project_id}/iou/nodes", tags=["IOU nodes"])
|
|
|
|
compute_api.include_router(
|
|
nat_nodes.router,
|
|
dependencies=[Depends(compute_authentication)],
|
|
prefix="/projects/{project_id}/nat/nodes",
|
|
tags=["NAT nodes"],
|
|
)
|
|
|
|
compute_api.include_router(qemu_nodes.router, prefix="/projects/{project_id}/qemu/nodes", tags=["Qemu nodes"])
|
|
|
|
compute_api.include_router(
|
|
virtualbox_nodes.router, prefix="/projects/{project_id}/virtualbox/nodes", tags=["VirtualBox nodes"]
|
|
)
|
|
|
|
compute_api.include_router(vmware_nodes.router, prefix="/projects/{project_id}/vmware/nodes", tags=["VMware nodes"])
|
|
|
|
compute_api.include_router(vpcs_nodes.router, prefix="/projects/{project_id}/vpcs/nodes", tags=["VPCS nodes"])
|