mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 14:30:52 +00:00
fix(sys-ctl-hlth-runtime): skip a container that stopped before its PHP probe
A swarm task that recycles itself, such as the Shopware worker, can stop between the container listing and the php exec. The probe error then failed the whole scan. A failed probe now asks whether the container still runs: a stopped one is reported as info, a running one still fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4fe43fde81
commit
b80da444a1
2 files changed
+77
-4
No files matched your search
@@ -459,17 +459,50 @@ def probe_php(container_id: str) -> PhpProbe | None:
|
||||
return parse_probe(result.stdout)
|
||||
|
||||
|
||||
def container_running(container_id: str) -> bool:
|
||||
"""Tell whether a container still runs.
|
||||
|
||||
Args:
|
||||
container_id: the container to inspect.
|
||||
|
||||
Returns:
|
||||
``True`` while ``container inspect`` reports it running, ``False``
|
||||
once it stopped or is gone.
|
||||
"""
|
||||
result = subprocess.run(
|
||||
[
|
||||
"container",
|
||||
"inspect",
|
||||
"--type",
|
||||
"container",
|
||||
"--format",
|
||||
"{{.State.Running}}",
|
||||
container_id,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=EXEC_TIMEOUT_SECONDS,
|
||||
)
|
||||
return result.returncode == 0 and result.stdout.strip() == "true"
|
||||
|
||||
|
||||
def check(
|
||||
containers: list[Container],
|
||||
hardened: dict,
|
||||
probe,
|
||||
running,
|
||||
) -> tuple[list[str], list[str]]:
|
||||
"""Evaluate the PHP hardening of every container.
|
||||
|
||||
A container that stopped between the listing and its probe, as a swarm
|
||||
task that recycles itself does, is reported as info, not as a failure.
|
||||
|
||||
Args:
|
||||
containers: the running containers.
|
||||
hardened: ``{stack: [service, ...]}`` that must run hardened PHP.
|
||||
probe: callable returning a :class:`PhpProbe` or ``None`` per id.
|
||||
running: callable telling whether a container id still runs.
|
||||
|
||||
Returns:
|
||||
``(failures, infos)`` as printable lines.
|
||||
@@ -486,7 +519,10 @@ def check(
|
||||
try:
|
||||
result = probe(container.id)
|
||||
except ProbeError as exc:
|
||||
failures.append(f"{container.name}: {exc}")
|
||||
if running(container.id):
|
||||
failures.append(f"{container.name}: {exc}")
|
||||
else:
|
||||
infos.append(f"{container.name}: stopped before its probe, skipped")
|
||||
continue
|
||||
if result is None:
|
||||
if must_harden:
|
||||
@@ -2248,7 +2284,7 @@ def main(argv=None) -> int:
|
||||
except (subprocess.TimeoutExpired, subprocess.CalledProcessError) as exc:
|
||||
print(f"❌ listing the running containers failed: {exc}")
|
||||
return 1
|
||||
failures, infos = check(containers, hardened, probe_php)
|
||||
failures, infos = check(containers, hardened, probe_php, container_running)
|
||||
remote = host_network_mounts() if "host" in config.targets else []
|
||||
infos += [
|
||||
f"host scan skips the network or automounted filesystem at {printable(point)}"
|
||||
|
||||
@@ -2056,7 +2056,12 @@ def _probe(expose_php=False, upload_progress=False):
|
||||
|
||||
|
||||
def _check(containers, probes, hardened):
|
||||
return SCRIPT.check(containers, hardened, lambda container_id: probes[container_id])
|
||||
return SCRIPT.check(
|
||||
containers,
|
||||
hardened,
|
||||
lambda container_id: probes[container_id],
|
||||
lambda _container_id: True,
|
||||
)
|
||||
|
||||
|
||||
class TestCheck(TestCase):
|
||||
@@ -2112,7 +2117,7 @@ class TestCheck(TestCase):
|
||||
failures, ["wp: declares php_hardening but has no php to verify it"]
|
||||
)
|
||||
|
||||
def test_a_probe_error_in_a_hardened_stack_is_a_failure(self):
|
||||
def test_a_probe_error_in_a_running_container_is_a_failure(self):
|
||||
def broken(_container_id):
|
||||
raise SCRIPT.ProbeError("php exited 255: boom")
|
||||
|
||||
@@ -2120,9 +2125,41 @@ class TestCheck(TestCase):
|
||||
[_container("wp", "wordpress", "wordpress")],
|
||||
{"wordpress": ["wordpress"]},
|
||||
broken,
|
||||
lambda _container_id: True,
|
||||
)
|
||||
self.assertEqual(failures, ["wp: php exited 255: boom"])
|
||||
|
||||
def test_a_container_that_stopped_before_its_probe_is_info_only(self):
|
||||
def gone(_container_id):
|
||||
raise SCRIPT.ProbeError(
|
||||
"php exited 1: Error response from daemon: container x is not running"
|
||||
)
|
||||
|
||||
failures, infos = SCRIPT.check(
|
||||
[_container("worker", "shopware", "worker")],
|
||||
{"shopware": ["worker"]},
|
||||
gone,
|
||||
lambda _container_id: False,
|
||||
)
|
||||
self.assertEqual(failures, [])
|
||||
self.assertEqual(infos, ["worker: stopped before its probe, skipped"])
|
||||
|
||||
|
||||
class TestContainerRunning(TestCase):
|
||||
def _running(self, returncode, stdout):
|
||||
completed = SimpleNamespace(returncode=returncode, stdout=stdout, stderr="")
|
||||
with mock.patch.object(SCRIPT.subprocess, "run", return_value=completed):
|
||||
return SCRIPT.container_running("abc")
|
||||
|
||||
def test_a_running_container_runs(self):
|
||||
self.assertTrue(self._running(0, "true\n"))
|
||||
|
||||
def test_a_stopped_container_does_not_run(self):
|
||||
self.assertFalse(self._running(0, "false\n"))
|
||||
|
||||
def test_a_removed_container_does_not_run(self):
|
||||
self.assertFalse(self._running(1, ""))
|
||||
|
||||
|
||||
class TestProbePhp(TestCase):
|
||||
def _run(self, returncode, stdout="", stderr=""):
|
||||
|
||||
Reference in new issue
Block a user