Move matrix-bridge-mautrix-googlechat to its own container network

This commit is contained in:
Slavi Pantaleev 2024-01-07 15:22:22 +02:00
parent c6c88c2503
commit f8f3318bb2
9 changed files with 182 additions and 65 deletions

View File

@ -1017,27 +1017,50 @@ matrix_mautrix_facebook_database_password: "{{ '%s' | format(matrix_homeserver_g
# We don't enable bridges by default.
matrix_mautrix_googlechat_enabled: false
matrix_mautrix_googlechat_systemd_required_services_list_auto: |
{{
matrix_addons_homeserver_systemd_services_list
+
([devture_postgres_identifier ~ '.service'] if (devture_postgres_enabled and matrix_mautrix_googlechat_database_hostname == devture_postgres_connection_hostname) else [])
}}
matrix_mautrix_googlechat_container_image_self_build: "{{ matrix_architecture not in ['amd64', 'arm64'] }}"
matrix_mautrix_googlechat_systemd_required_services_list: |
matrix_mautrix_googlechat_container_http_host_bind_port: "{{ (matrix_playbook_service_host_bind_interface_prefix ~ '9007') if matrix_playbook_service_host_bind_interface_prefix else '' }}"
matrix_mautrix_googlechat_container_network: "{{ matrix_addons_container_network }}"
matrix_mautrix_googlechat_container_additional_networks_auto: |-
{{
['docker.service']
(
([] if matrix_addons_homeserver_container_network == '' else [matrix_addons_homeserver_container_network])
+
['matrix-' + matrix_homeserver_implementation + '.service']
([devture_postgres_container_network] if (devture_postgres_enabled and matrix_mautrix_googlechat_database_hostname == devture_postgres_connection_hostname and matrix_mautrix_googlechat_container_network != devture_postgres_container_network) else [])
+
([devture_postgres_identifier ~ '.service'] if devture_postgres_enabled else [])
+
(['matrix-nginx-proxy.service'] if matrix_nginx_proxy_enabled else [])
([matrix_playbook_reverse_proxyable_services_additional_network] if (matrix_playbook_reverse_proxyable_services_additional_network and matrix_mautrix_gmessages_container_labels_traefik_enabled) else [])
) | unique
}}
matrix_mautrix_googlechat_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}"
matrix_mautrix_googlechat_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}"
matrix_mautrix_googlechat_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}"
matrix_mautrix_googlechat_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}"
matrix_mautrix_googlechat_container_labels_metrics_middleware_basic_auth_enabled: "{{ matrix_metrics_exposure_http_basic_auth_enabled }}"
matrix_mautrix_googlechat_container_labels_metrics_middleware_basic_auth_users: "{{ matrix_metrics_exposure_http_basic_auth_users }}"
matrix_mautrix_googlechat_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'gc.as.token', rounds=655555) | to_uuid }}"
matrix_mautrix_googlechat_homeserver_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'gc.hs.token', rounds=655555) | to_uuid }}"
matrix_mautrix_googlechat_container_http_host_bind_port: "{{ (matrix_playbook_service_host_bind_interface_prefix ~ '9007') if matrix_playbook_service_host_bind_interface_prefix else '' }}"
matrix_mautrix_googlechat_login_shared_secret: "{{ matrix_synapse_ext_password_provider_shared_secret_auth_shared_secret if matrix_synapse_ext_password_provider_shared_secret_auth_enabled else '' }}"
matrix_mautrix_googlechat_metrics_enabled: "{{ prometheus_enabled }}"
matrix_mautrix_googlechat_metrics_proxying_enabled: "{{ matrix_mautrix_googlechat_metrics_enabled and matrix_metrics_exposure_enabled }}"
matrix_mautrix_googlechat_metrics_proxying_hostname: "{{ matrix_metrics_exposure_hostname }}"
matrix_mautrix_googlechat_metrics_proxying_path_prefix: "{{ matrix_metrics_exposure_path_prefix }}/mautrix-googlechat"
# Postgres is the default, except if not using internal Postgres server
matrix_mautrix_googlechat_database_engine: "{{ 'postgres' if devture_postgres_enabled else 'sqlite' }}"
matrix_mautrix_googlechat_database_hostname: "{{ devture_postgres_connection_hostname if devture_postgres_enabled else '' }}"

