diff --git a/webapps_migration/migrate_superx.conf.sam b/webapps_migration/migrate_superx.conf.sam deleted file mode 100644 index 6c37ac1..0000000 --- a/webapps_migration/migrate_superx.conf.sam +++ /dev/null @@ -1,89 +0,0 @@ -# migrate_superx.conf -# Konfiguration fuer die Migration einer bestehenden SuperX-Installation -# in eine Tomcat-Webapp-Struktur. -# -# Beide Dateien muessen im selben Verzeichnis liegen: -# migrate_superx.conf -# migrate_superx_webapp.sh -# -# Aufruf: -# ./migrate_superx_webapp.sh -# -# Produktiv mit Rechte-/Owner-Anpassung meist: -# sudo ./migrate_superx_webapp.sh -# -# Testlauf: -# DRY_RUN="true" - -# Bestehende SQL_ENV der alten Installation. -SQL_ENV="/home/superx/db/bin/SQL_ENV" - -# Migrationsmodus: -# full: -# - kopiert die bestehende Webapp nach TARGET_WEBAPP -# - kopiert oder verschiebt db nach TARGET_WEBAPP/WEB-INF/conf/edustore/db -# db_only: -# - kopiert keine Webapp -# - verwendet die bestehende WEBAPP aus der SQL_ENV als Ziel, wenn TARGET_WEBAPP="auto" -# - kopiert oder verschiebt nur db nach WEBAPP/WEB-INF/conf/edustore/db -MIGRATION_MODE="full" - -# Uebertragung des DB-Verzeichnisses: -# copy: db wird per rsync kopiert, Quelle bleibt erhalten. -# move: db wird per mv verschoben, Quelle ist danach nicht mehr am alten Ort. -# Bei move darf TARGET_DB noch nicht existieren. -DB_TRANSFER_MODE="copy" - -# Ziel-Webapp-Verzeichnis. -# Bei MIGRATION_MODE=full muss hier ein konkreter Pfad stehen. -# Bei MIGRATION_MODE=db_only kann TARGET_WEBAPP="auto" gesetzt werden. -TARGET_WEBAPP="/var/lib/tomcat/webapps/superx" - -# Benutzer/Gruppe fuer die Zielstruktur. -TOMCAT_USER="tomcat" -SUPERX_GROUP="superx" - -# Optionaler Tomcat-Service. -TOMCAT_SERVICE="tomcat10" -STOP_TOMCAT="false" -START_TOMCAT="false" - -# rsync-Verhalten fuer Webapp-Kopie und DB-Kopie. -DELETE_TARGET="false" - -# Wenn Ziel-Webapp bereits existiert, trotzdem weitermachen? -ALLOW_EXISTING_TARGET="true" - -# Wenn Ziel-DB bereits existiert: -# Bei DB_TRANSFER_MODE=copy erlaubt true das Ergaenzen/Ueberschreiben per rsync. -# Bei DB_TRANSFER_MODE=move darf Ziel-DB nie existieren. -ALLOW_EXISTING_TARGET_DB="true" - -# Testlauf ohne Aenderungen. -DRY_RUN="false" - -# Gruppen-/User-Verwaltung. -ADD_TOMCAT_TO_GROUP="true" -CREATE_GROUP_IF_MISSING="false" - -# SQL_ENV im Ziel anpassen. -UPDATE_SQL_ENV="true" - -# Rechte nach dem Kopieren/Verschieben setzen. -SET_RIGHTS="true" - -# Owner/Gruppe setzen. -# auto: root setzt chown; gleicher User mit passender Gruppe ueberspringt chown; sonst Abbruch. -# true: chown immer versuchen. -# false: chown nie ausfuehren. -SET_OWNER="auto" - -# chmod setzen. -SET_CHMOD="true" - -# Sicherheitspruefung fuer bereits migrierte Struktur. -# Nur im Notfall auf true setzen. -FORCE_ALREADY_MIGRATED="false" - -# Optional: zusaetzliche Ausgabe. -VERBOSE="true" diff --git a/webapps_migration/migrate_superx_webapp.sh b/webapps_migration/migrate_superx_webapp.sh index 41a3af1..3115677 100644 --- a/webapps_migration/migrate_superx_webapp.sh +++ b/webapps_migration/migrate_superx_webapp.sh @@ -1,29 +1,15 @@ #!/usr/bin/env bash # migrate_superx_webapp.sh +# Version 1.5 # -# Migriert eine bestehende SuperX-Installation in eine neue Tomcat-Webapp-Struktur. -# -# Erwartete Dateien im selben Verzeichnis: -# migrate_superx.conf -# migrate_superx_webapp.sh -# -# Modi: -# MIGRATION_MODE=full -# Webapp wird nach TARGET_WEBAPP kopiert. -# db wird nach TARGET_WEBAPP/WEB-INF/conf/edustore/db kopiert oder verschoben. -# -# MIGRATION_MODE=db_only -# Webapp wird nicht kopiert. -# db wird in die bestehende oder angegebene Webapp unter WEB-INF/conf/edustore/db kopiert oder verschoben. -# -# DB_TRANSFER_MODE: -# copy: db wird per rsync kopiert. -# move: db wird per mv verschoben. Ziel-db darf dabei noch nicht existieren. +# Migriert eine bestehende SuperX-Installation in eine neue Webapp-Struktur. +# Standardziel: /home/superx/webapps/superx +# Optional: Symlink aus Tomcat-webapps auf TARGET_WEBAPP. set -euo pipefail SCRIPT_NAME="$(basename "$0")" -SCRIPT_VERSION="1.4" +SCRIPT_VERSION="1.5" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CONFIG_FILE="$SCRIPT_DIR/migrate_superx.conf" @@ -31,48 +17,22 @@ log() { echo "[$SCRIPT_NAME] $*"; } warn() { echo "[$SCRIPT_NAME] WARNUNG: $*" >&2; } fail() { echo "[$SCRIPT_NAME] FEHLER: $*" >&2; exit 1; } -is_true() { - case "${1:-false}" in true|TRUE|yes|YES|ja|JA|1) return 0 ;; *) return 1 ;; esac -} - -is_false() { - case "${1:-false}" in false|FALSE|no|NO|nein|NEIN|0|"") return 0 ;; *) return 1 ;; esac -} - -is_auto() { - case "${1:-}" in auto|AUTO|Auto) return 0 ;; *) return 1 ;; esac -} - +is_true() { case "${1:-false}" in true|TRUE|yes|YES|ja|JA|1) return 0;; *) return 1;; esac; } +is_false() { case "${1:-false}" in false|FALSE|no|NO|nein|NEIN|0|"") return 0;; *) return 1;; esac; } +is_auto() { case "${1:-}" in auto|AUTO|Auto) return 0;; *) return 1;; esac; } is_root() { [ "$(id -u)" -eq 0 ]; } current_user() { id -un; } user_exists() { id "$1" >/dev/null 2>&1; } group_exists() { getent group "$1" >/dev/null 2>&1; } - -user_in_group() { - local user="$1" - local group="$2" - id -nG "$user" 2>/dev/null | tr ' ' '\n' | grep -qx "$group" -} - -current_user_matches_target() { - [ "$(current_user)" = "$TOMCAT_USER" ] && user_in_group "$(current_user)" "$SUPERX_GROUP" -} - -canon_path() { - if command -v realpath >/dev/null 2>&1; then - realpath -m "$1" - else - printf '%s\n' "$1" - fi -} +user_in_group() { id -nG "$1" 2>/dev/null | tr ' ' '\n' | grep -qx "$2"; } +current_user_matches_target() { [ "$(current_user)" = "$WEBAPP_USER" ] && user_in_group "$(current_user)" "$WEBAPP_GROUP"; } +canon_path() { if command -v realpath >/dev/null 2>&1; then realpath -m "$1"; else printf '%s\n' "$1"; fi; } run() { if is_true "${DRY_RUN:-false}"; then echo "[DRY-RUN] $*" else - if is_true "${VERBOSE:-false}"; then - echo "+ $*" - fi + if is_true "${VERBOSE:-false}"; then echo "+ $*"; fi eval "$@" fi } @@ -82,40 +42,38 @@ usage() { Nutzung: ./$SCRIPT_NAME -Hinweis: - Die Datei migrate_superx.conf muss im selben Verzeichnis liegen wie dieses Skript. - -Migrationsmodi: - MIGRATION_MODE=full - MIGRATION_MODE=db_only +Die Datei migrate_superx.conf muss im selben Verzeichnis liegen wie dieses Skript. +Erwartet: $CONFIG_FILE -DB-Uebertragung: - DB_TRANSFER_MODE=copy - DB_TRANSFER_MODE=move +MIGRATION_MODE: full | db_only +DB_TRANSFER_MODE: copy | move USAGE } load_config() { [ -f "$CONFIG_FILE" ] || { usage; fail "Config-Datei nicht gefunden: $CONFIG_FILE"; } - # shellcheck disable=SC1090 . "$CONFIG_FILE" : "${SQL_ENV:?SQL_ENV ist nicht gesetzt}" : "${TARGET_WEBAPP:?TARGET_WEBAPP ist nicht gesetzt}" - : "${TOMCAT_USER:?TOMCAT_USER ist nicht gesetzt}" - : "${SUPERX_GROUP:?SUPERX_GROUP ist nicht gesetzt}" + : "${WEBAPP_USER:?WEBAPP_USER ist nicht gesetzt}" + : "${WEBAPP_GROUP:?WEBAPP_GROUP ist nicht gesetzt}" MIGRATION_MODE="${MIGRATION_MODE:-full}" DB_TRANSFER_MODE="${DB_TRANSFER_MODE:-copy}" STOP_TOMCAT="${STOP_TOMCAT:-false}" START_TOMCAT="${START_TOMCAT:-false}" TOMCAT_SERVICE="${TOMCAT_SERVICE:-tomcat10}" + CREATE_TOMCAT_SYMLINK="${CREATE_TOMCAT_SYMLINK:-false}" + TOMCAT_WEBAPPS_DIR="${TOMCAT_WEBAPPS_DIR:-/var/lib/tomcat10/webapps}" + TOMCAT_CONTEXT_NAME="${TOMCAT_CONTEXT_NAME:-superx}" + REPLACE_EXISTING_SYMLINK="${REPLACE_EXISTING_SYMLINK:-false}" DELETE_TARGET="${DELETE_TARGET:-false}" ALLOW_EXISTING_TARGET="${ALLOW_EXISTING_TARGET:-true}" ALLOW_EXISTING_TARGET_DB="${ALLOW_EXISTING_TARGET_DB:-true}" DRY_RUN="${DRY_RUN:-false}" - ADD_TOMCAT_TO_GROUP="${ADD_TOMCAT_TO_GROUP:-true}" + ADD_WEBAPP_USER_TO_GROUP="${ADD_WEBAPP_USER_TO_GROUP:-true}" CREATE_GROUP_IF_MISSING="${CREATE_GROUP_IF_MISSING:-false}" UPDATE_SQL_ENV="${UPDATE_SQL_ENV:-true}" SET_RIGHTS="${SET_RIGHTS:-true}" @@ -124,68 +82,56 @@ load_config() { FORCE_ALREADY_MIGRATED="${FORCE_ALREADY_MIGRATED:-false}" VERBOSE="${VERBOSE:-true}" - case "$MIGRATION_MODE" in full|db_only) ;; *) fail "Ungueltiger Wert fuer MIGRATION_MODE: $MIGRATION_MODE. Erlaubt: full, db_only." ;; esac - case "$DB_TRANSFER_MODE" in copy|move) ;; *) fail "Ungueltiger Wert fuer DB_TRANSFER_MODE: $DB_TRANSFER_MODE. Erlaubt: copy, move." ;; esac - case "$SET_OWNER" in auto|AUTO|Auto|true|TRUE|yes|YES|ja|JA|1|false|FALSE|no|NO|nein|NEIN|0) ;; *) fail "Ungueltiger Wert fuer SET_OWNER: $SET_OWNER. Erlaubt: auto, true, false." ;; esac + case "$MIGRATION_MODE" in full|db_only) ;; *) fail "Ungueltiger MIGRATION_MODE: $MIGRATION_MODE";; esac + case "$DB_TRANSFER_MODE" in copy|move) ;; *) fail "Ungueltiger DB_TRANSFER_MODE: $DB_TRANSFER_MODE";; esac + case "$SET_OWNER" in auto|AUTO|Auto|true|TRUE|yes|YES|ja|JA|1|false|FALSE|no|NO|nein|NEIN|0) ;; *) fail "Ungueltiger SET_OWNER: $SET_OWNER";; esac } validate_basic_paths() { [ -f "$SQL_ENV" ] || fail "SQL_ENV nicht gefunden: $SQL_ENV" - if [ "$MIGRATION_MODE" = "full" ]; then [ "$TARGET_WEBAPP" != "auto" ] || fail "TARGET_WEBAPP=auto ist bei MIGRATION_MODE=full nicht erlaubt." fi - if [ "$TARGET_WEBAPP" != "auto" ]; then case "$TARGET_WEBAPP" in - ""|"/"|"/var"|"/var/lib"|"/var/lib/tomcat"|"/var/lib/tomcat/webapps"|"/srv"|"/srv/tomcat"|"/srv/tomcat/webapps"|"/home"|"/tmp") - fail "TARGET_WEBAPP ist zu allgemein/gefaehrlich: $TARGET_WEBAPP" - ;; + ""|"/"|"/var"|"/var/lib"|"/var/lib/tomcat"|"/var/lib/tomcat/webapps"|"/var/lib/tomcat10"|"/var/lib/tomcat10/webapps"|"/srv"|"/srv/tomcat"|"/srv/tomcat/webapps"|"/home"|"/tmp") + fail "TARGET_WEBAPP ist zu allgemein/gefaehrlich: $TARGET_WEBAPP";; esac fi + if is_true "$CREATE_TOMCAT_SYMLINK"; then + [ -n "$TOMCAT_WEBAPPS_DIR" ] || fail "TOMCAT_WEBAPPS_DIR ist leer." + [ -n "$TOMCAT_CONTEXT_NAME" ] || fail "TOMCAT_CONTEXT_NAME ist leer." + case "$TOMCAT_CONTEXT_NAME" in */*|.*|"") fail "TOMCAT_CONTEXT_NAME ist ungueltig: $TOMCAT_CONTEXT_NAME";; esac + fi } check_source_not_already_migrated() { - local superx_real webapp_real - superx_real="$(canon_path "$SOURCE_SUPERX_DIR")" - webapp_real="$(canon_path "$SOURCE_WEBAPP")" - - SOURCE_SUPERX_DIR_REAL="$superx_real" - SOURCE_WEBAPP_REAL="$webapp_real" - - case "$superx_real" in - "$webapp_real"/*) + SOURCE_SUPERX_DIR_REAL="$(canon_path "$SOURCE_SUPERX_DIR")" + SOURCE_WEBAPP_REAL="$(canon_path "$SOURCE_WEBAPP")" + case "$SOURCE_SUPERX_DIR_REAL" in + "$SOURCE_WEBAPP_REAL"/*) if is_true "$FORCE_ALREADY_MIGRATED"; then - warn "SUPERX_DIR liegt bereits innerhalb von WEBAPP. FORCE_ALREADY_MIGRATED=true ist gesetzt, Migration wird trotzdem fortgesetzt." + warn "SUPERX_DIR liegt bereits innerhalb von WEBAPP. FORCE_ALREADY_MIGRATED=true, Migration wird trotzdem fortgesetzt." else - fail "SUPERX_DIR liegt bereits innerhalb von WEBAPP. Die Installation sieht bereits nach der neuen Webapp-Struktur aus. + fail "SUPERX_DIR liegt bereits innerhalb von WEBAPP. Die Installation sieht bereits migriert aus. SUPERX_DIR=$SOURCE_SUPERX_DIR WEBAPP=$SOURCE_WEBAPP -Realpfade: -SUPERX_DIR=$superx_real -WEBAPP=$webapp_real - -Migration wird abgebrochen. - -Nur wenn du die Risiken kennst und trotzdem migrieren willst: +Nur im Notfall uebersteuern mit: FORCE_ALREADY_MIGRATED=\"true\"" - fi - ;; + fi;; esac } load_old_sql_env() { # shellcheck disable=SC1090 . "$SQL_ENV" - [ -n "${SUPERX_DIR:-}" ] || fail "SUPERX_DIR wurde durch SQL_ENV nicht gesetzt." [ -n "${WEBAPP:-}" ] || fail "WEBAPP wurde durch SQL_ENV nicht gesetzt." SOURCE_SUPERX_DIR="$SUPERX_DIR" SOURCE_WEBAPP="$WEBAPP" - check_source_not_already_migrated if [ -d "$SOURCE_SUPERX_DIR/db/bin" ]; then @@ -198,12 +144,11 @@ load_old_sql_env() { SOURCE_DB="$SOURCE_SUPERX_DIR/db" SOURCE_TYPE="ALT_ODER_FULL_RELEASE" else - fail "DB-Verzeichnis konnte nicht erkannt werden. Erwartet: \$SUPERX_DIR/db/bin oder \$SUPERX_DIR/bin. SUPERX_DIR=$SOURCE_SUPERX_DIR" + fail "DB-Verzeichnis konnte nicht erkannt werden. SUPERX_DIR=$SOURCE_SUPERX_DIR" fi [ -d "$SOURCE_WEBAPP/WEB-INF" ] || fail "WEBAPP scheint keine SuperX-Webapp zu sein: $SOURCE_WEBAPP/WEB-INF fehlt." [ -d "$SOURCE_DB/bin" ] || fail "DB-bin-Verzeichnis fehlt: $SOURCE_DB/bin" - [ -f "$SOURCE_DB/bin/SQL_ENV" ] || warn "Quelle enthaelt keine SQL_ENV unter: $SOURCE_DB/bin/SQL_ENV" if [ "$MIGRATION_MODE" = "db_only" ] && [ "$TARGET_WEBAPP" = "auto" ]; then TARGET_WEBAPP="$SOURCE_WEBAPP" @@ -212,92 +157,62 @@ load_old_sql_env() { TARGET_SUPERX_DIR="$TARGET_WEBAPP/WEB-INF/conf/edustore" TARGET_DB="$TARGET_SUPERX_DIR/db" TARGET_SQL_ENV="$TARGET_DB/bin/SQL_ENV" - SOURCE_DB_REAL="$(canon_path "$SOURCE_DB")" TARGET_DB_REAL="$(canon_path "$TARGET_DB")" TARGET_WEBAPP_REAL="$(canon_path "$TARGET_WEBAPP")" + TOMCAT_SYMLINK_PATH="$TOMCAT_WEBAPPS_DIR/$TOMCAT_CONTEXT_NAME" } normalize_owner_mode() { EFFECTIVE_SET_OWNER="$SET_OWNER" - - if ! is_true "$SET_RIGHTS"; then EFFECTIVE_SET_OWNER="false"; return 0; fi - if is_false "$SET_OWNER"; then EFFECTIVE_SET_OWNER="false"; return 0; fi - + if ! is_true "$SET_RIGHTS" || is_false "$SET_OWNER"; then EFFECTIVE_SET_OWNER="false"; return 0; fi if is_auto "$SET_OWNER"; then if is_root; then EFFECTIVE_SET_OWNER="true"; return 0; fi if current_user_matches_target; then EFFECTIVE_SET_OWNER="false" - log "SET_OWNER=auto: chown wird uebersprungen, weil laufender User '$TOMCAT_USER' ist und zur Gruppe '$SUPERX_GROUP' gehoert." + log "SET_OWNER=auto: chown wird uebersprungen, weil laufender User '$WEBAPP_USER' ist und zur Gruppe '$WEBAPP_GROUP' gehoert." return 0 fi - EFFECTIVE_SET_OWNER="true" - return 0 + EFFECTIVE_SET_OWNER="true"; return 0 fi - if is_true "$SET_OWNER"; then EFFECTIVE_SET_OWNER="true"; return 0; fi } root_needed_reasons() { local reasons=() - - if is_true "$STOP_TOMCAT"; then reasons+=("STOP_TOMCAT=true"); fi - if is_true "$START_TOMCAT"; then reasons+=("START_TOMCAT=true"); fi - if is_true "$CREATE_GROUP_IF_MISSING" && ! group_exists "$SUPERX_GROUP"; then reasons+=("CREATE_GROUP_IF_MISSING=true und Gruppe '$SUPERX_GROUP' fehlt"); fi - - if is_true "$ADD_TOMCAT_TO_GROUP"; then - if user_exists "$TOMCAT_USER" && group_exists "$SUPERX_GROUP"; then - if ! user_in_group "$TOMCAT_USER" "$SUPERX_GROUP"; then - reasons+=("ADD_TOMCAT_TO_GROUP=true und '$TOMCAT_USER' ist noch nicht in Gruppe '$SUPERX_GROUP'") - fi - fi + is_true "$STOP_TOMCAT" && reasons+=("STOP_TOMCAT=true") + is_true "$START_TOMCAT" && reasons+=("START_TOMCAT=true") + is_true "$CREATE_GROUP_IF_MISSING" && ! group_exists "$WEBAPP_GROUP" && reasons+=("CREATE_GROUP_IF_MISSING=true und Gruppe '$WEBAPP_GROUP' fehlt") + if is_true "$ADD_WEBAPP_USER_TO_GROUP" && user_exists "$WEBAPP_USER" && group_exists "$WEBAPP_GROUP" && ! user_in_group "$WEBAPP_USER" "$WEBAPP_GROUP"; then + reasons+=("ADD_WEBAPP_USER_TO_GROUP=true und '$WEBAPP_USER' ist noch nicht in Gruppe '$WEBAPP_GROUP'") fi - - if is_true "$SET_RIGHTS" && is_true "$EFFECTIVE_SET_OWNER"; then reasons+=("chown auf '$TOMCAT_USER:$SUPERX_GROUP'"); fi - - if [ "${#reasons[@]}" -gt 0 ]; then printf '%s\n' "${reasons[@]}"; return 0; fi + is_true "$SET_RIGHTS" && is_true "$EFFECTIVE_SET_OWNER" && reasons+=("chown auf '$WEBAPP_USER:$WEBAPP_GROUP'") + if is_true "$CREATE_TOMCAT_SYMLINK" && [ -d "$TOMCAT_WEBAPPS_DIR" ] && [ ! -w "$TOMCAT_WEBAPPS_DIR" ]; then + reasons+=("CREATE_TOMCAT_SYMLINK=true und TOMCAT_WEBAPPS_DIR ist nicht beschreibbar: $TOMCAT_WEBAPPS_DIR") + fi + [ "${#reasons[@]}" -gt 0 ] && printf '%s\n' "${reasons[@]}" && return 0 return 1 } validate_privileges() { normalize_owner_mode - if is_root; then return 0; fi - + is_root && return 0 local reasons if reasons="$(root_needed_reasons)"; then - cat >&2 </dev/null 2>&1 || fail "systemctl nicht gefunden, kann Tomcat nicht stoppen." - run "systemctl stop '$TOMCAT_SERVICE'" + [ "$TARGET_WEBAPP" = "$SOURCE_WEBAPP" ] && [ "$MIGRATION_MODE" = "full" ] && fail "TARGET_WEBAPP darf bei MIGRATION_MODE=full nicht identisch mit SOURCE_WEBAPP sein." + [ -e "$TARGET_WEBAPP" ] && [ ! -d "$TARGET_WEBAPP" ] && fail "TARGET_WEBAPP existiert, ist aber kein Verzeichnis: $TARGET_WEBAPP" + [ "$MIGRATION_MODE" = "full" ] && [ -d "$TARGET_WEBAPP/WEB-INF" ] && ! is_true "$ALLOW_EXISTING_TARGET" && fail "Ziel-Webapp existiert bereits: $TARGET_WEBAPP" + [ "$SOURCE_DB_REAL" = "$TARGET_DB_REAL" ] && fail "SOURCE_DB und TARGET_DB sind identisch." + case "$TARGET_DB_REAL" in "$SOURCE_DB_REAL"/*) fail "TARGET_DB liegt innerhalb von SOURCE_DB.";; esac + case "$SOURCE_DB_REAL" in "$TARGET_DB_REAL"/*) fail "SOURCE_DB liegt innerhalb von TARGET_DB.";; esac + [ "$DB_TRANSFER_MODE" = "move" ] && [ -e "$TARGET_DB" ] && fail "DB_TRANSFER_MODE=move, aber TARGET_DB existiert bereits: $TARGET_DB" + [ "$DB_TRANSFER_MODE" = "copy" ] && [ -e "$TARGET_DB" ] && ! is_true "$ALLOW_EXISTING_TARGET_DB" && fail "TARGET_DB existiert bereits: $TARGET_DB" + if is_true "$CREATE_TOMCAT_SYMLINK" && { [ -e "$TOMCAT_SYMLINK_PATH" ] || [ -L "$TOMCAT_SYMLINK_PATH" ]; }; then + if [ -L "$TOMCAT_SYMLINK_PATH" ] && is_true "$REPLACE_EXISTING_SYMLINK"; then :; else + fail "Tomcat-Symlink/Ziel existiert bereits: $TOMCAT_SYMLINK_PATH" + fi fi } -start_tomcat_if_requested() { - if is_true "$START_TOMCAT"; then - command -v systemctl >/dev/null 2>&1 || fail "systemctl nicht gefunden, kann Tomcat nicht starten." - run "systemctl start '$TOMCAT_SERVICE'" - fi -} +stop_tomcat_if_requested() { is_true "$STOP_TOMCAT" && run "systemctl stop '$TOMCAT_SERVICE'"; } +start_tomcat_if_requested() { is_true "$START_TOMCAT" && run "systemctl start '$TOMCAT_SERVICE'"; } copy_webapp_if_requested() { - if [ "$MIGRATION_MODE" != "full" ]; then - log "MIGRATION_MODE=db_only: Webapp wird nicht kopiert." - return 0 - fi - + [ "$MIGRATION_MODE" != "full" ] && { log "MIGRATION_MODE=db_only: Webapp wird nicht kopiert."; return 0; } command -v rsync >/dev/null 2>&1 || fail "rsync ist nicht installiert oder nicht im PATH." run "mkdir -p '$TARGET_WEBAPP'" - - RSYNC_DELETE="" - if is_true "$DELETE_TARGET"; then RSYNC_DELETE="--delete"; fi - + RSYNC_DELETE=""; is_true "$DELETE_TARGET" && RSYNC_DELETE="--delete" log "Kopiere Webapp..." run "rsync -a $RSYNC_DELETE '$SOURCE_WEBAPP/' '$TARGET_WEBAPP/'" } transfer_db() { run "mkdir -p '$TARGET_SUPERX_DIR'" - if [ "$DB_TRANSFER_MODE" = "copy" ]; then command -v rsync >/dev/null 2>&1 || fail "rsync ist nicht installiert oder nicht im PATH." - - RSYNC_DELETE="" - if is_true "$DELETE_TARGET"; then RSYNC_DELETE="--delete"; fi - + RSYNC_DELETE=""; is_true "$DELETE_TARGET" && RSYNC_DELETE="--delete" log "Kopiere DB-Verzeichnis nach WEB-INF/conf/edustore/db..." run "mkdir -p '$TARGET_DB'" run "rsync -a $RSYNC_DELETE '$SOURCE_DB/' '$TARGET_DB/'" - return 0 - fi - - if [ "$DB_TRANSFER_MODE" = "move" ]; then + else log "Verschiebe DB-Verzeichnis nach WEB-INF/conf/edustore/db..." run "mkdir -p '$(dirname "$TARGET_DB")'" run "mv '$SOURCE_DB' '$TARGET_DB'" - return 0 fi - - fail "Unbekannter DB_TRANSFER_MODE: $DB_TRANSFER_MODE" } backup_target_sql_env() { @@ -466,158 +321,78 @@ backup_target_sql_env() { } update_target_sql_env() { - if ! is_true "$UPDATE_SQL_ENV"; then return 0; fi + is_true "$UPDATE_SQL_ENV" || return 0 [ -f "$TARGET_SQL_ENV" ] || fail "Ziel-SQL_ENV nicht gefunden: $TARGET_SQL_ENV" - backup_target_sql_env - - if is_true "$DRY_RUN"; then - echo "[DRY-RUN] passe SQL_ENV an: $TARGET_SQL_ENV" - return 0 - fi - + if is_true "$DRY_RUN"; then echo "[DRY-RUN] passe SQL_ENV an: $TARGET_SQL_ENV"; return 0; fi python3 - "$TARGET_SQL_ENV" "$TARGET_SUPERX_DIR" "$TARGET_WEBAPP" <<'PY' from pathlib import Path -import re -import sys - -path = Path(sys.argv[1]) -new_superx_dir = sys.argv[2] -new_webapp = sys.argv[3] - -text = path.read_text(encoding="utf-8", errors="surrogateescape").splitlines() - -out = [] -seen_umask = False -seen_superx = False -seen_webapp = False -seen_export_superx = False -seen_export_webapp = False - -for line in text: - stripped = line.strip() - - if re.match(r"^umask\s+", stripped): - if not seen_umask: - out.append("umask 002") - seen_umask = True - else: - out.append("# " + line) - continue - - if re.match(r"^SUPERX_DIR\s*=", stripped): - if not seen_superx: - out.append(f'SUPERX_DIR="{new_superx_dir}"') - seen_superx = True - else: - out.append("# " + line) - continue - - if re.match(r"^WEBAPP\s*=", stripped): - if not seen_webapp: - out.append(f'WEBAPP="{new_webapp}"') - seen_webapp = True - else: - out.append("# " + line) - continue - - if re.match(r"^export\s+SUPERX_DIR\b", stripped): - seen_export_superx = True - out.append(line) - continue - - if re.match(r"^export\s+WEBAPP\b", stripped): - seen_export_webapp = True - out.append(line) - continue - - out.append(line) - -insert = [] -if not seen_umask: - insert.append("umask 002") -if not seen_superx: - insert.append(f'SUPERX_DIR="{new_superx_dir}"') -if not seen_export_superx: - insert.append("export SUPERX_DIR") -if not seen_webapp: - insert.append(f'WEBAPP="{new_webapp}"') -if not seen_export_webapp: - insert.append("export WEBAPP") - -if insert: - out = insert + [""] + out - -path.write_text("\n".join(out) + "\n", encoding="utf-8", errors="surrogateescape") +import re, sys +path = Path(sys.argv[1]); new_superx_dir = sys.argv[2]; new_webapp = sys.argv[3] +lines = path.read_text(encoding='utf-8', errors='surrogateescape').splitlines() +out=[]; seen_umask=seen_superx=seen_webapp=seen_export_superx=seen_export_webapp=False +for line in lines: + s=line.strip() + if re.match(r'^umask\s+', s): + if not seen_umask: out.append('umask 002'); seen_umask=True + else: out.append('# '+line) + elif re.match(r'^SUPERX_DIR\s*=', s): + if not seen_superx: out.append(f'SUPERX_DIR="{new_superx_dir}"'); seen_superx=True + else: out.append('# '+line) + elif re.match(r'^WEBAPP\s*=', s): + if not seen_webapp: out.append(f'WEBAPP="{new_webapp}"'); seen_webapp=True + else: out.append('# '+line) + elif re.match(r'^export\s+SUPERX_DIR\b', s): + seen_export_superx=True; out.append(line) + elif re.match(r'^export\s+WEBAPP\b', s): + seen_export_webapp=True; out.append(line) + else: out.append(line) +insert=[] +if not seen_umask: insert.append('umask 002') +if not seen_superx: insert.append(f'SUPERX_DIR="{new_superx_dir}"') +if not seen_export_superx: insert.append('export SUPERX_DIR') +if not seen_webapp: insert.append(f'WEBAPP="{new_webapp}"') +if not seen_export_webapp: insert.append('export WEBAPP') +if insert: out = insert + [''] + out +path.write_text('\n'.join(out)+'\n', encoding='utf-8', errors='surrogateescape') PY } set_rights() { - if ! is_true "$SET_RIGHTS"; then - log "Rechte setzen ist deaktiviert." - return 0 - fi - + is_true "$SET_RIGHTS" || { log "Rechte setzen ist deaktiviert."; return 0; } [ -d "$TARGET_WEBAPP" ] || fail "TARGET_WEBAPP fehlt: $TARGET_WEBAPP" - - if is_true "$EFFECTIVE_SET_OWNER"; then - log "Setze Owner/Gruppe..." - run "chown -R '$TOMCAT_USER:$SUPERX_GROUP' '$TARGET_WEBAPP'" - else - log "Owner/Gruppe werden nicht gesetzt." - fi - + if is_true "$EFFECTIVE_SET_OWNER"; then run "chown -R '$WEBAPP_USER:$WEBAPP_GROUP' '$TARGET_WEBAPP'"; else log "Owner/Gruppe werden nicht gesetzt."; fi if is_true "$SET_CHMOD"; then - log "Setze Verzeichnisrechte mit setgid-Bit..." run "find '$TARGET_WEBAPP' -type d -exec chmod 2775 {} +" - - log "Setze Standard-Dateirechte..." run "find '$TARGET_WEBAPP' -type f -exec chmod 664 {} +" - - log "Setze ausfuehrbare Rechte fuer db/bin..." - if [ -d "$TARGET_DB/bin" ]; then - run "find '$TARGET_DB/bin' -type f -exec chmod 775 {} +" - else - warn "db/bin im Ziel nicht gefunden: $TARGET_DB/bin" - fi - - log "Setze ausfuehrbare Rechte fuer *.x und *.sh..." + [ -d "$TARGET_DB/bin" ] && run "find '$TARGET_DB/bin' -type f -exec chmod 775 {} +" run "find '$TARGET_WEBAPP' -type f \\( -name '*.x' -o -name '*.sh' \\) -exec chmod 775 {} +" - else - log "chmod ist deaktiviert." fi } +create_tomcat_symlink_if_requested() { + is_true "$CREATE_TOMCAT_SYMLINK" || { log "Tomcat-Symlink wird nicht erstellt."; return 0; } + log "Erstelle Tomcat-Symlink..." + run "mkdir -p '$TOMCAT_WEBAPPS_DIR'" + [ -L "$TOMCAT_SYMLINK_PATH" ] && is_true "$REPLACE_EXISTING_SYMLINK" && run "rm '$TOMCAT_SYMLINK_PATH'" + run "ln -s '$TARGET_WEBAPP' '$TOMCAT_SYMLINK_PATH'" +} + final_notes() { - cat <