Files
Slavi PantaleevandClaude Opus 5.5 bff3f54d53 Harden pre-start container cleanup against name-release races
Containers are created with `--rm`, so Docker removes them asynchronously after they stop.
The pre-start `docker rm` returns immediately while that removal is still in progress
(its error is suppressed), and the following `docker create --name` can fail with
"The container name ... is already in use".

This is easy to hit since systemd_service_manager switched its default restart mode to `all-at-once`:
many containers stop and start at the same time and Docker falls behind on removals.
The playbook then fails at "Restart systemd services needing restart at once",
even though the services recover on their own (`Restart=always`) a little later.

The external roles (MASH, devture) were hardened against this in 2026-03
(see mother-of-all-self-hosting/ansible-role-valkey@0ba79c0), but the roles in `roles/custom` never were.
This ports the same bounded cleanup loop to them: stop, force-remove,
and wait until Docker releases the name, failing explicitly after a timeout.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-16 19:04:04 +03:00

176 lines
10 KiB
YAML

# SPDX-FileCopyrightText: 2019 - 2025 Slavi Pantaleev
# SPDX-FileCopyrightText: 2019 Daniel Løvbrøtte Olsen
# SPDX-FileCopyrightText: 2020 John Goerzen
# SPDX-FileCopyrightText: 2021 - 2023 MDAD project contributors
# SPDX-FileCopyrightText: 2021 Aaron Raimist
# SPDX-FileCopyrightText: 2021 Ahmad Haghighi
# SPDX-FileCopyrightText: 2022 Jim Myhrberg
# SPDX-FileCopyrightText: 2022 Marko Weltzer
# SPDX-FileCopyrightText: 2022 Nikita Chernyi
# SPDX-FileCopyrightText: 2023 Samuel Meenzen
#
# SPDX-License-Identifier: AGPL-3.0-or-later
---
# matrix-appservice-discord is a Matrix <-> Discord bridge
# Project source code URL: https://github.com/matrix-org/matrix-appservice-discord
matrix_bridge_appservice_discord_enabled: false
# renovate: datasource=docker depName=ghcr.io/matrix-org/matrix-appservice-discord
matrix_bridge_appservice_discord_version: v4.0.0
matrix_bridge_appservice_discord_container_image: "{{ matrix_bridge_appservice_discord_container_image_registry_prefix }}matrix-org/matrix-appservice-discord:{{ matrix_bridge_appservice_discord_version }}"
matrix_bridge_appservice_discord_container_image_registry_prefix: "{{ matrix_bridge_appservice_discord_container_image_registry_prefix_upstream }}"
matrix_bridge_appservice_discord_container_image_registry_prefix_upstream: "{{ matrix_bridge_appservice_discord_container_image_registry_prefix_upstream_default }}"
matrix_bridge_appservice_discord_container_image_registry_prefix_upstream_default: "ghcr.io/"
matrix_bridge_appservice_discord_base_path: "{{ matrix_base_data_path }}/appservice-discord"
matrix_bridge_appservice_discord_config_path: "{{ matrix_base_data_path }}/appservice-discord/config"
matrix_bridge_appservice_discord_data_path: "{{ matrix_base_data_path }}/appservice-discord/data"
# Get your own keys at https://discordapp.com/developers/applications/me/create
matrix_bridge_appservice_discord_client_id: ''
matrix_bridge_appservice_discord_bot_token: ''
matrix_bridge_appservice_discord_appservice_token: ''
matrix_bridge_appservice_discord_homeserver_token: ''
matrix_bridge_appservice_discord_homeserver_domain: "{{ matrix_domain }}"
# Controls whether the matrix-appservice-discord container exposes its HTTP port (tcp/9005 in the container).
#
# Takes an "<ip>:<port>" or "<port>" value (e.g. "127.0.0.1:9005"), or empty string to not expose.
matrix_bridge_appservice_discord_container_http_host_bind_port: ''
matrix_bridge_appservice_discord_container_network: ""
matrix_bridge_appservice_discord_container_additional_networks: "{{ matrix_bridge_appservice_discord_container_additional_networks_auto + matrix_bridge_appservice_discord_container_additional_networks_custom }}"
matrix_bridge_appservice_discord_container_additional_networks_auto: []
matrix_bridge_appservice_discord_container_additional_networks_custom: []
# A list of extra arguments to pass to the container
matrix_bridge_appservice_discord_container_extra_arguments: []
# Controls how long the pre-start cleanup should wait for stale container-name release
# before failing startup explicitly.
matrix_bridge_appservice_discord_container_cleanup_timeout_seconds: "{{ devture_systemd_docker_base_container_cleanup_timeout_seconds }}"
# Controls the delay between pre-start cleanup retries.
matrix_bridge_appservice_discord_container_cleanup_retry_delay_seconds: "{{ devture_systemd_docker_base_container_cleanup_retry_delay_seconds }}"
# Shell command used in the systemd pre-start phase to deterministically remove a stale
# container and wait until Docker releases its name.
matrix_bridge_appservice_discord_container_cleanup_command: |
i=0;
while [ "$i" -lt {{ matrix_bridge_appservice_discord_container_cleanup_timeout_seconds }} ]; do
{{ devture_systemd_docker_base_host_command_docker }} stop -t {{ devture_systemd_docker_base_container_stop_grace_time_seconds }} matrix-appservice-discord >/dev/null 2>&1 || true;
{{ devture_systemd_docker_base_host_command_docker }} rm -f matrix-appservice-discord >/dev/null 2>&1 || true;
if {{ devture_systemd_docker_base_host_command_docker }} container inspect matrix-appservice-discord >/dev/null 2>&1; then
i=$((i + 1));
sleep {{ matrix_bridge_appservice_discord_container_cleanup_retry_delay_seconds }};
continue;
fi;
if {{ devture_systemd_docker_base_host_command_docker }} info >/dev/null 2>&1; then
exit 0;
fi;
echo "Failed to query Docker daemon while cleaning up matrix-appservice-discord" >&2;
exit 1;
done;
echo "Failed to remove stale container matrix-appservice-discord after {{ matrix_bridge_appservice_discord_container_cleanup_timeout_seconds }} attempts" >&2;
exit 1
# List of systemd services that matrix-appservice-discord.service depends on.
matrix_bridge_appservice_discord_systemd_required_services_list: "{{ matrix_bridge_appservice_discord_systemd_required_services_list_default + matrix_bridge_appservice_discord_systemd_required_services_list_auto + matrix_bridge_appservice_discord_systemd_required_services_list_custom }}"
matrix_bridge_appservice_discord_systemd_required_services_list_default: "{{ [devture_systemd_docker_base_docker_service_name] if devture_systemd_docker_base_docker_service_name else [] }}"
matrix_bridge_appservice_discord_systemd_required_services_list_auto: []
matrix_bridge_appservice_discord_systemd_required_services_list_custom: []
# List of systemd services that matrix-appservice-discord.service wants
matrix_bridge_appservice_discord_systemd_wanted_services_list: []
matrix_bridge_appservice_discord_appservice_url: 'http://matrix-appservice-discord:9005'
matrix_bridge_appservice_discord_bridge_domain: "{{ matrix_domain }}"
# As of right now, the homeserver URL must be a public URL. See below.
matrix_bridge_appservice_discord_bridge_homeserverUrl: "{{ matrix_homeserver_url }}" # noqa var-naming
matrix_bridge_appservice_discord_bridge_disablePresence: false # noqa var-naming
matrix_bridge_appservice_discord_bridge_enableSelfServiceBridging: false # noqa var-naming
matrix_bridge_appservice_discord_bridge_disablePortalBridging: false # noqa var-naming
# Database-related configuration fields.
#
# To use SQLite, stick to these defaults.
#
# To use Postgres:
# - change the engine (`matrix_bridge_appservice_discord_database_engine: 'postgres'`)
# - adjust your database credentials via the `matrix_bridge_appservice_discord_database_*` variables
matrix_bridge_appservice_discord_database_engine: 'sqlite'
matrix_bridge_appservice_discord_sqlite_database_path_local: "{{ matrix_bridge_appservice_discord_data_path }}/discord.db"
matrix_bridge_appservice_discord_sqlite_database_path_in_container: "/data/discord.db"
matrix_bridge_appservice_discord_database_username: 'matrix_appservice_discord'
matrix_bridge_appservice_discord_database_password: 'some-password'
matrix_bridge_appservice_discord_database_hostname: ''
matrix_bridge_appservice_discord_database_port: 5432
matrix_bridge_appservice_discord_database_name: 'matrix_appservice_discord'
# These 2 variables are what actually ends up in the bridge configuration.
# It's best if you don't change them directly, but rather redefine the sub-variables that constitute them.
matrix_bridge_appservice_discord_database_filename: "{{ matrix_bridge_appservice_discord_sqlite_database_path_in_container }}"
matrix_bridge_appservice_discord_database_connString: 'postgresql://{{ matrix_bridge_appservice_discord_database_username }}:{{ matrix_bridge_appservice_discord_database_password }}@{{ matrix_bridge_appservice_discord_database_hostname }}:{{ matrix_bridge_appservice_discord_database_port }}/{{ matrix_bridge_appservice_discord_database_name }}' # noqa var-naming
# Tells whether the bot should make use of "Privileged Gateway Intents".
#
# Enabling this means that you need to enable it for the bot (Discord application) as well,
# by triggering all Intent checkboxes on a page like this: `https://discord.com/developers/applications/694448564151123988/bot`
#
# Learn more: https://gist.github.com/advaith1/e69bcc1cdd6d0087322734451f15aa2f
matrix_bridge_appservice_discord_auth_usePrivilegedIntents: false # noqa var-naming
matrix_bridge_appservice_discord_configuration_yaml: "{{ lookup('template', 'templates/config.yaml.j2') }}"
matrix_bridge_appservice_discord_configuration_extension_yaml: |
# Your custom YAML configuration goes here.
# This configuration extends the default starting configuration (`matrix_bridge_appservice_discord_configuration_yaml`).
#
# You can override individual variables from the default configuration, or introduce new ones.
#
# If you need something more special, you can take full control by
# completely redefining `matrix_bridge_appservice_discord_configuration_yaml`.
matrix_bridge_appservice_discord_configuration_extension: "{{ matrix_bridge_appservice_discord_configuration_extension_yaml | from_yaml if matrix_bridge_appservice_discord_configuration_extension_yaml | from_yaml is mapping else {} }}"
matrix_bridge_appservice_discord_configuration: "{{ matrix_bridge_appservice_discord_configuration_yaml | from_yaml | combine(matrix_bridge_appservice_discord_configuration_extension, recursive=True) }}"
matrix_bridge_appservice_discord_registration_yaml: |
#jinja2: lstrip_blocks: True
id: appservice-discord
as_token: "{{ matrix_bridge_appservice_discord_appservice_token }}"
hs_token: "{{ matrix_bridge_appservice_discord_homeserver_token }}"
namespaces:
users:
- exclusive: true
regex: '@_discord_.*:{{ matrix_bridge_appservice_discord_homeserver_domain | regex_escape }}'
aliases:
- exclusive: true
regex: '#_discord_.*:{{ matrix_bridge_appservice_discord_homeserver_domain | regex_escape }}'
url: {{ matrix_bridge_appservice_discord_appservice_url }}
sender_localpart: _discord_bot
rate_limited: false
protocols:
- discord
matrix_bridge_appservice_discord_registration: "{{ matrix_bridge_appservice_discord_registration_yaml | from_yaml }}"
# matrix_bridge_appservice_discord_restart_necessary controls whether the service
# will be restarted (when true) or merely started (when false) by the
# systemd service manager role (when conditional restart is enabled).
#
# This value is automatically computed during installation based on whether
# any configuration files, the systemd service file, or the container image changed.
# The default of `false` means "no restart needed" — appropriate when the role's
# installation tasks haven't run (e.g., due to --tags skipping them).
matrix_bridge_appservice_discord_restart_necessary: false