View File

@ -33,11 +33,59 @@ matrix_mautrix_googlechat_command_prefix: "!gc"
# Takes an "<ip>:<port>" or "<port>" value (e.g. "127.0.0.1:9007"), or empty string to not expose.
matrix_mautrix_googlechat_container_http_host_bind_port: ''
matrix_mautrix_googlechat_container_network: ""
matrix_mautrix_googlechat_container_additional_networks: "{{ matrix_mautrix_googlechat_container_additional_networks_auto + matrix_mautrix_googlechat_container_additional_networks_custom }}"
matrix_mautrix_googlechat_container_additional_networks_auto: []
matrix_mautrix_googlechat_container_additional_networks_custom: []
# matrix_mautrix_googlechat_container_labels_traefik_enabled controls whether labels to assist a Traefik reverse-proxy will be attached to the container.
# See `../templates/labels.j2` for details.
#
# To inject your own other container labels, see `matrix_mautrix_googlechat_container_labels_additional_labels`.
matrix_mautrix_googlechat_container_labels_traefik_enabled: true
matrix_mautrix_googlechat_container_labels_traefik_docker_network: "{{ matrix_mautrix_googlechat_container_network }}"
matrix_mautrix_googlechat_container_labels_traefik_entrypoints: web-secure
matrix_mautrix_googlechat_container_labels_traefik_tls_certResolver: default # noqa var-naming
# Controls whether labels will be added that expose mautrix-googlechat's public endpoint
matrix_mautrix_googlechat_container_labels_public_endpoint_enabled: true
matrix_mautrix_googlechat_container_labels_public_endpoint_hostname: ""
matrix_mautrix_googlechat_container_labels_public_endpoint_path_prefix: "{{ matrix_mautrix_googlechat_public_endpoint }}"
matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_rule: "Host(`{{ matrix_mautrix_googlechat_container_labels_public_endpoint_hostname }}`) && PathPrefix(`{{ matrix_mautrix_googlechat_container_labels_public_endpoint_path_prefix }}`)"
matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_priority: 0
matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_entrypoints: "{{ matrix_mautrix_googlechat_container_labels_traefik_entrypoints }}"
matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_tls: "{{ matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_entrypoints != 'web' }}"
matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_tls_certResolver: "{{ matrix_mautrix_googlechat_container_labels_traefik_tls_certResolver }}" # noqa var-naming
# Controls whether labels will be added that expose mautrix-googlechat's metrics
matrix_mautrix_googlechat_container_labels_metrics_enabled: "{{ matrix_mautrix_googlechat_metrics_enabled and matrix_mautrix_googlechat_metrics_proxying_enabled }}"
matrix_mautrix_googlechat_container_labels_metrics_traefik_rule: "Host(`{{ matrix_mautrix_googlechat_metrics_proxying_hostname }}`) && PathPrefix(`{{ matrix_mautrix_googlechat_metrics_proxying_path_prefix }}`)"
matrix_mautrix_googlechat_container_labels_metrics_traefik_priority: 0
matrix_mautrix_googlechat_container_labels_metrics_traefik_entrypoints: "{{ matrix_mautrix_googlechat_container_labels_traefik_entrypoints }}"
matrix_mautrix_googlechat_container_labels_metrics_traefik_tls: "{{ matrix_mautrix_googlechat_container_labels_metrics_traefik_entrypoints != 'web' }}"
matrix_mautrix_googlechat_container_labels_metrics_traefik_tls_certResolver: "{{ matrix_mautrix_googlechat_container_labels_traefik_tls_certResolver }}" # noqa var-naming
matrix_mautrix_googlechat_container_labels_metrics_middleware_basic_auth_enabled: false
# See: https://doc.traefik.io/traefik/middlewares/http/basicauth/#users
matrix_mautrix_googlechat_container_labels_metrics_middleware_basic_auth_users: ''
# matrix_mautrix_googlechat_container_labels_additional_labels contains a multiline string with additional labels to add to the container label file.
# See `../templates/labels.j2` for details.
#
# Example:
# matrix_mautrix_googlechat_container_labels_additional_labels: |
# my.label=1
# another.label="here"
matrix_mautrix_googlechat_container_labels_additional_labels: ''
# A list of extra arguments to pass to the container
matrix_mautrix_googlechat_container_extra_arguments: []
# List of systemd services that matrix-mautrix-googlechat.service depends on.
matrix_mautrix_googlechat_systemd_required_services_list: ['docker.service']
matrix_mautrix_googlechat_systemd_required_services_list: "{{ matrix_mautrix_googlechat_systemd_required_services_list_default + matrix_mautrix_googlechat_systemd_required_services_list_auto + matrix_mautrix_googlechat_systemd_required_services_list_custom }}"
matrix_mautrix_googlechat_systemd_required_services_list_default: ['docker.service']
matrix_mautrix_googlechat_systemd_required_services_list_auto: []
matrix_mautrix_googlechat_systemd_required_services_list_custom: []
# List of systemd services that matrix-mautrix-googlechat.service wants
matrix_mautrix_googlechat_systemd_wanted_services_list: []
@ -45,6 +93,16 @@ matrix_mautrix_googlechat_systemd_wanted_services_list: []
matrix_mautrix_googlechat_appservice_token: ''
matrix_mautrix_googlechat_homeserver_token: ''
# Whether or not metrics endpoint should be enabled.
# Enabling them is usually enough for a local (in-container) Prometheus to consume them.
# If metrics need to be consumed by another (external) Prometheus server, consider exposing them via `matrix_mautrix_googlechat_metrics_proxying_enabled`.
matrix_mautrix_googlechat_metrics_enabled: false
# Controls whether metrics should be proxied (exposed) on a public URL.
matrix_mautrix_googlechat_metrics_proxying_enabled: false
matrix_mautrix_googlechat_metrics_proxying_hostname: ''
matrix_mautrix_googlechat_metrics_proxying_path_prefix: ''
# Whether or not created rooms should have federation enabled.
# If false, created portal rooms will never be federated.
matrix_mautrix_googlechat_federate_rooms: true

