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.

Detach the Unraid boot entry so later go lines run

rcourtman4f3e1706123e
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index fabcbb890c..af8464bd54 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -160,6 +160,33 @@ Unraid/QNAP/Linux/PowerShell, TLS/identity/repetition/cleanup and containing
publication remain separate obligations; this source proof does not resolve
#1099 or claim installed restoration.
+### Unraid boot entry starts its watchdog detached
+
+Unraid's `rc.local` runs `/boot/config/go` to completion, and the generated
+`start-pulse-agent.sh` wrapper is an endless watchdog loop. The installer's
+`go` entry (`render_unraid_go_launch`) therefore backgrounds the wrapper with
+its output redirected. A foreground launch never returns, so every line kept
+after the Pulse entry, such as swap or a hosts entry the agent needs, never
+runs and the host reports as down after a reboot. Installation replaces only
+the `# Pulse Agent` marker and command lines that name a `start-pulse-agent.sh`
+wrapper or this runtime binary outside a comment, which covers earlier
+foreground, hand-edited and old-location entries; a comment is set aside only
+when it is certain to be one. A launch reached only indirectly, such as a
+relative path after `cd`, is not detected. Other lines that mention
+pulse-agent, comments and co-installed agents stay. Line endings become LF, as
+Unraid's `fromdos` makes them before running `go`, and a replacement that would
+leave `go` unparseable is refused with a warning instead. Removal admits the
+detached entry and the earlier foreground form for the selected wrapper.
+
+`TestInstallSHUnraidGoEntryDoesNotBlockLaterBootLines` executes the generated
+`go` with its output on a pipe and requires it to return, release the pipe, run
+the next line and start the wrapper; a planted foreground entry must hang the
+same harness. `TestInstallSHUnraidGoEntryReplacesOnlyPulseLaunches` pins the
+replacement, the syntax refusal and the removal filter's admission of the
+written entry, and the NAS teardown controls render their Unraid entry through
+the same emitter. These are private bash fixtures, not native Unraid boot
+acceptance.
+
### Patrol maintenance coverage adds no agent authority
The Patrol operator-state projection in `internal/api/router.go` now reads the
diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md
index a8d4d6f5a9..bca1c17a8e 100644
--- a/docs/release-control/v6/internal/subsystems/deployment-installability.md
+++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md
@@ -103,6 +103,33 @@ Unraid/QNAP/Linux/PowerShell, TLS/identity/repetition/cleanup and containing
publication remain separate obligations; this source proof does not resolve
#1099 or claim installed restoration.
+### Unraid boot entry starts its watchdog detached
+
+Unraid's `rc.local` runs `/boot/config/go` to completion, and the generated
+`start-pulse-agent.sh` wrapper is an endless watchdog loop. The installer's
+`go` entry (`render_unraid_go_launch`) therefore backgrounds the wrapper with
+its output redirected. A foreground launch never returns, so every line kept
+after the Pulse entry, such as swap or a hosts entry the agent needs, never
+runs and the host reports as down after a reboot. Installation replaces only
+the `# Pulse Agent` marker and command lines that name a `start-pulse-agent.sh`
+wrapper or this runtime binary outside a comment, which covers earlier
+foreground, hand-edited and old-location entries; a comment is set aside only
+when it is certain to be one. A launch reached only indirectly, such as a
+relative path after `cd`, is not detected. Other lines that mention
+pulse-agent, comments and co-installed agents stay. Line endings become LF, as
+Unraid's `fromdos` makes them before running `go`, and a replacement that would
+leave `go` unparseable is refused with a warning instead. Removal admits the
+detached entry and the earlier foreground form for the selected wrapper.
+
+`TestInstallSHUnraidGoEntryDoesNotBlockLaterBootLines` executes the generated
+`go` with its output on a pipe and requires it to return, release the pipe, run
+the next line and start the wrapper; a planted foreground entry must hang the
+same harness. `TestInstallSHUnraidGoEntryReplacesOnlyPulseLaunches` pins the
+replacement, the syntax refusal and the removal filter's admission of the
+written entry, and the NAS teardown controls render their Unraid entry through
+the same emitter. These are private bash fixtures, not native Unraid boot
+acceptance.
+
### TrueNAS SCALE removal retires every owned middleware boot task before recovery erasure
The [agent lifecycle removal contract](agent-lifecycle.md#truenas-scale-removal-retires-every-owned-middleware-boot-task-before-recovery-erasure)
diff --git a/scripts/install.sh b/scripts/install.sh
index 07ea20b7d3..ddf5472b6c 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -3913,7 +3913,7 @@ nas_agent_removal_paths_safe() {
# line containing pulse-agent; that also deletes unrelated tools and installs.
# status returns one fixed word, never the source (which may contain secrets).
filter_nas_agent_boot_file() {
- local platform="$1" path="$2" mode="$3" size="" expected="" last=""
+ local platform="$1" path="$2" mode="$3" size="" expected="" last="" unraid_launch=""
if [[ ! -e "$path" && ! -L "$path" ]]; then
[[ "$mode" != status ]] || printf 'absent\n'
return 0
@@ -3927,11 +3927,14 @@ filter_nas_agent_boot_file() {
fi
if [[ "$platform" == QNAP ]]; then
expected=$(render_qnap_autorun_block "$STATE_DIR/start-pulse-agent.sh") || return 1
+ else
+ unraid_launch=$(render_unraid_go_launch "$STATE_DIR/start-pulse-agent.sh") || return 1
fi
- PULSE_NAS_BOOT_EXPECTED="$expected" LC_ALL=C awk -v platform="$platform" -v mode="$mode" \
+ PULSE_NAS_BOOT_EXPECTED="$expected" PULSE_NAS_BOOT_LAUNCH="$unraid_launch" LC_ALL=C awk -v platform="$platform" -v mode="$mode" \
-v wrapper="$STATE_DIR/start-pulse-agent.sh" -v runtime="$INSTALL_DIR/$BINARY_NAME" '
function launch(s) {
- if (platform == "Unraid") return s == "bash " wrapper || s == "bash \"" wrapper "\""
+ # Installers before the detached entry launched the wrapper in the foreground.
+ if (platform == "Unraid") return s == ENVIRON["PULSE_NAS_BOOT_LAUNCH"] || s == "bash " wrapper || s == "bash \"" wrapper "\""
return s == wrapper " &" || s == "\"" wrapper "\" &" ||
s == "sh " wrapper " &" || s == "sh \"" wrapper "\" &"
}
@@ -4129,6 +4132,77 @@ teardown_nas_agent_boot_owner() {
confirm_nas_agent_removal_quiescent "$platform"
}
+# Unraid's rc.local runs go (as /var/tmp/go, after fromdos) to completion, and
+# the wrapper is an endless watchdog loop. Launched in the foreground it never
+# returns, so every later line in go is skipped. Background it with its output
+# redirected so it also holds no descriptor that rc.local may be waiting on.
+render_unraid_go_launch() {
+ printf 'bash %q >> %q 2>&1 &\n' "$1" "/var/log/${AGENT_NAME}.log"
+}
+
+# go is the user's boot script. Remove only the installer's marker and command
+# lines that reference a Pulse wrapper or this runtime binary, which covers
+# foreground, hand-edited and old-location entries, so the new entry is the
+# only launch. Other lines that mention pulse-agent (log directories, hosts
+# entries, a co-installed agent, comments) stay. Line endings become LF, as
+# Unraid's fromdos makes them before running go, so removal can parse the file.
+# A removed line must not leave go unparseable either: bash stops at a syntax
+# error, which skips the rest of boot just as a hang does.
+write_unraid_go_entry() {
+ local go_script="$1" wrapper_script="$2" runtime_binary="$3" filtered=""
+ if [[ -f "$go_script" ]]; then
+ filtered=$(mktemp) || return 1
+ if ! LC_ALL=C awk -v runtime="$runtime_binary" '
+ # Whether s names word as a whole file name: neither neighbour can
+ # continue the name. A runtime path must not continue a longer path.
+ function refers(s, word, slash, rest, i, b, a) {
+ rest = s
+ while ((i = index(rest, word)) > 0) {
+ b = i > 1 ? substr(rest, i - 1, 1) : ""
+ a = substr(rest, i + length(word), 1)
+ if (b !~ /[A-Za-z0-9._-]/ && (slash || b != "/") && a !~ /[A-Za-z0-9._-]/) return 1
+ rest = substr(rest, i + length(word))
+ }
+ return 0
+ }
+ # Text before a comment, only when the comment is certain. A
+ # backslash, an expansion, a backquote or an unterminated quote
+ # makes the line doubtful, and a doubtful line is still checked.
+ function code(s, i, c, q) {
+ q = ""
+ for (i = 1; i <= length(s); i++) {
+ c = substr(s, i, 1)
+ if (q == "\047") { if (c == "\047") q = ""; continue }
+ if (c == "\\" || c == "$" || c == "`") return s
+ if (q == "\"") { if (c == "\"") q = ""; continue }
+ if (c == "\"" || c == "\047") { q = c; continue }
+ if (c == "#" && (i == 1 || substr(s, i - 1, 1) ~ /[[:space:];&|(]/)) return substr(s, 1, i - 1)
+ }
+ return s
+ }
+ {
+ line = $0
+ sub(/\r$/, "", line)
+ if (line == "# Pulse Agent") next
+ command = code(line)
+ if (refers(command, "start-pulse-agent.sh", 1) || refers(command, runtime, 0)) next
+ print line
+ }
+ ' "$go_script" > "$filtered" ||
+ { LC_ALL=C awk '{ sub(/\r$/, ""); print }' "$go_script" | bash -n 2>/dev/null &&
+ ! bash -n "$filtered" 2>/dev/null; } ||
+ ! cat "$filtered" > "$go_script"; then
+ rm -f "$filtered"
+ return 1
+ fi
+ rm -f "$filtered"
+ else
+ printf '#!/bin/bash\n' > "$go_script" || return 1
+ chmod +x "$go_script"
+ fi
+ { printf '# Pulse Agent\n'; render_unraid_go_launch "$wrapper_script"; } >> "$go_script"
+}
+
remove_qnap_autorun_block() {
local autorun_path="$1"
@@ -7552,28 +7626,15 @@ done
EOF
- # Add to /boot/config/go if not already present
- GO_MARKER="# Pulse Agent"
- if [[ -f "$GO_SCRIPT" ]]; then
- # Remove any existing Pulse agent entries (line-by-line, not range-based)
- sed -i "/^${GO_MARKER}$/d" "$GO_SCRIPT" 2>/dev/null || true
- sed -i '/pulse-agent/d' "$GO_SCRIPT" 2>/dev/null || true
+ # Replace the startup entry in /boot/config/go. The entry runs the wrapper
+ # through bash because /boot is FAT32 and has no execute bits.
+ if write_unraid_go_entry "$GO_SCRIPT" "$WRAPPER_SCRIPT" "$RUNTIME_BINARY"; then
+ log_info "Added startup entry to ${GO_SCRIPT}..."
else
- # Create go script if it doesn't exist
- echo "#!/bin/bash" > "$GO_SCRIPT"
- chmod +x "$GO_SCRIPT"
+ log_warn "Could not update ${GO_SCRIPT} safely. To start the agent on boot, replace its Pulse Agent lines with:"
+ log_warn " $(render_unraid_go_launch "$WRAPPER_SCRIPT")"
fi
- # Append startup entry (use bash explicitly since /boot is FAT32 and doesn't support execute bits)
- cat >> "$GO_SCRIPT" <<EOF
-
-${GO_MARKER}
-bash ${WRAPPER_SCRIPT}
-
-EOF
-
- log_info "Added startup entry to ${GO_SCRIPT}..."
-
# Start the agent now using the wrapper script (includes watchdog)
# Use shell backgrounding instead of nohup for broader compatibility (QNAP, etc.)
log_info "Starting agent with watchdog..."
diff --git a/scripts/installtests/agent_nas_teardown.py b/scripts/installtests/agent_nas_teardown.py
index 53bca0b3c7..d0f6d4bb4a 100644
--- a/scripts/installtests/agent_nas_teardown.py
+++ b/scripts/installtests/agent_nas_teardown.py
@@ -26,6 +26,7 @@ FUNCTIONS = (
"read_nas_agent_removal_processes", "nas_agent_processes_absent", "quiesce_nas_agent_for_removal",
"confirm_nas_agent_removal_quiescent", "teardown_nas_agent_boot_owner",
"remove_qnap_autorun_block", "render_qnap_autorun_block", "append_qnap_autorun_block",
+ "render_unraid_go_launch", "write_unraid_go_entry",
)
@@ -224,23 +225,24 @@ class AgentNASTeardown(unittest.TestCase):
bootfile = root / "flash/autorun.sh" if platform == "QNAP" else root / "go"
unrelated = "#!/bin/sh\necho before\nbash /other/start-pulse-agent-prod.sh\necho pulse-agent-prod\n/boot/config/pulse/telegraf/start_telegraf.sh\n"
bootfile.write_text(unrelated)
- producer = definitions(self.source, ("portable_sed_in_place", "remove_qnap_autorun_block", "render_qnap_autorun_block", "append_qnap_autorun_block"))
+ producer = definitions(self.source, ("portable_sed_in_place", "remove_qnap_autorun_block", "render_qnap_autorun_block", "append_qnap_autorun_block", "render_unraid_go_launch", "write_unraid_go_entry"))
+ # Consume the real production boot emitters, not a second literal.
if platform == "QNAP":
- # Consume the real production bootstrap emitter, not a second literal.
command = "AGENT_NAME=pulse-agent\nSTATE_DIR=" + shlex.quote(str(root / "state")) + "\n" + producer + "\nappend_qnap_autorun_block " + shlex.quote(str(bootfile)) + " " + shlex.quote(str(target)) + " " + shlex.quote(str(root / "state"))
- subprocess.run(["bash", "-c", command], check=True, capture_output=True, text=True)
else:
- bootfile.write_text(unrelated + "# Pulse Agent\nbash " + str(target) + "\n")
+ command = "AGENT_NAME=pulse-agent\n" + producer + "\nwrite_unraid_go_entry " + shlex.quote(str(bootfile)) + " " + shlex.quote(str(target)) + " " + shlex.quote(str(root / "bin/pulse-agent"))
+ subprocess.run(["bash", "-c", command], check=True, capture_output=True, text=True)
bootfile.write_text(bootfile.read_text() + "echo after\n")
if boot == "duplicate":
if platform == "QNAP":
block = bootfile.read_text().split("# Pulse Agent bootstrap begin", 1)[1].split("# Pulse Agent bootstrap end", 1)[0]
bootfile.write_text(bootfile.read_text() + "# Pulse Agent bootstrap begin" + block + "# Pulse Agent bootstrap end\n")
+ # Earlier installers' foreground entry still retires with the current one.
else: bootfile.write_text(bootfile.read_text() + "# Pulse Agent\nbash " + str(target) + "\n")
elif boot == "foreign": bootfile.write_text(bootfile.read_text().replace(str(target), "/other/start-pulse-agent.sh"))
elif boot == "indirect": bootfile.write_text(bootfile.read_text() + "bash -c 'bash " + str(target) + "'\n")
elif boot == "unterminated": bootfile.write_text(bootfile.read_text().replace("# Pulse Agent bootstrap end", "# unknown") if platform == "QNAP" else bootfile.read_text() + "# Pulse Agent\n")
- elif boot == "changed-block": bootfile.write_text(bootfile.read_text().replace("_pulse_wait_max=1800", "_pulse_wait_max=999") if platform == "QNAP" else bootfile.read_text().replace("bash " + str(target), "bash " + str(target) + " --unknown"))
+ elif boot == "changed-block": bootfile.write_text(bootfile.read_text().replace("_pulse_wait_max=1800", "_pulse_wait_max=999") if platform == "QNAP" else bootfile.read_text().replace(str(target) + " >>", str(target) + " --unknown >>"))
elif boot == "no-newline": bootfile.write_text(bootfile.read_text().rstrip("\n"))
elif boot == "binary": bootfile.write_bytes(bootfile.read_bytes() + b'\0\n')
elif boot == "oversized": bootfile.write_text(bootfile.read_text() + "#" * 1048577 + "\n")
@@ -624,7 +626,11 @@ remove_safe_profile_state_dir() { rm -rf -- "$SAFE_PROFILE_STATE_DIR"; }
render_file.write_text(setup + emitter + '\nappend_qnap_autorun_block ' + shlex.quote(str(bootfile)) + ' "$WRAPPER_SCRIPT" "$STATE_DIR"\n')
rendered = subprocess.run(['bash', str(render_file)], env=env, capture_output=True, text=True)
self.assertEqual(rendered.returncode, 0, rendered.stdout + rendered.stderr)
- else: bootfile.write_text('#!/bin/bash\n# Pulse Agent\nbash ' + str(watchdog) + '\n')
+ else:
+ emitter = definitions(self.source, ('render_unraid_go_launch', 'write_unraid_go_entry'))
+ render_file.write_text(setup + emitter + '\nwrite_unraid_go_entry ' + shlex.quote(str(bootfile)) + ' "$WRAPPER_SCRIPT" "$RUNTIME_BINARY"\n')
+ rendered = subprocess.run(['bash', str(render_file)], env=env, capture_output=True, text=True)
+ self.assertEqual(rendered.returncode, 0, rendered.stdout + rendered.stderr)
functions = definitions(self.source)
functions = functions.replace('/boot/config/go', str(bootfile))
functions = functions.replace('[[ -d /boot/config && ! -L /boot/config ]]', '[[ -d ' + str(root) + ' && ! -L ' + str(root) + ' ]]')
diff --git a/scripts/installtests/install_sh_test.go b/scripts/installtests/install_sh_test.go
index 386c8799bb..c6240f9609 100644
--- a/scripts/installtests/install_sh_test.go
+++ b/scripts/installtests/install_sh_test.go
@@ -2858,107 +2858,268 @@ func TestInstallSHUsesCanonicalSysVEnablementHelper(t *testing.T) {
}
}
-// TestUnraidGoScriptCleanup verifies the sed commands that remove pulse entries
-// from /boot/config/go without disturbing other entries.
-func TestUnraidGoScriptCleanup(t *testing.T) {
- cases := []struct {
- name string
- before string
- after string
- }{
- {
- name: "pulse entry with trailing blank line",
- before: `#!/bin/bash
-/boot/config/pulse/telegraf/start_telegraf.sh
-
-# Pulse Agent
-bash /boot/config/plugins/pulse-agent/start-pulse-agent.sh
+func runUnraidGoWriter(functions, goScript, wrapper, runtimeBinary string) ([]byte, error) {
+ script := "AGENT_NAME=pulse-agent\n" + functions + "\nwrite_unraid_go_entry \"$1\" \"$2\" \"$3\"\n"
+ return exec.Command("bash", "-c", script, "bash", goScript, wrapper, runtimeBinary).CombinedOutput()
+}
-# Other stuff
-echo hello
-`,
- // The comment and command lines are removed; the trailing blank line remains.
- // This is harmless in /boot/config/go.
- after: `#!/bin/bash
-/boot/config/pulse/telegraf/start_telegraf.sh
+func unraidGoEntryShellFunctions(t *testing.T) string {
+ t.Helper()
+ return extractInstallShellFunction(t, "render_unraid_go_launch") + "\n" +
+ extractInstallShellFunction(t, "write_unraid_go_entry") + "\n"
+}
+// TestInstallSHUnraidGoEntryReplacesOnlyPulseLaunches runs the installer's
+// /boot/config/go writer. go is the user's boot script: only the installer's
+// marker and command lines that reference a Pulse wrapper or the runtime
+// binary are replaced. The earlier cleanup, sed '/pulse-agent/d', also deleted
+// any user line that merely mentioned pulse-agent. Removal must admit the
+// written entry, and a removal that would leave go unparseable is refused.
+func TestInstallSHUnraidGoEntryReplacesOnlyPulseLaunches(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Unraid boot-file fixture needs bash")
+ }
+ dir := t.TempDir()
+ stateDir := filepath.Join(dir, "plugins", "pulse-agent")
+ installDir := filepath.Join(dir, "bin")
+ wrapper := filepath.Join(stateDir, "start-pulse-agent.sh")
+ runtimeBinary := filepath.Join(installDir, "pulse-agent")
+ entry := "# Pulse Agent\nbash " + wrapper + " >> /var/log/pulse-agent.log 2>&1 &\n"
+ userTop := "#!/bin/bash\n/boot/config/pulse/telegraf/start_telegraf.sh\nmkdir -p /var/log/pulse-agent\n"
+ userRest := "# pulse-agent reaches its server through this hosts entry\n" +
+ "echo '192.0.2.10 delly' >> /etc/hosts\n" +
+ "bash /boot/config/plugins/pulse-agent-prod/start-pulse-agent-prod.sh &\n" +
+ runtimeBinary + "-prod --url https://prod.example.invalid &\n" +
+ "swapon /mnt/cache/swapfile # pulse-agent needs swap\n"
+ user := userTop + userRest
+ manualIf := "#!/bin/bash\nif [ -x " + runtimeBinary + " ]; then\n echo manual agent\nfi\n"
-# Other stuff
-echo hello
-`,
+ cases := []struct {
+ name string
+ before string // empty: go does not exist yet
+ after string
+ refused bool
+ // Removal refuses any command line naming the wrapper, even in a
+ // trailing comment; that conservatism is removal's, not the writer's.
+ skipRemoval bool
+ }{
+ {
+ name: "missing go is created",
+ after: "#!/bin/bash\n" + entry,
+ },
+ {
+ name: "user script without an entry",
+ before: user,
+ after: user + entry,
+ },
+ {
+ // Exactly what earlier installers appended; its blank lines stay.
+ name: "foreground entry from earlier installers",
+ before: userTop + "\n# Pulse Agent\nbash " + wrapper + "\n\n" + userRest,
+ after: userTop + "\n\n" + userRest + entry,
},
{
- name: "pulse entry without trailing blank line",
- before: `#!/bin/bash
-# Pulse Agent
-bash /boot/config/plugins/pulse-agent/start-pulse-agent.sh
-# Other stuff
-echo hello
-`,
- after: `#!/bin/bash
-# Other stuff
-echo hello
-`,
+ // Unraid runs go through fromdos, so CRLF lines become LF.
+ name: "hand-edited, old-location and direct runtime launches",
+ before: strings.ReplaceAll(userTop, "\n", "\r\n") +
+ "# Pulse Agent\r\n" +
+ "setsid bash " + wrapper + " >/dev/null 2>&1 < /dev/null &\r\n" +
+ "bash " + wrapper + ">/dev/null\n" +
+ "echo \"starting #1\"; bash " + wrapper + "\n" +
+ "echo starting\\ #1; bash " + wrapper + "\n" +
+ "echo $'starting \\' #1'; bash " + wrapper + "\n" +
+ "echo ${v:- #1}; bash " + wrapper + "\n" +
+ "cd " + stateDir + "; w=start-pulse-agent.sh; bash \"$w\"\n" +
+ "bash " + wrapper + "${x:+}\n" +
+ "bash /mnt/user/appdata/old/start-pulse-agent.sh\n" +
+ runtimeBinary + " --url https://pulse.example.invalid &\n" +
+ userRest,
+ after: user + entry,
},
{
- name: "no pulse entries - unchanged",
- before: `#!/bin/bash
-echo hello
-echo world
-`,
- after: `#!/bin/bash
-echo hello
-echo world
-`,
+ name: "command whose comment names the wrapper",
+ before: "#!/bin/bash\nswapon /mnt/cache/swapfile # start-pulse-agent.sh needs this\n",
+ after: "#!/bin/bash\nswapon /mnt/cache/swapfile # start-pulse-agent.sh needs this\n" + entry,
+ skipRemoval: true,
},
{
- name: "telegraf line containing pulse is kept",
- before: `#!/bin/bash
-/boot/config/pulse/telegraf/start_telegraf.sh
-# Pulse Agent
-bash /boot/config/plugins/pulse-agent/start-pulse-agent.sh
-
-echo done
-`,
- // The telegraf line is NOT removed (no "pulse-agent" in it).
- // Comment and command lines are deleted individually.
- after: `#!/bin/bash
-/boot/config/pulse/telegraf/start_telegraf.sh
-
-echo done
-`,
+ name: "removal that would break go's syntax",
+ before: manualIf,
+ after: manualIf,
+ refused: true,
+ },
+ {
+ // Judged as fromdos leaves it, not by the raw CRLF bytes.
+ name: "removal that would break CRLF go's syntax",
+ before: strings.ReplaceAll(manualIf, "\n", "\r\n"),
+ after: strings.ReplaceAll(manualIf, "\n", "\r\n"),
+ refused: true,
},
}
- for _, tc := range cases {
+ functions := unraidGoEntryShellFunctions(t)
+ removal := extractInstallShellFunction(t, "portable_path_uid") + "\n" +
+ extractInstallShellFunction(t, "nas_agent_removal_file_safe") + "\n" +
+ extractInstallShellFunction(t, "filter_nas_agent_boot_file") + "\n" +
+ extractInstallShellFunction(t, "render_unraid_go_launch") + "\n"
+ for i, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
- dir := t.TempDir()
- goScript := filepath.Join(dir, "go")
- if err := os.WriteFile(goScript, []byte(tc.before), 0755); err != nil {
- t.Fatal(err)
+ goScript := filepath.Join(dir, fmt.Sprintf("go-%d", i))
+ if tc.before != "" {
+ if err := os.WriteFile(goScript, []byte(tc.before), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for run := 1; run <= 2; run++ {
+ out, err := runUnraidGoWriter(functions, goScript, wrapper, runtimeBinary)
+ if tc.refused != (err != nil) {
+ t.Fatalf("write_unraid_go_entry run %d: err=%v, want refused=%v\n%s", run, err, tc.refused, out)
+ }
+ got, err := os.ReadFile(goScript)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(got) != tc.after {
+ t.Fatalf("go after install %d:\n--- got ---\n%s\n--- want ---\n%s", run, got, tc.after)
+ }
+ }
+ if tc.refused || tc.skipRemoval {
+ return
}
- // Run the exact same sed commands from install.sh (line-by-line, not range-based)
- script := `
- GO_SCRIPT="` + goScript + `"
- # Remove unified agent entries
- sed -i '' '/^# Pulse Agent$/d' "$GO_SCRIPT" 2>/dev/null || sed -i '/^# Pulse Agent$/d' "$GO_SCRIPT" 2>/dev/null || true
- sed -i '' '/pulse-agent/d' "$GO_SCRIPT" 2>/dev/null || sed -i '/pulse-agent/d' "$GO_SCRIPT" 2>/dev/null || true
- `
- out, err := exec.Command("bash", "-c", script).CombinedOutput()
+ script := "AGENT_NAME=pulse-agent BINARY_NAME=pulse-agent STATE_DIR=\"$1\" INSTALL_DIR=\"$2\"\n" + removal +
+ "\nfilter_nas_agent_boot_file Unraid \"$3\" status && filter_nas_agent_boot_file Unraid \"$3\" filter\n"
+ out, err := exec.Command("bash", "-c", script, "bash", stateDir, installDir, goScript).CombinedOutput()
if err != nil {
- t.Fatalf("bash: %v\n%s", err, out)
+ t.Fatalf("removal filter refused the installed entry: %v\n%s", err, out)
+ }
+ if want := "present\n" + strings.TrimSuffix(tc.after, entry); string(out) != want {
+ t.Fatalf("removal filter:\n--- got ---\n%s\n--- want ---\n%s", out, want)
}
+ })
+ }
+}
- got, err := os.ReadFile(goScript)
- if err != nil {
- t.Fatal(err)
+// TestInstallSHUnraidGoEntryDoesNotBlockLaterBootLines guards a real incident.
+// The go entry ran the watchdog wrapper in the foreground, and the wrapper
+// never returns, so after a reboot every line below it in go (swap, a hosts
+// entry the agent needed) was skipped and the host showed as down for two days.
+// Execute the go file the installer writes, with output on a pipe as a boot
+// logger would read it: it must return, release the pipe, run the next line
+// and start the wrapper. The planted foreground entry must hang the harness.
+func TestInstallSHUnraidGoEntryDoesNotBlockLaterBootLines(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Unraid boot-file fixture needs bash")
+ }
+ dir := t.TempDir()
+ logDir := filepath.Join(dir, "log")
+ if err := os.Mkdir(logDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ wrapper := filepath.Join(dir, "start-pulse-agent.sh")
+ pidFile := filepath.Join(dir, "wrapper.pid")
+ // Like the generated watchdog loop, this wrapper does not return. The PID
+ // is renamed into place so a reader never sees a partial write.
+ stub := "#!/bin/bash\necho $$ > '" + pidFile + ".tmp' && mv '" + pidFile + ".tmp' '" + pidFile + "'\nexec sleep 120\n"
+ if err := os.WriteFile(wrapper, []byte(stub), 0755); err != nil {
+ t.Fatal(err)
+ }
+ waitForWrapper := func(limit time.Duration) (int, bool) {
+ deadline := time.Now().Add(limit)
+ for {
+ if data, err := os.ReadFile(pidFile); err == nil {
+ if pid, err := strconv.Atoi(strings.TrimSpace(string(data))); err == nil {
+ return pid, true
+ }
}
+ if time.Now().After(deadline) {
+ return 0, false
+ }
+ time.Sleep(50 * time.Millisecond)
+ }
+ }
+ stopWrapper := func() {
+ if pid, ok := waitForWrapper(5 * time.Second); ok {
+ if process, err := os.FindProcess(pid); err == nil {
+ _ = process.Kill()
+ }
+ }
+ _ = os.Remove(pidFile)
+ }
+ t.Cleanup(stopWrapper)
- if string(got) != tc.after {
- t.Errorf("go script mismatch:\n--- got ---\n%s\n--- want ---\n%s", got, tc.after)
+ runGo := func(goScript string, limit time.Duration) bool {
+ cmd := exec.Command("bash", goScript)
+ var out strings.Builder
+ cmd.Stdout = &out
+ cmd.Stderr = &out
+ // Bound the output drain once bash exits, even if a child kept the pipe.
+ cmd.WaitDelay = 5 * time.Second
+ if err := cmd.Start(); err != nil {
+ t.Fatal(err)
+ }
+ done := make(chan error, 1)
+ go func() { done <- cmd.Wait() }()
+ select {
+ case err := <-done:
+ if errors.Is(err, exec.ErrWaitDelay) {
+ t.Fatalf("go returned but its entry kept the boot output pipe open\n%s", out.String())
}
- })
+ if err != nil {
+ t.Fatalf("go exited with %v\n%s", err, out.String())
+ }
+ return true
+ case <-time.After(limit):
+ _ = cmd.Process.Kill()
+ stopWrapper()
+ <-done
+ return false
+ }
+ }
+
+ after := filepath.Join(dir, "after-pulse-entry")
+ userAfter := "touch '" + after + "'\n"
+
+ planted := filepath.Join(dir, "go-foreground")
+ if err := os.WriteFile(planted, []byte("#!/bin/bash\n# Pulse Agent\nbash "+wrapper+"\n"+userAfter), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if runGo(planted, 2*time.Second) {
+ t.Fatal("planted foreground entry returned; the harness cannot detect a blocking go entry")
+ }
+ if _, err := os.Stat(after); err == nil {
+ t.Fatal("planted foreground entry ran the line after it")
+ }
+
+ // The entry logs under /var/log, which the test cannot write.
+ functions := strings.ReplaceAll(unraidGoEntryShellFunctions(t), "/var/log/", logDir+"/")
+ goScript := filepath.Join(dir, "go")
+ if err := os.WriteFile(goScript, []byte("#!/bin/bash\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if out, err := runUnraidGoWriter(functions, goScript, wrapper, filepath.Join(dir, "pulse-agent")); err != nil {
+ t.Fatalf("write_unraid_go_entry: %v\n%s", err, out)
+ }
+ f, err := os.OpenFile(goScript, os.O_APPEND|os.O_WRONLY, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := f.WriteString(userAfter); err != nil {
+ t.Fatal(err)
+ }
+ if err := f.Close(); err != nil {
+ t.Fatal(err)
+ }
+
+ if !runGo(goScript, 20*time.Second) {
+ contents, _ := os.ReadFile(goScript)
+ t.Fatalf("go did not return: the Pulse entry blocks every later boot line\n%s", contents)
+ }
+ if _, err := os.Stat(after); err != nil {
+ t.Fatalf("the line after the Pulse entry did not run: %v", err)
+ }
+ if _, ok := waitForWrapper(10 * time.Second); !ok {
+ t.Fatal("the detached go entry did not start the wrapper")
}
}