Files
Khaemhat/docker/entrypoint.sh
T
Alby96andClaude Opus 5.5 12277733f5
CI / Build and test (SQLite) (pull_request) Successful in 43s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 15s
CI / Docker image (pull_request) Successful in 59s
feat: bootstrap solution, Docker image and Gitea pipelines (M0)
Start development with the skeleton every later milestone builds on, so
that code, image and release path exist and are verified from day one.

- Khaemhat.sln with the ten projects of SPEC section 3 and six MSTest
  projects; shared build rules with warnings as errors, analyzers and
  Central Package Management restricted to the allowlist.
- Architecture tests and scripts/check-packages.sh fail on any project
  reference or package that SPEC does not allow.
- Khaemhat.Web serves an Italian "under construction" page with the
  official icons and version, plus /health/live, /health/ready and
  /health (ADR 0012); honours KHAEMHAT_URLS and KHAEMHAT_LOG_LEVEL.
- docker/Dockerfile (non-root app user aligned to PUID/PGID by the
  entrypoint, pg_dump 17, healthcheck, OCI and Unraid labels) and
  compose files for PostgreSQL and a standalone SQLite demo.
- Gitea Actions: ci.yml (build, tests, image smoke test, PostgreSQL
  16/17 on tags, YAML check), release.yml (image and release from a
  vX.Y.Z tag, skipping versions already published) and wiki.yml.
- Development scripts, VS Code launch/tasks for debugging, and ADR
  0012-0017 deciding the M0 open points of docs/STATUS.md.

Closes #1, #2, #3, #4, #5, #114

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:53:39 +01:00

85 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Container entrypoint (docs/DEPLOY.md §2.4, ADR 0013, ADR 0014). It only prepares the user, waits for the system
# database and starts the app: migrations and bootstrap live in the .NET code.
set -euo pipefail
APP_DLL=/app/Khaemhat.Web.dll
DATA_DIR="${KHAEMHAT_DATA_DIR:-/data}"
DB_WAIT_SECONDS=120
log() { printf '%s khaemhat-entrypoint: %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*" >&2; }
fail() { log "ERRORE: $*"; exit 1; }
# The system database URL, from KHAEMHAT_SYSTEM_DB or KHAEMHAT_SYSTEM_DB_FILE (ADR 0014). Only host and port are used.
system_db() {
if [[ -n "${KHAEMHAT_SYSTEM_DB:-}" && -n "${KHAEMHAT_SYSTEM_DB_FILE:-}" ]]; then
fail "KHAEMHAT_SYSTEM_DB e KHAEMHAT_SYSTEM_DB_FILE sono entrambe impostate: usane una sola."
fi
if [[ -n "${KHAEMHAT_SYSTEM_DB_FILE:-}" ]]; then
[[ -r "$KHAEMHAT_SYSTEM_DB_FILE" ]] || fail "KHAEMHAT_SYSTEM_DB_FILE: file non leggibile ($KHAEMHAT_SYSTEM_DB_FILE)."
head -n 1 "$KHAEMHAT_SYSTEM_DB_FILE" | tr -d '\r\n'
else
printf '%s' "${KHAEMHAT_SYSTEM_DB:-}"
fi
}
# Waits until the PostgreSQL server of the system database accepts connections. SQLite needs no wait.
wait_for_database() {
local url host port rest deadline
url="$(system_db)"
case "$url" in
postgres://* | postgresql://*) ;;
*) return 0 ;;
esac
rest="${url#*://}" # user:pwd@host:port/db?x
rest="${rest##*@}" # host:port/db?x
rest="${rest%%/*}" # host:port
rest="${rest%%\?*}"
host="${rest%%:*}"
port=5432
[[ "$rest" == *:* ]] && port="${rest##*:}"
[[ -n "$host" ]] || fail "KHAEMHAT_SYSTEM_DB: host mancante."
deadline=$((SECONDS + DB_WAIT_SECONDS))
until pg_isready -q -h "$host" -p "$port" -t 3; do
if (( SECONDS >= deadline )); then
fail "il database di sistema su $host:$port non risponde dopo ${DB_WAIT_SECONDS}s."
fi
log "attendo il database di sistema su $host:$port..."
sleep 2
done
log "database di sistema raggiungibile su $host:$port."
}
# Aligns the "app" user with PUID/PGID and gives it /data (ADR 0013). Only when started as root.
prepare_user() {
local puid="${PUID:-$(id -u app)}" pgid="${PGID:-$(id -g app)}"
[[ "$puid" =~ ^[0-9]+$ && "$pgid" =~ ^[0-9]+$ ]] || fail "PUID e PGID devono essere numeri (PUID=$puid, PGID=$pgid)."
(( puid != 0 && pgid != 0 )) || fail "PUID e PGID non possono essere 0: l'applicazione non gira come root."
if [[ "$(id -g app)" != "$pgid" ]]; then
groupmod -o -g "$pgid" app
fi
if [[ "$(id -u app)" != "$puid" ]]; then
usermod -o -u "$puid" app
fi
mkdir -p "$DATA_DIR"
if [[ "$(stat -c '%u:%g' "$DATA_DIR")" != "$puid:$pgid" ]]; then
log "assegno $DATA_DIR a $puid:$pgid..."
chown -R "$puid:$pgid" "$DATA_DIR"
fi
}
wait_for_database
if [[ "$(id -u)" == "0" ]]; then
prepare_user
log "avvio Khaemhat come utente app ($(id -u app):$(id -g app))."
exec setpriv --reuid=app --regid=app --init-groups -- dotnet "$APP_DLL" "$@"
fi
log "avvio Khaemhat come utente $(id -u):$(id -g)."
exec dotnet "$APP_DLL" "$@"