From c2ca1361fee6562c25e7fe4fe5d309136dab4c9e Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 12 May 2025 12:43:17 +0200 Subject: [PATCH] Handle database connection vars --- api/.env.example | 2 +- docker-compose.dev-v5.yml | 188 ------------------------------------ docker-compose.dev.yml | 2 + scanner/.env.example | 2 +- scanner/scanner/__init__.py | 2 - scanner/scanner/database.py | 15 ++- 6 files changed, 17 insertions(+), 194 deletions(-) delete mode 100644 docker-compose.dev-v5.yml diff --git a/api/.env.example b/api/.env.example index 25721a6f..5a016a50 100644 --- a/api/.env.example +++ b/api/.env.example @@ -20,7 +20,7 @@ IMAGES_PATH=./images # https://www.postgresql.org/docs/current/libpq-envars.html PGUSER=kyoo PGPASSWORD=password -PGDATABASE=kyooDB +PGDATABASE=kyoo PGHOST=postgres PGPORT=5432 # PGOPTIONS=-c search_path=kyoo,public diff --git a/docker-compose.dev-v5.yml b/docker-compose.dev-v5.yml deleted file mode 100644 index bf29b057..00000000 --- a/docker-compose.dev-v5.yml +++ /dev/null @@ -1,188 +0,0 @@ -x-transcoder: &transcoder-base - build: - context: ./transcoder - dockerfile: Dockerfile.dev - networks: - default: - aliases: - - transcoder - ports: - - "7666:7666" - restart: on-failure - cpus: 1 - env_file: - - ./.env - environment: - - GOCODER_PREFIX=/video - volumes: - - ./transcoder:/app - - ${LIBRARY_ROOT}:/video:ro - - ${CACHE_ROOT}:/cache - - transcoder_metadata:/metadata - -services: - front: - build: - context: ./front - dockerfile: Dockerfile.dev - volumes: - - ./front:/app - - /app/.yarn - - /app/node_modules - - /app/apps/mobile/node_modules - - /app/apps/web/.next/ - - /app/apps/mobile/.expo/ - ports: - - "3000:3000" - - "8081:8081" - restart: on-failure - environment: - - KYOO_URL=${KYOO_URL:-http://api:5000/api} - labels: - - "traefik.enable=true" - - "traefik.http.routers.front.rule=PathPrefix(`/`)" - - auth: - build: - context: ./auth - dockerfile: Dockerfile.dev - restart: on-failure - depends_on: - postgres: - condition: service_healthy - ports: - - "4568:4568" - env_file: - - ./.env - environment: - - KEIBI_PREFIX=/auth - volumes: - - ./auth:/app - labels: - - "traefik.enable=true" - - "traefik.http.routers.auth.rule=PathPrefix(`/auth/`) || PathPrefix(`/.well-known/`)" - - api: - build: - context: ./api - dockerfile: Dockerfile.dev - restart: on-failure - depends_on: - postgres: - condition: service_healthy - volumes: - - ./api:/app - - /app/node_modules - ports: - - "3567:3567" - environment: - - KYOO_PREFIX=/api - - JWT_ISSUER=${PUBLIC_URL} - env_file: - - ./.env - labels: - - "traefik.enable=true" - - "traefik.http.routers.api.rule=PathPrefix(`/api/`) || PathPrefix(`/swagger`)" - - "traefik.http.routers.api.middlewares=phantom-token" - - "traefik.http.middlewares.phantom-token.forwardauth.address=http://auth:4568/auth/jwt" - - "traefik.http.middlewares.phantom-token.forwardauth.authRequestHeaders=Authorization,X-Api-Key" - - "traefik.http.middlewares.phantom-token.forwardauth.authResponseHeaders=Authorization" - - # scanner: - # build: ./scanner - # restart: on-failure - # depends_on: - # back: - # condition: service_healthy - # env_file: - # - ./.env - # environment: - # - KYOO_URL=${KYOO_URL:-http://back:5000/api} - # volumes: - # - ${LIBRARY_ROOT}:/video:ro - # - # matcher: - # build: ./scanner - # command: matcher - # restart: on-failure - # depends_on: - # back: - # condition: service_healthy - # env_file: - # - ./.env - # environment: - # - KYOO_URL=${KYOO_URL:-http://back:5000/api} - - transcoder: - <<: *transcoder-base - profiles: ['', 'cpu'] - - transcoder-nvidia: - <<: *transcoder-base - deploy: - resources: - reservations: - devices: - - capabilities: [gpu] - driver: cdi - device_ids: - - nvidia.com/gpu=all - environment: - - GOCODER_PREFIX=/video - - GOCODER_HWACCEL=nvidia - profiles: ['nvidia'] - - transcoder-vaapi: - <<: *transcoder-base - devices: - - /dev/dri:/dev/dri - environment: - - GOCODER_PREFIX=/video - - GOCODER_HWACCEL=vaapi - - GOCODER_VAAPI_RENDERER=${GOCODER_VAAPI_RENDERER:-/dev/dri/renderD128} - profiles: ['vaapi'] - # qsv is the same setup as vaapi but with the hwaccel env var different - transcoder-qsv: - <<: *transcoder-base - devices: - - /dev/dri:/dev/dri - environment: - - GOCODER_PREFIX=/video - - GOCODER_HWACCEL=qsv - - GOCODER_VAAPI_RENDERER=${GOCODER_VAAPI_RENDERER:-/dev/dri/renderD128} - profiles: ['qsv'] - - traefik: - image: traefik:v3.4 - restart: on-failure - command: - - "--providers.docker=true" - - "--providers.docker.exposedbydefault=false" - - "--entryPoints.web.address=:8901" - - "--accesslog=true" - ports: - - "8901:8901" - volumes: - - "/var/run/docker.sock:/var/run/docker.sock:ro" - - postgres: - image: postgres:15 - restart: on-failure - env_file: - - ./.env - volumes: - - db:/var/lib/postgresql/data - ports: - - "5432:5432" - environment: - - POSTGRES_HOST_AUTH_METHOD=trust - command: ["postgres", "-c", "log_statement=all"] - healthcheck: - test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] - interval: 5s - timeout: 5s - retries: 5 - -volumes: - db: - transcoder_metadata: diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 5e0290aa..f0b7618f 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -104,6 +104,8 @@ services: - "4389:4389" environment: - KYOO_URL=${KYOO_URL:-http://api:3567/api} + - JWKS_URL="http://auth:4568/.well-known/jwks.json" + - JWT_ISSUER=${PUBLIC_URL} volumes: - ./scanner:/app - ${LIBRARY_ROOT}:/video:ro diff --git a/scanner/.env.example b/scanner/.env.example index 4d2b6f35..15b1a847 100644 --- a/scanner/.env.example +++ b/scanner/.env.example @@ -20,6 +20,6 @@ JWT_ISSUER=$PUBLIC_URL # https://www.postgresql.org/docs/current/libpq-envars.html PGUSER=kyoo PGPASSWORD=password -PGDATABASE=kyooDB +PGDATABASE=kyoo PGHOST=postgres PGPORT=5432 diff --git a/scanner/scanner/__init__.py b/scanner/scanner/__init__.py index 821e6b7e..3904e104 100644 --- a/scanner/scanner/__init__.py +++ b/scanner/scanner/__init__.py @@ -17,7 +17,6 @@ logging.getLogger("rebulk").setLevel(logging.WARNING) @asynccontextmanager async def lifespan(_): - print("starting lifetime") async with ( init_pool(), get_db() as db, @@ -34,7 +33,6 @@ async def lifespan(_): # there's no way someone else used the same id, right? is_master = await db.fetchval("select pg_try_advisory_lock(198347)") if is_master: - print("this is master") _ = await asyncio.create_task(scanner.scan(remove_deleted=True)) _ = await asyncio.create_task(scanner.monitor()) yield diff --git a/scanner/scanner/database.py b/scanner/scanner/database.py index f4f47b2d..ba6566b6 100644 --- a/scanner/scanner/database.py +++ b/scanner/scanner/database.py @@ -1,5 +1,6 @@ +import os from contextlib import asynccontextmanager -from typing import cast +from typing import Any, cast from asyncpg import Connection, Pool, create_pool @@ -8,7 +9,17 @@ pool: Pool @asynccontextmanager async def init_pool(): - async with await create_pool() as p: + url = os.environ.get("POSTGRES_URL") + connection: dict[str, Any] = ( + { + "user": os.environ.get("PGUSER", "kyoo"), + "host": os.environ.get("PGHOST", "postgres"), + "password": os.environ.get("PGPASSWORD", "password"), + } + if url is None + else {"dns": url} + ) + async with await create_pool(**connection) as p: global pool pool = p yield