feat(docker): add opt-in boot and shutdown units for hosts that run Docker with live-restore
docker_start_missed installs a boot unit that starts the containers dockerd's restore pass skipped after a reboot of the whole host. docker_stop_at_shutdown installs a unit that ends container processes at shutdown while the network and NFS shares are still up, so a container holding an NFS mount no longer stalls the shutdown. Both default to false and do nothing without /usr/bin/docker. Refs #369 Assisted-by: Claude Code
This commit is contained in:
1 parent
4a7189c067
commit
567e9cca7c
16 files changed
+917
-6
No files matched your search
@@ -2,3 +2,5 @@ COPYING no-smart-quotes
|
||||
LICENSES/GPL-3.0-only.txt no-smart-quotes
|
||||
LICENSES/GPL-3.0-or-later.txt no-smart-quotes
|
||||
roles/docker/molecule/default/files/build-stub-repo.sh shebang # run by the docker scenario, not by Ansible
|
||||
roles/docker/files/docker-start-missed shebang # a command the role installs on the host, run by systemd, not an Ansible module
|
||||
roles/docker/files/docker-stop-at-shutdown shebang # a command the role installs on the host, run by systemd, not an Ansible module
|
||||
@@ -0,0 +1,269 @@
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Run with: python3 -m unittest discover -s tests/scripts"""
|
||||
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
import unittest
|
||||
from datetime import UTC, datetime
|
||||
from types import SimpleNamespace
|
||||
|
||||
ROLE_FILES = pathlib.Path(__file__).resolve().parents[2] / "roles/docker/files"
|
||||
# The script has no .py suffix (it is installed as a command), so name the loader.
|
||||
loader = importlib.machinery.SourceFileLoader(
|
||||
"docker_start_missed", str(ROLE_FILES / "docker-start-missed")
|
||||
)
|
||||
spec = importlib.util.spec_from_loader("docker_start_missed", loader)
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
loader.exec_module(m)
|
||||
|
||||
BOOT = datetime(2026, 11, 17, 14, 40, 14, tzinfo=UTC)
|
||||
|
||||
|
||||
def container(
|
||||
name,
|
||||
policy="unless-stopped",
|
||||
code=255,
|
||||
finished="2026-11-17T14:40:16.660987249Z",
|
||||
running=False,
|
||||
status="exited",
|
||||
):
|
||||
return {
|
||||
"Name": f"/{name}",
|
||||
"HostConfig": {"RestartPolicy": {"Name": policy}},
|
||||
"State": {
|
||||
"Running": running,
|
||||
"Status": "running" if running else status,
|
||||
"ExitCode": code,
|
||||
"FinishedAt": finished,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class Missed(unittest.TestCase):
|
||||
def test_the_skipped_container_is_selected(self):
|
||||
self.assertEqual(m.missed([container("prometheus")], BOOT), ["prometheus"])
|
||||
|
||||
def test_running_container_is_left_alone(self):
|
||||
self.assertEqual(
|
||||
m.missed([container("grafana", running=True, code=0)], BOOT), []
|
||||
)
|
||||
|
||||
def test_a_manual_stop_stays_stopped(self):
|
||||
for code in (0, 137, 143, 1):
|
||||
self.assertEqual(m.missed([container("x", code=code)], BOOT), [], code)
|
||||
|
||||
def test_died_before_this_boot_is_not_ours(self):
|
||||
self.assertEqual(
|
||||
m.missed([container("x", finished="2026-11-16T10:00:00.1Z")], BOOT), []
|
||||
)
|
||||
|
||||
def test_boot_tolerance(self):
|
||||
self.assertEqual(
|
||||
m.missed([container("x", finished="2026-11-17T14:40:10Z")], BOOT),
|
||||
["x"],
|
||||
"within 5 s before the computed boot time",
|
||||
)
|
||||
self.assertEqual(
|
||||
m.missed([container("x", finished="2026-11-17T14:40:08Z")], BOOT), []
|
||||
)
|
||||
|
||||
def test_only_unless_stopped_and_always(self):
|
||||
for policy, expected in (
|
||||
("unless-stopped", ["x"]),
|
||||
("always", ["x"]),
|
||||
("on-failure", []),
|
||||
("no", []),
|
||||
("", []),
|
||||
):
|
||||
self.assertEqual(
|
||||
m.missed([container("x", policy=policy)], BOOT), expected, policy
|
||||
)
|
||||
|
||||
def test_never_started_container_has_no_finish_time(self):
|
||||
self.assertEqual(
|
||||
m.missed([container("x", finished="0001-01-01T00:00:00Z")], BOOT), []
|
||||
)
|
||||
self.assertEqual(m.missed([container("x", finished="")], BOOT), [])
|
||||
|
||||
def test_a_restarting_container_is_left_to_docker(self):
|
||||
self.assertEqual(m.missed([container("x", status="restarting")], BOOT), [])
|
||||
|
||||
def test_names_are_sorted_and_stripped(self):
|
||||
self.assertEqual(
|
||||
m.missed(
|
||||
[
|
||||
container("blackbox"),
|
||||
container("prometheus"),
|
||||
container("grafana", running=True),
|
||||
],
|
||||
BOOT,
|
||||
),
|
||||
["blackbox", "prometheus"],
|
||||
)
|
||||
|
||||
|
||||
class ParseTime(unittest.TestCase):
|
||||
def test_nanoseconds(self):
|
||||
self.assertEqual(
|
||||
m.parse_time("2026-11-17T14:40:16.660987249Z"),
|
||||
datetime(2026, 11, 17, 14, 40, 16, 660987, tzinfo=UTC),
|
||||
)
|
||||
|
||||
def test_no_fraction(self):
|
||||
self.assertEqual(
|
||||
m.parse_time("2026-11-17T14:40:16Z"),
|
||||
datetime(2026, 11, 17, 14, 40, 16, tzinfo=UTC),
|
||||
)
|
||||
|
||||
|
||||
class FakeDocker:
|
||||
"""Stands in for the docker CLI: `exited` is what `ps` and `inspect` report, `start_rc` the exit code per name."""
|
||||
|
||||
def __init__(self, exited, start_rc=None, info_rc=0, stays_up=True):
|
||||
self.exited, self.start_rc, self.info_rc, self.stays_up = (
|
||||
exited,
|
||||
start_rc or {},
|
||||
info_rc,
|
||||
stays_up,
|
||||
)
|
||||
self.calls = []
|
||||
|
||||
def __call__(self, *args):
|
||||
self.calls.append(args)
|
||||
out = SimpleNamespace(returncode=0, stdout="", stderr="")
|
||||
if args[0] == "info":
|
||||
out.returncode = self.info_rc
|
||||
elif args[0] == "ps":
|
||||
out.stdout = " ".join(f"id{i}" for i, _container in enumerate(self.exited))
|
||||
elif args[0] == "inspect" and args[1] != "-f":
|
||||
out.stdout = json.dumps(self.exited)
|
||||
elif args[0] == "inspect":
|
||||
out.stdout = "true\n" if self.stays_up else "false\n"
|
||||
elif args[0] == "start":
|
||||
out.returncode = self.start_rc.get(args[1], 0)
|
||||
out.stderr = "boom" if out.returncode else ""
|
||||
return out
|
||||
|
||||
def started(self):
|
||||
return [c[1] for c in self.calls if c[0] == "start"]
|
||||
|
||||
|
||||
class RunOnce(unittest.TestCase):
|
||||
def run_it(self, fake):
|
||||
logs = []
|
||||
rc = m.run_once(
|
||||
0, run=fake, sleep=lambda _s: None, boot_time=BOOT, log=logs.append
|
||||
)
|
||||
return rc, logs
|
||||
|
||||
def test_starts_the_skipped_ones_only(self):
|
||||
fake = FakeDocker([container("prometheus"), container("manual", code=0)])
|
||||
rc, logs = self.run_it(fake)
|
||||
self.assertEqual((rc, fake.started()), (0, ["prometheus"]))
|
||||
self.assertTrue(any("prometheus" in line for line in logs))
|
||||
|
||||
def test_nothing_to_do(self):
|
||||
fake = FakeDocker([])
|
||||
self.assertEqual(self.run_it(fake)[0], 0)
|
||||
self.assertEqual(fake.started(), [])
|
||||
|
||||
def test_a_failed_start_fails_the_unit_but_the_others_still_start(self):
|
||||
fake = FakeDocker([container("a"), container("b")], start_rc={"a": 1})
|
||||
rc, logs = self.run_it(fake)
|
||||
self.assertEqual((rc, fake.started()), (1, ["a", "b"]))
|
||||
self.assertTrue(any("could not start a" in line for line in logs))
|
||||
|
||||
def test_a_container_that_does_not_stay_up_fails_the_unit(self):
|
||||
rc, logs = self.run_it(FakeDocker([container("a")], stays_up=False))
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertTrue(any("did not stay up" in line for line in logs))
|
||||
|
||||
def test_dry_run_reports_and_starts_nothing(self):
|
||||
fake = FakeDocker([container("prometheus")])
|
||||
logs = []
|
||||
rc = m.run_once(
|
||||
0,
|
||||
run=fake,
|
||||
sleep=lambda _s: None,
|
||||
boot_time=BOOT,
|
||||
log=logs.append,
|
||||
dry_run=True,
|
||||
)
|
||||
self.assertEqual((rc, fake.started()), (0, []))
|
||||
self.assertEqual(logs, ["would start: prometheus"])
|
||||
|
||||
def test_inspect_failing_is_a_docker_problem(self):
|
||||
class Fake(FakeDocker):
|
||||
def __call__(self, *args):
|
||||
out = super().__call__(*args)
|
||||
if args[0] == "inspect":
|
||||
out.returncode, out.stdout, out.stderr = 1, "", "daemon error"
|
||||
return out
|
||||
|
||||
rc, logs = self.run_it(Fake([container("a")]))
|
||||
self.assertEqual(rc, 2)
|
||||
self.assertTrue(any("inspect failed" in line for line in logs))
|
||||
|
||||
def test_a_removed_container_does_not_stop_the_rest(self):
|
||||
class Fake(FakeDocker):
|
||||
def __call__(self, *args):
|
||||
out = super().__call__(*args)
|
||||
if args[0] == "inspect" and args[1] != "-f":
|
||||
out.returncode = 1 # docker prints the containers it found and exits 1 for the missing one
|
||||
return out
|
||||
|
||||
fake = Fake([container("a")])
|
||||
self.assertEqual((self.run_it(fake)[0], fake.started()), (0, ["a"]))
|
||||
|
||||
def test_unreadable_inspect_output_is_a_docker_problem(self):
|
||||
class Fake(FakeDocker):
|
||||
def __call__(self, *args):
|
||||
out = super().__call__(*args)
|
||||
if args[0] == "inspect" and args[1] != "-f":
|
||||
out.stdout = "not json"
|
||||
return out
|
||||
|
||||
fake = Fake([container("a")])
|
||||
self.assertEqual((self.run_it(fake)[0], fake.started()), (2, []))
|
||||
|
||||
def test_a_timestamp_in_an_unexpected_format_is_a_docker_problem(self):
|
||||
fake = FakeDocker([container("a", finished="Nov 17 14:40:16 2026")])
|
||||
self.assertEqual((self.run_it(fake)[0], fake.started()), (2, []))
|
||||
|
||||
def test_ps_failing_is_a_docker_problem(self):
|
||||
class Fake(FakeDocker):
|
||||
def __call__(self, *args):
|
||||
out = super().__call__(*args)
|
||||
if args[0] == "ps":
|
||||
out.returncode, out.stderr = 1, "denied"
|
||||
return out
|
||||
|
||||
self.assertEqual(self.run_it(Fake([container("a")]))[0], 2)
|
||||
|
||||
def test_docker_never_answers(self):
|
||||
fake = FakeDocker([container("a")], info_rc=1)
|
||||
rc, _logs = self.run_it(fake)
|
||||
self.assertEqual((rc, fake.started()), (2, []))
|
||||
|
||||
|
||||
class StopAtShutdownScript(unittest.TestCase):
|
||||
def test_it_never_calls_docker_stop_or_kill(self):
|
||||
# Both set the manually-stopped marker, so a container with restart policy unless-stopped would not start at the next boot.
|
||||
code = [
|
||||
line
|
||||
for line in (ROLE_FILES / "docker-stop-at-shutdown")
|
||||
.read_text()
|
||||
.splitlines()
|
||||
if not line.lstrip().startswith("#")
|
||||
]
|
||||
self.assertEqual(
|
||||
[line for line in code if re.search(r"\bdocker\s+(stop|kill)\b", line)], []
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user