Skip to content

Commit

Permalink
core: kraken: make manifest_url a settings and allow using multiple o…
Browse files Browse the repository at this point in the history
…f it
  • Loading branch information
Williangalvani committed Sep 22, 2023
1 parent 577d6d1 commit 145d4a4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
34 changes: 23 additions & 11 deletions core/services/kraken/kraken.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
from loguru import logger

from exceptions import ContainerDoesNotExist, ExtensionNotFound
from settings import Extension, SettingsV1
from settings import Extension, SettingsV2

REPO_URL = "https://bluerobotics.github.io/BlueOS-Extensions-Repository/manifest.json"
SERVICE_NAME = "Kraken"


Expand Down Expand Up @@ -79,17 +78,30 @@ async def check(self, extension: Extension) -> None:
await self.start_extension(extension)

def load_settings(self) -> None:
self.manager = Manager(SERVICE_NAME, SettingsV1)
self.manager = Manager(SERVICE_NAME, SettingsV2)
self.settings = self.manager.settings

async def fetch_manifest(self) -> Any:
async with aiohttp.ClientSession() as session:
async with session.get(REPO_URL) as resp:
if resp.status != 200:
print(f"Error status {resp.status}")
raise RuntimeError(f"Could not fetch manifest file: response status : {resp.status}")
self.manifest_cache = await resp.json()
return await resp.json(content_type=None)
async def fetch_manifest(self, session: aiohttp.ClientSession, url: str) -> Any:
async with session.get(url) as resp:
if resp.status != 200:
print(f"Error status {resp.status}")
raise RuntimeError(f"Could not fetch manifest file: response status : {resp.status}")
self.manifest_cache = await resp.json()
return await resp.json(content_type=None)

async def fetch_manifests(self) -> Any:
urls = self.settings.manifest_urls
responses = []
try:
async with aiohttp.ClientSession() as session:
responses.append(await asyncio.gather(*[self.fetch_manifest(session, url) for url in urls]))
except Exception as error:
logger.error(f"Unable to fetch manifest. {error}")

for response in responses:
print(response)
self.manifest_cache.extend(response)
return self.manifest_cache

async def get_configured_extensions(self) -> List[Extension]:
return cast(List[Extension], self.settings.extensions)
Expand Down
4 changes: 2 additions & 2 deletions core/services/kraken/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def is_valid(self) -> bool:

@app.get("/extensions_manifest", status_code=status.HTTP_200_OK)
@version(1, 0)
async def fetch_manifest() -> Any:
return await kraken.fetch_manifest()
async def fetch_manifests() -> Any:
return await kraken.fetch_manifests()


@app.get("/installed_extensions", status_code=status.HTTP_200_OK)
Expand Down
26 changes: 25 additions & 1 deletion core/services/kraken/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from typing import Any, Dict

from commonwealth.settings import settings
from pykson import BooleanField, JsonObject, ObjectListField, StringField
from pykson import BooleanField, JsonObject, ListField, ObjectListField, StringField

GITHUB_URL = "https://bluerobotics.github.io/BlueOS-Extensions-Repository/manifest.json"
BAZAAR_URL = "https://app.blueos.cloud/api/agent/bazaar/manifest/"


class Extension(JsonObject):
Expand Down Expand Up @@ -50,3 +53,24 @@ def migrate(self, data: Dict[str, Any]) -> None:
super().migrate(data)

data["VERSION"] = SettingsV1.VERSION


class SettingsV2(SettingsV1):
VERSION = 2
manifest_urls = ListField(StringField())

def __init__(self, *args: str, **kwargs: int) -> None:
super().__init__(*args, **kwargs)
self.VERSION = SettingsV2.VERSION
# this shold run in migrate() but migrate is never called ???
if len(self.manifest_urls) == 0:
self.manifest_urls.append(GITHUB_URL)

def migrate(self, data: Dict[str, Any]) -> None:
if data["VERSION"] == SettingsV2.VERSION:
return

if data["VERSION"] < SettingsV2.VERSION:
super().migrate(data)

data["VERSION"] = SettingsV2.VERSION

0 comments on commit 145d4a4

Please sign in to comment.