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.

Fix FreeBSD agent update recovery

rcourtman7897a9131ebc
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index a11667f3eb..659f3c31eb 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -256,6 +256,13 @@ update, profile rollout, command reachability, or fleet-control authority.
smoke checks prove a live agent runtime, not merely a running service
wrapper.
25. `scripts/install.sh` shared with `deployment-installability`: the shell installer is both a deployment installability entry point and a canonical agent lifecycle runtime continuity boundary.
+ Legacy update recovery is cross-platform lifecycle continuity. Linux may
+ read procfs or a systemd unit, while FreeBSD and pfSense must recover the
+ same URL, token, feature, identity, and trust arguments from the live
+ process via `ps`/`procstat` or from the installed rc.d service script.
+ Successful recovery always migrates the raw legacy token into the v6
+ installer-owned token file; absence of complete local URL and token state
+ remains a fail-closed update, never a fresh enrollment.
Server update planning is part of the same lifecycle contract. The System
Updates plan must surface a structured upgrade-readiness verdict before an
@@ -1168,7 +1175,8 @@ surface and no new `internal/api/` lifecycle handler.
Persistence-sensitive NAS targets must keep one canonical continuity model here: installer-owned bootstraps may use flash-backed or immutable-root launch hooks only as thin trampolines, while the durable wrapper, state, and reboot-surviving binary copy stay in the governed persistent state directory that updater continuity also refreshes.
Unix `--update` re-entry must also preserve lifecycle identity for legacy
v5.1.x agents that do not yet have v6 `connection.env` state. When a
- running `pulse-agent` process or its systemd unit already carries the Pulse
+ running `pulse-agent` process, its systemd unit, or its FreeBSD rc.d service
+ script already carries the Pulse
URL, token, feature flags, agent id, hostname, or trust posture, the shell
installer may recover those values for the update handoff, but the resulting
v6 service must be rendered through the shared exec-argument builder and
diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md
index 6be38561ca..6e2e042598 100644
--- a/docs/release-control/v6/internal/subsystems/deployment-installability.md
+++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md
@@ -294,6 +294,13 @@ TLS floor in the dynamic config.
used Go's single-dash flag spelling, the installer-owned recovery path must
accept both single-dash and double-dash forms for recovered agent args
without weakening the existing missing-state failure behavior.
+ FreeBSD and pfSense updates have the same continuity obligation without
+ Linux procfs or systemd: the installer must read live process arguments via
+ `ps` (and environment via `procstat` when available), then fall back to the
+ installed rc.d service's `command_args` and `PULSE_*` exports. The parser
+ must preserve quoted argument values without evaluating service-file shell
+ content, and the rewritten rc.d service must use `--token-file` rather than
+ retaining a recovered raw token.
The shell installer must disclose `--enable-commands` as Pulse command
execution, disabled by default, and must name both Patrol actions and
Proxmox LXC Docker inventory as the operator-visible reasons to enable it.
@@ -805,7 +812,7 @@ TLS floor in the dynamic config.
1. Update this contract when canonical deployment or installer entry points move
2. Keep deployment runtime and shared API proof routing aligned in `registry.json`
-3. Preserve explicit coverage for installer parity, update planning, and deployment bootstrap behavior when these surfaces change. Shell installer update recovery changes must keep `scripts/installtests/install_sh_test.go` covering both persisted `connection.env` recovery and legacy running-process/service recovery, including single-dash v5 agent flags and the rule that upgraded service args use `--token-file` instead of raw `--token`.
+3. Preserve explicit coverage for installer parity, update planning, and deployment bootstrap behavior when these surfaces change. Shell installer update recovery changes must keep `scripts/installtests/install_sh_test.go` covering both persisted `connection.env` recovery and legacy running-process/service recovery across Linux and FreeBSD/rc.d, including single-dash v5 agent flags, non-procfs process inspection, and the rule that upgraded service args use `--token-file` instead of raw `--token`.
4. Keep stable and prerelease packet lineage explicit when `docs/releases/` or
`VERSION` changes: preserve already-shipped RC packets under dedicated
historical filenames before reusing canonical stable names, keep
diff --git a/scripts/install.sh b/scripts/install.sh
index 6534017334..7e572f2a6b 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -1847,25 +1847,120 @@ collect_running_agent_pids() {
done
}
+split_recovered_shell_words() {
+ local payload="$1"
+ local word=""
+ local quote=""
+ local char=""
+ local escaped="false"
+ local in_word="false"
+ local index=0
+
+ for ((index = 0; index < ${#payload}; index++)); do
+ char="${payload:index:1}"
+
+ if [[ "$escaped" == "true" ]]; then
+ word+="$char"
+ in_word="true"
+ escaped="false"
+ continue
+ fi
+
+ if [[ "$char" == "\\" && "$quote" != "'" ]]; then
+ escaped="true"
+ in_word="true"
+ continue
+ fi
+
+ if [[ -n "$quote" ]]; then
+ if [[ "$char" == "$quote" ]]; then
+ quote=""
+ else
+ word+="$char"
+ fi
+ in_word="true"
+ continue
+ fi
+
+ case "$char" in
+ "'"|'"')
+ quote="$char"
+ in_word="true"
+ ;;
+ ' '|$'\t'|$'\n')
+ if [[ "$in_word" == "true" ]]; then
+ printf '%s\n' "$word"
+ word=""
+ in_word="false"
+ fi
+ ;;
+ *)
+ word+="$char"
+ in_word="true"
+ ;;
+ esac
+ done
+
+ if [[ "$escaped" == "true" ]]; then
+ word+="\\"
+ fi
+ if [[ "$in_word" == "true" ]]; then
+ printf '%s\n' "$word"
+ fi
+
+ [[ -z "$quote" ]]
+}
+
+running_agent_arg_stream() {
+ local pid="$1"
+ local proc_root="${2:-/proc}"
+ local payload=""
+
+ if [[ -r "${proc_root}/${pid}/cmdline" ]]; then
+ tr '\0' '\n' < "${proc_root}/${pid}/cmdline" 2>/dev/null
+ return 0
+ fi
+
+ command -v ps >/dev/null 2>&1 || return 1
+ payload=$(ps -ww -o command= -p "$pid" 2>/dev/null || true)
+ [[ -n "$payload" ]] || return 1
+ split_recovered_shell_words "$payload"
+}
+
+running_agent_env_stream() {
+ local pid="$1"
+ local proc_root="${2:-/proc}"
+
+ if [[ -r "${proc_root}/${pid}/environ" ]]; then
+ tr '\0' '\n' < "${proc_root}/${pid}/environ" 2>/dev/null
+ return 0
+ fi
+
+ # FreeBSD exposes process environment through procstat instead of procfs.
+ command -v procstat >/dev/null 2>&1 || return 1
+ procstat -e "$pid" 2>/dev/null | awk '
+ NR > 1 {
+ $1 = ""
+ $2 = ""
+ sub(/^[[:space:]]+/, "")
+ print
+ }
+ ' | tr ' ' '\n'
+}
+
recover_connection_state_from_running_agent() {
local pid=""
local recovered="false"
- [[ -d /proc ]] || return 1
-
while IFS= read -r pid; do
[[ -n "$pid" && "$pid" != "$$" ]] || continue
recovered="false"
- if [[ -r "/proc/${pid}/cmdline" ]]; then
- if recover_connection_state_from_arg_stream < <(tr '\0' '\n' < "/proc/${pid}/cmdline" 2>/dev/null); then
- recovered="true"
- fi
+ if recover_connection_state_from_arg_stream < <(running_agent_arg_stream "$pid"); then
+ recovered="true"
fi
- if [[ -r "/proc/${pid}/environ" ]]; then
- if recover_connection_state_from_env_stream < <(tr '\0' '\n' < "/proc/${pid}/environ" 2>/dev/null); then
- recovered="true"
- fi
+ if recover_connection_state_from_env_stream < <(running_agent_env_stream "$pid"); then
+ recovered="true"
fi
if [[ "$recovered" == "true" ]] && recovered_connection_state_ready; then
return 0
@@ -1893,7 +1988,7 @@ recover_connection_state_from_systemd_unit() {
case "$line" in
ExecStart=*)
payload="${line#ExecStart=}"
- if recover_connection_state_from_arg_stream < <(printf '%s\n' "$payload" | tr ' ' '\n'); then
+ if recover_connection_state_from_arg_stream < <(split_recovered_shell_words "$payload"); then
recovered="true"
fi
;;
@@ -1913,6 +2008,64 @@ recover_connection_state_from_systemd_unit() {
return 1
}
+recover_connection_state_from_service_scripts() {
+ local candidate=""
+ local line=""
+ local payload=""
+ local assignment=""
+ local key=""
+ local value=""
+ local recovered="false"
+ local candidates=(
+ "/usr/local/etc/rc.d/${AGENT_NAME}"
+ "${TRUENAS_STATE_DIR}/pulse-agent.service"
+ "/etc/init.d/${AGENT_NAME}"
+ "/etc/init/${AGENT_NAME}.conf"
+ "/boot/config/plugins/pulse-agent/start-pulse-agent.sh"
+ )
+
+ if (( $# > 0 )); then
+ candidates=("$@")
+ fi
+
+ for candidate in "${candidates[@]}"; do
+ [[ -n "$candidate" && -f "$candidate" ]] || continue
+ recovered="false"
+
+ while IFS= read -r line; do
+ line="${line#"${line%%[![:space:]]*}"}"
+ case "$line" in
+ command_args=*)
+ payload=$(strip_recovered_arg_quotes "${line#command_args=}")
+ if recover_connection_state_from_arg_stream < <(split_recovered_shell_words "$payload"); then
+ recovered="true"
+ fi
+ ;;
+ exec\ *)
+ payload="${line#exec }"
+ if recover_connection_state_from_arg_stream < <(split_recovered_shell_words "$payload"); then
+ recovered="true"
+ fi
+ ;;
+ export\ PULSE_*=*|PULSE_*=*)
+ assignment="${line#export }"
+ key="${assignment%%=*}"
+ value=$(strip_recovered_arg_quotes "${assignment#*=}")
+ if recover_connection_state_from_env_stream < <(printf '%s=%s\n' "$key" "$value"); then
+ recovered="true"
+ fi
+ ;;
+ esac
+ done < "$candidate"
+
+ if [[ "$recovered" == "true" ]] && recovered_connection_state_ready; then
+ return 0
+ fi
+ done
+
+ return 1
+}
+
recover_connection_state_from_existing_agent() {
if recover_connection_state_from_running_agent; then
log_info "Recovered connection details from the running Pulse Agent process."
@@ -1922,6 +2075,10 @@ recover_connection_state_from_existing_agent() {
log_info "Recovered connection details from the existing Pulse Agent service."
return 0
fi
+ if recover_connection_state_from_service_scripts; then
+ log_info "Recovered connection details from the existing Pulse Agent service script."
+ return 0
+ fi
return 1
}
diff --git a/scripts/installtests/install_sh_test.go b/scripts/installtests/install_sh_test.go
index e6de37d6f8..20e69a989c 100644
--- a/scripts/installtests/install_sh_test.go
+++ b/scripts/installtests/install_sh_test.go
@@ -681,6 +681,9 @@ func TestInstallSHSupportsSavedStateUpdateMode(t *testing.T) {
`recover_connection_state_from_existing_agent() {`,
`recover_connection_state_from_running_agent`,
`recover_connection_state_from_systemd_unit`,
+ `recover_connection_state_from_service_scripts`,
+ `running_agent_arg_stream() {`,
+ `running_agent_env_stream() {`,
`recover_connection_state_from_arg_stream`,
`recover_token_from_default_agent_token_file() {`,
`normalize_recovered_agent_arg_key() {`,
@@ -793,6 +796,177 @@ ARGS
}
}
+func TestInstallSHRecoversV5ProcessArgsWithoutProcfs(t *testing.T) {
+ script := `
+ PULSE_URL=""
+ PULSE_TOKEN=""
+ INTERVAL="30s"
+ INTERVAL_EXPLICIT="false"
+ ENABLE_HOST="true"
+ HOST_EXPLICIT="false"
+ ENABLE_DOCKER=""
+ DOCKER_EXPLICIT="false"
+ ENABLE_KUBERNETES=""
+ KUBERNETES_EXPLICIT="false"
+ KUBECONFIG_PATH=""
+ ENABLE_PROXMOX=""
+ PROXMOX_EXPLICIT="false"
+ PROXMOX_TYPE=""
+ INSECURE="false"
+ ENABLE_COMMANDS="false"
+ ENROLL="false"
+ HEALTH_ADDR=""
+ HEALTH_ADDR_SET="false"
+ AGENT_ID=""
+ HOSTNAME_OVERRIDE=""
+ STATE_DIR="/var/lib/pulse-agent"
+ TRUENAS_STATE_DIR="/data/pulse-agent"
+ CURL_CA_BUNDLE=""
+ KUBE_INCLUDE_ALL_PODS="false"
+ KUBE_INCLUDE_ALL_DEPLOYMENTS="false"
+ DISK_EXCLUDES=()
+ RUNTIME_TOKEN_FILE="/var/lib/pulse-agent/token"
+ BINARY_NAME="pulse-agent"
+` + extractInstallShellFunction(t, "strip_recovered_arg_quotes") + `
+` + extractInstallShellFunction(t, "normalize_recovered_agent_arg_key") + `
+` + extractInstallShellFunction(t, "apply_recovered_agent_arg_value") + `
+` + extractInstallShellFunction(t, "recovered_connection_state_ready") + `
+` + extractInstallShellFunction(t, "recover_token_from_default_agent_token_file") + `
+` + extractInstallShellFunction(t, "recover_connection_state_from_arg_stream") + `
+` + extractInstallShellFunction(t, "split_recovered_shell_words") + `
+` + extractInstallShellFunction(t, "running_agent_arg_stream") + `
+` + extractInstallShellFunction(t, "recover_connection_state_from_running_agent") + `
+` + extractInstallShellFunction(t, "build_exec_arg_items") + `
+` + extractInstallShellFunction(t, "join_exec_arg_items") + `
+` + extractInstallShellFunction(t, "build_exec_args") + `
+ collect_running_agent_pids() { printf '%s\n' 4242; }
+ running_agent_env_stream() { return 1; }
+ ps() {
+ printf '%s\n' "/usr/local/bin/pulse-agent -url http://192.168.2.96:7655 -token deadbeef -enable-host -enable-docker -insecure -agent-id firewall-1 -hostname 'edge firewall'"
+ }
+
+ if recover_connection_state_from_running_agent; then
+ echo "READY"
+ else
+ echo "NOT_READY"
+ fi
+ build_exec_args
+ printf 'URL=%s\nTOKEN=%s\nDOCKER=%s\nINSECURE=%s\nAGENT_ID=%s\nHOSTNAME=%s\nEXEC_ARGS=%s\n' \
+ "$PULSE_URL" "$PULSE_TOKEN" "$ENABLE_DOCKER" "$INSECURE" "$AGENT_ID" "$HOSTNAME_OVERRIDE" "$EXEC_ARGS"
+ `
+
+ out, err := exec.Command("bash", "-c", script).CombinedOutput()
+ if err != nil {
+ t.Fatalf("bash: %v\n%s", err, out)
+ }
+ got := string(out)
+ for _, needle := range []string{
+ "READY",
+ "URL=http://192.168.2.96:7655",
+ "TOKEN=deadbeef",
+ "DOCKER=true",
+ "INSECURE=true",
+ "AGENT_ID=firewall-1",
+ "HOSTNAME=edge firewall",
+ "--token-file /var/lib/pulse-agent/token",
+ } {
+ if !strings.Contains(got, needle) {
+ t.Fatalf("non-procfs process recovery missing %q:\n%s", needle, got)
+ }
+ }
+ if strings.Contains(got, "--token deadbeef") {
+ t.Fatalf("non-procfs process recovery leaked raw token into service args:\n%s", got)
+ }
+}
+
+func TestInstallSHRecoversV5FreeBSDRCServiceState(t *testing.T) {
+ rcScript := filepath.Join(t.TempDir(), "pulse-agent")
+ if err := os.WriteFile(rcScript, []byte(`#!/bin/sh
+command="/usr/local/bin/pulse-agent"
+command_args="-url http://192.168.2.96:7655 -token deadbeef -enable-host -insecure -agent-id firewall-1 -hostname 'edge firewall'"
+export PULSE_CACERT='/conf/pulse-ca.pem'
+`), 0600); err != nil {
+ t.Fatalf("write rc.d fixture: %v", err)
+ }
+
+ script := `
+ PULSE_URL=""
+ PULSE_TOKEN=""
+ INTERVAL="30s"
+ INTERVAL_EXPLICIT="false"
+ ENABLE_HOST="true"
+ HOST_EXPLICIT="false"
+ ENABLE_DOCKER=""
+ DOCKER_EXPLICIT="false"
+ ENABLE_KUBERNETES=""
+ KUBERNETES_EXPLICIT="false"
+ KUBECONFIG_PATH=""
+ ENABLE_PROXMOX=""
+ PROXMOX_EXPLICIT="false"
+ PROXMOX_TYPE=""
+ INSECURE="false"
+ ENABLE_COMMANDS="false"
+ ENROLL="false"
+ HEALTH_ADDR=""
+ HEALTH_ADDR_SET="false"
+ AGENT_ID=""
+ HOSTNAME_OVERRIDE=""
+ STATE_DIR="/var/lib/pulse-agent"
+ TRUENAS_STATE_DIR="/data/pulse-agent"
+ CURL_CA_BUNDLE=""
+ KUBE_INCLUDE_ALL_PODS="false"
+ KUBE_INCLUDE_ALL_DEPLOYMENTS="false"
+ DISK_EXCLUDES=()
+ RUNTIME_TOKEN_FILE="/var/lib/pulse-agent/token"
+ AGENT_NAME="pulse-agent"
+` + extractInstallShellFunction(t, "strip_recovered_arg_quotes") + `
+` + extractInstallShellFunction(t, "normalize_recovered_agent_arg_key") + `
+` + extractInstallShellFunction(t, "apply_recovered_agent_arg_value") + `
+` + extractInstallShellFunction(t, "recovered_connection_state_ready") + `
+` + extractInstallShellFunction(t, "recover_token_from_default_agent_token_file") + `
+` + extractInstallShellFunction(t, "recover_connection_state_from_arg_stream") + `
+` + extractInstallShellFunction(t, "recover_connection_state_from_env_stream") + `
+` + extractInstallShellFunction(t, "split_recovered_shell_words") + `
+` + extractInstallShellFunction(t, "recover_connection_state_from_service_scripts") + `
+` + extractInstallShellFunction(t, "build_exec_arg_items") + `
+` + extractInstallShellFunction(t, "join_exec_arg_items") + `
+` + extractInstallShellFunction(t, "build_exec_args") + `
+ if recover_connection_state_from_service_scripts "${PULSE_TEST_RC_SCRIPT:?}"; then
+ echo "READY"
+ else
+ echo "NOT_READY"
+ fi
+ build_exec_args
+ printf 'URL=%s\nTOKEN=%s\nINSECURE=%s\nAGENT_ID=%s\nHOSTNAME=%s\nCACERT=%s\nEXEC_ARGS=%s\n' \
+ "$PULSE_URL" "$PULSE_TOKEN" "$INSECURE" "$AGENT_ID" "$HOSTNAME_OVERRIDE" "$CURL_CA_BUNDLE" "$EXEC_ARGS"
+ `
+
+ cmd := exec.Command("bash", "-c", script)
+ cmd.Env = append(os.Environ(), "PULSE_TEST_RC_SCRIPT="+rcScript)
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("bash: %v\n%s", err, out)
+ }
+ got := string(out)
+ for _, needle := range []string{
+ "READY",
+ "URL=http://192.168.2.96:7655",
+ "TOKEN=deadbeef",
+ "INSECURE=true",
+ "AGENT_ID=firewall-1",
+ "HOSTNAME=edge firewall",
+ "CACERT=/conf/pulse-ca.pem",
+ "--token-file /var/lib/pulse-agent/token",
+ } {
+ if !strings.Contains(got, needle) {
+ t.Fatalf("FreeBSD rc.d recovery missing %q:\n%s", needle, got)
+ }
+ }
+ if strings.Contains(got, "--token deadbeef") {
+ t.Fatalf("FreeBSD rc.d recovery leaked raw token into service args:\n%s", got)
+ }
+}
+
func TestInstallSHRejectsPartialRecoveredProcessConnectionState(t *testing.T) {
script := `
PULSE_URL=""