Switch mautrix-whatsapp from matrix-nginx-proxy to matrix-homeserver-proxy

This commit is contained in:
Slavi Pantaleev 2024-01-02 17:41:36 +02:00
parent 8eb07e8d85
commit feaf1ee7e7
7 changed files with 129 additions and 63 deletions

View File

@ -1323,29 +1323,47 @@ matrix_mautrix_whatsapp_enabled: false
matrix_mautrix_whatsapp_container_image_self_build: "{{ matrix_architecture not in ['arm64', 'amd64'] }}"
matrix_mautrix_whatsapp_systemd_required_services_list: |
matrix_mautrix_whatsapp_container_network: "{{ matrix_bridges_container_network }}"
matrix_mautrix_whatsapp_container_additional_networks_auto: |-
{{
['docker.service']
(
([] if matrix_bridges_homeserver_container_network == '' else [matrix_bridges_homeserver_container_network])
+
([devture_postgres_container_network] if devture_postgres_enabled and matrix_mautrix_whatsapp_database_hostname == devture_postgres_connection_hostname else [])
+
([matrix_playbook_reverse_proxyable_services_additional_network] if matrix_playbook_reverse_proxyable_services_additional_network and matrix_mautrix_whatsapp_container_labels_traefik_enabled else [])
) | unique
}}
matrix_mautrix_whatsapp_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}"
matrix_mautrix_whatsapp_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}"
matrix_mautrix_whatsapp_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}"
matrix_mautrix_whatsapp_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}"
matrix_mautrix_whatsapp_container_labels_metrics_middleware_basic_auth_enabled: "{{ matrix_metrics_exposure_http_basic_auth_enabled }}"
matrix_mautrix_whatsapp_container_labels_metrics_middleware_basic_auth_users: "{{ matrix_metrics_exposure_http_basic_auth_users }}"
matrix_mautrix_whatsapp_systemd_required_services_list_auto: |
{{
matrix_bots_homeserver_systemd_services_list
+
['matrix-' + matrix_homeserver_implementation + '.service']
+
([devture_postgres_identifier ~ '.service'] if devture_postgres_enabled else [])
+
(['matrix-nginx-proxy.service'] if matrix_nginx_proxy_enabled else [])
([devture_postgres_identifier ~ '.service'] if devture_postgres_enabled and matrix_mautrix_whatsapp_database_hostname == devture_postgres_connection_hostname else [])
}}
matrix_mautrix_whatsapp_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'whats.as.token', rounds=655555) | to_uuid }}"
matrix_mautrix_whatsapp_homeserver_address: "{{ matrix_bridges_homeserver_client_api_url }}"
matrix_mautrix_whatsapp_homeserver_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'whats.hs.token', rounds=655555) | to_uuid }}"
matrix_mautrix_whatsapp_login_shared_secret: "{{ matrix_synapse_ext_password_provider_shared_secret_auth_shared_secret if matrix_synapse_ext_password_provider_shared_secret_auth_enabled else '' }}"
# People using an external Prometheus server will need to toggle all of these to be able to consume metrics remotely:
# - `matrix_mautrix_whatsapp_metrics_enabled`
# - `matrix_mautrix_whatsapp_proxying_metrics_enabled`
# - `matrix_nginx_proxy_proxy_matrix_metrics_enabled`
matrix_mautrix_whatsapp_metrics_enabled: "{{ prometheus_enabled }}"
matrix_mautrix_whatsapp_metrics_proxying_enabled: "{{ matrix_mautrix_whatsapp_metrics_enabled and matrix_metrics_exposure_enabled }}"
matrix_mautrix_whatsapp_metrics_proxying_hostname: "{{ matrix_metrics_exposure_hostname }}"
matrix_mautrix_whatsapp_metrics_proxying_path_prefix: "{{ matrix_metrics_exposure_path_prefix }}/mautrix-whatsapp"
# Postgres is the default, except if not using internal Postgres server
matrix_mautrix_whatsapp_database_engine: "{{ 'postgres' if devture_postgres_enabled else 'sqlite' }}"
matrix_mautrix_whatsapp_database_hostname: "{{ devture_postgres_connection_hostname if devture_postgres_enabled else '' }}"

View File

