Fix remote Compose status parsing
Homelab Main / validate (push) Successful in 1m19s Details
Homelab Main / running-update (push) Successful in 3s Details
Homelab Main / debian-ops (push) Successful in 2m35s Details

This commit is contained in:
juvenal.diaz 2026-11-11 09:02:50 -06:00
parent 01d99f09d8
commit 70d5843a5f
3 changed files with 73 additions and 5 deletions

View File

@ -25,6 +25,9 @@ they may have been stopped intentionally. Discovery uses one Docker snapshot
per host; services with no container cannot be inferred from that snapshot.
Remote snapshots require noninteractive SSH and sudo and warn when access is
unavailable. The OCI edge snapshot uses its inventoried Tailscale address.
SSH diagnostics are kept separate from the container data, and login banners
are ignored only before the remote snapshot marker. Unexpected data inside
the snapshot remains an error.
The cluster scope lists Argo CD application health and sync state from one
snapshot. Degraded or missing applications are failures; progressing, unknown,

View File

@ -66,22 +66,36 @@ status_focus_dns() {
# One snapshot per host, with one report row for each Compose service instance.
status_focus_compose_report() {
local area="$1" host="$2" user="$3" key="$4" snapshot rows severity name detail
local area="$1" host="$2" user="$3" key="$4" snapshot rows severity name detail err_file
local marker='__JEANNIE_COMPOSE_SNAPSHOT_V1__'
local -a docker_cmd=(sudo -n docker ps -a --filter label=com.docker.compose.project --format '{{json .}}')
err_file="$(mktemp "${REPORT_UI_WORK_DIR:-${TMPDIR:-/tmp}}/compose-stderr.XXXXXX")" || {
report_ui_emit warn "${area}" "Compose inventory" "Cannot create diagnostic file" ""; return 0;
}
if [[ "${host}" == local ]]; then
snapshot="$(timeout 8 "${docker_cmd[@]}" 2>&1)" || {
report_ui_emit warn "${area}" "Compose inventory" "Cannot read Docker containers" "${snapshot}"; return 0;
snapshot="$(timeout 8 "${docker_cmd[@]}" 2>"${err_file}")" || {
detail="$(<"${err_file}")"; rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Cannot read Docker containers" "${detail}"; return 0;
}
else
if [[ -z "${host}" || -z "${user}" ]]; then
rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Host or user missing from inventory" ""; return 0
fi
snapshot="$(timeout 8 ssh -n -i "${key}" -o BatchMode=yes -o ConnectTimeout=3 \
-o ConnectionAttempts=1 -o StrictHostKeyChecking=yes "${user}@${host}" \
"sudo -n docker ps -a --filter label=com.docker.compose.project --format '{{json .}}'" 2>&1)" || {
report_ui_emit warn "${area}" "Compose inventory" "Cannot read remote Docker containers" "${snapshot}"; return 0;
"printf '%s\\n' '${marker}'; sudo -n docker ps -a --filter label=com.docker.compose.project --format '{{json .}}'" 2>"${err_file}")" || {
detail="$(<"${err_file}")"; rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Cannot read remote Docker containers" "${detail}"; return 0;
}
if [[ "${snapshot}" != "${marker}" && "${snapshot}" != *$'\n'"${marker}" && "${snapshot}" != "${marker}"$'\n'* && "${snapshot}" != *$'\n'"${marker}"$'\n'* ]]; then
rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Remote Docker snapshot marker missing" "Remote command returned unexpected output"; return 0
fi
snapshot="${snapshot#*"${marker}"}"
snapshot="${snapshot#$'\n'}"
fi
rm -f "${err_file}"
rows="$(printf '%s\n' "${snapshot}" | python3 "${REPO_ROOT}/scripts/status-app-snapshot.py" compose 2>&1)" || {
report_ui_emit warn "${area}" "Compose inventory" "Cannot interpret Docker snapshot" "${rows}"; return 0;
}

View File

@ -1,6 +1,7 @@
"""Exercise selection and profile logic without contacting homelab hosts."""
import os
import json
import subprocess
import unittest
from pathlib import Path
@ -73,6 +74,56 @@ class FocusedStatusTests(unittest.TestCase):
self.assertIn("Orange Pi|Native K3s checks|skip", result)
self.assertNotIn("Orange Pi|K3s runtime|", result)
def test_remote_compose_snapshot_ignores_banner_and_stderr(self):
container = json.dumps({
"Names": "nginx", "State": "running", "Status": "Up 2 minutes",
"Labels": "com.docker.compose.project=edge,com.docker.compose.service=nginx",
})
env = os.environ.copy()
env.update(REPO_ROOT=str(ROOT), MOCK_CONTAINER=container)
code = r'''
source "$1"
timeout() { shift; "$@"; }
ssh() {
printf 'Welcome to the edge\n__JEANNIE_COMPOSE_SNAPSHOT_V1__\n%s\n' "$MOCK_CONTAINER"
printf 'bash: warning: setlocale failed\n' >&2
}
report_ui_emit() { printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4"; }
status_focus_compose_report 'OCI edge Compose' example ubuntu /key
'''
result = subprocess.run(["bash", "-c", code, "test", str(SCRIPT)],
env=env, capture_output=True, text=True, check=True)
self.assertIn("ok|OCI edge Compose|edge/nginx (nginx)|", result.stdout)
self.assertNotIn("Cannot interpret", result.stdout)
def test_remote_compose_failure_reports_stderr(self):
env = os.environ.copy()
env["REPO_ROOT"] = str(ROOT)
code = r'''
source "$1"
timeout() { shift; "$@"; }
ssh() { printf 'permission denied\n' >&2; return 1; }
report_ui_emit() { printf '%s|%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" "$5"; }
status_focus_compose_report 'OCI edge Compose' example ubuntu /key
'''
result = subprocess.run(["bash", "-c", code, "test", str(SCRIPT)],
env=env, capture_output=True, text=True, check=True)
self.assertIn("Cannot read remote Docker containers|permission denied", result.stdout)
def test_remote_compose_rejects_output_without_marker(self):
env = os.environ.copy()
env["REPO_ROOT"] = str(ROOT)
code = r'''
source "$1"
timeout() { shift; "$@"; }
ssh() { printf 'unexpected output\n'; }
report_ui_emit() { printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4"; }
status_focus_compose_report 'OCI edge Compose' example ubuntu /key
'''
result = subprocess.run(["bash", "-c", code, "test", str(SCRIPT)],
env=env, capture_output=True, text=True, check=True)
self.assertIn("Remote Docker snapshot marker missing", result.stdout)
if __name__ == "__main__":
unittest.main()