Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Stop legacy sensor cleanup on unconfirmed service or container state

pulse-triage[bot]99d7b5e671fa
diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md
index 10cd0c5077..9dd27223f1 100644
--- a/docs/release-control/v6/internal/subsystems/deployment-installability.md
+++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md
@@ -15,6 +15,36 @@
## Purpose
+### Legacy sensor cleanup requires observed stopped targets
+
+The legacy sensor uninstaller must confirm each existing unit has stopped and
+was disabled before removing keys, mounts or files. Missing units are a no-op;
+failed or unknown service observation/stop/disable blocks further cleanup.
+Local container enumeration must succeed. Only current (pre-snapshot) legacy
+mount entries are eligible: snapshots and unrelated mounts remain unchanged.
+For each affected exact container, a successful status read must identify
+running or stopped. Previously running targets require a successful bounded
+stop and an explicit stopped readback; every subsequent configuration mutation
+requires a fresh stopped readback. Unknown state or failed stop means no edits
+or restart of that target. A failed `pct set` never falls back to direct edits.
+Direct legacy-line removal preserves file ownership/mode and reports errors.
+
+Only a previously running target may be restarted after cleanup or a confirmed
+mutation failure, and its running state must be observed. Lost stopped state
+prevents both further edits and automatic restoration. Failed enumeration,
+container edits/restoration, service cleanup or reload returns nonzero and
+cannot report whole cleanup complete. Incomplete container cleanup preserves
+legacy files and API access for local recovery. This does not remove the need
+for the guide's planned outage, backups and independent workload checks.
+
+`TestUninstallSensorProxyMountSafety`, `TestUninstallSensorProxyServiceSafety`
+and `TestUninstallSensorProxyMainReportsIncompleteLocalCleanup` execute isolated
+shell fixtures for status/stop/set/edit/start failures, lost state, only-prior-
+active restoration, snapshots, absent units and completion propagation. Only
+the fixed config directory is substituted; lifecycle tools are mocks, not native
+Proxmox or installed systemd acceptance. Remote SSH trust, purge consent and
+API-user removal opt-in remain unchanged.
+
### Audit report cleanup belongs to the original invoking process
The npm audit wrapper retains a captured advisory until the parent classifies
diff --git a/scripts/installtests/uninstall_sensor_proxy_safety_test.go b/scripts/installtests/uninstall_sensor_proxy_safety_test.go
new file mode 100644
index 0000000000..ef4e88805a
--- /dev/null
+++ b/scripts/installtests/uninstall_sensor_proxy_safety_test.go
@@ -0,0 +1,257 @@
+package installtests
+
+import (
+ "context"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+)
+
+// Substitute only the fixed config directory in an exact script copy. Every
+// external lifecycle action is a fixture function, never a native pct/systemctl.
+func sensorProxySafetyScript(t *testing.T, dir string) string {
+ t.Helper()
+ source, err := os.ReadFile(repoFile("scripts", "uninstall-sensor-proxy.sh"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ const fixed = `conf="/etc/pve/lxc/${ctid}.conf"`
+ if strings.Count(string(source), fixed) != 1 {
+ t.Fatal("expected one fixed LXC config path")
+ }
+ script := strings.Replace(string(source), fixed, `conf="${FIXTURE_DIR}/${ctid}.conf"`, 1)
+ path := filepath.Join(dir, "uninstall-fixture.sh")
+ if err := os.WriteFile(path, []byte(script), 0600); err != nil {
+ t.Fatal(err)
+ }
+ return path
+}
+
+func runSensorProxySafetyShell(t *testing.T, script, dir, scenario, body string) (string, int) {
+ t.Helper()
+ ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
+ defer cancel()
+ cmd := exec.CommandContext(ctx, "bash", "-c", body, "bash", script)
+ cmd.Env = append(os.Environ(), "FIXTURE_DIR="+dir, "SCENARIO="+scenario)
+ out, err := cmd.CombinedOutput()
+ if ctx.Err() != nil {
+ t.Fatalf("fixture deadline: %v\n%s", ctx.Err(), out)
+ }
+ if err == nil {
+ return string(out), 0
+ }
+ if e, ok := err.(*exec.ExitError); ok {
+ return string(out), e.ExitCode()
+ }
+ t.Fatalf("fixture execution: %v\n%s", err, out)
+ return "", -1
+}
+
+const sensorProxyMountFixture = `source "$1"
+timeout() { [[ "$1" != --kill-after=* ]] || shift; shift; "$@"; }
+sleep() { :; }
+pct() {
+ printf '%s\n' "$*" >>"$FIXTURE_DIR/actions"
+ local count=0 state=""
+ case "$1" in
+ list)
+ printf 'VMID Status Lock Name\n101 running test\n'
+ [[ "$SCENARIO" != list_failure ]] || return 9
+ ;;
+ status)
+ [[ "$2" == 101 ]] || return 90
+ [[ ! -f "$FIXTURE_DIR/count" ]] || read -r count <"$FIXTURE_DIR/count"
+ count=$((count + 1)); printf '%s\n' "$count" >"$FIXTURE_DIR/count"
+ case "$SCENARIO:$count" in
+ status_failure:1) printf 'status: stopped\n'; return 9 ;;
+ status_unknown:1) printf 'status: paused\n'; return 0 ;;
+ status_malformed:1) printf 'status: stopped\nstatus: running\n'; return 0 ;;
+ stopped_read_failure:2) return 9 ;;
+ lost_before_set:3|lost_before_raw:4) printf 'status: running\n'; return 0 ;;
+ lost_before_restore:5) return 9 ;;
+ esac
+ read -r state <"$FIXTURE_DIR/state"
+ printf 'status: %s\n' "$state"
+ ;;
+ stop)
+ [[ "$2" == 101 ]] || return 90
+ [[ "$SCENARIO" != stop_failure ]] || return 9
+ [[ "$SCENARIO" == stop_no_effect ]] || printf 'stopped\n' >"$FIXTURE_DIR/state"
+ ;;
+ set)
+ [[ "$*" == 'set 101 -delete mp0' ]] || return 90
+ [[ "$SCENARIO" != set_failure ]] || return 9
+ ;;
+ start)
+ [[ "$2" == 101 ]] || return 90
+ [[ "$SCENARIO" != start_failure ]] || return 9
+ [[ "$SCENARIO" == start_no_effect ]] || printf 'running\n' >"$FIXTURE_DIR/state"
+ ;;
+ *) return 90 ;;
+ esac
+}
+mv() {
+ [[ "$SCENARIO" != write_failure ]] || return 9
+ command mv "$@"
+}
+cleanup_stale_sensor_proxy_mounts
+`
+
+func TestUninstallSensorProxyMountSafety(t *testing.T) {
+ main := "hostname: workload\nmp0: /run/pulse-sensor-proxy,mp=/sensor\nmp1: /data,mp=/data\nlxc.mount.entry: /run/pulse-sensor-proxy sensor none bind 0 0\n"
+ snapshot := "[saved]\nmp0: /run/pulse-sensor-proxy,mp=/sensor\nlxc.mount.entry: /run/pulse-sensor-proxy sensor none bind 0 0\n"
+ cases := []struct {
+ name, initial, conf, want, actions string
+ success bool
+ }{
+ {"stopped", "stopped", main + snapshot, "hostname: workload\nmp1: /data,mp=/data\n" + snapshot, "list\nstatus 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\n", true},
+ {"running", "running", main + snapshot, "hostname: workload\nmp1: /data,mp=/data\n" + snapshot, "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\nstatus 101\nstart 101\nstatus 101\n", true},
+ {"no_snapshot", "stopped", main, "hostname: workload\nmp1: /data,mp=/data\n", "list\nstatus 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\n", true},
+ {"snapshot_only", "running", "hostname: workload\n" + snapshot, "hostname: workload\n" + snapshot, "list\n", true},
+ {"snapshot_first", "running", snapshot, snapshot, "list\n", true},
+ {"list_failure", "running", main, main, "list\n", false},
+ {"status_failure", "running", main, main, "list\nstatus 101\n", false},
+ {"status_unknown", "running", main, main, "list\nstatus 101\n", false},
+ {"status_malformed", "running", main, main, "list\nstatus 101\n", false},
+ {"stop_failure", "running", main, main, "list\nstatus 101\nstop 101\n", false},
+ {"stop_no_effect", "running", main, main, "list\nstatus 101\nstop 101\nstatus 101\n", false},
+ {"stopped_read_failure", "running", main, main, "list\nstatus 101\nstop 101\nstatus 101\n", false},
+ {"lost_before_set", "running", main, main, "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\n", false},
+ {"lost_before_raw", "running", main, main, "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\n", false},
+ {"set_failure", "running", main, main, "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\nstart 101\nstatus 101\n", false},
+ {"write_failure", "running", main, main, "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\nstatus 101\nstart 101\nstatus 101\n", false},
+ {"lost_before_restore", "running", main, "hostname: workload\nmp1: /data,mp=/data\n", "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\nstatus 101\n", false},
+ {"start_failure", "running", main, "hostname: workload\nmp1: /data,mp=/data\n", "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\nstatus 101\nstart 101\n", false},
+ {"start_no_effect", "running", main, "hostname: workload\nmp1: /data,mp=/data\n", "list\nstatus 101\nstop 101\nstatus 101\nstatus 101\nset 101 -delete mp0\nstatus 101\nstatus 101\nstart 101\nstatus 101\n", false},
+ }
+ for _, tc := range cases {
+ // Adverse controls include a real snapshot section so the original
+ // script reaches its ignored-stop/mutation path (not its unrelated
+ // no-snapshot pipefail bug).
+ if !tc.success {
+ tc.conf += snapshot
+ tc.want += snapshot
+ }
+ t.Run(tc.name, func(t *testing.T) {
+ dir := t.TempDir()
+ script := sensorProxySafetyScript(t, dir)
+ conf := filepath.Join(dir, "101.conf")
+ if err := os.WriteFile(conf, []byte(tc.conf), 0640); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "state"), []byte(tc.initial+"\n"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ out, code := runSensorProxySafetyShell(t, script, dir, tc.name, sensorProxyMountFixture)
+ if (code == 0) != tc.success {
+ t.Fatalf("exit %d; success=%v\n%s", code, tc.success, out)
+ }
+ if !tc.success && !strings.Contains(out, "[WARN]") {
+ t.Fatalf("silent incomplete cleanup: %s", out)
+ }
+ got, err := os.ReadFile(conf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(got) != tc.want {
+ t.Fatalf("configuration changed incorrectly:\n%s\nwant:\n%s", got, tc.want)
+ }
+ info, err := os.Stat(conf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if info.Mode().Perm() != 0640 {
+ t.Fatalf("configuration permissions changed: %v", info.Mode())
+ }
+ actions, err := os.ReadFile(filepath.Join(dir, "actions"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(actions) != tc.actions {
+ t.Fatalf("lifecycle/actions:\n%s\nwant:\n%s", actions, tc.actions)
+ }
+ })
+ }
+}
+
+func TestUninstallSensorProxyMainReportsIncompleteLocalCleanup(t *testing.T) {
+ for _, phase := range []string{"services", "containers", "reload", "success"} {
+ t.Run(phase, func(t *testing.T) {
+ dir := t.TempDir()
+ body := `source "$1"
+mark() { printf '%s\n' "$1" >>"$FIXTURE_DIR/actions"; }
+disable_legacy_units() { mark services; [[ "$SCENARIO" != services ]]; }
+cleanup_cluster_authorized_keys() { mark keys; }
+cleanup_stale_sensor_proxy_mounts() { mark containers; [[ "$SCENARIO" != containers ]]; }
+remove_legacy_files() { mark files; }
+remove_proxmox_access() { mark access; }
+systemctl_if_available() { mark reload; [[ "$SCENARIO" != reload ]]; }
+main --local-only
+`
+ out, code := runSensorProxySafetyShell(t, repoFile("scripts", "uninstall-sensor-proxy.sh"), dir, phase, body)
+ if (code == 0) != (phase == "success") {
+ t.Fatalf("exit %d:\n%s", code, out)
+ }
+ if strings.Contains(out, "Legacy pulse-sensor-proxy cleanup complete") != (phase == "success") {
+ t.Fatalf("incorrect completion message: %s", out)
+ }
+ want := "services\n"
+ if phase != "services" {
+ want += "keys\ncontainers\n"
+ }
+ if phase == "reload" || phase == "success" {
+ want += "files\naccess\nreload\n"
+ }
+ got, err := os.ReadFile(filepath.Join(dir, "actions"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(got) != want {
+ t.Fatalf("actions: %s want: %s", got, want)
+ }
+ })
+ }
+}
+
+func TestUninstallSensorProxyServiceSafety(t *testing.T) {
+ for _, scenario := range []string{"absent", "inactive", "read_failure", "empty", "stop_failure", "still_active", "state_failure", "disable_failure", "reload_failure"} {
+ t.Run(scenario, func(t *testing.T) {
+ dir := t.TempDir()
+ body := `source "$1"
+systemctl() {
+ printf '%s\n' "$*" >>"$FIXTURE_DIR/actions"
+ case "$1" in
+ show)
+ if [[ "$3" == --property=LoadState ]]; then
+ case "$SCENARIO" in absent) echo not-found;; read_failure) return 9;; empty) :;; *) echo loaded;; esac
+ else
+ case "$SCENARIO" in still_active) echo active;; state_failure) return 9;; *) echo inactive;; esac
+ fi ;;
+ stop) [[ "$SCENARIO" != stop_failure ]] ;;
+ disable) [[ "$SCENARIO" != disable_failure ]] ;;
+ daemon-reload) [[ "$SCENARIO" != reload_failure ]] ;;
+ *) return 90 ;;
+ esac
+}
+disable_legacy_units
+`
+ out, code := runSensorProxySafetyShell(t, repoFile("scripts", "uninstall-sensor-proxy.sh"), dir, scenario, body)
+ if (code == 0) != (scenario == "absent" || scenario == "inactive") {
+ t.Fatalf("exit %d:\n%s", code, out)
+ }
+ got, err := os.ReadFile(filepath.Join(dir, "actions"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if scenario == "absent" && (strings.Contains(string(got), "stop ") || strings.Contains(string(got), "disable ")) {
+ t.Fatalf("absent units mutated: %s", got)
+ }
+ if (scenario == "stop_failure" || scenario == "still_active" || scenario == "state_failure" || scenario == "empty" || scenario == "read_failure") && strings.Contains(string(got), "disable ") {
+ t.Fatalf("unconfirmed unit disabled: %s", got)
+ }
+ })
+ }
+}
diff --git a/scripts/installtests/uninstall_sensor_proxy_test.go b/scripts/installtests/uninstall_sensor_proxy_test.go
index 0e42e2265f..f53b274015 100644
--- a/scripts/installtests/uninstall_sensor_proxy_test.go
+++ b/scripts/installtests/uninstall_sensor_proxy_test.go
@@ -270,10 +270,10 @@ func TestUninstallSensorProxyScriptRemovesTempFootprintAndManagedKeys(t *testing
if err := os.MkdirAll(binDir, 0755); err != nil {
t.Fatalf("mkdir bin dir: %v", err)
}
- if err := os.WriteFile(filepath.Join(binDir, "systemctl"), []byte("#!/bin/sh\nexit 0\n"), 0755); err != nil {
+ if err := os.WriteFile(filepath.Join(binDir, "systemctl"), []byte("#!/bin/sh\ncase \"$1\" in show) echo not-found;; esac\nexit 0\n"), 0755); err != nil {
t.Fatalf("write systemctl stub: %v", err)
}
- if err := os.WriteFile(filepath.Join(binDir, "pct"), []byte("#!/bin/sh\nexit 0\n"), 0755); err != nil {
+ if err := os.WriteFile(filepath.Join(binDir, "pct"), []byte("#!/bin/sh\necho \"VMID Status Lock Name\"\nexit 0\n"), 0755); err != nil {
t.Fatalf("write pct stub: %v", err)
}
diff --git a/scripts/uninstall-sensor-proxy.sh b/scripts/uninstall-sensor-proxy.sh
index c28f7d4a82..5c347566a5 100755
--- a/scripts/uninstall-sensor-proxy.sh
+++ b/scripts/uninstall-sensor-proxy.sh
@@ -85,13 +85,17 @@ PY
systemctl_if_available() {
if ! command -v systemctl >/dev/null 2>&1; then
- return 0
+ print_warn "systemctl is unavailable; legacy service cleanup is unconfirmed"
+ return 1
fi
- systemctl "$@" >/dev/null 2>&1 || true
+ systemctl "$@" >/dev/null 2>&1
}
disable_legacy_units() {
local unit=""
+ local load_state=""
+ local active_state=""
+ local result=0
local units=(
pulse-sensor-proxy.service
pulse-sensor-proxy-selfheal.timer
@@ -100,11 +104,35 @@ disable_legacy_units() {
pulse-sensor-cleanup.service
)
+ if ! command -v systemctl >/dev/null 2>&1; then
+ print_warn "systemctl is unavailable; legacy service cleanup is unconfirmed"
+ return 1
+ fi
for unit in "${units[@]}"; do
- systemctl_if_available stop "$unit"
- systemctl_if_available disable "$unit"
+ if ! load_state=$(systemctl show "$unit" --property=LoadState --value 2>/dev/null); then
+ print_warn "Cannot inspect legacy unit ${unit}; cleanup is incomplete"
+ result=1
+ continue
+ fi
+ [[ "$load_state" == "not-found" ]] && continue
+ if [[ -z "$load_state" ]] || ! systemctl_if_available stop "$unit"; then
+ print_warn "Cannot stop legacy unit ${unit}; cleanup is incomplete"
+ result=1
+ continue
+ fi
+ if ! active_state=$(systemctl show "$unit" --property=ActiveState --value 2>/dev/null) ||
+ [[ "$active_state" != "inactive" && "$active_state" != "failed" ]]; then
+ print_warn "Legacy unit ${unit} is not confirmed stopped; cleanup is incomplete"
+ result=1
+ continue
+ fi
+ if ! systemctl_if_available disable "$unit"; then
+ print_warn "Cannot disable legacy unit ${unit}; cleanup is incomplete"
+ result=1
+ fi
done
- systemctl_if_available daemon-reload
+ systemctl_if_available daemon-reload || result=1
+ return "$result"
}
remove_managed_keys_from_authorized_keys_file() {
@@ -257,88 +285,137 @@ cleanup_cluster_authorized_keys() {
return "$status"
}
-main_section_snapshot_line() {
- local conf="$1"
- grep -n '^\[' "$conf" 2>/dev/null | head -1 | cut -d: -f1
+# Only current (pre-snapshot) mount entries are eligible for cleanup.
+sensor_proxy_mount_keys() {
+ awk '
+ /^\[/ { exit }
+ /^mp[0-9]+:.*pulse-sensor-proxy/ { sub(/:.*/, ""); print }
+ /^lxc\.mount\.entry:.*pulse-sensor-proxy/ { print "lxc.mount.entry" }
+ ' "$1"
}
cleanup_sensor_proxy_lines_in_conf() {
local conf="$1"
- local snapshot_line="${2:-}"
local tmp_file=""
- tmp_file=$(mktemp)
- if [[ -n "$snapshot_line" && "$snapshot_line" -gt 1 ]]; then
- awk -v snapshot_line="$snapshot_line" '
- NR < snapshot_line && ($0 ~ /^mp[0-9]+:.*pulse-sensor-proxy/ || $0 ~ /^lxc\.mount\.entry:.*pulse-sensor-proxy/) { next }
- { print }
- ' "$conf" >"$tmp_file"
- else
- awk '
- $0 ~ /^mp[0-9]+:.*pulse-sensor-proxy/ { next }
- $0 ~ /^lxc\.mount\.entry:.*pulse-sensor-proxy/ { next }
- { print }
- ' "$conf" >"$tmp_file"
+ tmp_file=$(mktemp) || return 1
+ if ! awk '
+ /^\[/ { snapshot = 1 }
+ !snapshot && ($0 ~ /^mp[0-9]+:.*pulse-sensor-proxy/ || $0 ~ /^lxc\.mount\.entry:.*pulse-sensor-proxy/) { next }
+ { print }
+ ' "$conf" >"$tmp_file" ||
+ ! chmod --reference="$conf" "$tmp_file" ||
+ ! chown --reference="$conf" "$tmp_file" ||
+ ! mv "$tmp_file" "$conf"; then
+ rm -f "$tmp_file"
+ return 1
fi
+}
- chmod --reference="$conf" "$tmp_file" 2>/dev/null || true
- chown --reference="$conf" "$tmp_file" 2>/dev/null || true
- mv "$tmp_file" "$conf"
+sensor_proxy_container_status() {
+ local status=""
+ status=$(pct status "$1" 2>/dev/null) || return 1
+ case "$status" in
+ "status: running") printf 'running\n' ;;
+ "status: stopped") printf 'stopped\n' ;;
+ *) return 1 ;;
+ esac
+}
+
+cleanup_sensor_proxy_container() {
+ local ctid="$1"
+ local conf="$2"
+ local keys=""
+ local key=""
+ local status=""
+ local was_running=false
+ local result=0
+
+ keys=$(sensor_proxy_mount_keys "$conf") || return 1
+ [[ -z "$keys" ]] && return 0
+ print_info "Cleaning stale pulse-sensor-proxy mount entries from container ${ctid}"
+ if ! status=$(sensor_proxy_container_status "$ctid"); then
+ print_warn "Container ${ctid} status is unknown; no configuration edits or restart attempted"
+ return 1
+ fi
+ if [[ "$status" == "running" ]]; then
+ was_running=true
+ if ! timeout --kill-after=1s 30 pct stop "$ctid" >/dev/null 2>&1; then
+ print_warn "Container ${ctid} stop failed; no configuration edits or restart attempted; check its state locally"
+ return 1
+ fi
+ fi
+ # A successful stop command is not a stopped-state observation. Recheck
+ # before every mutation, including the direct legacy mount-line removal.
+ if ! status=$(sensor_proxy_container_status "$ctid") || [[ "$status" != "stopped" ]]; then
+ print_warn "Container ${ctid} is not confirmed stopped; no configuration edits or restart attempted"
+ return 1
+ fi
+ while IFS= read -r key; do
+ [[ "$key" == "lxc.mount.entry" ]] && continue
+ if ! status=$(sensor_proxy_container_status "$ctid") || [[ "$status" != "stopped" ]]; then
+ print_warn "Container ${ctid} stopped state was lost; no further edits or restart attempted"
+ return 1
+ fi
+ if ! timeout --kill-after=1s 15 pct set "$ctid" -delete "$key" >/dev/null 2>&1; then
+ print_warn "Container ${ctid} mount ${key} removal failed; no direct configuration fallback attempted"
+ result=1
+ break
+ fi
+ done <<<"$keys"
+ if (( result == 0 )); then
+ if ! status=$(sensor_proxy_container_status "$ctid") || [[ "$status" != "stopped" ]]; then
+ print_warn "Container ${ctid} stopped state was lost; no further edits or restart attempted"
+ return 1
+ fi
+ if ! cleanup_sensor_proxy_lines_in_conf "$conf"; then
+ print_warn "Container ${ctid} configuration cleanup failed"
+ result=1
+ elif ! keys=$(sensor_proxy_mount_keys "$conf") || [[ -n "$keys" ]]; then
+ print_warn "Container ${ctid} still has legacy mount entries; cleanup is incomplete"
+ result=1
+ fi
+ fi
+ if [[ "$was_running" == "true" ]]; then
+ if ! status=$(sensor_proxy_container_status "$ctid") || [[ "$status" != "stopped" ]]; then
+ print_warn "Container ${ctid} state is unconfirmed for restoration; no restart attempted"
+ return 1
+ fi
+ if ! timeout --kill-after=1s 30 pct start "$ctid" >/dev/null 2>&1 ||
+ ! status=$(sensor_proxy_container_status "$ctid") || [[ "$status" != "running" ]]; then
+ print_warn "Container ${ctid} was running before cleanup but its running state was not restored; check it locally"
+ result=1
+ fi
+ fi
+ return "$result"
}
cleanup_stale_sensor_proxy_mounts() {
local ctid=""
local conf=""
- local snapshot_line=""
- local status=""
- local was_running=false
- local mp_keys=""
- local mp_key=""
- local cleaned=0
+ local containers=""
+ local result=0
if ! command -v pct >/dev/null 2>&1; then
- return 0
+ print_warn "pct is unavailable; LXC mount cleanup is unconfirmed"
+ return 1
+ fi
+ if ! containers=$(pct list 2>/dev/null) || [[ "$containers" != VMID* ]]; then
+ print_warn "Cannot list local containers; LXC mount cleanup is incomplete"
+ return 1
fi
-
while IFS= read -r ctid; do
[[ -z "$ctid" ]] && continue
+ if [[ ! "$ctid" =~ ^[0-9]+$ ]]; then
+ print_warn "Invalid container identity; LXC mount cleanup is incomplete"
+ result=1
+ continue
+ fi
conf="/etc/pve/lxc/${ctid}.conf"
[[ -f "$conf" ]] || continue
- grep -q 'pulse-sensor-proxy' "$conf" 2>/dev/null || continue
-
- print_info "Cleaning stale pulse-sensor-proxy mount entries from container ${ctid}"
- status=$(pct status "$ctid" 2>/dev/null | awk '{print $2}') || true
- was_running=false
- [[ "$status" == "running" ]] && was_running=true
-
- if [[ "$was_running" == "true" ]]; then
- timeout 30 pct stop "$ctid" >/dev/null 2>&1 || true
- sleep 2
- fi
-
- snapshot_line=$(main_section_snapshot_line "$conf")
- if [[ -n "$snapshot_line" && "$snapshot_line" -gt 1 ]]; then
- mp_keys=$(head -n "$((snapshot_line - 1))" "$conf" 2>/dev/null | grep -E '^mp[0-9]+:.*pulse-sensor-proxy' | sed 's/:.*//') || true
- else
- mp_keys=$(grep -E '^mp[0-9]+:.*pulse-sensor-proxy' "$conf" 2>/dev/null | sed 's/:.*//') || true
- fi
-
- while IFS= read -r mp_key; do
- [[ -z "$mp_key" ]] && continue
- timeout 15 pct set "$ctid" -delete "$mp_key" >/dev/null 2>&1 || true
- cleaned=$((cleaned + 1))
- done <<<"$mp_keys"
-
- cleanup_sensor_proxy_lines_in_conf "$conf" "$snapshot_line"
-
- if [[ "$was_running" == "true" ]]; then
- timeout 30 pct start "$ctid" >/dev/null 2>&1 || print_warn "Container ${ctid} was running before cleanup but could not be restarted automatically"
- fi
- done < <(pct list 2>/dev/null | tail -n +2 | awk '{print $1}')
-
- if (( cleaned > 0 )); then
- print_success "Removed legacy pulse-sensor-proxy mount entries from Proxmox LXC config(s)"
- fi
+ cleanup_sensor_proxy_container "$ctid" "$conf" || result=1
+ done < <(printf '%s\n' "$containers" | tail -n +2 | awk '{print $1}')
+ return "$result"
}
remove_path_if_present() {
@@ -443,6 +520,7 @@ PY
main() {
local cluster_key_cleanup_status=0
+ local container_cleanup_status=0
while [[ $# -gt 0 ]]; do
case "$1" in
@@ -496,12 +574,22 @@ main() {
done
print_info "Starting legacy pulse-sensor-proxy cleanup"
- disable_legacy_units
+ if ! disable_legacy_units; then
+ print_warn "Legacy service cleanup is incomplete; leaving keys, containers, files and API access unchanged"
+ return 1
+ fi
cleanup_cluster_authorized_keys || cluster_key_cleanup_status=$?
- cleanup_stale_sensor_proxy_mounts
+ cleanup_stale_sensor_proxy_mounts || container_cleanup_status=$?
+ if (( container_cleanup_status != 0 )); then
+ print_warn "LXC cleanup is incomplete; leaving legacy files and API access unchanged; check affected containers locally"
+ return "$container_cleanup_status"
+ fi
remove_legacy_files
remove_proxmox_access
- systemctl_if_available daemon-reload
+ if ! systemctl_if_available daemon-reload; then
+ print_warn "Cannot reload service configuration; cleanup is incomplete"
+ return 1
+ fi
if (( cluster_key_cleanup_status != 0 )); then
print_warn "Local cleanup completed, but one or more remote cluster nodes still require trusted, local cleanup"
return "$cluster_key_cleanup_status"