#!/usr/bin/env bash # OpenGenesisLINK Wiki v35 – Linux x64, Code-Wiki, Anfragen, SMTP und Rollback. set -Eeuo pipefail if (( EUID != 0 )); then echo "FEHLER: Bitte mit sudo bzw. als root ausführen." >&2 exit 1 fi ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" SRC="${1:-$ROOT_DIR/opengenesis-wiki-linux-amd64}" SERVICE_SRC="$ROOT_DIR/deploy/opengenesis-wiki.service" TARGET_DIR="/opt/opengenesis-wiki/bin" TARGET="$TARGET_DIR/opengenesis-wiki" SERVICE="/etc/systemd/system/opengenesis-wiki.service" SERVICE_NAME="opengenesis-wiki.service" DROPIN_DIR="/etc/systemd/system/opengenesis-wiki.service.d" DROPIN="$DROPIN_DIR/download-stats.conf" INQUIRY_DROPIN="$DROPIN_DIR/zz-inquiries.conf" BASE="http://127.0.0.1:18080" for tool in file curl systemctl install cp mv mktemp uname sha256sum; do command -v "$tool" >/dev/null 2>&1 || { echo "FEHLER: Programm fehlt: $tool" >&2; exit 1; } done [[ -s "$SRC" ]] || { echo "FEHLER: Binary fehlt oder ist leer: $SRC" >&2; exit 1; } [[ -f "$SERVICE_SRC" ]] || { echo "FEHLER: systemd-Unit fehlt: $SERVICE_SRC" >&2; exit 1; } [[ -f "$ROOT_DIR/deploy/opengenesis-wiki-inquiries.conf" ]] || { echo "FEHLER: Anfragen-Drop-in fehlt." >&2; exit 1; } # WICHTIG: Architekturfehler müssen vor systemctl stop und vor dem Binary-Tausch abbrechen. MACHINE="$(uname -m)" case "$MACHINE" in x86_64|amd64) ;; *) echo "FEHLER: Dieser Rechner ist $MACHINE; erforderlich ist AMD64/x86_64. Kein Austausch durchgeführt." >&2; exit 2 ;; esac if ! file "$SRC" | grep -Eq 'x86-64|x86_64'; then echo "FEHLER: Ungültiges Binary für AMD64/x86_64: $(file "$SRC")" >&2 exit 2 fi # Integrität des mitgelieferten Linux-x64-Binaries vor dem Dienststopp prüfen. if [[ "$SRC" == "$ROOT_DIR/opengenesis-wiki-linux-amd64" && -f "$ROOT_DIR/SHA256SUMS" ]]; then (cd "$ROOT_DIR" && sha256sum --status -c SHA256SUMS) || { echo 'FEHLER: Die SHA-256-Prüfsumme des x64-Binaries stimmt nicht. Kein Austausch durchgeführt.' >&2 exit 2 } fi if ! id opengenesis >/dev/null 2>&1; then useradd --system --home /opt/opengenesis-wiki --shell /usr/sbin/nologin opengenesis fi install -d -o root -g root -m 0755 "$TARGET_DIR" BACKUP_DIR="$(mktemp -d "$TARGET_DIR/.update-backup-XXXXXX")" UPDATED=0 SUCCEEDED=0 WAS_ACTIVE=0 systemctl is-active --quiet "$SERVICE_NAME" && WAS_ACTIVE=1 || true if [[ -f "$TARGET" ]]; then cp -p "$TARGET" "$BACKUP_DIR/binary"; fi if [[ -f "$SERVICE" ]]; then cp -p "$SERVICE" "$BACKUP_DIR/unit"; fi if [[ -f "$INQUIRY_DROPIN" ]]; then cp -p "$INQUIRY_DROPIN" "$BACKUP_DIR/zz-inquiries.conf"; fi finalize() { local code=$? trap - EXIT if (( UPDATED == 1 && SUCCEEDED == 0 )); then echo "FEHLER: Update/Prüfung fehlgeschlagen; vorherige Dateien werden wiederhergestellt." >&2 systemctl stop "$SERVICE_NAME" >/dev/null 2>&1 || true if [[ -f "$BACKUP_DIR/binary" ]]; then cp -p "$BACKUP_DIR/binary" "$TARGET" else rm -f "$TARGET" fi if [[ -f "$BACKUP_DIR/unit" ]]; then cp -p "$BACKUP_DIR/unit" "$SERVICE" else rm -f "$SERVICE" fi if [[ -f "$BACKUP_DIR/zz-inquiries.conf" ]]; then install -d -o root -g root -m 0755 "$DROPIN_DIR" cp -p "$BACKUP_DIR/zz-inquiries.conf" "$INQUIRY_DROPIN" else rm -f -- "$INQUIRY_DROPIN" fi rm -f -- "$TARGET.new" systemctl daemon-reload || true if (( WAS_ACTIVE == 1 )); then systemctl restart "$SERVICE_NAME" || true fi journalctl -u "$SERVICE_NAME" -n 20 --no-pager >&2 || true fi [[ -z "${BACKUP_DIR:-}" ]] || rm -rf -- "$BACKUP_DIR" exit "$code" } trap finalize EXIT printf 'Architektur: %s | Binary: %s\n' "$MACHINE" "$(file -b "$SRC")" # Ausfallfenster verkleinern: neue Binary vor dem Stoppen sicher ablegen. install -o root -g root -m 0755 "$SRC" "$TARGET.new" UPDATED=1 systemctl stop "$SERVICE_NAME" 2>/dev/null || true mv -f "$TARGET.new" "$TARGET" # Vorhandene angepasste Service-Datei erhalten, wenn sie bereits das Ziel-Binary startet. if [[ ! -f "$SERVICE" ]] || ! grep -Fq "ExecStart=$TARGET" "$SERVICE"; then install -o root -g root -m 0644 "$SERVICE_SRC" "$SERVICE" fi # The inbox and unsent messages live in a private systemd-managed state dir. # Leave the previous download counter untouched as an archival data file. # An optional root-only SMTP environment file is never overwritten. install -d -o root -g root -m 0755 "$DROPIN_DIR" install -d -o root -g root -m 0700 /etc/opengenesis-wiki install -o root -g root -m 0644 "$ROOT_DIR/deploy/opengenesis-wiki-inquiries.conf" "$INQUIRY_DROPIN" systemctl daemon-reload systemctl enable "$SERVICE_NAME" >/dev/null systemctl restart "$SERVICE_NAME" READY=0 for i in {1..30}; do if systemctl is-active --quiet "$SERVICE_NAME" && curl --max-time 2 -fsS "$BASE/healthz" >/dev/null 2>&1; then READY=1 break fi sleep 0.5 done (( READY == 1 )) || { echo "FEHLER: Go-Wiki startet nicht oder /healthz ist nicht erreichbar." >&2; exit 1; } # Technische Routen prüfen statt redaktioneller Texte (die bei Branding-Änderungen wechseln). # Keine externen GitHub-API-Anfragen als Voraussetzung des lokalen Deployments. for path in /de/ /en/ /de/code/ /en/code/ /de/code/fixes /en/code/fixes \ /de/docs/server-download /en/docs/server-download \ /de/docs/license /en/docs/license /api/project/status \ /wiki-static/css/app.css /wiki-static/js/app.js /sitemap.xml; do if ! curl --max-time 10 -fsS -o /dev/null "$BASE$path"; then echo "FEHLER: Lokale Wiki-Route funktioniert nicht: $path" >&2 exit 1 fi echo "OK: $path" done [[ "$(curl --max-time 5 -fsS "$BASE/healthz")" == "ok" ]] || { echo 'FEHLER: /healthz ist nicht ok.' >&2; exit 1; } # Former public source/download interfaces must return HTTP 410. for path in /viewgit/ /download/server/latest.zip /download/server/main.zip /api/downloads/stats /api/releases/latest; do CODE="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 5 "$BASE$path")" [[ "$CODE" == "410" ]] || { echo "FEHLER: Alte Download-/Code-Route nicht gesperrt: $path ($CODE)" >&2; exit 1; } done CODE="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 5 "$BASE/api/inquiries")" [[ "$CODE" == "405" ]] || { echo "FEHLER: Produktanfrage-API antwortet mit $CODE statt 405 auf GET" >&2; exit 1; } PAGE="$BACKUP_DIR/home.html" SITEMAP_FILE="$BACKUP_DIR/sitemap.xml" curl --max-time 10 -fsS -o "$PAGE" "$BASE/de/" curl --max-time 10 -fsS -o "$SITEMAP_FILE" "$BASE/sitemap.xml" grep -Fq 'id="inquiryDialog"' "$PAGE" || { echo 'FEHLER: Anfragemodal fehlt auf der Startseite.' >&2; exit 1; } grep -Fq 'info@opengenesislink.de' "$PAGE" || { echo 'FEHLER: Kontaktadresse fehlt.' >&2; exit 1; } grep -Fq 'https://opengenesislink.de/de/' "$SITEMAP_FILE" || { echo 'FEHLER: Sitemap-Domain unerwartet.' >&2; exit 1; } grep -Fq 'https://opengenesislink.de/de/code/fixes' "$SITEMAP_FILE" || { echo 'FEHLER: Neue Code-Dokumentation fehlt in Sitemap.' >&2; exit 1; } SUCCEEDED=1 printf '\nOpenGenesisLINK Wiki erfolgreich aktualisiert.\n' systemctl --no-pager --full status "$SERVICE_NAME" || true printf 'Lokale Wiki: %s/de/\nCode-Dokumentation: %s/de/code/\nProduktanfrage: %s/de/docs/server-download\nSMTP-Konfiguration (optional): /etc/opengenesis-wiki/inquiry-smtp.env\n' "$BASE" "$BASE" "$BASE"