Updating docker entrypoint to initialize the database

This commit is contained in:
2025-09-06 15:24:02 -04:00
parent 6d12ce25d3
commit 5eca8e08b1
5 changed files with 79 additions and 10 deletions

63
entrypoint.sh Normal file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/env bash
set -euo pipefail
DATA_DIR="${DOSFRONTEND_CONFIG_DIR:-/app/data}"
DB_PATH="${DATA_DIR}/roms.db"
# Make sure data dir exists & is writable for uid 1000
mkdir -p "$DATA_DIR" "$DATA_DIR/images" "$DATA_DIR/logs" "$DATA_DIR/roms" "$DATA_DIR/metadata"
# Attempt to fix perms when volume mounted as root
if [ "$(id -u)" -ne 0 ]; then
# we're not root; try a writable touch to detect perms
if ! touch "$DATA_DIR/.permcheck" 2>/dev/null; then
echo "WARNING: $DATA_DIR not writable by current user. Consider running as root or fixing volume ownership."
else
rm -f "$DATA_DIR/.permcheck"
fi
else
chown -R 1000:1000 "$DATA_DIR" || true
fi
# Initialize / migrate DB
if [ ! -f "$DB_PATH" ]; then
echo "No database found. Initializing…"
python /app/src/migrate.py init || true
fi
# Always try to move to latest
python /app/src/migrate.py upgrade || true
# Non-interactive admin creation (optional)
if [ "${DOSVAULT_ADMIN_USERNAME:-}" ] && [ "${DOSVAULT_ADMIN_EMAIL:-}" ] && [ "${DOSVAULT_ADMIN_PASSWORD:-}" ]; then
python - <<'PY'
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from os import getenv
from libs.config import Config
from libs.database import User_table, UserRole
from libs.auth import AuthManager
cfg=Config()
engine=create_engine(f"sqlite+pysqlite:///{cfg.database_path}")
Session=sessionmaker(bind=engine)
db=Session()
try:
existing = db.query(User_table).filter(User_table.role==UserRole.SUPER.value).first()
if not existing:
AuthManager.create_user(
db,
getenv("DOSVAULT_ADMIN_USERNAME"),
getenv("DOSVAULT_ADMIN_EMAIL"),
getenv("DOSVAULT_ADMIN_PASSWORD"),
UserRole.SUPER.value
)
print("Admin user created.")
else:
print(f"Admin already exists: {existing.username}")
finally:
db.close()
PY
fi
# Run app
exec python -m uvicorn src.webapp:app --host 0.0.0.0 --port 8080