Address remaining Codex review findings
**LXC Bind Mount Removal**: - Changed from sed to `pct set -delete` for safer mount removal - Validates syntax and prevents breaking container configs - Finds mount points by grepping for pulse-sensor-proxy, extracts mp number **API Token Parsing** (three-tier fallback): 1. Try `pveum --output-format json` with python3 JSON parsing 2. Fall back to `pvesh get /access/users/pulse-monitor@pam/token` JSON API 3. Last resort: parse table output with improved filtering (handles more Unicode chars) **Retry Logic**: - Rename cleanup-request.json to .processing instead of deleting immediately - Allows retry on failure (processing file persists if script crashes) - Remove .processing file only on successful completion - Prevents loops while enabling failure recovery These complete all 8 issues identified by Codex review.
This commit is contained in:
parent
bcd8d4e0fa
commit
fe53d64730
1 changed files with 69 additions and 22 deletions
|
|
@ -1716,15 +1716,19 @@ fi
|
||||||
|
|
||||||
log_info "Processing cleanup request from $CLEANUP_REQUEST"
|
log_info "Processing cleanup request from $CLEANUP_REQUEST"
|
||||||
|
|
||||||
# Read and parse the cleanup request, then delete immediately to prevent re-processing
|
# Read and parse the cleanup request
|
||||||
CLEANUP_DATA=$(cat "$CLEANUP_REQUEST")
|
CLEANUP_DATA=$(cat "$CLEANUP_REQUEST")
|
||||||
HOST=$(echo "$CLEANUP_DATA" | grep -o '"host":"[^"]*"' | cut -d'"' -f4 || echo "")
|
HOST=$(echo "$CLEANUP_DATA" | grep -o '"host":"[^"]*"' | cut -d'"' -f4 || echo "")
|
||||||
REQUESTED_AT=$(echo "$CLEANUP_DATA" | grep -o '"requestedAt":"[^"]*"' | cut -d'"' -f4 || echo "")
|
REQUESTED_AT=$(echo "$CLEANUP_DATA" | grep -o '"requestedAt":"[^"]*"' | cut -d'"' -f4 || echo "")
|
||||||
|
|
||||||
log_info "Cleanup requested at: ${REQUESTED_AT:-unknown}"
|
log_info "Cleanup requested at: ${REQUESTED_AT:-unknown}"
|
||||||
|
|
||||||
# Delete request file IMMEDIATELY to prevent loops (before any long-running operations)
|
# Rename request file to .processing to prevent re-triggering while allowing retry on failure
|
||||||
rm -f "$CLEANUP_REQUEST"
|
PROCESSING_FILE="${CLEANUP_REQUEST}.processing"
|
||||||
|
mv "$CLEANUP_REQUEST" "$PROCESSING_FILE" 2>/dev/null || {
|
||||||
|
log_warn "Failed to rename cleanup request file, may have been processed by another instance"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
# If no specific host was provided, clean up all known nodes
|
# If no specific host was provided, clean up all known nodes
|
||||||
if [[ -z "$HOST" ]]; then
|
if [[ -z "$HOST" ]]; then
|
||||||
|
|
@ -1796,22 +1800,56 @@ else
|
||||||
# 2. Delete API tokens and user
|
# 2. Delete API tokens and user
|
||||||
log_info "Removing Proxmox API tokens and pulse-monitor user"
|
log_info "Removing Proxmox API tokens and pulse-monitor user"
|
||||||
if command -v pveum >/dev/null 2>&1; then
|
if command -v pveum >/dev/null 2>&1; then
|
||||||
# Use JSON output if available (Proxmox 7.0+), fall back to text parsing
|
# Try JSON output first (pveum with --output-format json)
|
||||||
if pveum user token list --output-format json-pretty pulse-monitor@pam >/dev/null 2>&1; then
|
TOKEN_IDS=""
|
||||||
TOKEN_IDS=$(pveum user token list --output-format json-pretty pulse-monitor@pam 2>/dev/null | \
|
if command -v python3 >/dev/null 2>&1; then
|
||||||
grep -o '"tokenid":"[^"]*"' | cut -d'"' -f4 || true)
|
# Try pveum with JSON output
|
||||||
else
|
if TOKEN_JSON=$(pveum user token list pulse-monitor@pam --output-format json 2>/dev/null); then
|
||||||
# Fall back to parsing table output, filtering table decorations
|
TOKEN_IDS=$(echo "$TOKEN_JSON" | python3 -c '
|
||||||
TOKEN_IDS=$(pveum user token list pulse-monitor@pam 2>/dev/null | \
|
import sys, json
|
||||||
grep -v '^[│┌└╞]' | awk 'NR>1 && /pulse/ {print $1}' | grep -v '^$' || true)
|
try:
|
||||||
|
data = json.load(sys.stdin)
|
||||||
|
if isinstance(data, list):
|
||||||
|
for item in data:
|
||||||
|
if "tokenid" in item:
|
||||||
|
print(item["tokenid"])
|
||||||
|
except: pass
|
||||||
|
' || true)
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for token_id in $TOKEN_IDS; do
|
# Fall back to pvesh JSON API if pveum JSON didn't work
|
||||||
log_info "Deleting API token: $token_id"
|
if [[ -z "$TOKEN_IDS" ]] && command -v pvesh >/dev/null 2>&1; then
|
||||||
pveum user token remove pulse-monitor@pam "${token_id}" 2>&1 | \
|
if TOKEN_JSON=$(pvesh get /access/users/pulse-monitor@pam/token 2>/dev/null); then
|
||||||
logger -t "$LOG_TAG" -p user.info || \
|
TOKEN_IDS=$(echo "$TOKEN_JSON" | python3 -c '
|
||||||
log_warn "Failed to delete token $token_id"
|
import sys, json
|
||||||
done
|
try:
|
||||||
|
data = json.load(sys.stdin)
|
||||||
|
if isinstance(data, dict) and "data" in data:
|
||||||
|
for item in data["data"]:
|
||||||
|
if "tokenid" in item:
|
||||||
|
print(item["tokenid"])
|
||||||
|
except: pass
|
||||||
|
' 2>/dev/null || true)
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Last resort: parse table output with better filtering
|
||||||
|
if [[ -z "$TOKEN_IDS" ]]; then
|
||||||
|
TOKEN_IDS=$(pveum user token list pulse-monitor@pam 2>/dev/null | \
|
||||||
|
awk 'NR>1 && /^[[:space:]]*pulse/ {print $1}' | grep -v '^[│┌└╞─]' | grep -v '^$' || true)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -n "$TOKEN_IDS" ]]; then
|
||||||
|
for token_id in $TOKEN_IDS; do
|
||||||
|
log_info "Deleting API token: $token_id"
|
||||||
|
pveum user token remove pulse-monitor@pam "${token_id}" 2>&1 | \
|
||||||
|
logger -t "$LOG_TAG" -p user.info || \
|
||||||
|
log_warn "Failed to delete token $token_id"
|
||||||
|
done
|
||||||
|
else
|
||||||
|
log_info "No API tokens found for pulse-monitor@pam"
|
||||||
|
fi
|
||||||
|
|
||||||
# Remove the pulse-monitor user
|
# Remove the pulse-monitor user
|
||||||
log_info "Removing pulse-monitor@pam user"
|
log_info "Removing pulse-monitor@pam user"
|
||||||
|
|
@ -1827,11 +1865,17 @@ else
|
||||||
if command -v pct >/dev/null 2>&1; then
|
if command -v pct >/dev/null 2>&1; then
|
||||||
for ctid in $(pct list 2>/dev/null | awk 'NR>1 {print $1}' || true); do
|
for ctid in $(pct list 2>/dev/null | awk 'NR>1 {print $1}' || true); do
|
||||||
CONF_FILE="/etc/pve/lxc/${ctid}.conf"
|
CONF_FILE="/etc/pve/lxc/${ctid}.conf"
|
||||||
if [[ -f "$CONF_FILE" ]] && grep -q "pulse-sensor-proxy" "$CONF_FILE" 2>/dev/null; then
|
if [[ -f "$CONF_FILE" ]]; then
|
||||||
log_info "Removing bind mount from container $ctid"
|
# Find pulse-sensor-proxy mount points and remove them using pct
|
||||||
sed -i '/pulse-sensor-proxy/d' "$CONF_FILE" 2>&1 | \
|
for mp_key in $(grep -o "^mp[0-9]\+:" "$CONF_FILE" | grep -f <(grep "pulse-sensor-proxy" "$CONF_FILE" | grep -o "^mp[0-9]\+:") || true); do
|
||||||
logger -t "$LOG_TAG" -p user.info || \
|
mp_num="${mp_key%:}"
|
||||||
log_warn "Failed to update config for container $ctid"
|
log_info "Removing ${mp_num} (pulse-sensor-proxy) from container $ctid"
|
||||||
|
if pct set "$ctid" -delete "${mp_num}" 2>&1 | logger -t "$LOG_TAG" -p user.info; then
|
||||||
|
log_info "Successfully removed ${mp_num} from container $ctid"
|
||||||
|
else
|
||||||
|
log_warn "Failed to remove ${mp_num} from container $ctid"
|
||||||
|
fi
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
@ -1908,6 +1952,9 @@ else
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Remove processing file on success
|
||||||
|
rm -f "$PROCESSING_FILE"
|
||||||
|
|
||||||
log_info "Cleanup completed successfully"
|
log_info "Cleanup completed successfully"
|
||||||
exit 0
|
exit 0
|
||||||
CLEANUP_EOF
|
CLEANUP_EOF
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue