diff --git a/roles/web-app-stalwart/tasks/store_guard.yml b/roles/web-app-stalwart/tasks/store_guard.yml index e27b5b4e48..fea4cadb20 100644 --- a/roles/web-app-stalwart/tasks/store_guard.yml +++ b/roles/web-app-stalwart/tasks/store_guard.yml @@ -20,37 +20,36 @@ {{ stalwart_store_marker is not skipped and (stalwart_store_marker.content | b64decode | trim) == (STALWART_STORE_LAYOUT_VERSION | string) }} -- name: "🔍 Find the store database on the shared PostgreSQL" - database_query: - config: >- - {{ lookup('database', application_id) | combine({ - 'username': 'postgres', - 'name': 'postgres', - 'password': lookup('config', 'svc-db-postgres', 'secrets.credentials.POSTGRES_PASSWORD'), - }) }} - query_file: "{{ role_path }}/files/sql/store/database.sql" - named_args: - name: "{{ lookup('database', application_id, 'name') }}" - expect_rows: true - mask_values: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" - register: stalwart_store_database - until: stalwart_store_database is succeeded - retries: 6 - delay: 5 - changed_when: false - no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" - when: - - not (stalwart_store_marked | bool) - - STALWART_DATABASE_SHARED | bool +- name: "🔍 Find a store on a host without the 1.x store marker" + when: not (stalwart_store_marked | bool) + block: + - name: "🔍 Find the store database on the shared PostgreSQL" + database_query: + config: >- + {{ lookup('database', application_id) | combine({ + 'username': 'postgres', + 'name': 'postgres', + 'password': lookup('config', 'svc-db-postgres', 'secrets.credentials.POSTGRES_PASSWORD'), + }) }} + query_file: "{{ role_path }}/files/sql/store/database.sql" + named_args: + name: "{{ lookup('database', application_id, 'name') }}" + expect_rows: true + mask_values: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" + register: stalwart_store_database + until: stalwart_store_database is succeeded + retries: 6 + delay: 5 + changed_when: false + no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" + when: STALWART_DATABASE_SHARED | bool -- name: "🔍 Find the volume of the dedicated store database" - ansible.builtin.command: >- - container volume ls -q --filter name=^{{ lookup('database', application_id, 'volume') }}$ - register: stalwart_store_volume - changed_when: false - when: - - not (stalwart_store_marked | bool) - - not (STALWART_DATABASE_SHARED | bool) + - name: "🔍 Find the volume of the dedicated store database" + ansible.builtin.command: >- + container volume ls -q --filter name=^{{ lookup('database', application_id, 'volume') }}$ + register: stalwart_store_volume + changed_when: false + when: not (STALWART_DATABASE_SHARED | bool) - name: "🔧 Record whether a store exists that this host never deployed Stalwart 1.x on" ansible.builtin.set_fact: @@ -71,7 +70,7 @@ - --filter - >- {{ ('label=com.docker.swarm.service.name=' ~ lookup('database', application_id, 'service_name')) - if DEPLOYMENT_MODE == 'swarm' + if IS_SWARM_MODE else ('name=^' ~ lookup('database', application_id, 'container') ~ '$') }} register: stalwart_store_engine changed_when: false @@ -85,7 +84,7 @@ - stalwart_store_engine.stdout | trim | length > 0 fail_msg: >- The dedicated store database of Stalwart, service '{{ lookup('database', application_id, 'host') }}' - of stack '{{ application_id | get_entity_name }}', is not running on {{ inventory_hostname }}, + of stack '{{ application_id | entity_name }}', is not running on {{ inventory_hostname }}, so the deploy cannot read the layout of the store in volume '{{ lookup('database', application_id, 'volume') }}'. This host has no record of a Stalwart 1.x deploy ({{ STALWART_STORE_MARKER_FILE }}), so that store may predate 1.0, and the deploy stops before Stalwart {{ lookup('config', application_id, 'services.stalwart.version') }} opens it. @@ -108,21 +107,21 @@ no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" when: stalwart_store_exists | bool -- name: "🚚 Migrate the 0.16 store to 1.x with stalwart-migrate" - ansible.builtin.include_tasks: store_guard/migrate.yml - when: - - stalwart_store_layout is not skipped - - stalwart_store_layout.rows[0][0] not in ['bytea', 'absent'] - -- name: "✅ Ensure Stalwart opens no store from before 1.0" - ansible.builtin.assert: - that: - - stalwart_store_layout.rows[0][0] in ['bytea', 'absent'] - fail_msg: >- - The store in database '{{ lookup('database', application_id, 'name') }}' still predates Stalwart 1.0 - after the migration (type of column u.v: {{ stalwart_store_layout.rows[0][0] }}, a 1.x store holds bytea), - so the deploy stops before Stalwart {{ lookup('config', application_id, 'services.stalwart.version') }} opens it. - Inspect {{ STALWART_MIGRATE_DIR }} on this host, rerun {{ STALWART_MIGRATE_SCRIPT }} phase by phase - ("Upgrading from 0.16" in roles/web-app-stalwart/README.md), then deploy again. - quiet: true +- name: "🚚 Migrate a 0.16 store and ensure Stalwart opens none from before 1.0" when: stalwart_store_layout is not skipped + block: + - name: "🚚 Migrate the 0.16 store to 1.x with stalwart-migrate" + ansible.builtin.include_tasks: store_guard/migrate.yml + when: stalwart_store_layout.rows[0][0] not in ['bytea', 'absent'] + + - name: "✅ Ensure Stalwart opens no store from before 1.0" + ansible.builtin.assert: + that: + - stalwart_store_layout.rows[0][0] in ['bytea', 'absent'] + fail_msg: >- + The store in database '{{ lookup('database', application_id, 'name') }}' still predates Stalwart 1.0 + after the migration (type of column u.v: {{ stalwart_store_layout.rows[0][0] }}, a 1.x store holds bytea), + so the deploy stops before Stalwart {{ lookup('config', application_id, 'services.stalwart.version') }} opens it. + Inspect {{ STALWART_MIGRATE_DIR }} on this host, rerun {{ STALWART_MIGRATE_SCRIPT }} phase by phase + ("Upgrading from 0.16" in roles/web-app-stalwart/README.md), then deploy again. + quiet: true diff --git a/roles/web-app-stalwart/templates/migrate.env.j2 b/roles/web-app-stalwart/templates/migrate.env.j2 index 2493ae0c46..9c71350ebd 100644 --- a/roles/web-app-stalwart/templates/migrate.env.j2 +++ b/roles/web-app-stalwart/templates/migrate.env.j2 @@ -18,6 +18,6 @@ STALWART_CONTAINER={{ STALWART_CONTAINER | quote }} STACK_DIR={{ lookup('container', application_id, 'directories.instance') | quote }} COMPOSE_SERVICE=stalwart STALWART_ADDRESS={{ STALWART_CONTAINER_ADDRESS }} -{% if DEPLOYMENT_MODE == 'swarm' %} +{% if IS_SWARM_MODE %} STALWART_SERVICE={{ lookup('container_service', application_id, 'stalwart') | quote }} {% endif %} diff --git a/tests/unit/python/roles/web-app-stalwart/test_store_guard.py b/tests/unit/python/roles/web-app-stalwart/test_store_guard.py index d4fac3929c..19fe4c9f3e 100644 --- a/tests/unit/python/roles/web-app-stalwart/test_store_guard.py +++ b/tests/unit/python/roles/web-app-stalwart/test_store_guard.py @@ -11,16 +11,21 @@ from __future__ import annotations import base64 import unittest +from typing import TYPE_CHECKING from ansible.parsing.dataloader import DataLoader from ansible.template import Templar from utils.cache.yaml import load_yaml_any +from utils.roles.invocations import NESTED_KEYS from utils.roles.mapping import ROLE_FILE_VARS_MAIN from utils.templating.ansible import _trust_as_template from . import PROJECT_ROOT +if TYPE_CHECKING: + from collections.abc import Iterator + ROLE = PROJECT_ROOT / "roles" / "web-app-stalwart" GUARD = ROLE / "tasks" / "store_guard.yml" MANAGER_OPS = ROLE / "tasks" / "manager_ops.yml" @@ -50,6 +55,23 @@ def _module(task: dict, name: str) -> dict | None: return task.get(f"ansible.builtin.{name}") or task.get(name) +def _conditions(task: dict) -> list[str]: + if "when" not in task: + return [] + when = task["when"] + return when if isinstance(when, list) else [when] + + +def _walk(tasks: list[dict], gates: list[str]) -> Iterator[tuple[dict, list[str]]]: + for task in tasks: + conditions = gates + _conditions(task) + nested = [key for key in NESTED_KEYS if key in task] + if not nested: + yield task, conditions + for key in nested: + yield from _walk(task[key], conditions) + + def _layout_version() -> str: return load_yaml_any(str(VARS), default_if_missing={})[ "STALWART_STORE_LAYOUT_VERSION" @@ -83,11 +105,8 @@ def _guard(results: dict[str, dict], *, shared: bool) -> str: "STALWART_DATABASE_SHARED": shared, "STALWART_STORE_LAYOUT_VERSION": _layout_version(), } - for task in load_yaml_any(str(GUARD), default_if_missing=[]): - when = task.get("when", []) - if not all( - _holds(c, facts) for c in (when if isinstance(when, list) else [when]) - ): + for task, conditions in _walk(load_yaml_any(str(GUARD), default_if_missing=[]), []): + if not all(_holds(c, facts) for c in conditions): if "register" in task: facts[task["register"]] = SKIPPED continue