mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-12-03 08:17:52 +00:00
feat(constructor): stop a deploy in a pre-flight when the host cannot carry it
The constructor now runs tasks/utils/constructor/preflight.yml before the package install and fails with a named remedy when: the running kernel has no module index on disk or the clock is not synchronised (chrony leap status, else timedatectl, outside containers); / or the docker root is fuller than SIZE_PERCENT_MAXIMUM_DISK_USAGE (90); a docker network contradicts the entity that declares it (name, compose project label, subnet outside its block, overlap with another declared subnet); a declared container lacks or a declared volume carries a foreign entity label; a docker default address pool overlaps the mesh, Tor or a declared subnet; a host process outside docker holds a public port of the deploy; or, in assert mode, a clearnet domain of the deploy does not resolve. A volume without any label passes, since compose adopts it and installs before this change created the Synapse volume with docker run. web-app-discourse declares its launcher-started containers through networks.local.launcher, and web-app-matrix labels a new Synapse volume with its compose project. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ad3fe32d9
commit
a67dd9b372
39 files changed
+2400
-2
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import unittest
|
||||
|
||||
from ansible.errors import AnsibleFilterError
|
||||
|
||||
from plugins.filter.preflight import (
|
||||
FilterModule,
|
||||
preflight_disk_usage,
|
||||
preflight_port_cgroups,
|
||||
preflight_port_conflicts,
|
||||
)
|
||||
|
||||
SS_LINES = (
|
||||
'tcp LISTEN 0 100 0.0.0.0:25 0.0.0.0:* users:(("master",pid=812,fd=13))',
|
||||
'tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=3001,fd=6))',
|
||||
'tcp LISTEN 0 511 0.0.0.0:443 0.0.0.0:* users:(("docker-proxy",pid=7,fd=4))',
|
||||
)
|
||||
SS = "\n".join(SS_LINES)
|
||||
PORTS = {
|
||||
"25": ["web-app-mailu (mailu.smtp)"],
|
||||
"80": ["svc-prx-openresty (openresty.http)"],
|
||||
"443": ["svc-prx-openresty (openresty.https)"],
|
||||
}
|
||||
|
||||
|
||||
class TestPreflightFilters(unittest.TestCase):
|
||||
def test_the_filters_are_registered(self):
|
||||
self.assertEqual(
|
||||
set(FilterModule().filters()),
|
||||
{
|
||||
"preflight_disk_usage",
|
||||
"preflight_port_cgroups",
|
||||
"preflight_port_conflicts",
|
||||
},
|
||||
)
|
||||
|
||||
def test_disk_usage_reads_df_against_the_limit(self):
|
||||
output = "Filesystem 1024-blocks Used Available Capacity Mounted on\n/dev/sda1 10 9 1 95% /\n"
|
||||
self.assertEqual(
|
||||
preflight_disk_usage(output, ["/"], "90"),
|
||||
["/ lies on /, which is 95 % full (limit 90 %)"],
|
||||
)
|
||||
|
||||
def test_unparsable_df_output_is_a_filter_error(self):
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
preflight_disk_usage("header only\n", ["/"], 90)
|
||||
|
||||
def test_the_cgroup_files_of_non_docker_listeners_are_listed(self):
|
||||
self.assertEqual(
|
||||
preflight_port_cgroups(SS, PORTS),
|
||||
["/proc/812/cgroup", "/proc/3001/cgroup"],
|
||||
)
|
||||
|
||||
def test_unparsable_ss_output_is_a_filter_error(self):
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
preflight_port_cgroups("garbage", PORTS)
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
preflight_port_conflicts("garbage", {"skipped": True}, PORTS)
|
||||
|
||||
def test_a_containerised_listener_passes_and_a_host_one_fails(self):
|
||||
probe = {
|
||||
"stdout": "/proc/812/cgroup:0::/system.slice/postfix.service\n"
|
||||
"/proc/3001/cgroup:0::/system.slice/docker-0123456789abcdef.scope"
|
||||
}
|
||||
self.assertEqual(
|
||||
preflight_port_conflicts(SS, probe, PORTS),
|
||||
[
|
||||
(
|
||||
"master (pid 812) listens on tcp 0.0.0.0:25, "
|
||||
"which web-app-mailu (mailu.smtp) binds"
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
def test_a_skipped_cgroup_probe_means_no_container(self):
|
||||
ss = 'tcp LISTEN 0 1 0.0.0.0:443 0.0.0.0:* users:(("docker-proxy",pid=7,fd=4))'
|
||||
self.assertEqual(preflight_port_conflicts(ss, {"skipped": True}, PORTS), [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,94 @@
|
||||
import json
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.address_pool_overlaps import LookupModule
|
||||
from utils.preflight.context import DeployClosureContext
|
||||
|
||||
MODULE = "plugins.lookup.address_pool_overlaps"
|
||||
SUBNETS = {"web-app-matomo": "192.168.31.0/24"}
|
||||
RESERVED = {"NETWORK_MESH_POOL": "10.100.0.0/16"}
|
||||
|
||||
|
||||
def _context():
|
||||
return DeployClosureContext(
|
||||
roles=["sys-ctl-hlth-csp", "web-app-matomo"],
|
||||
applications={},
|
||||
config=lambda role, path, default: SUBNETS.get(role, default),
|
||||
lookup=mock.Mock(),
|
||||
)
|
||||
|
||||
|
||||
class TestAddressPoolOverlapsLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with (
|
||||
mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
),
|
||||
mock.patch(
|
||||
f"{MODULE}.entity_name", side_effect=lambda role: role.split("-", 2)[-1]
|
||||
),
|
||||
):
|
||||
return LookupModule().run(terms, variables={})
|
||||
|
||||
@staticmethod
|
||||
def _daemon(*bases):
|
||||
return json.dumps(
|
||||
{"default-address-pools": [{"base": base, "size": 24} for base in bases]}
|
||||
)
|
||||
|
||||
def test_role_subnets_and_reserved_ranges_are_compared(self):
|
||||
result = self._run(
|
||||
[self._daemon("192.168.0.0/16", "10.0.0.0/8"), ["web-app-matomo"], RESERVED]
|
||||
)
|
||||
self.assertEqual(
|
||||
result,
|
||||
[
|
||||
[
|
||||
(
|
||||
"docker default address pool 192.168.0.0/16 overlaps "
|
||||
"192.168.31.0/24, which web-app-matomo declares for its "
|
||||
"network 'matomo'"
|
||||
),
|
||||
(
|
||||
"docker default address pool 10.0.0.0/8 overlaps 10.100.0.0/16, "
|
||||
"which NETWORK_MESH_POOL reserves"
|
||||
),
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
def test_a_clear_pool_and_an_absent_file_pass(self):
|
||||
self.assertEqual(
|
||||
self._run([self._daemon("10.208.0.0/12"), ["web-app-matomo"], RESERVED]),
|
||||
[[]],
|
||||
)
|
||||
self.assertEqual(self._run(["{}", ["web-app-matomo"], RESERVED]), [[]])
|
||||
|
||||
def test_an_unparsable_daemon_json_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(["{not json", [], RESERVED])
|
||||
|
||||
def test_a_pool_without_base_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(['{"default-address-pools": [{"size": 24}]}', [], RESERVED])
|
||||
|
||||
def test_non_mapping_arguments_are_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(["[]", [], RESERVED])
|
||||
|
||||
def test_a_closure_error_becomes_an_ansible_error(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(["{}", [], RESERVED], side_effect=TypeError("x"))
|
||||
|
||||
def test_a_wrong_term_count_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run(["{}", []])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,50 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.deploy_domains import LookupModule
|
||||
from utils.preflight.context import DeployClosureContext
|
||||
|
||||
MODULE = "plugins.lookup.deploy_domains"
|
||||
DOMAINS = {
|
||||
"web-app-matomo": ["matomo.example.org", "matomo.abc.onion"],
|
||||
"web-app-nextcloud": {"canonical": "cloud.example.org"},
|
||||
}
|
||||
|
||||
|
||||
def _context():
|
||||
return DeployClosureContext(
|
||||
roles=["svc-db-mariadb", "web-app-matomo", "web-app-nextcloud"],
|
||||
applications={},
|
||||
config=mock.Mock(),
|
||||
lookup=lambda name, role, default: DOMAINS.get(role, default),
|
||||
)
|
||||
|
||||
|
||||
class TestDeployDomainsLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
):
|
||||
return LookupModule().run(terms, variables={})
|
||||
|
||||
def test_the_clearnet_domains_of_every_closure_role_are_listed(self):
|
||||
self.assertEqual(
|
||||
self._run([["web-app-matomo"]]),
|
||||
[["cloud.example.org", "matomo.example.org"]],
|
||||
)
|
||||
|
||||
def test_a_closure_error_becomes_an_ansible_error(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run([[]], side_effect=TypeError("x"))
|
||||
|
||||
def test_a_wrong_term_count_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run([[], []])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,127 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.docker_ownership import LookupModule
|
||||
from utils.preflight.context import DeployClosureContext
|
||||
|
||||
MODULE = "plugins.lookup.docker_ownership"
|
||||
APPLICATIONS = {
|
||||
"web-app-matomo": {
|
||||
"services": {
|
||||
"matomo": {"name": "matomo", "image": "matomo"},
|
||||
"mariadb": {"enabled": True, "shared": True},
|
||||
}
|
||||
},
|
||||
"svc-db-mariadb": {"services": {"mariadb": {"name": "mariadb"}}},
|
||||
"web-app-discourse": {"services": {"discourse": {"name": "discourse"}}},
|
||||
}
|
||||
LAUNCHERS = {"web-app-discourse"}
|
||||
VOLUMES = {
|
||||
"web-app-matomo": {
|
||||
"data": {"type": "volume", "name": "matomo_data"},
|
||||
"cache": {},
|
||||
"php_hardening": {"type": "config", "source": "/etc/php.ini"},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _config(role, path, default):
|
||||
if path == "networks.local.launcher":
|
||||
return role in LAUNCHERS
|
||||
return APPLICATIONS[role]["services"][path.split(".")[1]]["name"]
|
||||
|
||||
|
||||
def _context(roles=("svc-db-mariadb", "web-app-matomo", "web-app-discourse")):
|
||||
return DeployClosureContext(
|
||||
roles=list(roles),
|
||||
applications=APPLICATIONS,
|
||||
config=_config,
|
||||
lookup=lambda name, role: VOLUMES.get(role, {}),
|
||||
)
|
||||
|
||||
|
||||
class TestDockerOwnershipLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with (
|
||||
mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
) as context,
|
||||
mock.patch(
|
||||
f"{MODULE}.entity_name", side_effect=lambda role: role.split("-", 2)[-1]
|
||||
),
|
||||
):
|
||||
result = LookupModule().run(terms, variables={})
|
||||
return result, context
|
||||
|
||||
def test_containers_declared_by_name_are_checked(self):
|
||||
probe = {"stdout": "matomo\tnextcloud\t\nmariadb\tmariadb\t\nlegacy\t\t"}
|
||||
result, _context_mock = self._run(["container", probe, ["web-app-matomo"]])
|
||||
self.assertEqual(
|
||||
result,
|
||||
[
|
||||
[
|
||||
(
|
||||
"container 'matomo' belongs to compose project 'nextcloud', "
|
||||
"but it is the container of web-app-matomo (entity 'matomo')"
|
||||
)
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
def test_a_launcher_role_container_needs_no_compose_label(self):
|
||||
probe = {"stdout": "discourse\t\t\nmatomo\tmatomo\t"}
|
||||
result, _context_mock = self._run(["container", probe, ["web-app-discourse"]])
|
||||
self.assertEqual(result, [[]])
|
||||
|
||||
def test_volumes_use_their_name_or_the_compose_default(self):
|
||||
probe = {
|
||||
"stdout": (
|
||||
"matomo_data\tnextcloud\t\n"
|
||||
"matomo_cache\tnextcloud\t\n"
|
||||
"matomo_php_hardening\tnextcloud\t"
|
||||
)
|
||||
}
|
||||
result, _context_mock = self._run(["volume", probe, ["web-app-matomo"]])
|
||||
self.assertEqual(
|
||||
result,
|
||||
[
|
||||
[
|
||||
(
|
||||
"volume 'matomo_data' belongs to compose project 'nextcloud', "
|
||||
"but it is the volume of web-app-matomo (entity 'matomo')"
|
||||
),
|
||||
(
|
||||
"volume 'matomo_cache' belongs to compose project 'nextcloud', "
|
||||
"but it is the volume of web-app-matomo (entity 'matomo')"
|
||||
),
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
def test_the_host_groups_reach_the_closure(self):
|
||||
_result, context = self._run(["volume", {"stdout": ""}, ["web-app-matomo"]])
|
||||
self.assertEqual(context.call_args.args[2], ["web-app-matomo"])
|
||||
|
||||
def test_an_unknown_kind_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(["network", {"stdout": ""}, []])
|
||||
|
||||
def test_an_unparsable_probe_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(["container", {"stdout": "only-a-name"}, []])
|
||||
|
||||
def test_a_closure_error_becomes_an_ansible_error(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(["container", {"stdout": ""}, []], side_effect=ValueError("x"))
|
||||
|
||||
def test_a_wrong_term_count_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run(["container", {"stdout": ""}])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,99 @@
|
||||
import json
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.network_ownership import LookupModule
|
||||
|
||||
MODULE = "plugins.lookup.network_ownership"
|
||||
SUBNETS = {"web-app-matomo": "192.168.31.0/24"}
|
||||
|
||||
|
||||
def _config_run(terms, variables=None):
|
||||
role, _path, default = terms
|
||||
return [SUBNETS.get(role, default)]
|
||||
|
||||
|
||||
class TestNetworkOwnershipLookup(unittest.TestCase):
|
||||
def _run(self, terms, closure=None, side_effect=None):
|
||||
applications = mock.Mock()
|
||||
applications.run.return_value = [{"web-app-matomo": {}}]
|
||||
config = mock.Mock()
|
||||
config.run.side_effect = _config_run
|
||||
with (
|
||||
mock.patch(
|
||||
f"{MODULE}.lookup_loader.get",
|
||||
side_effect=lambda name, **kwargs: {
|
||||
"applications": applications,
|
||||
"config": config,
|
||||
}[name],
|
||||
),
|
||||
mock.patch(f"{MODULE}.types_from_group_names", return_value=["server"]),
|
||||
mock.patch(
|
||||
f"{MODULE}.deploy_closure",
|
||||
return_value=closure or {"web-app-matomo", "sys-ctl-hlth-csp"},
|
||||
side_effect=side_effect,
|
||||
) as closure_mock,
|
||||
mock.patch(
|
||||
f"{MODULE}.entity_name", side_effect=lambda role: role.split("-", 2)[-1]
|
||||
),
|
||||
):
|
||||
result = LookupModule().run(terms, variables={"group_names": terms[-1]})
|
||||
return result, closure_mock
|
||||
|
||||
@staticmethod
|
||||
def _probe(*networks):
|
||||
return {"rc": 0, "stdout": json.dumps(list(networks))}
|
||||
|
||||
def test_roles_with_a_subnet_are_checked_against_the_networks(self):
|
||||
probe = self._probe(
|
||||
{
|
||||
"Name": "matomo",
|
||||
"Labels": {"com.docker.compose.project": "matomo"},
|
||||
"IPAM": {"Config": [{"Subnet": "192.168.40.0/24"}]},
|
||||
}
|
||||
)
|
||||
result, _closure = self._run([probe, ["web-app-matomo"]])
|
||||
self.assertEqual(
|
||||
result,
|
||||
[
|
||||
[
|
||||
"network 'matomo' holds 192.168.40.0/24, but web-app-matomo declares 192.168.31.0/24"
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
def test_matching_networks_return_no_errors(self):
|
||||
probe = self._probe(
|
||||
{
|
||||
"Name": "matomo",
|
||||
"Labels": {"com.docker.compose.project": "matomo"},
|
||||
"IPAM": {"Config": [{"Subnet": "192.168.31.0/24"}]},
|
||||
}
|
||||
)
|
||||
result, _closure = self._run([probe, ["web-app-matomo"]])
|
||||
self.assertEqual(result, [[]])
|
||||
|
||||
def test_the_host_groups_seed_the_closure(self):
|
||||
_result, closure = self._run([self._probe(), ["web-app-matomo", "servers"]])
|
||||
self.assertEqual(closure.call_args.args[1], ["web-app-matomo", "servers"])
|
||||
self.assertEqual(closure.call_args.kwargs["host_types"], ["server"])
|
||||
|
||||
def test_an_unparsable_probe_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run([{"rc": 0, "stdout": "not json"}, ["web-app-matomo"]])
|
||||
|
||||
def test_a_closure_error_becomes_an_ansible_error(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run(
|
||||
[self._probe(), ["web-app-matomo"]], side_effect=ValueError("broken")
|
||||
)
|
||||
|
||||
def test_a_wrong_term_count_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run([self._probe()])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,82 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.public_ports import LookupModule
|
||||
from utils.preflight.context import DeployClosureContext
|
||||
|
||||
MODULE = "plugins.lookup.public_ports"
|
||||
APPLICATIONS = {
|
||||
"web-app-mailu": {
|
||||
"services": {
|
||||
"mailu": {"ports": {"public": {"smtp": 25, "imaps": "993"}}},
|
||||
"redis": {"enabled": False},
|
||||
}
|
||||
},
|
||||
"web-svc-coturn": {
|
||||
"services": {
|
||||
"coturn": {
|
||||
"ports": {
|
||||
"public": {
|
||||
"stun_turn": 3478,
|
||||
"relay": {"start": 20000, "end": 39999},
|
||||
},
|
||||
"local": {"http": 8064},
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"web-app-other": {
|
||||
"services": {"other": {"ports": {"public": {"smtp": 25, "flag": True}}}}
|
||||
},
|
||||
"sys-ctl-hlth-csp": {},
|
||||
}
|
||||
|
||||
|
||||
def _config(role, path, default):
|
||||
key = path.split(".")[1]
|
||||
return APPLICATIONS[role]["services"][key]["ports"]["public"]
|
||||
|
||||
|
||||
def _context():
|
||||
return DeployClosureContext(
|
||||
roles=sorted(APPLICATIONS),
|
||||
applications=APPLICATIONS,
|
||||
config=_config,
|
||||
lookup=mock.Mock(),
|
||||
)
|
||||
|
||||
|
||||
class TestPublicPortsLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
):
|
||||
return LookupModule().run(terms, variables={})
|
||||
|
||||
def test_single_ports_map_onto_every_role_that_binds_them(self):
|
||||
self.assertEqual(
|
||||
self._run([["web-app-mailu"]]),
|
||||
[
|
||||
{
|
||||
"25": ["web-app-mailu (mailu.smtp)", "web-app-other (other.smtp)"],
|
||||
"993": ["web-app-mailu (mailu.imaps)"],
|
||||
"3478": ["web-svc-coturn (coturn.stun_turn)"],
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
def test_a_closure_error_becomes_an_ansible_error(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
self._run([[]], side_effect=ValueError("x"))
|
||||
|
||||
def test_a_wrong_term_count_is_rejected(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
LookupModule().run([])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user