feat: add service restart, verification, and full rollback orchestration to native port takeover
This commit is contained in:
@@ -234,10 +234,110 @@ rollback_config_changes() {
|
||||
done
|
||||
}
|
||||
|
||||
NATIVE_SERVICE_NAME=""
|
||||
DID_RESTART_NATIVE=0
|
||||
DID_RESTART_DA=0
|
||||
# Guards rollback_service_state so it only acts while wired via the EXIT trap
|
||||
# below. NOTE: this is intentionally an EXIT trap, not an ERR trap - see the
|
||||
# comment above the trap installation in main() for why.
|
||||
ROLLBACK_ARMED=0
|
||||
|
||||
detect_native_service_name() {
|
||||
NATIVE_SERVICE_NAME="$(mysql_service_name)"
|
||||
log "Wykryta usługa natywnej MariaDB: $NATIVE_SERVICE_NAME."
|
||||
}
|
||||
|
||||
restart_native_service() {
|
||||
systemctl restart "$NATIVE_SERVICE_NAME" || die "Nie udało się zrestartować usługi $NATIVE_SERVICE_NAME."
|
||||
DID_RESTART_NATIVE=1
|
||||
}
|
||||
|
||||
verify_native_listening_new_port() {
|
||||
local i
|
||||
for i in $(seq 1 30); do
|
||||
if port_is_listening "$NEW_NATIVE_PORT" && ! port_is_listening 3306; then
|
||||
log "Natywna instancja nasłuchuje na porcie $NEW_NATIVE_PORT, port 3306 wolny."
|
||||
return 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
die "Natywna instancja nie nasłuchuje na porcie $NEW_NATIVE_PORT po restarcie (lub port 3306 wciąż zajęty)."
|
||||
}
|
||||
|
||||
restart_directadmin_service() {
|
||||
systemctl restart directadmin || die "Nie udało się zrestartować usługi directadmin."
|
||||
DID_RESTART_DA=1
|
||||
}
|
||||
|
||||
verify_da_connectivity_new_port() {
|
||||
local bin
|
||||
bin="$(mysql_detect_binary mysql)" || die "Nie znaleziono klienta mysql/mariadb do weryfikacji łączności DA."
|
||||
MYSQL_PWD="$NATIVE_CURRENT_PASS" "$bin" \
|
||||
--user="$NATIVE_CURRENT_USER" \
|
||||
--protocol=TCP --host=127.0.0.1 --port="$NEW_NATIVE_PORT" \
|
||||
-e "SELECT 1" >/dev/null 2>>"$LOG_FILE" \
|
||||
|| die "DirectAdmin nie może połączyć się z natywną instancją na nowym porcie $NEW_NATIVE_PORT."
|
||||
log "Potwierdzono łączność DirectAdmina z natywną instancją na porcie $NEW_NATIVE_PORT."
|
||||
}
|
||||
|
||||
rollback_service_state() {
|
||||
[ "$ROLLBACK_ARMED" -eq 1 ] || return 0
|
||||
rollback_config_changes
|
||||
restore_custombuild_mysql_management
|
||||
if [ -n "$NATIVE_SERVICE_NAME" ]; then
|
||||
systemctl restart "$NATIVE_SERVICE_NAME" \
|
||||
|| log "OSTRZEŻENIE: nie udało się zrestartować $NATIVE_SERVICE_NAME podczas wycofywania zmian."
|
||||
fi
|
||||
if [ "$DID_RESTART_DA" -eq 1 ]; then
|
||||
systemctl restart directadmin \
|
||||
|| log "OSTRZEŻENIE: nie udało się zrestartować directadmin podczas wycofywania zmian."
|
||||
fi
|
||||
log "Wycofano zmiany przejęcia portu natywnej instancji MariaDB."
|
||||
}
|
||||
|
||||
main() {
|
||||
require_root
|
||||
check_os_guard
|
||||
read_native_mysql_conf
|
||||
read_current_mysql_inst
|
||||
|
||||
if already_migrated; then
|
||||
log "Natywna instancja już działa na porcie $NATIVE_CURRENT_PORT (nie 3306) - pomijam przejęcie."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
check_native_reachable
|
||||
preflight_new_port_free
|
||||
detect_native_service_name
|
||||
|
||||
# NOTE: deliberately an EXIT trap, not an ERR trap as originally planned.
|
||||
# die() (used by every failure path below) calls `exit` directly, and bash's
|
||||
# ERR trap does not fire for a function that terminates via an explicit
|
||||
# `exit` call - it only fires when a command's non-zero status propagates
|
||||
# through normal control flow. An EXIT trap fires on *any* shell exit
|
||||
# (explicit exit, errexit-triggered exit, or falling off the end of the
|
||||
# script), which is what "any failure from this point on triggers
|
||||
# rollback" actually requires here. ROLLBACK_ARMED guards it so it is a
|
||||
# no-op before this point and after a successful run.
|
||||
ROLLBACK_ARMED=1
|
||||
trap 'rollback_service_state' EXIT
|
||||
|
||||
disable_custombuild_mysql_management
|
||||
apply_config_mutations
|
||||
restart_native_service
|
||||
verify_native_listening_new_port
|
||||
restart_directadmin_service
|
||||
verify_da_connectivity_new_port
|
||||
|
||||
ROLLBACK_ARMED=0
|
||||
trap - EXIT
|
||||
log "Przejęcie portu natywnej instancji MariaDB zakończone powodzeniem: $NATIVE_CURRENT_PORT -> $NEW_NATIVE_PORT."
|
||||
}
|
||||
|
||||
[ -r "$COMMON_FILE" ] || die "missing helper: $COMMON_FILE"
|
||||
# shellcheck source=./mysql_common.sh
|
||||
source "$COMMON_FILE"
|
||||
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
: # main() is added in Task 4.
|
||||
main "$@"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user