From 7d625011a120c9a6fb4befab7f2c4d02c2a78a76 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Sun, 7 Jan 2024 10:23:01 +0200 Subject: [PATCH] Move matrix-bridge-appservice-discord to its own container network --- group_vars/matrix_servers | 33 +++++++++++-------- .../defaults/main.yml | 11 ++++++- .../matrix-appservice-discord.service.j2 | 12 +++++-- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index af230afd8..d984b787a 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -504,25 +504,30 @@ matrix_integration_manager_ui_url: "{{ matrix_dimension_integrations_ui_url if m # We don't enable bridges by default. matrix_appservice_discord_enabled: false -# Normally, matrix-nginx-proxy is enabled and nginx can reach matrix-appservice-discord over the container network. -# If matrix-nginx-proxy is not enabled, or you otherwise have a need for it, you can expose -# matrix-appservice-discord's client-server port to the local host. +matrix_appservice_discord_systemd_required_services_list_auto: | + {{ + matrix_addons_homeserver_systemd_services_list + + + ([devture_postgres_identifier ~ '.service'] if (devture_postgres_enabled and matrix_appservice_discord_database_hostname == devture_postgres_connection_hostname) else []) + }} + + matrix_appservice_discord_container_http_host_bind_port: "{{ (matrix_playbook_service_host_bind_interface_prefix ~ '9005') if matrix_playbook_service_host_bind_interface_prefix else '' }}" +matrix_appservice_discord_container_network: "{{ matrix_addons_container_network }}" + +matrix_appservice_discord_container_additional_networks_auto: |- + {{ + ( + ([] if matrix_addons_homeserver_container_network == '' else [matrix_addons_homeserver_container_network]) + + + ([devture_postgres_container_network] if (devture_postgres_enabled and matrix_appservice_discord_database_hostname == devture_postgres_connection_hostname and matrix_appservice_discord_container_network != devture_postgres_container_network) else []) + ) | unique + }} + # If the homeserver disables presence, it's likely better (less wasteful) to also disable presence on the bridge side. matrix_appservice_discord_bridge_disablePresence: "{{ not matrix_synapse_presence_enabled }}" -matrix_appservice_discord_systemd_required_services_list: | - {{ - ['docker.service'] - + - ['matrix-' + matrix_homeserver_implementation + '.service'] - + - ([devture_postgres_identifier ~ '.service'] if devture_postgres_enabled else []) - + - (['matrix-nginx-proxy.service'] if matrix_nginx_proxy_enabled else []) - }} - matrix_appservice_discord_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'discord.as.token', rounds=655555) | to_uuid }}" matrix_appservice_discord_homeserver_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'discord.hs.token', rounds=655555) | to_uuid }}" diff --git a/roles/custom/matrix-bridge-appservice-discord/defaults/main.yml b/roles/custom/matrix-bridge-appservice-discord/defaults/main.yml index a0dfc9f11..c32897007 100644 --- a/roles/custom/matrix-bridge-appservice-discord/defaults/main.yml +++ b/roles/custom/matrix-bridge-appservice-discord/defaults/main.yml @@ -29,11 +29,20 @@ matrix_appservice_discord_homeserver_domain: "{{ matrix_domain }}" # Takes an ":" or "" value (e.g. "127.0.0.1:9005"), or empty string to not expose. matrix_appservice_discord_container_http_host_bind_port: '' +matrix_appservice_discord_container_network: "" + +matrix_appservice_discord_container_additional_networks: "{{ matrix_appservice_discord_container_additional_networks_auto + matrix_appservice_discord_container_additional_networks_custom }}" +matrix_appservice_discord_container_additional_networks_auto: [] +matrix_appservice_discord_container_additional_networks_custom: [] + # A list of extra arguments to pass to the container matrix_appservice_discord_container_extra_arguments: [] # List of systemd services that matrix-appservice-discord.service depends on. -matrix_appservice_discord_systemd_required_services_list: ['docker.service'] +matrix_appservice_discord_systemd_required_services_list: "{{ matrix_appservice_discord_systemd_required_services_list_default + matrix_appservice_discord_systemd_required_services_list_auto + matrix_appservice_discord_systemd_required_services_list_custom }}" +matrix_appservice_discord_systemd_required_services_list_default: ['docker.service'] +matrix_appservice_discord_systemd_required_services_list_auto: [] +matrix_appservice_discord_systemd_required_services_list_custom: [] # List of systemd services that matrix-appservice-discord.service wants matrix_appservice_discord_systemd_wanted_services_list: [] diff --git a/roles/custom/matrix-bridge-appservice-discord/templates/systemd/matrix-appservice-discord.service.j2 b/roles/custom/matrix-bridge-appservice-discord/templates/systemd/matrix-appservice-discord.service.j2 index d470fd3de..a60c19f83 100644 --- a/roles/custom/matrix-bridge-appservice-discord/templates/systemd/matrix-appservice-discord.service.j2 +++ b/roles/custom/matrix-bridge-appservice-discord/templates/systemd/matrix-appservice-discord.service.j2 @@ -19,11 +19,13 @@ ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_s # Intentional delay, so that the homeserver (we likely depend on) can manage to start. ExecStartPre={{ matrix_host_command_sleep }} 5 -ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-discord \ +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \ + --rm \ + --name=matrix-appservice-discord \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ - --network={{ matrix_docker_network }} \ + --network={{ matrix_appservice_discord_container_network }} \ {% if matrix_appservice_discord_container_http_host_bind_port %} -p {{ matrix_appservice_discord_container_http_host_bind_port }}:9005 \ {% endif %} @@ -35,6 +37,12 @@ ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name {{ matrix_appservice_discord_docker_image }} \ node /build/src/discordas.js -p 9005 -c /cfg/config.yaml -f /cfg/registration.yaml +{% for network in matrix_appservice_discord_container_additional_networks %} +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-appservice-discord +{% endfor %} + +ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-appservice-discord + ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} stop --time={{ devture_systemd_docker_base_container_stop_grace_time_seconds }} matrix-appservice-discord 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-appservice-discord 2>/dev/null || true' Restart=always