Files
computer-playbook/plugins/lookup/postgres_libraries.py
T
kevinveenbirkenbach 30417d2515 fix(svc-db-postgres): build on the official multi-arch postgres 18 image
postgis/postgis publishes amd64 only, so every postgres consumer failed
on arm64. The official postgres image serves both architectures and
already ships the contrib extensions bloom, pg_trgm and unaccent.

PostGIS moves into the postgres_libraries registry and is installed from
PGDG (postgresql-18-postgis-3) only for a consumer that declares it as
an extension, which is mobilizon alone. apt update reads every source
list again, because the PostGIS package depends on GDAL from Debian
main, which a PGDG-only update left without an install candidate.

Postgres 18 keeps its cluster in /var/lib/postgresql/18/docker and its
entrypoint treats a volume at the former /var/lib/postgresql/data as an
old database, so the central and the dedicated data volume both mount at
/var/lib/postgresql. Existing 17 clusters are not migrated: their data
directory trips the entrypoint's old-data check and the container
refuses to start.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
(cherry picked from commit f78ebe46a4)
2026-10-01 22:19:09 +02:00

70 lines
2.1 KiB
Python

"""Resolve postgres-extension library install recipes.
Usage:
{{ lookup('postgres_libraries', ['vector', 'bloom']) }}
Takes a list of extension names and returns the subset that needs a
custom library install on top of the official postgres base image
(the contrib extensions bloom, pg_trgm and unaccent are already in the
base — those are filtered out). Each returned entry is a dict with:
- extension: the extension name (matches the SQL identifier)
- name: the library/source name (used in the Dockerfile context)
- apt_package: the PGDG package suffix, installed as
postgresql-<major>-<apt_package>
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
if TYPE_CHECKING:
from collections.abc import Sequence
_REGISTRY: dict[str, dict[str, str]] = {
"postgis": {
"name": "postgis",
"apt_package": "postgis-3",
},
"vector": {
"name": "pgvector",
"apt_package": "pgvector",
},
}
class LookupModule(LookupBase):
def run(
self,
terms: Sequence[Any] | None,
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[Any]:
terms = list(terms or [])
if len(terms) != 1:
raise AnsibleError(
"postgres_libraries: expected exactly one term — a list "
"of extension names."
)
extensions = terms[0]
if not isinstance(extensions, (list, tuple)):
raise AnsibleError(
"postgres_libraries: first term must be a list of extension names."
)
recipes: list[dict[str, str]] = []
seen: set[str] = set()
for ext in extensions:
ext_name = str(ext).strip()
if not ext_name or ext_name in seen:
continue
seen.add(ext_name)
recipe = _REGISTRY.get(ext_name)
if recipe is not None:
recipes.append({"extension": ext_name, **recipe})
return [recipes]