Skip to content

Commit

Permalink
WIP - Adding multiple manifest and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoMario109 committed May 13, 2024
1 parent 667c179 commit ddcd3f1
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 709 deletions.
3 changes: 1 addition & 2 deletions core/services/kraken/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
# Routers
from api.v1.routers import index_router_v1, extension_router_v1
from api.v2.routers import index_router_v2, extension_router_v2, container_router_v2
# TEMP - REMOVE
from temp.apis import GenericErrorHandlingRoute
from commonwealth.utils.apis import GenericErrorHandlingRoute

#
# Main API App
Expand Down
7 changes: 0 additions & 7 deletions core/services/kraken/api/v2/routers/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ async def list() -> list[ExtensionModel]:
]


@extension_router_v2.get("/manifest", status_code=status.HTTP_200_OK)
@version(2, 0)
async def manifest() -> Response:
# TODO - Change to pydantic models
return await Kraken.fetch_manifest()


@extension_router_v2.post("/install", status_code=status.HTTP_201_CREATED, response_class=StreamingResponse)
@version(2, 0)
async def install(identifier: str, tag: str | None = None) -> StreamingResponse:
Expand Down
47 changes: 47 additions & 0 deletions core/services/kraken/api/v2/routers/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from fastapi import APIRouter, status, HTTPException
from fastapi_versioning import version

from app.manifest import ManifestManager
from app.manifest.models import ManifestBase, Manifest

# Creates Extension router
manifest_router_v2 = APIRouter(
prefix="/manifest",
tags=["manifest"],
responses={
status.HTTP_404_NOT_FOUND: {"description": "Not found"}
},
)

manager = ManifestManager.instance()

# Endpoints

@manifest_router_v2.get("/", status_code=status.HTTP_200_OK)
@version(2, 0)
async def list_manifest() -> list[Manifest]:
return await manager.fetch()

@manifest_router_v2.get("/{identifier}", status_code=status.HTTP_200_OK)
@version(2, 0)
async def get_manifest(identifier: str) -> Manifest:
manifest = await manager.fetch_by_identifier(identifier)
if not manifest:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Manifest not found")

return manifest

@manifest_router_v2.post("/", status_code=status.HTTP_201_CREATED)
@version(2, 0)
async def create_manifest(body: ManifestBase) -> str:
return await manager.add(body)

@manifest_router_v2.put("/{identifier}", status_code=status.HTTP_204_NO_CONTENT)
@version(2, 0)
async def update_manifest(identifier: str, body: ManifestBase) -> None:
await manager.update(identifier, body)

@manifest_router_v2.delete("/{identifier}", status_code=status.HTTP_204_NO_CONTENT)
@version(2, 0)
async def delete_manifest(identifier: str) -> None:
await manager.remove(identifier)
11 changes: 3 additions & 8 deletions core/services/kraken/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class CommandLineArgs:
Represents command-line arguments for the client.
Attributes:
debug: bool - Whether to enable debug mode
debug (bool): Enable debug mode
host (str): Host to server kraken on
port (int): Port to server kraken on
"""

debug: bool
Expand All @@ -18,13 +20,6 @@ class CommandLineArgs:

@staticmethod
def from_args() -> "CommandLineArgs":
"""
Parses command-line arguments and returns a CommandLineArgs instance.
Returns:
CommandLineArgs: Instance of CommandLineArgs with the parsed arguments
"""

parser = argparse.ArgumentParser(
description = "Kraken Extension manager client."
)
Expand Down
8 changes: 6 additions & 2 deletions core/services/kraken/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# This file is used to define general configurations for the app

# Current running service name
SERVICE_NAME = 'kraken'

__all__ = ["SERVICE_NAME"]
EXT_DEV_MANIFEST_URL = "https://raw.githubusercontent.com/bluerobotics/BlueOS-Extensions-Repository/gh-pages-dev/manifest.json"
EXT_PROD_MANIFEST_URL = "https://bluerobotics.github.io/BlueOS-Extensions-Repository/manifest.json"

__all__ = [
"SERVICE_NAME", "EXT_DEV_MANIFEST_URL", "EXT_PROD_MANIFEST_URL"
]
15 changes: 15 additions & 0 deletions core/services/kraken/docker/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import aiodocker

class DockerCtx(object):
"""
Context manager for Docker client.
"""

def __init__(self) -> None:
self._client: aiodocker.Docker = aiodocker.Docker()

async def __aenter__(self) -> aiodocker.Docker:
return self._client

async def __aexit__(self, exc_type, exc, tb) -> None:
await self._client.close()
12 changes: 12 additions & 0 deletions core/services/kraken/docker/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pydantic import BaseModel

class ContainerModel(BaseModel):
name: str
image: str
image_id: str
status: str

class ContainerUsageModel(BaseModel):
cpu: str
memory: float
disk: int
4 changes: 4 additions & 0 deletions core/services/kraken/extension/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from app.extension.extension import Extension
from app.extension.model import ExtensionModel

__all__ = ["Extension", "ExtensionModel"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from manifest import Manifest
from manifest.models import Image
from settings import Extension as ExtensionSettings, SettingsV1
from kraken.docker import DockerCtx
from app.docker.docker import DockerCtx
from kraken.exceptions import ExtensionContainerNotFound, ExtensionNotFound, ExtensionPullFailed


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pydantic import BaseModel


class ExtensionModel(BaseModel):
"""
Represents an extension model.
Expand All @@ -22,35 +21,3 @@ class ExtensionModel(BaseModel):
permissions: str
enabled: str
user_permissions: str


class ContainerModel(BaseModel):
"""
Represents a Docker container model.
Attributes:
name (str): The name of the container.
image (str): The image used by the container.
image_id (str): The ID of the image.
status (str): The status of the container.
"""

name: str
image: str
image_id: str
status: str


class ContainerUsageModel(BaseModel):
"""
Represents the usage of a Docker container.
Attributes:
cpu (str): The CPU usage of the container.
memory (float): The memory usage of the container.
disk (int): The disk usage of the container.
"""

cpu: str
memory: float
disk: int
Loading

0 comments on commit ddcd3f1

Please sign in to comment.