View File

@ -1,43 +0,0 @@
---
- name: Fail if matrix-nginx-proxy role already executed
ansible.builtin.fail:
msg: >-
Trying to append Mautrix googlechat's reverse-proxying configuration to matrix-nginx-proxy,
but it's pointless since the matrix-nginx-proxy role had already executed.
To fix this, please change the order of roles in your playbook,
so that the matrix-nginx-proxy role would run after the matrix-bridge-mautrix-googlechat role.
when: matrix_nginx_proxy_role_executed | default(False) | bool
- name: Generate Mautrix googlechat proxying configuration for matrix-nginx-proxy
ansible.builtin.set_fact:
matrix_mautrix_googlechat_matrix_nginx_proxy_configuration: |
location {{ matrix_mautrix_googlechat_public_endpoint }} {
{% if matrix_nginx_proxy_enabled | default(False) %}
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "matrix-mautrix-googlechat:8080";
proxy_pass http://$backend;
{% else %}
{# Generic configuration for use outside of our container setup #}
proxy_pass http://127.0.0.1:9007;
{% endif %}
}
- name: Register Mautrix googlechat proxying configuration with matrix-nginx-proxy
ansible.builtin.set_fact:
matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks: |
{{
matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks | default([])
+
[matrix_mautrix_googlechat_matrix_nginx_proxy_configuration]
}}
- name: Warn about reverse-proxying if matrix-nginx-proxy not used
ansible.builtin.debug:
msg: >-
NOTE: You've enabled the Mautrix googlechat bridge but are not using the matrix-nginx-proxy
reverse proxy.
Please make sure that you're proxying the `{{ matrix_mautrix_googlechat_public_endpoint }}`
URL endpoint to the matrix-mautrix-googlechat container.
You can expose the container's port using the `matrix_mautrix_googlechat_container_http_host_bind_port` variable.
when: "matrix_mautrix_googlechat_enabled | bool and not matrix_nginx_proxy_enabled | default(False) | bool"

View File

@ -1,14 +1,5 @@
---
- tags:
- setup-all
- setup-nginx-proxy
- install-all
- install-nginx-proxy
block:
- when: matrix_mautrix_googlechat_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml"
- tags:
- setup-all
- setup-mautrix-googlechat

View File

@ -113,6 +113,21 @@
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
- name: Ensure mautrix-googlechat support files installed
ansible.builtin.template:
src: "{{ role_path }}/templates/{{ item }}.j2"
dest: "{{ matrix_mautrix_googlechat_base_path }}/{{ item }}"
mode: 0640
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
with_items:
- labels
- name: Ensure matrix-mautrix-googlechat container network is created
community.general.docker_network:
name: "{{ matrix_mautrix_googlechat_container_network }}"
driver: bridge
- name: Ensure matrix-mautrix-googlechat.service installed
ansible.builtin.template:
src: "{{ role_path }}/templates/systemd/matrix-mautrix-googlechat.service.j2"

View File

@ -10,3 +10,8 @@
- {'name': 'matrix_mautrix_googlechat_appservice_token', when: true}
- {'name': 'matrix_mautrix_googlechat_homeserver_token', when: true}
- {'name': 'matrix_mautrix_googlechat_database_hostname', when: "{{ matrix_mautrix_googlechat_database_engine == 'postgres' }}"}
- {'name': 'matrix_mautrix_googlechat_container_network', when: true}
- {'name': 'matrix_mautrix_googlechat_metrics_proxying_hostname', when: "{{ matrix_mautrix_googlechat_metrics_enabled }}"}
- {'name': 'matrix_mautrix_googlechat_metrics_proxying_path_prefix', when: "{{ matrix_mautrix_googlechat_metrics_enabled }}"}
- {'name': 'matrix_mautrix_googlechat_container_labels_public_endpoint_hostname', when: "{{ matrix_mautrix_googlechat_container_labels_public_endpoint_enabled }}"}
- {'name': 'matrix_mautrix_googlechat_container_labels_public_endpoint_path_prefix', when: "{{ matrix_mautrix_googlechat_container_labels_public_endpoint_enabled }}"}

View File

@ -42,6 +42,11 @@ appservice:
as_token: "{{ matrix_mautrix_googlechat_appservice_token }}"
hs_token: "{{ matrix_mautrix_googlechat_homeserver_token }}"
# Prometheus telemetry config. Requires prometheus-client to be installed.
metrics:
enabled: {{ matrix_mautrix_googlechat_metrics_enabled | to_json }}
listen_port: 8000
# Bridge config
bridge:
# Localpart template of MXIDs for googlechat users.

View File

@ -0,0 +1,55 @@
{% if matrix_mautrix_googlechat_container_labels_traefik_enabled %}
traefik.enable=true
{% if matrix_mautrix_googlechat_container_labels_traefik_docker_network %}
traefik.docker.network={{ matrix_mautrix_googlechat_container_labels_traefik_docker_network }}
{% endif %}
{#
Public
#}
{% if matrix_mautrix_googlechat_container_labels_public_endpoint_enabled %}
traefik.http.routers.matrix-mautrix-googlechat-public.rule={{ matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_rule }}
{% if matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_priority | int > 0 %}
traefik.http.routers.matrix-mautrix-googlechat-public.priority={{ matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_priority }}
{% endif %}
traefik.http.routers.matrix-mautrix-googlechat-public.service=matrix-mautrix-googlechat-public
traefik.http.routers.matrix-mautrix-googlechat-public.entrypoints={{ matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_entrypoints }}
traefik.http.routers.matrix-mautrix-googlechat-public.tls={{ matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_tls | to_json }}
{% if matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_tls %}
traefik.http.routers.matrix-mautrix-googlechat-public.tls.certResolver={{ matrix_mautrix_googlechat_container_labels_public_endpoint_traefik_tls_certResolver }}
{% endif %}
traefik.http.services.matrix-mautrix-googlechat-public.loadbalancer.server.port=8080
{% endif %}
{#
/Public
#}
{#
Metrics
#}
{% if matrix_mautrix_googlechat_container_labels_metrics_enabled %}
traefik.http.routers.matrix-mautrix-googlechat-metrics.rule={{ matrix_mautrix_googlechat_container_labels_metrics_traefik_rule }}
{% if matrix_mautrix_googlechat_container_labels_metrics_traefik_priority | int > 0 %}
traefik.http.routers.matrix-mautrix-googlechat-metrics.priority={{ matrix_mautrix_googlechat_container_labels_metrics_traefik_priority }}
{% endif %}
traefik.http.routers.matrix-mautrix-googlechat-metrics.service=matrix-mautrix-googlechat-metrics
traefik.http.routers.matrix-mautrix-googlechat-metrics.entrypoints={{ matrix_mautrix_googlechat_container_labels_metrics_traefik_entrypoints }}
traefik.http.routers.matrix-mautrix-googlechat-metrics.tls={{ matrix_mautrix_googlechat_container_labels_metrics_traefik_tls | to_json }}
{% if matrix_mautrix_googlechat_container_labels_metrics_traefik_tls %}
traefik.http.routers.matrix-mautrix-googlechat-metrics.tls.certResolver={{ matrix_mautrix_googlechat_container_labels_metrics_traefik_tls_certResolver }}
{% endif %}
{% if matrix_mautrix_googlechat_container_labels_metrics_middleware_basic_auth_enabled %}
traefik.http.routers.matrix-mautrix-googlechat-metrics.middlewares=matrix-mautrix-googlechat-metrics-basic-auth
traefik.http.middlewares.matrix-mautrix-googlechat-metrics-basic-auth.basicauth.users={{ matrix_mautrix_googlechat_container_labels_metrics_middleware_basic_auth_users }}
{% endif %}
traefik.http.services.matrix-mautrix-googlechat-metrics.loadbalancer.server.port=8000
{% endif %}
{#
/Metrics
#}
{% endif %}
{{ matrix_mautrix_googlechat_container_labels_additional_labels }}

View File

@ -17,11 +17,13 @@ Environment="HOME={{ devture_systemd_docker_base_systemd_unit_home_path }}"
# 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-mautrix-googlechat \
ExecStart={{ devture_systemd_docker_base_host_command_docker }} create \
--rm \
--name=matrix-mautrix-googlechat \
--log-driver=none \
--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
--cap-drop=ALL \
--network={{ matrix_docker_network }} \
--network={{ matrix_mautrix_googlechat_container_network }} \
{% if matrix_mautrix_googlechat_container_http_host_bind_port %}
-p {{ matrix_mautrix_googlechat_container_http_host_bind_port }}:8080 \
{% endif %}
@ -33,6 +35,12 @@ ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name
{{ matrix_mautrix_googlechat_docker_image }} \
python3 -m mautrix_googlechat -c /config/config.yaml --no-update
{% for network in matrix_mautrix_googlechat_container_additional_networks %}
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-mautrix-googlechat
{% endfor %}
ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-mautrix-googlechat
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-mautrix-googlechat 2>/dev/null || true'
ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-mautrix-googlechat 2>/dev/null || true'
Restart=always