@ -27,11 +27,49 @@ matrix_mautrix_whatsapp_appservice_address: "http://matrix-mautrix-whatsapp:8080
matrix_mautrix_whatsapp_command_prefix: "!wa"
matrix_mautrix_whatsapp_container_network: ""
matrix_mautrix_whatsapp_container_additional_networks: "{{ matrix_mautrix_whatsapp_container_additional_networks_auto + matrix_mautrix_whatsapp_container_additional_networks_custom }}"
matrix_mautrix_whatsapp_container_additional_networks_auto: []
matrix_mautrix_whatsapp_container_additional_networks_custom: []
# matrix_mautrix_whatsapp_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_whatsapp_container_labels_additional_labels`.
matrix_mautrix_whatsapp_container_labels_traefik_enabled: true
matrix_mautrix_whatsapp_container_labels_traefik_docker_network: "{{ matrix_mautrix_whatsapp_container_network }}"
matrix_mautrix_whatsapp_container_labels_traefik_entrypoints: web-secure
matrix_mautrix_whatsapp_container_labels_traefik_tls_certResolver: default
# Controls whether labels will be added that expose mautrix-whatsapp's metrics
matrix_mautrix_whatsapp_container_labels_metrics_enabled: "{{ matrix_mautrix_whatsapp_metrics_enabled and matrix_mautrix_whatsapp_metrics_proxying_enabled }}"
matrix_mautrix_whatsapp_container_labels_metrics_traefik_rule: "Host(`{{ matrix_mautrix_whatsapp_metrics_proxying_hostname }}`) && PathPrefix(`{{ matrix_mautrix_whatsapp_metrics_proxying_path_prefix }}`)"
matrix_mautrix_whatsapp_container_labels_metrics_traefik_priority: 0
matrix_mautrix_whatsapp_container_labels_metrics_traefik_entrypoints: "{{ matrix_mautrix_whatsapp_container_labels_traefik_entrypoints }}"
matrix_mautrix_whatsapp_container_labels_metrics_traefik_tls: "{{ matrix_mautrix_whatsapp_container_labels_metrics_traefik_entrypoints != 'web' }}"
matrix_mautrix_whatsapp_container_labels_metrics_traefik_tls_certResolver: "{{ matrix_mautrix_whatsapp_container_labels_traefik_tls_certResolver }}" # noqa var-naming
matrix_mautrix_whatsapp_container_labels_metrics_middleware_basic_auth_enabled: false
# See: https://doc.traefik.io/traefik/middlewares/http/basicauth/#users
matrix_mautrix_whatsapp_container_labels_metrics_middleware_basic_auth_users: ''
# matrix_mautrix_whatsapp_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_whatsapp_container_labels_additional_labels: |
# my.label=1
# another.label="here"
matrix_mautrix_whatsapp_container_labels_additional_labels: ''
# A list of extra arguments to pass to the container
matrix_mautrix_whatsapp_container_extra_arguments: []
# List of systemd services that matrix-mautrix-whatsapp.service depends on.
matrix_mautrix_whatsapp_systemd_required_services_list: ['docker.service']
matrix_mautrix_whatsapp_systemd_required_services_list: "{{ matrix_mautrix_whatsapp_systemd_required_services_list_default + matrix_mautrix_whatsapp_systemd_required_services_list_auto + matrix_mautrix_whatsapp_systemd_required_services_list_custom }}"
matrix_mautrix_whatsapp_systemd_required_services_list_default: ['docker.service']
matrix_mautrix_whatsapp_systemd_required_services_list_auto: []
matrix_mautrix_whatsapp_systemd_required_services_list_custom: []
# List of systemd services that matrix-mautrix-whatsapp.service wants
matrix_mautrix_whatsapp_systemd_wanted_services_list: []
@ -54,10 +92,10 @@ matrix_mautrix_whatsapp_federate_rooms: true
# If metrics need to be consumed by another (external) Prometheus server, consider exposing them via `matrix_mautrix_whatsapp_metrics_proxying_enabled`.
matrix_mautrix_whatsapp_metrics_enabled: false
# Controls whether metrics should be proxied (exposed) on `matrix.DOMAIN/metrics/mautrix-whatsapp`.
# This will only work take effect if `matrix_nginx_proxy_proxy_matrix_metrics_enabled: true`.
# See the `matrix-nginx-proxy` role for details about enabling `matrix_nginx_proxy_proxy_matrix_metrics_enabled`.
# Controls whether metrics should be proxied (exposed) on a public URL
matrix_mautrix_whatsapp_metrics_proxying_enabled: false
matrix_mautrix_whatsapp_metrics_proxying_hostname: ''
matrix_mautrix_whatsapp_metrics_proxying_path_prefix: ''
# Database-related configuration fields.
#

View File

