diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b69d41..4ab1784 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,8 +37,8 @@ jobs: - name: Vet run: go vet ./... - build-arm64: - name: ARM64 production build + build-amd64: + name: Linux x64 production build runs-on: ubuntu-latest timeout-minutes: 20 steps: @@ -55,22 +55,36 @@ jobs: env: CGO_ENABLED: '0' GOOS: linux - GOARCH: arm64 - run: go build -trimpath -o /tmp/landwelle-portal-linux-arm64 . + GOARCH: amd64 + run: go build -trimpath -o /tmp/landwelle-portal-linux-amd64 . - name: Build migrator env: CGO_ENABLED: '0' GOOS: linux - GOARCH: arm64 - run: go build -trimpath -o /tmp/landwelle-migrate-linux-arm64 ./cmd/migrate + GOARCH: amd64 + run: go build -trimpath -o /tmp/landwelle-migrate-linux-amd64 ./cmd/migrate - name: Build adminpass env: CGO_ENABLED: '0' GOOS: linux - GOARCH: arm64 - run: go build -trimpath -o /tmp/landwelle-adminpass-linux-arm64 ./cmd/adminpass + GOARCH: amd64 + run: go build -trimpath -o /tmp/landwelle-adminpass-linux-amd64 ./cmd/adminpass + - name: Verify x64 ELF binaries + run: | + for name in portal migrate adminpass; do + path="/tmp/landwelle-${name}-linux-amd64" + file "$path" | grep -q 'ELF 64-bit.*x86-64' + go version -m "$path" | grep -q 'GOARCH=amd64' + done + + - name: Build from production x64 scripts + run: | + ./scripts/build-x64.sh + ./scripts/build-migrator-x64.sh + file dist/landwelle-portal-linux-amd64 | grep -q 'x86-64' + ./dist/landwelle-portal-linux-amd64 --version | grep -q 'Landwelle v' frontend-static: name: JavaScript and deployment-script checks @@ -96,7 +110,7 @@ jobs: run: | trap 'rm -f destination_one_ci_sentinel.go' EXIT printf 'package main\n' > destination_one_ci_sentinel.go - if ./scripts/build-pi.sh > /tmp/landwelle-build-guard.log 2>&1; then + if ./scripts/build-x64.sh > /tmp/landwelle-build-guard.log 2>&1; then echo "Build unexpectedly accepted an untracked Go source" >&2 exit 1 fi diff --git a/Dockerfile b/Dockerfile index fe5716e..1fab380 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,23 @@ -FROM golang:1.23-alpine AS build +# Build complete Go portal for Linux x64; does not depend on the builder CPU. +FROM --platform=$BUILDPLATFORM golang:1.24.4-bookworm AS build WORKDIR /src -COPY go.mod main.go ./ -COPY web ./web -RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/landwelle-portal . +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v1 \ + go build -trimpath -ldflags="-s -w" -o /out/landwelle-portal . -FROM scratch -COPY --from=build /out/landwelle-portal /landwelle-portal +# psql and ffmpeg are required at runtime by the current application. +FROM debian:bookworm-slim +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + ca-certificates tzdata postgresql-client ffmpeg && \ + rm -rf /var/lib/apt/lists/* && \ + useradd --system --uid 10001 --home-dir /var/lib/landwelle --shell /usr/sbin/nologin landwelle && \ + install -d -o landwelle -g landwelle -m 0750 /var/lib/landwelle +COPY --from=build /out/landwelle-portal /usr/local/bin/landwelle-portal +ENV ADDR=:2200 LANDWELLE_STATE_DIR=/var/lib/landwelle +USER landwelle +VOLUME ["/var/lib/landwelle"] EXPOSE 2200 -ENTRYPOINT ["/landwelle-portal"] +ENTRYPOINT ["/usr/local/bin/landwelle-portal"] diff --git a/architecture_x64_v1575_test.go b/architecture_x64_v1575_test.go new file mode 100644 index 0000000..604964f --- /dev/null +++ b/architecture_x64_v1575_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "os" + "strings" + "testing" +) +func TestLinuxX64ReleaseBuildAndDeployScripts(t *testing.T) { + for _, path := range []string{"scripts/build-x64.sh","scripts/build-migrator-x64.sh","scripts/update-portal-on-x64.sh","scripts/update-migrations-on-x64.sh"} { + b, err := os.ReadFile(path) + if err != nil {t.Fatal(err)} + s := string(b) + if !strings.Contains(s, "linux-amd64") || !strings.Contains(s,"x86_64") { + t.Fatalf("%s is not configured for Linux x64",path) + } + if strings.Contains(s,"GOARCH=arm64") {t.Errorf("%s still builds ARM64",path)} + } +} +func TestLinuxX64ReleaseCI(t *testing.T) { + for path, tokens := range map[string][]string{ + ".github/workflows/ci.yml":{"build-amd64:", "GOARCH: amd64", "Linux x64 production build"}, + "scripts/migrate.sh":{"TARGET_ARCH=\"amd64\"","TARGET_ARCH=\"arm64\""}, + "Dockerfile":{"GOARCH=amd64","postgresql-client","ffmpeg"}, + } { + content,err := os.ReadFile(path) + if err != nil {t.Fatal(err)} + for _, token := range tokens { + if !strings.Contains(string(content),token) {t.Errorf("%s: missing %q",path,token)} + } + } +} diff --git a/main.go b/main.go index aba0dd0..6700570 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ const ( apiVersion = "v1" apiContractVersion = "1.0.0" applicationName = "Landwelle Radio" - applicationVersion = "1.5.74" + applicationVersion = "1.5.75" ) var publicTemplateFiles = []string{ diff --git a/main_test.go b/main_test.go index b3cae5f..579e664 100644 --- a/main_test.go +++ b/main_test.go @@ -181,7 +181,7 @@ func TestWeatherHelpers(t *testing.T) { } func TestApplicationVersion(t *testing.T) { - if applicationVersion != "1.5.74" { + if applicationVersion != "1.5.75" { t.Fatalf("unexpected application version %q", applicationVersion) } } diff --git a/scripts/build-migrator-x64.sh b/scripts/build-migrator-x64.sh new file mode 100755 index 0000000..56115c5 --- /dev/null +++ b/scripts/build-migrator-x64.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$ROOT" + +if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then + echo "Für diesen x64-Schritt ist ein Linux-x86_64-Rechner erforderlich." >&2 + exit 1 +fi +mkdir -p dist +# Build darf go.mod/go.sum nicht automatisch verändern. +go mod download +go mod verify + +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v1 \ + go build -trimpath -ldflags="-s -w" \ + -o dist/landwelle-migrate-linux-amd64 ./cmd/migrate + +echo "Erstellt: dist/landwelle-migrate-linux-amd64" diff --git a/scripts/build-x64.sh b/scripts/build-x64.sh new file mode 100755 index 0000000..368d267 --- /dev/null +++ b/scripts/build-x64.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +set -euo pipefail +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +mkdir -p "$ROOT_DIR/dist" +cd "$ROOT_DIR" + +if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then + echo "Für diesen x64-Schritt ist ein Linux-x86_64-Rechner erforderlich." >&2 + exit 1 +fi + +# Go compiles all .go files in the package directory, including untracked files. +# A stale local source (e.g. destination_one.go) can break an otherwise green CI. +# Refuse to build instead of silently deleting potentially valuable local work. +if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + EXTRA_GO="$(git ls-files --others --exclude-standard -- '*.go' | grep -E '^[^/]+\.go$' || true)" + if [[ -n "$EXTRA_GO" ]]; then + printf 'Build abgebrochen: nicht versionierte Go-Dateien im Projektverzeichnis:\n%s\n' "$EXTRA_GO" >&2 + echo "Bitte Dateien sichern und aus dem Projektverzeichnis verschieben, bevor du neu baust." >&2 + exit 1 + fi +fi + +# Startkritische ServeMux-Routen vor dem Build statisch prüfen. +# contentPages() registriert seine Slugs später dynamisch als "/"+slug. Deshalb +# reicht ein grep nach wörtlichen HandleFunc-Aufrufen nicht aus: Ein Slug darf +# nicht gleichzeitig explizit in main.go registriert sein. +EXPLICIT_PUBLIC_ROUTES="$(grep -oE 'mux\.HandleFunc\("/[^"]+"' main.go | sed -E 's/.*HandleFunc\("([^"]+)"/\1/' | sort -u)" +CONTENT_PAGE_SLUGS="$(sed -n '/^func contentPages()/,/^}/p' content.go | sed -nE 's/^[[:space:]]*"([^"]+)"[[:space:]]*:.*/\1/p')" +while IFS= read -r slug; do + [[ -z "$slug" ]] && continue + if grep -Fxq "/$slug" <<<"$EXPLICIT_PUBLIC_ROUTES"; then + printf 'Build abgebrochen: Route /%s wird sowohl explizit als auch dynamisch über contentPages() registriert.\n' "$slug" >&2 + exit 1 + fi +done <<<"$CONTENT_PAGE_SLUGS" + +# Alte Build-Artefakte vor einem neuen Build entfernen. So kann ein fehlgeschlagener +# Build niemals versehentlich ein altes Portal-Binary für das Update stehen lassen. +rm -f "$ROOT_DIR/dist/landwelle-portal-linux-amd64" "$ROOT_DIR/dist/landwelle-adminpass-linux-amd64" +go mod download +go mod verify +COMMIT="$(git rev-parse --short=12 HEAD 2>/dev/null || printf unknown)" +BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)" +LDFLAGS="-s -w -X main.buildCommit=$COMMIT -X main.buildTime=$BUILD_TIME" +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v1 go build -trimpath -ldflags="$LDFLAGS" -o dist/landwelle-portal-linux-amd64 . +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v1 go build -trimpath -ldflags="-s -w" -o dist/landwelle-adminpass-linux-amd64 ./cmd/adminpass +SOURCE_VERSION="$(sed -nE 's/^[[:space:]]*applicationVersion[[:space:]]*=[[:space:]]*"([^"]+)".*/\1/p' main.go | head -n1)" +BUILT_VERSION="$("$ROOT_DIR/dist/landwelle-portal-linux-amd64" --version 2>/dev/null || true)" +if [[ -z "$SOURCE_VERSION" || "$BUILT_VERSION" != "Landwelle v$SOURCE_VERSION" ]]; then + printf 'Build abgebrochen: Binary-Version passt nicht zum Quellstand (Quelle: %s, Binary: %s).\n' "${SOURCE_VERSION:-unbekannt}" "${BUILT_VERSION:-unbekannt}" >&2 + rm -f "$ROOT_DIR/dist/landwelle-portal-linux-amd64" + exit 1 +fi +printf '%s\n' "x64-Portal erstellt: $ROOT_DIR/dist/landwelle-portal-linux-amd64" "Version: $BUILT_VERSION" "Commit: $COMMIT" "Build-Zeit: $BUILD_TIME" diff --git a/scripts/migrate.sh b/scripts/migrate.sh index 5390b2b..be6e3a5 100755 --- a/scripts/migrate.sh +++ b/scripts/migrate.sh @@ -4,7 +4,14 @@ set -euo pipefail PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$PROJECT_DIR" -MIGRATOR="${MIGRATOR:-$PROJECT_DIR/dist/landwelle-migrate-linux-arm64}" +if [[ -z "${MIGRATOR:-}" ]]; then + case "$(uname -m)" in + x86_64) TARGET_ARCH="amd64" ;; + aarch64|arm64) TARGET_ARCH="arm64" ;; + *) echo "Nicht unterstützte CPU-Architektur: $(uname -m)" >&2; exit 1 ;; + esac + MIGRATOR="$PROJECT_DIR/dist/landwelle-migrate-linux-$TARGET_ARCH" +fi ENV_FILE="${ENV_FILE:-/etc/landwelle/landwelle.env}" if [[ ! -x "$MIGRATOR" ]]; then diff --git a/scripts/setup-admin-on-x64.sh b/scripts/setup-admin-on-x64.sh new file mode 100755 index 0000000..dbd9b97 --- /dev/null +++ b/scripts/setup-admin-on-x64.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +set -euo pipefail + +ENV_FILE="/etc/landwelle/landwelle.env" +HASHER="/opt/landwelle/landwelle-adminpass" +ADMIN_USER="${LANDWELLE_ADMIN_USER:-admin}" + +if [[ $EUID -ne 0 ]]; then + echo "Bitte mit sudo starten: sudo ./scripts/setup-admin-on-x64.sh" >&2 + exit 1 +fi +if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then + echo "Für diesen x64-Schritt ist ein Linux-x86_64-Rechner erforderlich." >&2 + exit 1 +fi +if [[ ! -x "$HASHER" ]]; then + echo "Passwortwerkzeug fehlt: $HASHER. Installiere zuerst v11 mit update-portal-on-x64.sh." >&2 + exit 1 +fi + +read -rsp "Neues Admin-Passwort (mindestens 12 Zeichen): " PASSWORD +echo +read -rsp "Admin-Passwort wiederholen: " PASSWORD_REPEAT +echo +if [[ "$PASSWORD" != "$PASSWORD_REPEAT" ]]; then + echo "Die Passwörter stimmen nicht überein." >&2 + exit 1 +fi + +HASH=$(LANDWELLE_NEW_ADMIN_PASSWORD="$PASSWORD" "$HASHER") +unset PASSWORD PASSWORD_REPEAT + +install -d -o root -g root -m 750 /etc/landwelle +touch "$ENV_FILE" +TMP=$(mktemp) +grep -vE '^(LANDWELLE_ADMIN_USER|LANDWELLE_ADMIN_PASSWORD|LANDWELLE_ADMIN_PASSWORD_HASH)=' "$ENV_FILE" > "$TMP" || true +{ + cat "$TMP" + echo + echo "# Landwelle Adminbereich" + echo "LANDWELLE_ADMIN_USER=$ADMIN_USER" + echo "LANDWELLE_ADMIN_PASSWORD_HASH=$HASH" + echo "LANDWELLE_ADMIN_SESSION_TTL=12h" +} > "$ENV_FILE" +rm -f "$TMP" +chown root:root "$ENV_FILE" +chmod 600 "$ENV_FILE" + +systemctl restart landwelle-radio +for _ in {1..20}; do + if curl -fsS http://127.0.0.1:2200/health >/dev/null 2>&1; then + echo "Adminzugang ist eingerichtet: /admin" + exit 0 + fi + sleep 1 +done + +echo "Dienst wurde nicht rechtzeitig bereit. Logs:" >&2 +journalctl -u landwelle-radio -n 80 --no-pager >&2 +exit 1 diff --git a/scripts/update-migrations-on-x64.sh b/scripts/update-migrations-on-x64.sh new file mode 100755 index 0000000..ea1fd83 --- /dev/null +++ b/scripts/update-migrations-on-x64.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +set -euo pipefail + +if [[ ${EUID} -ne 0 ]]; then + echo "Bitte mit sudo starten: sudo ./scripts/update-migrations-on-x64.sh" >&2 + exit 1 +fi + +if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then + echo "Für diesen x64-Schritt ist ein Linux-x86_64-Rechner erforderlich." >&2 + exit 1 +fi +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +if [[ ! -f "$ROOT/dist/landwelle-migrate-linux-amd64" ]]; then + echo "Migrator fehlt: zuerst ./scripts/build-migrator-x64.sh starten." >&2 + exit 1 +fi +TARGET="${LANDWELLE_DIR:-/opt/landwelle}" +SERVICE="${LANDWELLE_SERVICE:-landwelle-radio}" +APP_USER="${LANDWELLE_USER:-landwelle}" +APP_GROUP="${LANDWELLE_GROUP:-landwelle}" +ENV_FILE="${LANDWELLE_ENV_FILE:-/etc/landwelle/landwelle.env}" + +if ! id "$APP_USER" >/dev/null 2>&1; then + echo "Systembenutzer $APP_USER existiert nicht." >&2 + exit 1 +fi + +if [[ ! -f "$ENV_FILE" ]]; then + echo "Konfiguration fehlt: $ENV_FILE" >&2 + exit 1 +fi + +if ! command -v psql >/dev/null 2>&1; then + echo "PostgreSQL-Client wird einmalig installiert ..." + apt-get update + DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql-client +fi + +install -d -o root -g root -m 755 "$TARGET/dist" "$TARGET/scripts" +install -d -o "$APP_USER" -g "$APP_GROUP" -m 750 \ + "$TARGET/migrations" "$TARGET/migrations/applied" + +install -o root -g root -m 755 \ + "$ROOT/dist/landwelle-migrate-linux-amd64" \ + "$TARGET/dist/landwelle-migrate-linux-amd64" + +install -o root -g root -m 755 \ + "$ROOT/scripts/migrate.sh" \ + "$TARGET/scripts/migrate.sh" + +# Bereinigt die beiden kurzzeitig falsch nummerierten Landwelle-Zuhause-Dateien. +# 0039 und 0040 sind im produktiven Stand bereits durch andere Fachmigrationen belegt. +# Die konsolidierte Zuhause-Navigation verwendet deshalb die bereits eingeführte Nummer 9000. +rm -f \ + "$ROOT/migrations/0039_landwelle_zuhause_navigation.sql" \ + "$ROOT/migrations/0040_landwelle_zuhause_navigation.sql" \ + "$TARGET/migrations/0039_landwelle_zuhause_navigation.sql" \ + "$TARGET/migrations/0040_landwelle_zuhause_navigation.sql" + +shopt -s nullglob +migration_files=("$ROOT"/migrations/*.sql) +if (( ${#migration_files[@]} > 0 )); then + for migration in "${migration_files[@]}"; do + install -o "$APP_USER" -g "$APP_GROUP" -m 640 \ + "$migration" "$TARGET/migrations/$(basename "$migration")" + done +fi + +# Der Dienstbenutzer braucht Leserechte auf die Environment-Datei und +# Ausführungsrecht auf allen übergeordneten Verzeichnissen, um sie öffnen zu können. +ENV_DIR="$(dirname "$ENV_FILE")" +install -d -o root -g "$APP_GROUP" -m 750 "$ENV_DIR" +chown root:"$APP_GROUP" "$ENV_FILE" +chmod 640 "$ENV_FILE" + +if ! runuser -u "$APP_USER" -- test -r "$ENV_FILE"; then + echo "Environment-Datei ist für $APP_USER nicht lesbar: $ENV_FILE" >&2 + echo "Berechtigungen:" >&2 + namei -l "$ENV_FILE" >&2 || true + exit 1 +fi + +if ! grep -q '^DATABASE_URL=' "$ENV_FILE"; then + echo "DATABASE_URL fehlt in $ENV_FILE" >&2 + exit 1 +fi + +echo "Starte PostgreSQL-Migrationen ..." +runuser -u "$APP_USER" -- \ + "$TARGET/scripts/migrate.sh" \ + --env-file "$ENV_FILE" \ + --dir "$TARGET/migrations" \ + --archive-dir "$TARGET/migrations/applied" + +if systemctl list-unit-files "$SERVICE.service" >/dev/null 2>&1; then + systemctl restart "$SERVICE" + systemctl --no-pager --full status "$SERVICE" || true +fi + +echo +echo "Fertig. Erfolgreiche SQL-Dateien liegen unter:" +echo " $TARGET/migrations/applied" diff --git a/scripts/update-portal-on-x64.sh b/scripts/update-portal-on-x64.sh new file mode 100755 index 0000000..b0ccbc0 --- /dev/null +++ b/scripts/update-portal-on-x64.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env bash +set -euo pipefail + +PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +SOURCE_BINARY="$PROJECT_DIR/dist/landwelle-portal-linux-amd64" +SOURCE_ADMINPASS="$PROJECT_DIR/dist/landwelle-adminpass-linux-amd64" +TARGET_DIR="/opt/landwelle" +TARGET_BINARY="$TARGET_DIR/landwelle-portal" +SOURCE_SERVICE="$PROJECT_DIR/deploy/landwelle-radio.service" +TARGET_SERVICE="/etc/systemd/system/landwelle-radio.service" +SERVICE="landwelle-radio" + +if [[ $EUID -ne 0 ]]; then + echo "Bitte mit sudo starten: sudo ./scripts/update-portal-on-x64.sh" >&2 + exit 1 +fi + +if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then + echo "Für diesen x64-Schritt ist ein Linux-x86_64-Rechner erforderlich." >&2 + exit 1 +fi + +if [[ ! -f "$SOURCE_BINARY" ]]; then + echo "x64-Binary fehlt: $SOURCE_BINARY" >&2 + exit 1 +fi +EXPECTED_VERSION="$($SOURCE_BINARY --version 2>/dev/null || true)" +if [[ -z "$EXPECTED_VERSION" ]]; then + echo "Version der x64-Binary konnte nicht ermittelt werden: $SOURCE_BINARY" >&2 + exit 1 +fi + +SOURCE_VERSION="$(sed -nE 's/^[[:space:]]*applicationVersion[[:space:]]*=[[:space:]]*"([^"]+)".*/\1/p' "$PROJECT_DIR/main.go" | head -n1)" +if [[ -z "$SOURCE_VERSION" ]]; then + echo "Quellversion konnte nicht aus $PROJECT_DIR/main.go ermittelt werden." >&2 + exit 1 +fi +if [[ "$EXPECTED_VERSION" != "Landwelle v$SOURCE_VERSION" ]]; then + echo "Update abgebrochen: dist-Binary ist veraltet oder stammt aus einem anderen Quellstand." >&2 + echo "Quellstand: Landwelle v$SOURCE_VERSION" >&2 + echo "dist-Binary: $EXPECTED_VERSION" >&2 + echo "Bitte zuerst ausführen: ./scripts/build-x64.sh" >&2 + exit 1 +fi + +if [[ ! -f "$SOURCE_ADMINPASS" ]]; then + echo "Admin-Passwortwerkzeug fehlt: $SOURCE_ADMINPASS" >&2 + exit 1 +fi + +install -d -o root -g root -m 755 "$TARGET_DIR" +install -d -o landwelle -g landwelle -m 750 /var/lib/landwelle + +BACKUP_BINARY="" +BACKUP_SERVICE="" +BACKUP_ADMINPASS="" +STAMP="$(date +%Y%m%d-%H%M%S)" +if [[ -f "$TARGET_BINARY" ]]; then + BACKUP_BINARY="$TARGET_BINARY.backup-$STAMP" + cp -a "$TARGET_BINARY" "$BACKUP_BINARY" +fi +if [[ -f "$TARGET_SERVICE" ]]; then + BACKUP_SERVICE="$TARGET_SERVICE.backup-$STAMP" + cp -a "$TARGET_SERVICE" "$BACKUP_SERVICE" +fi +if [[ -f "$TARGET_DIR/landwelle-adminpass" ]]; then + BACKUP_ADMINPASS="$TARGET_DIR/landwelle-adminpass.backup-$STAMP" + cp -a "$TARGET_DIR/landwelle-adminpass" "$BACKUP_ADMINPASS" +fi + +DEPLOY_STARTED=0 +rollback_if_needed() { + result=$? + trap - EXIT + if [[ "$result" -ne 0 && "$DEPLOY_STARTED" -eq 1 ]]; then + echo "Update fehlgeschlagen (Exit-Code $result). Starte automatisches Rollback." >&2 + set +e + systemctl stop "$SERVICE" + if [[ -n "$BACKUP_BINARY" && -f "$BACKUP_BINARY" ]]; then + install -o root -g root -m 755 "$BACKUP_BINARY" "$TARGET_BINARY" + else + rm -f "$TARGET_BINARY" + fi + if [[ -n "$BACKUP_ADMINPASS" && -f "$BACKUP_ADMINPASS" ]]; then + install -o root -g root -m 755 "$BACKUP_ADMINPASS" "$TARGET_DIR/landwelle-adminpass" + fi + if [[ -n "$BACKUP_SERVICE" && -f "$BACKUP_SERVICE" ]]; then + install -o root -g root -m 644 "$BACKUP_SERVICE" "$TARGET_SERVICE" + else + rm -f "$TARGET_SERVICE" + fi + rm -f "$TARGET_BINARY.new" + systemctl daemon-reload + if [[ -n "$BACKUP_BINARY" && -f "$BACKUP_BINARY" ]]; then + systemctl start "$SERVICE" + if curl -fsS --max-time 5 http://127.0.0.1:2200/ready >/dev/null 2>&1; then + echo "Rollback erfolgreich: vorherige Version ist wieder bereit." >&2 + else + echo "ACHTUNG: Vorherige Version ist noch nicht bereit. Bitte 'journalctl -u landwelle-radio' prüfen." >&2 + fi + else + echo "ACHTUNG: Kein vorheriges Portal-Binary vorhanden. Manueller Eingriff erforderlich." >&2 + fi + fi + exit "$result" +} +trap rollback_if_needed EXIT +DEPLOY_STARTED=1 + +if [[ -f "$SOURCE_SERVICE" ]]; then + install -o root -g root -m 644 "$SOURCE_SERVICE" "$TARGET_SERVICE" + systemctl daemon-reload +fi + +systemctl stop "$SERVICE" +install -o root -g root -m 755 "$SOURCE_BINARY" "$TARGET_BINARY.new" +mv -f "$TARGET_BINARY.new" "$TARGET_BINARY" +install -o root -g root -m 755 "$SOURCE_ADMINPASS" "$TARGET_DIR/landwelle-adminpass" +systemctl start "$SERVICE" + +for _ in {1..20}; do + if curl -fsS --max-time 5 http://127.0.0.1:2200/ready >/dev/null 2>&1; then + ACTIVE_VERSION="$(curl -fsS --max-time 5 http://127.0.0.1:2200/version 2>/dev/null | tr -d '\r\n' || true)" + if [[ "$ACTIVE_VERSION" != "$EXPECTED_VERSION" ]]; then + echo "Versionsprüfung fehlgeschlagen: erwartet $EXPECTED_VERSION, aktiv $(printf '%s' "$ACTIVE_VERSION")" >&2 + journalctl -u "$SERVICE" -n 80 --no-pager >&2 || true + exit 1 + fi + DEPLOY_STARTED=0 + echo "$ACTIVE_VERSION ist aktiv: http://127.0.0.1:2200" + if ! grep -qE '^LANDWELLE_ADMIN_PASSWORD(_HASH)?=' /etc/landwelle/landwelle.env 2>/dev/null; then + echo "Adminzugang noch einrichten: sudo ./scripts/setup-admin-on-x64.sh" + fi + exit 0 + fi + sleep 1 +done + +echo "Readiness-Check fehlgeschlagen." >&2 +journalctl -u "$SERVICE" -n 80 --no-pager >&2 || true +exit 1