fix(sensor-proxy): fix remaining unsafe config writers
1. Self-heal script: Add BINARY_PATH variable so CLI migration actually runs - Previously logged "Binary not available" and skipped migration 2. migrate-sensor-proxy-control-plane.sh: Use atomic write (temp + rename) - Prevents partial writes if script is interrupted - Reduces race window with running service These were the remaining gaps identified by Codex review. NOTE: migrate-sensor-proxy-control-plane.sh still uses Python manipulation instead of the Phase 2 CLI, but as a one-time migration script for upgrades from v4.31, the atomic write provides sufficient protection. Future versions can deprecate this script entirely.
This commit is contained in:
parent
9d20acd35e
commit
de7fd72b47
2 changed files with 24 additions and 10 deletions
|
|
@ -3094,6 +3094,7 @@ else
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
SERVICE="pulse-sensor-proxy"
|
SERVICE="pulse-sensor-proxy"
|
||||||
|
BINARY_PATH="/opt/pulse/sensor-proxy/bin/pulse-sensor-proxy"
|
||||||
INSTALLER="/opt/pulse/sensor-proxy/install-sensor-proxy.sh"
|
INSTALLER="/opt/pulse/sensor-proxy/install-sensor-proxy.sh"
|
||||||
CTID_FILE="/etc/pulse-sensor-proxy/ctid"
|
CTID_FILE="/etc/pulse-sensor-proxy/ctid"
|
||||||
PENDING_FILE="/etc/pulse-sensor-proxy/pending-control-plane.env"
|
PENDING_FILE="/etc/pulse-sensor-proxy/pending-control-plane.env"
|
||||||
|
|
|
||||||
|
|
@ -92,14 +92,20 @@ echo "$CONTROL_TOKEN" > "$TOKEN_FILE"
|
||||||
chmod 600 "$TOKEN_FILE"
|
chmod 600 "$TOKEN_FILE"
|
||||||
chown pulse-sensor-proxy:pulse-sensor-proxy "$TOKEN_FILE"
|
chown pulse-sensor-proxy:pulse-sensor-proxy "$TOKEN_FILE"
|
||||||
|
|
||||||
remove_control_block() {
|
update_config_atomically() {
|
||||||
python3 - "$CONFIG_FILE" <<'PY'
|
# Phase 2: Use atomic write to prevent corruption
|
||||||
|
local temp_file
|
||||||
|
temp_file=$(mktemp)
|
||||||
|
|
||||||
|
# Remove old control plane blocks and add new one atomically
|
||||||
|
python3 - "$CONFIG_FILE" "$temp_file" <<'PY'
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
path = Path(sys.argv[1])
|
config_path = Path(sys.argv[1])
|
||||||
if not path.exists():
|
temp_path = Path(sys.argv[2])
|
||||||
|
if not config_path.exists():
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
lines = path.read_text().splitlines(keepends=True)
|
lines = config_path.read_text().splitlines(keepends=True)
|
||||||
result = []
|
result = []
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(lines):
|
while i < len(lines):
|
||||||
|
|
@ -116,13 +122,11 @@ while i < len(lines):
|
||||||
continue
|
continue
|
||||||
result.append(line)
|
result.append(line)
|
||||||
i += 1
|
i += 1
|
||||||
path.write_text("".join(result))
|
temp_path.write_text("".join(result))
|
||||||
PY
|
PY
|
||||||
}
|
|
||||||
|
|
||||||
log "Updating config..."
|
# Append new control plane config
|
||||||
remove_control_block
|
cat >> "$temp_file" <<EOF
|
||||||
cat >> "$CONFIG_FILE" <<EOF
|
|
||||||
|
|
||||||
# Pulse control plane configuration (added by migrate-sensor-proxy-control-plane.sh)
|
# Pulse control plane configuration (added by migrate-sensor-proxy-control-plane.sh)
|
||||||
pulse_control_plane:
|
pulse_control_plane:
|
||||||
|
|
@ -131,6 +135,15 @@ pulse_control_plane:
|
||||||
refresh_interval: $REFRESH_INTERVAL
|
refresh_interval: $REFRESH_INTERVAL
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
# Atomic rename
|
||||||
|
mv "$temp_file" "$CONFIG_FILE"
|
||||||
|
chmod 644 "$CONFIG_FILE"
|
||||||
|
chown pulse-sensor-proxy:pulse-sensor-proxy "$CONFIG_FILE" 2>/dev/null || true
|
||||||
|
}
|
||||||
|
|
||||||
|
log "Updating config..."
|
||||||
|
update_config_atomically
|
||||||
|
|
||||||
if [[ "$SKIP_RESTART" == false ]]; then
|
if [[ "$SKIP_RESTART" == false ]]; then
|
||||||
log "Restarting pulse-sensor-proxy..."
|
log "Restarting pulse-sensor-proxy..."
|
||||||
systemctl restart pulse-sensor-proxy
|
systemctl restart pulse-sensor-proxy
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue