#!/usr/bin/env bash # sync_workbench.sh - Workbench-Raws auf die Programmschmiede spiegeln. # # Die Workbench ist nicht versioniert; die fertigen Assets liegen im Repo. # Dieses Skript hält die **Rohbilder** als Verteilungskopie auf dem FTP-Spiegel # und holt sie auf einem Rechner ohne Workbench zurück. # # Integritätsanker ist der SHA-256 aus art/workbench-manifest.json. Der # FTP-Spiegel ist eine Kopie, keine zweite Quelle der Wahrheit. # # tools/sync_workbench.sh --scan Manifest aus der Workbench schreiben # tools/sync_workbench.sh --check lokale Raws gegen das Manifest prüfen # tools/sync_workbench.sh --upload Raws hochladen (+ Manifest) # tools/sync_workbench.sh --download fehlende Raws vom FTP holen # tools/sync_workbench.sh --upload --dry-run # # Zugang: docs/credentials.txt (gitignored) oder TALORIA_CREDENTIALS. # Die Werte FTP_URL, FTP_USER, FTP_PASSWORD, FTP_PORT, FTP_VERIFY werden # gelesen; FTP_DIRECTORY wird ignoriert, das Ziel ist TALORIA_FTP_DIR # (Standard: taloria/workbench). set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" MANIFEST="$REPO_ROOT/art/workbench-manifest.json" WORKBENCH="${TALORIA_WORKBENCH:-$HOME/Dokumente/workbench_taloria}" RAW="$WORKBENCH/assets/raw" FTP_ASSET_DIR="${TALORIA_FTP_DIR:-taloria/workbench}" CREDENTIALS="${TALORIA_CREDENTIALS:-$REPO_ROOT/docs/credentials.txt}" if [[ ! -f "$CREDENTIALS" && -f "$HOME/repos/rinkplanner/docs/credentials.txt" ]]; then CREDENTIALS="$HOME/repos/rinkplanner/docs/credentials.txt" echo "Hinweis: nutze Zugang aus $CREDENTIALS" >&2 fi MODE="check" DRY=0 VERIFY=1 FORCE=0 ENV_FTP_HOST="${TALORIA_FTP_HOST:-}" ENV_FTP_PORT="${TALORIA_FTP_PORT:-}" ENV_FTP_USER="${TALORIA_FTP_USER:-}" ENV_FTP_PASSWORD="${TALORIA_FTP_PASSWORD:-}" ENV_FTP_VERIFY="${TALORIA_FTP_VERIFY:-}" FILE_FTP_URL="" FILE_FTP_USER="" FILE_FTP_PASSWORD="" FILE_FTP_VERIFY="" FILE_FTP_PORT="" usage() { cat <<'EOF' Aufruf: art/tools/sync_workbench.sh [--scan|--check|--upload|--download] [--dry-run] [--no-verify] [--force] --scan art/workbench-manifest.json aus assets/raw/ neu schreiben. --check Lokale Workbench gegen das Manifest prüfen (Standard). --upload Raws und Manifest auf den FTP-Spiegel laden. --download Manifest holen und fehlende Raws in die Workbench laden. --dry-run Nur zeigen, was passieren würde. --no-verify Nicht nach dem Upload herunterladen und hashen (schneller). --force Vorhandene Ziele mit abweichendem Hash überschreiben. Umgebung: TALORIA_WORKBENCH Workbench (Standard: ~/Dokumente/workbench_taloria) TALORIA_CREDENTIALS Zugangsdatei (Standard: /docs/credentials.txt) TALORIA_FTP_DIR Ziel auf dem Server (Standard: taloria/workbench) TALORIA_FTP_HOST/PORT/USER/PASSWORD/VERIFY überschreiben die Datei EOF } while [[ $# -gt 0 ]]; do case "$1" in --scan) MODE="scan" ;; --check) MODE="check" ;; --upload) MODE="upload" ;; --download) MODE="download" ;; --dry-run) DRY=1 ;; --no-verify) VERIFY=0 ;; --force) FORCE=1 ;; -h|--help) usage; exit 0 ;; *) echo "Unbekannte Option: $1" >&2; usage >&2; exit 2 ;; esac shift done # --- Manifest --------------------------------------------------------------- scan() { if [[ ! -d "$RAW" ]]; then echo "Workbench fehlt: $RAW" >&2 echo "Erst anlegen: python3 art/tools/setup.py workbench \"$WORKBENCH\"" >&2 exit 1 fi python3 - "$RAW" "$MANIFEST" "$FTP_ASSET_DIR" <<'PY' import hashlib import json import sys from pathlib import Path root = Path(sys.argv[1]) out = Path(sys.argv[2]) remote = sys.argv[3] files = [] for p in sorted(root.rglob("*")): if not p.is_file(): continue h = hashlib.sha256() with p.open("rb") as fh: for block in iter(lambda: fh.read(1 << 20), b""): h.update(block) files.append({ "path": str(p.relative_to(root)), "bytes": p.stat().st_size, "sha256": h.hexdigest(), }) doc = { "schema": 1, "root": "assets/raw", "remote": f"{remote}/raw", "files": files, } out.parent.mkdir(parents=True, exist_ok=True) out.write_text(json.dumps(doc, indent=2, ensure_ascii=False) + "\n") print(f"{out}: {len(files)} Dateien") PY } rows() { python3 - "$MANIFEST" <<'PY' import json import sys with open(sys.argv[1], encoding="utf-8") as fh: doc = json.load(fh) for e in doc.get("files", []): print("\t".join([e["path"], str(e["bytes"]), e["sha256"]])) PY } # --- Werkzeug --------------------------------------------------------------- file_size() { if stat -c '%s' "$1" >/dev/null 2>&1; then stat -c '%s' "$1" else stat -f '%z' "$1" fi } if command -v sha256sum >/dev/null 2>&1; then file_sha() { sha256sum "$1" | cut -d' ' -f1; } else file_sha() { shasum -a 256 "$1" | cut -d' ' -f1; } fi matches() { local path="$1" bytes="$2" sha="$3" [[ -f "$path" ]] || return 1 [[ "$(file_size "$path")" == "$bytes" ]] || return 1 [[ "$(file_sha "$path")" == "$sha" ]] } # --- FTP -------------------------------------------------------------------- REMOTE_READY=0 CURL_BASE=() read_creds() { [[ -f "$CREDENTIALS" ]] || return 0 while IFS= read -r line || [[ -n "$line" ]]; do line="${line%$'\r'}" [[ -z "$line" || "$line" == \#* || "$line" != *=* ]] && continue key="${line%%=*}"; value="${line#*=}" value="${value#\"}"; value="${value%\"}" value="${value#\'}"; value="${value%\'}" case "$key" in FTP_URL) FILE_FTP_URL="$value" ;; FTP_USER) FILE_FTP_USER="$value" ;; FTP_PASSWORD) FILE_FTP_PASSWORD="$value" ;; FTP_VERIFY) FILE_FTP_VERIFY="$value" ;; FTP_PORT) FILE_FTP_PORT="$value" ;; esac done < "$CREDENTIALS" } ensure_remote() { [[ "$REMOTE_READY" -eq 1 ]] && return read_creds case "$FILE_FTP_URL" in ftp://*) FILE_FTP_URL="${FILE_FTP_URL#ftp://}" ;; ftps://*) FILE_FTP_URL="${FILE_FTP_URL#ftps://}" ;; esac FILE_FTP_URL="${FILE_FTP_URL%%/*}" if [[ "$FILE_FTP_URL" == *:* ]]; then FILE_FTP_PORT="${FILE_FTP_URL##*:}" FILE_FTP_URL="${FILE_FTP_URL%:*}" fi FTP_HOST="${ENV_FTP_HOST:-$FILE_FTP_URL}" FTP_PORT="${ENV_FTP_PORT:-${FILE_FTP_PORT:-21}}" FTP_USER="${ENV_FTP_USER:-$FILE_FTP_USER}" FTP_PASSWORD="${ENV_FTP_PASSWORD:-$FILE_FTP_PASSWORD}" FTP_VERIFY="${ENV_FTP_VERIFY:-${FILE_FTP_VERIFY:-0}}" if [[ -z "$FTP_HOST" || -z "$FTP_USER" || -z "$FTP_PASSWORD" ]]; then echo "FTP-Zugang fehlt: $CREDENTIALS" >&2 echo "Setze TALORIA_CREDENTIALS oder TALORIA_FTP_*." >&2 exit 1 fi CURL_BASE=(--ssl-reqd --silent --show-error --connect-timeout 20 --max-time 900 --user "$FTP_USER:$FTP_PASSWORD") case "$FTP_VERIFY" in 1|true|TRUE|yes|YES) ;; *) CURL_BASE+=(--insecure) echo "Hinweis: TLS-Zertifikat wird nicht geprüft; Integrität sichert der SHA-256." >&2 ;; esac REMOTE_READY=1 } remote_url() { local p="${1#/}" printf 'ftp://%s:%s/%s/%s' "$FTP_HOST" "$FTP_PORT" "$FTP_ASSET_DIR" "$p" } remote_upload() { local p="$1" src="$2" curl "${CURL_BASE[@]}" --ftp-create-dirs --upload-file "$src" "$(remote_url "$p")" } remote_download() { local p="$1" dst="$2" curl "${CURL_BASE[@]}" --output "$dst" "$(remote_url "$p")" } # --- Modi ------------------------------------------------------------------- if [[ "$MODE" == "scan" ]]; then scan exit 0 fi if [[ ! -f "$MANIFEST" ]]; then echo "Manifest fehlt: $MANIFEST" >&2 echo "Erst scannen: art/tools/sync_workbench.sh --scan" >&2 exit 1 fi case "$MODE" in check) problems=0 while IFS=$'\t' read -r rel bytes sha; do if matches "$RAW/$rel" "$bytes" "$sha"; then echo " ok $rel" else if [[ ! -f "$RAW/$rel" ]]; then echo " FEHLT $rel" >&2 else echo " ABWEICHUNG $rel" >&2 fi problems=$((problems + 1)) fi done < <(rows) if [[ "$problems" -gt 0 ]]; then echo "" >&2 echo "$problems Datei(en) fehlen oder weichen ab." >&2 exit 1 fi echo "Workbench OK." ;; upload) ensure_remote problems=0 while IFS=$'\t' read -r rel bytes sha; do src="$RAW/$rel" if ! matches "$src" "$bytes" "$sha"; then echo " QUELLE FEHLT/FALSCH $rel" >&2 problems=$((problems + 1)) continue fi if [[ "$DRY" -eq 1 ]]; then echo " würde laden raw/$rel" continue fi echo " lade hoch raw/$rel" if ! remote_upload "raw/$rel" "$src"; then echo " UPLOAD FEHLGESCHLAGEN $rel" >&2 problems=$((problems + 1)) continue fi if [[ "$VERIFY" -eq 1 ]]; then tmp="$(mktemp "${TMPDIR:-/tmp}/taloria-raw.XXXXXX")" if remote_download "raw/$rel" "$tmp" && matches "$tmp" "$bytes" "$sha"; then rm -f "$tmp" else echo " KONTROLLE FEHLGESCHLAGEN $rel" >&2 rm -f "$tmp" problems=$((problems + 1)) fi fi done < <(rows) if [[ "$problems" -eq 0 ]]; then if [[ "$DRY" -eq 1 ]]; then echo " würde laden manifest.json" else echo " lade hoch manifest.json" remote_upload "manifest.json" "$MANIFEST" || problems=$((problems + 1)) fi fi if [[ "$problems" -gt 0 ]]; then echo "" >&2 echo "$problems Datei(en) nicht hochgeladen." >&2 exit 1 fi if [[ "$DRY" -eq 1 ]]; then echo "Dry-run: nichts hochgeladen." else echo "FTP-Spiegel OK: $FTP_ASSET_DIR" fi ;; download) ensure_remote tmp_manifest="$(mktemp "${TMPDIR:-/tmp}/taloria-manifest.XXXXXX")" if ! remote_download "manifest.json" "$tmp_manifest"; then rm -f "$tmp_manifest" echo "Remote-Manifest fehlt: $FTP_ASSET_DIR/manifest.json" >&2 exit 1 fi problems=0 while IFS=$'\t' read -r rel bytes sha; do dst="$RAW/$rel" if matches "$dst" "$bytes" "$sha"; then echo " ok $rel" continue fi if [[ -f "$dst" && "$FORCE" -ne 1 ]]; then echo " ABWEICHUNG $rel (--force zum Überschreiben)" >&2 problems=$((problems + 1)) continue fi if [[ "$DRY" -eq 1 ]]; then echo " würde holen raw/$rel" continue fi mkdir -p "$(dirname "$dst")" tmp="$(mktemp "${TMPDIR:-/tmp}/taloria-raw.XXXXXX")" if remote_download "raw/$rel" "$tmp" && matches "$tmp" "$bytes" "$sha"; then mv "$tmp" "$dst" echo " geholt $rel" else rm -f "$tmp" echo " DOWNLOAD FEHLGESCHLAGEN $rel" >&2 problems=$((problems + 1)) fi done < <(python3 - "$tmp_manifest" <<'PY' import json, sys with open(sys.argv[1], encoding="utf-8") as fh: doc = json.load(fh) for e in doc.get("files", []): print("\t".join([e["path"], str(e["bytes"]), e["sha256"]])) PY ) rm -f "$tmp_manifest" if [[ "$problems" -gt 0 ]]; then echo "" >&2 echo "$problems Datei(en) nicht geladen." >&2 exit 1 fi if [[ "$DRY" -eq 1 ]]; then echo "Dry-run: nichts geladen." else echo "Workbench ergänzt." fi ;; esac