|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from commonwealth.utils.streaming import streamer |
| 4 | +from fastapi import APIRouter, HTTPException, status |
| 5 | +from fastapi.responses import StreamingResponse |
| 6 | +from fastapi_versioning import versioned_api_route |
| 7 | + |
| 8 | +from api.v1.models import Extension |
| 9 | +from kraken import kraken_instance |
| 10 | + |
| 11 | +extension_router_v1 = APIRouter( |
| 12 | + prefix="/extension", |
| 13 | + tags=["extension_v1"], |
| 14 | + route_class=versioned_api_route(1, 0), |
| 15 | + responses={status.HTTP_404_NOT_FOUND: {"description": "Not found"}}, |
| 16 | +) |
| 17 | + |
| 18 | +@extension_router_v1.post("/install", status_code=status.HTTP_201_CREATED) |
| 19 | +async def install_extension(extension: Extension) -> Any: |
| 20 | + if not extension.is_valid(): |
| 21 | + raise HTTPException( |
| 22 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 23 | + detail="Invalid extension description", |
| 24 | + ) |
| 25 | + if not kraken_instance.has_enough_disk_space(): |
| 26 | + raise HTTPException( |
| 27 | + status_code=status.HTTP_507_INSUFFICIENT_STORAGE, |
| 28 | + detail="Not enough disk space to install the extension", |
| 29 | + ) |
| 30 | + compatible_digest = await kraken_instance.is_compatible_extension(extension.identifier, extension.tag) |
| 31 | + # Throw an exception only if compatible_digest is False, indicating the extension is in the manifest but it is |
| 32 | + # incompatible. If compatible_digest is None, we are going to trusty that the image is compatible and will work |
| 33 | + if compatible_digest is False: |
| 34 | + raise HTTPException( |
| 35 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 36 | + detail="Extension is not compatible with the current machine running BlueOS.", |
| 37 | + ) |
| 38 | + return StreamingResponse(streamer(kraken_instance.install_extension(extension, compatible_digest))) |
| 39 | + |
| 40 | + |
| 41 | +@extension_router_v1.post("/uninstall", status_code=status.HTTP_200_OK) |
| 42 | +async def uninstall_extension(extension_identifier: str) -> Any: |
| 43 | + return await kraken_instance.uninstall_extension_from_identifier(extension_identifier) |
| 44 | + |
| 45 | + |
| 46 | +@extension_router_v1.post("/update_to_version", status_code=status.HTTP_201_CREATED) |
| 47 | +async def update_extension(extension_identifier: str, new_version: str) -> Any: |
| 48 | + return StreamingResponse(streamer(kraken_instance.update_extension_to_version(extension_identifier, new_version))) |
| 49 | + |
| 50 | + |
| 51 | +@extension_router_v1.post("/enable", status_code=status.HTTP_200_OK) |
| 52 | +async def enable_extension(extension_identifier: str) -> Any: |
| 53 | + return await kraken_instance.enable_extension(extension_identifier) |
| 54 | + |
| 55 | + |
| 56 | +@extension_router_v1.post("/disable", status_code=status.HTTP_200_OK) |
| 57 | +async def disable_extension(extension_identifier: str) -> Any: |
| 58 | + return await kraken_instance.disable_extension(extension_identifier) |
| 59 | + |
| 60 | + |
| 61 | +@extension_router_v1.post("/restart", status_code=status.HTTP_202_ACCEPTED) |
| 62 | +async def restart_extension(extension_identifier: str) -> Any: |
| 63 | + return await kraken_instance.restart_extension(extension_identifier) |
0 commit comments