refactor stage_mqtt cleanup section without behavior change

This commit is contained in:
Andrei Smaliak 2026-03-14 07:43:09 +04:00
parent a3468bd732
commit 483776eaa2
1 changed files with 66 additions and 36 deletions

View File

@ -24,6 +24,16 @@ log() {
echo "$line" >>"$LOG" 2>/dev/null || true 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_n=0
step() { step() {
step_n=$((step_n+1)) 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" REMOTE_SET="/usr/local/sbin/iomatex-mqtt-remote-set"
HEALTH_SH="/usr/local/sbin/iomatex-mqtt-health.sh" 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" step "Full cleanup of previous MQTT/IOMATEX artifacts"
systemctl stop mosquitto 2>/dev/null || true for unit in "${MQTT_STOP_UNITS[@]}"; do
systemctl stop iomatex-mqtt-bridge.path iomatex-mqtt-bridge.service 2>/dev/null || true safe_systemctl stop "$unit"
systemctl stop iomatex-mqtt-apply.path iomatex-mqtt-apply.service 2>/dev/null || true done
systemctl stop iomatex-mqtt-health.timer iomatex-mqtt-health.service 2>/dev/null || true
systemctl disable iomatex-mqtt-bridge.path 2>/dev/null || true for unit in "${MQTT_DISABLE_UNITS[@]}"; do
systemctl disable iomatex-mqtt-apply.path 2>/dev/null || true safe_systemctl disable "$unit"
systemctl disable iomatex-mqtt-health.timer 2>/dev/null || true done
rm -f /etc/mosquitto/conf.d/00-iomatex-local.conf 2>/dev/null || true for path in "${MQTT_CLEANUP_FILES[@]}"; do
rm -f /etc/mosquitto/conf.d/01-iomatex-main-listener.conf 2>/dev/null || true remove_if_exists "$path"
rm -f /etc/mosquitto/conf.d/20-iomatex-bridge.conf 2>/dev/null || true done
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
rm -f /etc/iomatex/mqtt_bridge.env 2>/dev/null || true remove_if_exists /var/lib/mosquitto/*.db
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
systemctl daemon-reload >/dev/null 2>&1 || true systemctl daemon-reload >/dev/null 2>&1 || true
touch "$LOG" >/dev/null 2>&1 || true touch "$LOG" >/dev/null 2>&1 || true
@ -683,4 +713,4 @@ echo
echo "Timer check:" echo "Timer check:"
echo " systemctl list-timers --all | grep -F iomatex-mqtt-health" echo " systemctl list-timers --all | grep -F iomatex-mqtt-health"
echo echo
log "Stage1 MQTT finished successfully." log "Stage1 MQTT finished successfully."