From 8e43e004f40d3053e6af328de901bbd1f11d36aa Mon Sep 17 00:00:00 2001 From: Marek Miklewicz Date: Sun, 5 Jul 2026 21:57:57 +0200 Subject: [PATCH] feat: wire native MariaDB port takeover into install_db.sh when PORT=3306 --- scripts/install_db.sh | 13 ++++ .../install_db_native_takeover_wiring_test.sh | 72 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tests/install_db_native_takeover_wiring_test.sh diff --git a/scripts/install_db.sh b/scripts/install_db.sh index e5e5b9a..ca0a03c 100755 --- a/scripts/install_db.sh +++ b/scripts/install_db.sh @@ -17,6 +17,7 @@ ALT_MYSQL_CONF="/usr/local/directadmin/conf/alt-mysql.conf" ALT_MY_CNF="/usr/local/directadmin/conf/alt-my.cnf" ALT_METADATA_ENV="/usr/local/directadmin/conf/alt-mariadb.env" INSTANCE_CNF="/etc/alt-mariadb.cnf" +NATIVE_TAKEOVER_SCRIPT="${NATIVE_TAKEOVER_SCRIPT:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/setup/native_mariadb_port_takeover.sh}" SERVICE_NAME="alt-mariadb" SERVICE_FILE="/etc/systemd/system/alt-mariadb.service" LOG_DIR="/var/log/alt-mariadb" @@ -124,6 +125,17 @@ require_root() { fi } +maybe_takeover_native_port() { + if [[ "$PORT" != "3306" ]]; then + return 0 + fi + + [[ -x "$NATIVE_TAKEOVER_SCRIPT" ]] || die "Nie znaleziono skryptu przejęcia portu natywnego MariaDB: $NATIVE_TAKEOVER_SCRIPT" + + log "PORT=3306 wybrany dla alt-mariadb - uruchamiam przejęcie portu natywnej instancji MariaDB." + "$NATIVE_TAKEOVER_SCRIPT" || die "Przejęcie portu natywnej instancji MariaDB nie powiodło się. Przerwano instalację alt-mariadb." +} + detect_os() { [[ -r /etc/os-release ]] || die "Nie można odczytać /etc/os-release." . /etc/os-release @@ -852,6 +864,7 @@ main() { validate_port require_root detect_os + maybe_takeover_native_port load_da_credentials validate_paths install_dependencies diff --git a/tests/install_db_native_takeover_wiring_test.sh b/tests/install_db_native_takeover_wiring_test.sh new file mode 100644 index 0000000..8ef552b --- /dev/null +++ b/tests/install_db_native_takeover_wiring_test.sh @@ -0,0 +1,72 @@ +#!/bin/bash +set -euo pipefail + +PLUGIN_DIR="$(cd "$(dirname "$0")/.." && pwd)" +REAL_SCRIPT="$PLUGIN_DIR/scripts/install_db.sh" +TMP_DIR="$(mktemp -d)" +trap 'rm -rf "$TMP_DIR"' EXIT + +fail() { + echo "FAIL: $*" >&2 + exit 1 +} + +# Extract the exact, real maybe_takeover_native_port() function body from the +# real script (rather than reimplementing it), so this test exercises the +# actual production logic without triggering the script's own main() (which +# needs a full install environment). +FUNCS_FILE="$TMP_DIR/funcs.sh" +{ + echo '#!/bin/bash' + echo 'log() { echo "[INFO] $*"; }' + echo 'die() { echo "[BŁĄD] $*" >&2; exit 1; }' + awk '/^maybe_takeover_native_port\(\)/,/^\}/' "$REAL_SCRIPT" +} > "$FUNCS_FILE" + +grep -q '^maybe_takeover_native_port()' "$FUNCS_FILE" \ + || fail "could not extract maybe_takeover_native_port() from $REAL_SCRIPT" + +# shellcheck source=/dev/null +source "$FUNCS_FILE" + +mkdir -p "$TMP_DIR/setup" +CALL_LOG="$TMP_DIR/calls.log" +cat > "$TMP_DIR/setup/native_mariadb_port_takeover.sh" <> "$CALL_LOG" +exit 0 +EOF +chmod 755 "$TMP_DIR/setup/native_mariadb_port_takeover.sh" + +NATIVE_TAKEOVER_SCRIPT="$TMP_DIR/setup/native_mariadb_port_takeover.sh" + +# --- Scenario 1: PORT=3306 must call the takeover script --- +PORT="3306" +: > "$CALL_LOG" +maybe_takeover_native_port +grep -Fxq "CALLED" "$CALL_LOG" || fail "expected takeover script to be called when PORT=3306" + +# --- Scenario 2: PORT!=3306 must NOT call the takeover script --- +PORT="33033" +: > "$CALL_LOG" +maybe_takeover_native_port +[ ! -s "$CALL_LOG" ] || fail "expected takeover script NOT to be called when PORT=33033" + +# --- Scenario 3: takeover script failure must abort (non-zero exit) --- +cat > "$TMP_DIR/setup/native_mariadb_port_takeover.sh" <> "$CALL_LOG" +exit 1 +EOF +chmod 755 "$TMP_DIR/setup/native_mariadb_port_takeover.sh" + +PORT="3306" +set +e +FAIL_OUTPUT="$(maybe_takeover_native_port 2>&1)" +FAIL_STATUS=$? +set -e +[ "$FAIL_STATUS" -ne 0 ] || fail "maybe_takeover_native_port should fail when the takeover script fails" +printf '%s\n' "$FAIL_OUTPUT" | grep -Fq "Przejęcie portu" \ + || fail "expected a clear error message on takeover failure, got: $FAIL_OUTPUT" + +echo "install_db_native_takeover_wiring_test: OK"