@ -1,35 +0,0 @@
---
- name: Fail if matrix-nginx-proxy role already executed
ansible.builtin.fail:
msg: >-
Trying to append mautrix-whatapp-metrics'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-whatsapp role.
when: matrix_nginx_proxy_role_executed | default(False) | bool
- when: matrix_mautrix_whatsapp_metrics_proxying_enabled | bool
block:
- name: Generate mautrix-whatsapp metrics proxying configuration for matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-whatsapp)
ansible.builtin.set_fact:
matrix_mautrix_whatsapp_nginx_metrics_configuration_block: |
location /metrics/mautrix-whatsapp {
{% 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-whatsapp:8001";
proxy_pass http://$backend/metrics;
{% else %}
return 404 "matrix-nginx-proxy is disabled and no host port was bound to the container, so metrics are unavailable";
{% endif %}
}
- name: Register mautrix-whatsapp metrics proxying configuration with matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-whatsapp)
ansible.builtin.set_fact:
matrix_nginx_proxy_proxy_matrix_metrics_additional_system_location_configuration_blocks: |
{{
matrix_nginx_proxy_proxy_matrix_metrics_additional_system_location_configuration_blocks | default([])
+
[matrix_mautrix_whatsapp_nginx_metrics_configuration_block]
}}

View File

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

View File

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

View File

@ -0,0 +1,30 @@
{% if matrix_mautrix_whatsapp_container_labels_traefik_enabled %}
traefik.enable=true
{% if matrix_mautrix_whatsapp_container_labels_traefik_docker_network %}
traefik.docker.network={{ matrix_mautrix_whatsapp_container_labels_traefik_docker_network }}
{% endif %}
{# Metrics #}
{% if matrix_mautrix_whatsapp_container_labels_metrics_enabled %}
traefik.http.routers.matrix-mautrix-whatsapp-metrics.rule={{ matrix_mautrix_whatsapp_container_labels_metrics_traefik_rule }}
{% if matrix_mautrix_whatsapp_container_labels_metrics_traefik_priority | int > 0 %}
traefik.http.routers.matrix-mautrix-whatsapp-metrics.priority={{ matrix_mautrix_whatsapp_container_labels_metrics_traefik_priority }}
{% endif %}
traefik.http.routers.matrix-mautrix-whatsapp-metrics.service=matrix-mautrix-whatsapp-metrics
traefik.http.routers.matrix-mautrix-whatsapp-metrics.entrypoints={{ matrix_mautrix_whatsapp_container_labels_metrics_traefik_entrypoints }}
traefik.http.routers.matrix-mautrix-whatsapp-metrics.tls={{ matrix_mautrix_whatsapp_container_labels_metrics_traefik_tls | to_json }}
{% if matrix_mautrix_whatsapp_container_labels_metrics_traefik_tls %}
traefik.http.routers.matrix-mautrix-whatsapp-metrics.tls.certResolver={{ matrix_mautrix_whatsapp_container_labels_metrics_traefik_tls_certResolver }}
{% endif %}
{% if matrix_mautrix_whatsapp_container_labels_metrics_middleware_basic_auth_enabled %}
traefik.http.routers.matrix-mautrix-whatsapp-metrics.middlewares=matrix-mautrix-whatsapp-metrics-basic-auth
traefik.http.middlewares.matrix-mautrix-whatsapp-metrics-basic-auth.basicauth.users={{ matrix_mautrix_whatsapp_container_labels_metrics_middleware_basic_auth_users }}
{% endif %}
traefik.http.services.matrix-mautrix-whatsapp-metrics.loadbalancer.server.port=8001
{% endif %}
{% endif %}
{{ matrix_mautrix_whatsapp_container_labels_additional_labels }}

View File

@ -19,13 +19,16 @@ 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-mautrix-whatsapp \
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \
--rm \
--name=matrix-mautrix-whatsapp \
--log-driver=none \
--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
--cap-drop=ALL \
--network={{ matrix_docker_network }} \
-v {{ matrix_mautrix_whatsapp_config_path }}:/config:z \
-v {{ matrix_mautrix_whatsapp_data_path }}:/data:z \
--network={{ matrix_mautrix_whatsapp_container_network }} \
--mount type=bind,src={{ matrix_mautrix_whatsapp_config_path }},dst=/config \
--mount type=bind,src={{ matrix_mautrix_whatsapp_data_path }},dst=/data \
--label-file={{ matrix_mautrix_whatsapp_base_path }}/labels \
--workdir=/data \
{% for arg in matrix_mautrix_whatsapp_container_extra_arguments %}
{{ arg }} \
@ -33,6 +36,12 @@ ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name
{{ matrix_mautrix_whatsapp_docker_image }} \
/usr/bin/mautrix-whatsapp -c /config/config.yaml -r /config/registration.yaml
{% for network in matrix_mautrix_whatsapp_container_additional_networks %}
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-mautrix-whatsapp
{% endfor %}
ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-mautrix-whatsapp
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-whatsapp 2>/dev/null || true'
ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-mautrix-whatsapp 2>/dev/null || true'
Restart=always