From 483776eaa279fd7132bfacd08a02c9763b853878 Mon Sep 17 00:00:00 2001 From: Andrei Smaliak Date: Sat, 14 Mar 2026 07:43:09 +0400 Subject: [PATCH] refactor stage_mqtt cleanup section without behavior change --- stage_mqtt/stage_mqtt.sh | 102 +++++++++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 36 deletions(-) diff --git a/stage_mqtt/stage_mqtt.sh b/stage_mqtt/stage_mqtt.sh index 979b052..1c70241 100755 --- a/stage_mqtt/stage_mqtt.sh +++ b/stage_mqtt/stage_mqtt.sh @@ -24,6 +24,16 @@ log() { echo "$line" >>"$LOG" 2>/dev/null || true } +safe_systemctl() { + local action="$1" + shift + systemctl "$action" "$@" 2>/dev/null || true +} + +remove_if_exists() { + rm -f "$@" 2>/dev/null || true +} + step_n=0 step() { step_n=$((step_n+1)) @@ -74,45 +84,65 @@ APPLY="/usr/local/sbin/iomatex-mqtt-apply" REMOTE_SET="/usr/local/sbin/iomatex-mqtt-remote-set" HEALTH_SH="/usr/local/sbin/iomatex-mqtt-health.sh" +MQTT_STOP_UNITS=( + mosquitto + iomatex-mqtt-bridge.path + iomatex-mqtt-bridge.service + iomatex-mqtt-apply.path + iomatex-mqtt-apply.service + iomatex-mqtt-health.timer + iomatex-mqtt-health.service +) + +MQTT_DISABLE_UNITS=( + iomatex-mqtt-bridge.path + iomatex-mqtt-apply.path + iomatex-mqtt-health.timer +) + +MQTT_CLEANUP_FILES=( + /etc/mosquitto/conf.d/00-iomatex-local.conf + /etc/mosquitto/conf.d/01-iomatex-main-listener.conf + /etc/mosquitto/conf.d/20-iomatex-bridge.conf + /etc/mosquitto/conf.d/30-iomatex-bridge.conf + /etc/mosquitto/conf.d/40-iomatex-bridge.conf + /etc/mosquitto/conf.d/50-iomatex-bridge.conf + /etc/mosquitto/conf.d/60-iomatex-bridge.conf + /etc/mosquitto/conf.d/99-iomatex-debug.conf + /etc/mosquitto/conf.d/zz-iomatex-bridge.conf + /etc/iomatex/mqtt_bridge.env + /usr/local/sbin/iomatex-mqtt-apply + /usr/local/sbin/iomatex-mqtt-remote-set + /usr/local/sbin/iomatex-mqtt-health.sh + /usr/local/sbin/iomatex-mqtt-bridge-apply + /usr/local/sbin/iomatex-mqtt-bridge + /etc/systemd/system/iomatex-mqtt-apply.service + /etc/systemd/system/iomatex-mqtt-apply.path + /etc/systemd/system/iomatex-mqtt-bridge.service + /etc/systemd/system/iomatex-mqtt-bridge.path + /etc/systemd/system/iomatex-mqtt-health.service + /etc/systemd/system/iomatex-mqtt-health.timer + /etc/sudoers.d/90-iomatex-mqtt-apply + /var/lib/mosquitto/mosquitto.db + /run/iomatex-mqtt-apply.sha + /var/log/mosquitto/mosquitto.log + /var/log/iomatex-stage1-mqtt.log +) + step "Full cleanup of previous MQTT/IOMATEX artifacts" -systemctl stop mosquitto 2>/dev/null || true -systemctl stop iomatex-mqtt-bridge.path iomatex-mqtt-bridge.service 2>/dev/null || true -systemctl stop iomatex-mqtt-apply.path iomatex-mqtt-apply.service 2>/dev/null || true -systemctl stop iomatex-mqtt-health.timer iomatex-mqtt-health.service 2>/dev/null || true +for unit in "${MQTT_STOP_UNITS[@]}"; do + safe_systemctl stop "$unit" +done -systemctl disable iomatex-mqtt-bridge.path 2>/dev/null || true -systemctl disable iomatex-mqtt-apply.path 2>/dev/null || true -systemctl disable iomatex-mqtt-health.timer 2>/dev/null || true +for unit in "${MQTT_DISABLE_UNITS[@]}"; do + safe_systemctl disable "$unit" +done -rm -f /etc/mosquitto/conf.d/00-iomatex-local.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/01-iomatex-main-listener.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/20-iomatex-bridge.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/30-iomatex-bridge.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/40-iomatex-bridge.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/50-iomatex-bridge.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/60-iomatex-bridge.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/99-iomatex-debug.conf 2>/dev/null || true -rm -f /etc/mosquitto/conf.d/zz-iomatex-bridge.conf 2>/dev/null || true +for path in "${MQTT_CLEANUP_FILES[@]}"; do + remove_if_exists "$path" +done -rm -f /etc/iomatex/mqtt_bridge.env 2>/dev/null || true -rm -f /usr/local/sbin/iomatex-mqtt-apply 2>/dev/null || true -rm -f /usr/local/sbin/iomatex-mqtt-remote-set 2>/dev/null || true -rm -f /usr/local/sbin/iomatex-mqtt-health.sh 2>/dev/null || true -rm -f /usr/local/sbin/iomatex-mqtt-bridge-apply 2>/dev/null || true -rm -f /usr/local/sbin/iomatex-mqtt-bridge 2>/dev/null || true - -rm -f /etc/systemd/system/iomatex-mqtt-apply.service 2>/dev/null || true -rm -f /etc/systemd/system/iomatex-mqtt-apply.path 2>/dev/null || true -rm -f /etc/systemd/system/iomatex-mqtt-bridge.service 2>/dev/null || true -rm -f /etc/systemd/system/iomatex-mqtt-bridge.path 2>/dev/null || true -rm -f /etc/systemd/system/iomatex-mqtt-health.service 2>/dev/null || true -rm -f /etc/systemd/system/iomatex-mqtt-health.timer 2>/dev/null || true - -rm -f /etc/sudoers.d/90-iomatex-mqtt-apply 2>/dev/null || true - -rm -f /var/lib/mosquitto/mosquitto.db /var/lib/mosquitto/*.db 2>/dev/null || true -rm -f /run/iomatex-mqtt-apply.sha 2>/dev/null || true -rm -f /var/log/mosquitto/mosquitto.log /var/log/iomatex-stage1-mqtt.log 2>/dev/null || true +remove_if_exists /var/lib/mosquitto/*.db systemctl daemon-reload >/dev/null 2>&1 || true touch "$LOG" >/dev/null 2>&1 || true @@ -683,4 +713,4 @@ echo echo "Timer check:" echo " systemctl list-timers --all | grep -F iomatex-mqtt-health" echo -log "Stage1 MQTT finished successfully." \ No newline at end of file +log "Stage1 MQTT finished successfully."