- PHP-App unter hub/ für rinkplanner-hub.programmschmiede.de - MariaDB-Schema, Argon2id, Sessions, CSRF und Ratenbegrenzung - Einladungscodes, Admin-Bereich und Kür-Pakete mit Audiodatei - JSON-API v1 mit Bearer-Tokens für die Godot-App - Deployment und Erstinstallation über tools/deploy_hub.sh - Doku und Credentials-Vorlage ergänzt
234 lines
7.4 KiB
Bash
Executable file
234 lines
7.4 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Deployt den Rinkplanner-Hub auf rinkplanner-hub.programmschmiede.de.
|
|
#
|
|
# Aufruf:
|
|
# ./tools/deploy_hub.sh --check # Standard: Health, Sperren und Login-Seite prüfen
|
|
# ./tools/deploy_hub.sh --sync # Code + Konfiguration hochladen
|
|
# ./tools/deploy_hub.sh --install # Schema + Admin anlegen (einmalig)
|
|
# ./tools/deploy_hub.sh --all # sync + install + check
|
|
#
|
|
# Zugangsdaten kommen aus docs/credentials.txt (gitignored).
|
|
# Der Installer löscht sich nach Erfolg selbst; das Admin-Passwort wird nur
|
|
# per HTTPS übergeben und nie auf dem Server gespeichert.
|
|
set -euo pipefail
|
|
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
CREDENTIALS="$REPO_ROOT/docs/credentials.txt"
|
|
HUB_DIR="$REPO_ROOT/hub"
|
|
REMOTE_DIR="rinkplanner-hub.programmschmiede.de"
|
|
PUBLIC_URL="https://rinkplanner-hub.programmschmiede.de"
|
|
|
|
MODE="check"
|
|
FORCE=0
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--check) MODE="check" ;;
|
|
--sync) MODE="sync" ;;
|
|
--install) MODE="install" ;;
|
|
--all) MODE="all" ;;
|
|
-h|--help)
|
|
sed -n '2,16p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
|
|
exit 0
|
|
;;
|
|
*) echo "Unbekannte Option: $1" >&2; exit 2 ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
[[ -f "$CREDENTIALS" ]] || { echo "Fehlt: $CREDENTIALS" >&2; exit 1; }
|
|
|
|
cred() {
|
|
grep -m1 "^$1=" "$CREDENTIALS" | cut -d= -f2- | tr -d '\r'
|
|
}
|
|
|
|
FTP_URL="$(cred FTP_URL)"
|
|
FTP_USER="$(cred FTP_USER)"
|
|
FTP_PASSWORD="$(cred FTP_PASSWORD)"
|
|
DB_NAME="$(cred DB_NAME)"
|
|
DB_USER="$(cred DB_USER)"
|
|
DB_PASSWORD="$(cred DB_PASSWORD)"
|
|
HUB_ADMIN_USER="$(cred HUB_ADMIN_USER)"
|
|
HUB_ADMIN_PASSWORD="$(cred HUB_ADMIN_PASSWORD)"
|
|
|
|
HOST="${FTP_URL#ftp://}"
|
|
HOST="${HOST#ftps://}"
|
|
HOST="${HOST%%/*}"
|
|
HOST="${HOST%%:*}"
|
|
[[ -n "$HOST" && -n "$FTP_USER" ]] || { echo "FTP-Zugang unvollständig." >&2; exit 1; }
|
|
|
|
TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/rinkplanner-hub.XXXXXX")"
|
|
trap 'rm -rf "$TMP_DIR"' EXIT
|
|
|
|
CURL=(curl --ssl-reqd --insecure --silent --show-error --connect-timeout 20 --max-time 900 --user "$FTP_USER:$FTP_PASSWORD")
|
|
|
|
ftp_url() {
|
|
printf 'ftp://%s/%s/%s' "$HOST" "$REMOTE_DIR" "$1"
|
|
}
|
|
|
|
ftp_put() {
|
|
local local_path="$1" remote_path="$2"
|
|
"${CURL[@]}" --ftp-create-dirs -T "$local_path" "$(ftp_url "$remote_path")"
|
|
}
|
|
|
|
ftp_get() {
|
|
local remote_path="$1" local_path="$2"
|
|
"${CURL[@]}" -o "$local_path" "$(ftp_url "$remote_path")"
|
|
}
|
|
|
|
ftp_chmod() {
|
|
local mode="$1" remote_path="$2"
|
|
"${CURL[@]}" "ftp://$HOST/" -Q "SITE CHMOD $mode $REMOTE_DIR/$remote_path" >/dev/null || true
|
|
}
|
|
|
|
http_code() {
|
|
curl -sS -o /dev/null -w '%{http_code}' --max-time 30 "$1" || true
|
|
}
|
|
|
|
make_config() {
|
|
local token="$1"
|
|
HUB_INSTALL_TOKEN="$token" HUB_PUBLIC_URL="$PUBLIC_URL" \
|
|
DB_NAME="$DB_NAME" DB_USER="$DB_USER" DB_PASSWORD="$DB_PASSWORD" \
|
|
python3 - "$CREDENTIALS" "$TMP_DIR/config.local.php" <<'PY'
|
|
import os
|
|
import sys
|
|
|
|
def php(value):
|
|
return "'" + str(value).replace('\\', '\\\\').replace("'", "\\'") + "'"
|
|
|
|
path, target = sys.argv[1], sys.argv[2]
|
|
values = {}
|
|
with open(path, encoding='utf-8') as handle:
|
|
for line in handle:
|
|
line = line.rstrip('\n').rstrip('\r')
|
|
if '=' in line and not line.lstrip().startswith('#'):
|
|
key, value = line.split('=', 1)
|
|
values[key] = value
|
|
|
|
config = f"""<?php
|
|
declare(strict_types=1);
|
|
|
|
return [
|
|
'db' => [
|
|
'host' => 'localhost',
|
|
'port' => 3306,
|
|
'name' => {php(values.get('DB_NAME', ''))},
|
|
'user' => {php(values.get('DB_USER', ''))},
|
|
'password' => {php(values.get('DB_PASSWORD', ''))},
|
|
'charset' => 'utf8mb4',
|
|
],
|
|
'app' => [
|
|
'base_path' => '',
|
|
'public_url' => {php(os.environ['HUB_PUBLIC_URL'])},
|
|
'storage_path' => dirname(__DIR__) . '/storage',
|
|
'install_token' => {php(os.environ['HUB_INSTALL_TOKEN'])},
|
|
'max_routine_bytes' => 8 * 1024 * 1024,
|
|
'max_audio_bytes' => 50 * 1024 * 1024,
|
|
'session_idle_seconds' => 14 * 86400,
|
|
'session_absolute_seconds' => 30 * 86400,
|
|
'remember_seconds' => 30 * 86400,
|
|
'login_window_seconds' => 900,
|
|
'login_max_per_user' => 5,
|
|
'login_max_per_ip' => 10,
|
|
'uploads_per_hour' => 20,
|
|
],
|
|
];
|
|
"""
|
|
with open(target, 'w', encoding='utf-8') as handle:
|
|
handle.write(config)
|
|
os.chmod(target, 0o600)
|
|
PY
|
|
}
|
|
|
|
upload_tree() {
|
|
local source_root="$1" remote_prefix="$2"
|
|
while IFS= read -r file; do
|
|
local relative="${file#"$source_root"/}"
|
|
case "$relative" in
|
|
*.example.php|config.local.php|.DS_Store|*.swp) continue ;;
|
|
esac
|
|
ftp_put "$file" "$remote_prefix$relative"
|
|
echo " $remote_prefix$relative"
|
|
done < <(find "$source_root" -type f | sort)
|
|
}
|
|
|
|
do_sync() {
|
|
local token
|
|
token="$(openssl rand -hex 24)"
|
|
make_config "$token"
|
|
echo "Lade App-Code hoch …"
|
|
upload_tree "$HUB_DIR/app" "app/"
|
|
upload_tree "$HUB_DIR/views" "views/"
|
|
upload_tree "$HUB_DIR/sql" "sql/"
|
|
upload_tree "$HUB_DIR/web" ""
|
|
echo "Lade Konfiguration hoch …"
|
|
ftp_put "$TMP_DIR/config.local.php" "app/config.local.php"
|
|
ftp_chmod 600 "app/config.local.php"
|
|
echo "Sync fertig. Install-Token liegt nur auf dem Server."
|
|
}
|
|
|
|
install_if_needed() {
|
|
local config_file="$TMP_DIR/remote-config.php"
|
|
if ! ftp_get "app/config.local.php" "$config_file" 2>/dev/null; then
|
|
echo "Konfiguration fehlt auf dem Server; zuerst --sync ausführen." >&2
|
|
exit 1
|
|
fi
|
|
local token
|
|
token="$(python3 - "$config_file" <<'PY'
|
|
import re, sys
|
|
text = open(sys.argv[1], encoding='utf-8').read()
|
|
match = re.search(r"'install_token'\s*=>\s*'([^']+)'", text)
|
|
print(match.group(1) if match else '')
|
|
PY
|
|
)"
|
|
[[ -n "$token" ]] || { echo "Install-Token nicht gefunden." >&2; exit 1; }
|
|
|
|
ftp_put "$HUB_DIR/install.php" "install.php"
|
|
echo "Rufe Installer auf …"
|
|
local response status
|
|
response="$(curl -sS --max-time 300 -w '\n%{http_code}' \
|
|
--data-urlencode "token=$token" \
|
|
--data-urlencode "admin_user=$HUB_ADMIN_USER" \
|
|
--data-urlencode "admin_password=$HUB_ADMIN_PASSWORD" \
|
|
--data-urlencode "admin_display=$HUB_ADMIN_USER" \
|
|
"$PUBLIC_URL/install.php" 2>&1)" || true
|
|
status="${response##*$'\n'}"
|
|
local body="${response%$'\n'*}"
|
|
echo "$body"
|
|
"${CURL[@]}" "ftp://$HOST/" -Q "-DELE $REMOTE_DIR/install.php" >/dev/null 2>&1 || true
|
|
if [[ "$status" != "200" ]]; then
|
|
echo "Installation fehlgeschlagen (HTTP $status)." >&2
|
|
exit 1
|
|
fi
|
|
echo "Installation abgeschlossen."
|
|
}
|
|
|
|
do_check() {
|
|
local ok=0
|
|
local health
|
|
health="$(curl -sS --max-time 30 "$PUBLIC_URL/health" || true)"
|
|
if [[ "$health" == *"ok"* ]]; then
|
|
echo "Health: OK"
|
|
else
|
|
echo "Health: FEHLER ($health)" >&2
|
|
ok=1
|
|
fi
|
|
local login_code config_code storage_code install_code
|
|
login_code="$(http_code "$PUBLIC_URL/login")"
|
|
config_code="$(http_code "$PUBLIC_URL/app/config.local.php")"
|
|
storage_code="$(http_code "$PUBLIC_URL/storage/")"
|
|
install_code="$(http_code "$PUBLIC_URL/install.php")"
|
|
[[ "$login_code" == "200" ]] && echo "Login-Seite: OK" || { echo "Login-Seite: HTTP $login_code" >&2; ok=1; }
|
|
[[ "$config_code" == "403" || "$config_code" == "404" ]] && echo "Konfiguration gesperrt: OK" || { echo "Konfiguration erreichbar: HTTP $config_code" >&2; ok=1; }
|
|
[[ "$storage_code" == "403" || "$storage_code" == "404" ]] && echo "Storage gesperrt: OK" || { echo "Storage erreichbar: HTTP $storage_code" >&2; ok=1; }
|
|
[[ "$install_code" == "404" ]] && echo "Installer entfernt: OK" || echo "Hinweis: Installer liefert HTTP $install_code" >&2
|
|
if [[ "$ok" -ne 0 ]]; then
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
case "$MODE" in
|
|
sync) do_sync ;;
|
|
install) install_if_needed ;;
|
|
check) do_check ;;
|
|
all) do_sync; install_if_needed; do_check ;;
|
|
esac
|