Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support local files with matching filenames #123

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ A simplistic, opinionated remote update server implementing hawkBit™'s [DDI AP
### Installation

1. Install dependencies using [Poetry](https://python-poetry.org/):

```bash
poetry install
```
2. Launch gooseBit:

2. Create the database:

```bash
poetry run aerich upgrade
```

3. Launch gooseBit:
```bash
python main.py
```
Expand Down
10 changes: 8 additions & 2 deletions goosebit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib.metadata
from contextlib import asynccontextmanager
from logging import getLogger
from typing import Annotated

from fastapi import Depends, FastAPI
Expand All @@ -16,12 +17,17 @@
from goosebit.ui.static import static
from goosebit.ui.templates import templates

logger = getLogger(__name__)


@asynccontextmanager
async def lifespan(_: FastAPI):
await db.init()
db_ready = await db.init()
if not db_ready:
logger.exception("DB does not exist, try running `poetry run aerich upgrade`.")
await metrics.init()
yield
if db_ready:
yield
await db.close()


Expand Down
13 changes: 12 additions & 1 deletion goosebit/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from logging import getLogger

from tortoise import Tortoise
from tortoise.exceptions import OperationalError

from goosebit.db.config import TORTOISE_CONF
from goosebit.db.models import Device

logger = getLogger(__name__)


async def init():
async def init() -> bool:
await Tortoise.init(config=TORTOISE_CONF)
try:
await Device.first()
except OperationalError:
return False
return True


async def close():
Expand Down
20 changes: 5 additions & 15 deletions goosebit/updates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import shutil
from pathlib import Path
from urllib.parse import unquote, urlparse
Expand All @@ -10,6 +12,7 @@
from goosebit.db.models import Hardware, Software
from goosebit.updater.manager import UpdateManager

from ..settings import config
from . import swdesc


Expand Down Expand Up @@ -42,7 +45,8 @@ async def create_software_update(uri: str, temp_file: Path | None) -> Software:

# for local file: rename temp file to final name
if parsed_uri.scheme == "file":
path = _unique_path(parsed_uri)
filename = Path(url2pathname(unquote(parsed_uri.path))).name
path = config.artifacts_dir.joinpath(update_info["hash"], filename)
path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(temp_file, path)
uri = path.absolute().as_uri()
Expand Down Expand Up @@ -85,20 +89,6 @@ async def _is_software_colliding(update_info):
return is_colliding


def _unique_path(uri):
path = Path(url2pathname(unquote(uri.path)))
if not path.exists():
return path

counter = 1
new_path = path.with_name(f"{path.stem}-{counter}{path.suffix}")
while new_path.exists():
counter += 1
new_path = path.with_name(f"{path.stem}-{counter}{path.suffix}")

return new_path


async def generate_chunk(request: Request, updater: UpdateManager) -> list:
_, software = await updater.get_update()
if software is None:
Expand Down