diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 4c6d8a8ac..198eec546 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -1361,19 +1361,36 @@ matrix_mautrix_telegram_database_password: "{{ '%s' | format(matrix_homeserver_g # We don't enable bridges by default. matrix_mautrix_twitter_enabled: false +matrix_mautrix_twitter_systemd_required_services_list_auto: | + {{ + matrix_addons_homeserver_systemd_services_list + + + ([devture_postgres_identifier ~ '.service'] if (devture_postgres_enabled and matrix_mautrix_twitter_database_hostname == devture_postgres_connection_hostname) else []) + }} + matrix_mautrix_twitter_container_image_self_build: "{{ matrix_architecture not in ['amd64', 'arm64'] }}" -matrix_mautrix_twitter_systemd_required_services_list: | +matrix_mautrix_twitter_container_network: "{{ matrix_addons_container_network }}" + +matrix_mautrix_twitter_container_additional_networks_auto: |- {{ - ['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 []) + ( + ([] if matrix_addons_homeserver_container_network == '' else [matrix_addons_homeserver_container_network]) + + + ([devture_postgres_container_network] if (devture_postgres_enabled and matrix_mautrix_twitter_database_hostname == devture_postgres_connection_hostname and matrix_mautrix_twitter_container_network != devture_postgres_container_network) else []) + + + ([matrix_playbook_reverse_proxyable_services_additional_network] if matrix_playbook_reverse_proxyable_services_additional_network and matrix_mautrix_twitter_container_labels_traefik_enabled else []) + ) | unique }} +matrix_mautrix_twitter_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}" +matrix_mautrix_twitter_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}" +matrix_mautrix_twitter_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}" +matrix_mautrix_twitter_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}" + +matrix_mautrix_twitter_container_labels_metrics_middleware_basic_auth_enabled: "{{ matrix_metrics_exposure_http_basic_auth_enabled }}" +matrix_mautrix_twitter_container_labels_metrics_middleware_basic_auth_users: "{{ matrix_metrics_exposure_http_basic_auth_users }}" + matrix_mautrix_twitter_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'twt.as.token', rounds=655555) | to_uuid }}" matrix_mautrix_twitter_homeserver_address: "{{ matrix_addons_homeserver_client_api_url }}" @@ -1381,6 +1398,12 @@ matrix_mautrix_twitter_homeserver_token: "{{ '%s' | format(matrix_homeserver_gen matrix_mautrix_twitter_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_twitter_metrics_enabled: "{{ prometheus_enabled }}" + +matrix_mautrix_twitter_metrics_proxying_enabled: "{{ matrix_mautrix_twitter_metrics_enabled and matrix_metrics_exposure_enabled }}" +matrix_mautrix_twitter_metrics_proxying_hostname: "{{ matrix_metrics_exposure_hostname }}" +matrix_mautrix_twitter_metrics_proxying_path_prefix: "{{ matrix_metrics_exposure_path_prefix }}/mautrix-twitter" + matrix_mautrix_twitter_database_hostname: "{{ devture_postgres_connection_hostname if devture_postgres_enabled else '' }}" matrix_mautrix_twitter_database_password: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'mau.twt.db', rounds=655555) | to_uuid if devture_postgres_enabled else '' }}" diff --git a/roles/custom/matrix-bridge-mautrix-twitter/defaults/main.yml b/roles/custom/matrix-bridge-mautrix-twitter/defaults/main.yml index 8cdd76311..091a6899d 100644 --- a/roles/custom/matrix-bridge-mautrix-twitter/defaults/main.yml +++ b/roles/custom/matrix-bridge-mautrix-twitter/defaults/main.yml @@ -32,11 +32,49 @@ matrix_mautrix_twitter_bridge_permissions: | | combine({matrix_admin: 'admin'} if matrix_admin else {}) }} +matrix_mautrix_twitter_container_network: "" + +matrix_mautrix_twitter_container_additional_networks: "{{ matrix_mautrix_twitter_container_additional_networks_auto + matrix_mautrix_twitter_container_additional_networks_custom }}" +matrix_mautrix_twitter_container_additional_networks_auto: [] +matrix_mautrix_twitter_container_additional_networks_custom: [] + +# matrix_mautrix_twitter_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_twitter_container_labels_additional_labels`. +matrix_mautrix_twitter_container_labels_traefik_enabled: true +matrix_mautrix_twitter_container_labels_traefik_docker_network: "{{ matrix_mautrix_twitter_container_network }}" +matrix_mautrix_twitter_container_labels_traefik_entrypoints: web-secure +matrix_mautrix_twitter_container_labels_traefik_tls_certResolver: default # noqa var-naming + +# Controls whether labels will be added that expose mautrix-instagram's metrics +matrix_mautrix_twitter_container_labels_metrics_enabled: "{{ matrix_mautrix_twitter_metrics_enabled and matrix_mautrix_twitter_metrics_proxying_enabled }}" +matrix_mautrix_twitter_container_labels_metrics_traefik_rule: "Host(`{{ matrix_mautrix_twitter_metrics_proxying_hostname }}`) && PathPrefix(`{{ matrix_mautrix_twitter_metrics_proxying_path_prefix }}`)" +matrix_mautrix_twitter_container_labels_metrics_traefik_priority: 0 +matrix_mautrix_twitter_container_labels_metrics_traefik_entrypoints: "{{ matrix_mautrix_twitter_container_labels_traefik_entrypoints }}" +matrix_mautrix_twitter_container_labels_metrics_traefik_tls: "{{ matrix_mautrix_twitter_container_labels_metrics_traefik_entrypoints != 'web' }}" +matrix_mautrix_twitter_container_labels_metrics_traefik_tls_certResolver: "{{ matrix_mautrix_twitter_container_labels_traefik_tls_certResolver }}" # noqa var-naming +matrix_mautrix_twitter_container_labels_metrics_middleware_basic_auth_enabled: false +# See: https://doc.traefik.io/traefik/middlewares/http/basicauth/#users +matrix_mautrix_twitter_container_labels_metrics_middleware_basic_auth_users: '' + +# matrix_mautrix_twitter_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_twitter_container_labels_additional_labels: | +# my.label=1 +# another.label="here" +matrix_mautrix_twitter_container_labels_additional_labels: '' + # A list of extra arguments to pass to the container matrix_mautrix_twitter_container_extra_arguments: [] # List of systemd services that matrix-mautrix-twitter.service depends on. -matrix_mautrix_twitter_systemd_required_services_list: ['docker.service'] +matrix_mautrix_twitter_systemd_required_services_list: "{{ matrix_mautrix_twitter_systemd_required_services_list_default + matrix_mautrix_twitter_systemd_required_services_list_auto + matrix_mautrix_twitter_systemd_required_services_list_custom }}" +matrix_mautrix_twitter_systemd_required_services_list_default: ['docker.service'] +matrix_mautrix_twitter_systemd_required_services_list_auto: [] +matrix_mautrix_twitter_systemd_required_services_list_custom: [] # List of systemd services that matrix-mautrix-twitter.service wants matrix_mautrix_twitter_systemd_wanted_services_list: [] @@ -84,9 +122,10 @@ matrix_mautrix_twitter_logging_level: WARNING # If metrics need to be consumed by another (external) Prometheus server, consider exposing them via `matrix_mautrix_twitter_metrics_proxying_enabled`. matrix_mautrix_twitter_metrics_enabled: false -# Controls whether metrics should be proxied (exposed) on a public URL +# Controls whether metrics should be exposed on a public URL. matrix_mautrix_twitter_metrics_proxying_enabled: false -# TODO - add more variables for controlling the hostname, path prefix, etc. +matrix_mautrix_twitter_metrics_proxying_hostname: '' +matrix_mautrix_twitter_metrics_proxying_path_prefix: '' # Default configuration template which covers the generic use case. # You can customize it by controlling the various variables inside it. @@ -121,7 +160,7 @@ matrix_mautrix_twitter_registration_yaml: | - exclusive: true regex: '^@{{ matrix_mautrix_twitter_appservice_bot_username | regex_escape }}:{{ matrix_mautrix_twitter_homeserver_domain | regex_escape }}$' url: {{ matrix_mautrix_twitter_appservice_address }} - # See https://github.com/tulir/mautrix-signal/issues/43 + # See https://github.com/tulir/mautrix-twitter/issues/43 sender_localpart: _bot_{{ matrix_mautrix_twitter_appservice_bot_username }} rate_limited: false de.sorunome.msc2409.push_ephemeral: true diff --git a/roles/custom/matrix-bridge-mautrix-twitter/tasks/inject_into_nginx_proxy.yml b/roles/custom/matrix-bridge-mautrix-twitter/tasks/inject_into_nginx_proxy.yml deleted file mode 100644 index e4d431283..000000000 --- a/roles/custom/matrix-bridge-mautrix-twitter/tasks/inject_into_nginx_proxy.yml +++ /dev/null @@ -1,28 +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-twitter role. - when: matrix_nginx_proxy_role_executed | default(False) | bool - -- when: matrix_mautrix_twitter_metrics_proxying_enabled | bool - block: - - name: Generate mautrix-twitter metrics proxying configuration for matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-twitter) - ansible.builtin.set_fact: - matrix_mautrix_twitter_nginx_metrics_configuration_block: | - location /metrics/mautrix-twitter { - {% 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-twitter:8000"; - 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 %} - } - -# Injection code was here diff --git a/roles/custom/matrix-bridge-mautrix-twitter/tasks/main.yml b/roles/custom/matrix-bridge-mautrix-twitter/tasks/main.yml index 655e52fbc..bb5a3a32e 100644 --- a/roles/custom/matrix-bridge-mautrix-twitter/tasks/main.yml +++ b/roles/custom/matrix-bridge-mautrix-twitter/tasks/main.yml @@ -1,14 +1,5 @@ --- -- tags: - - setup-all - - setup-nginx-proxy - - install-all - - install-nginx-proxy - block: - - when: matrix_mautrix_twitter_enabled | bool and matrix_mautrix_twitter_metrics_enabled | bool - ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml" - - tags: - setup-all - setup-mautrix-twitter diff --git a/roles/custom/matrix-bridge-mautrix-twitter/tasks/setup_install.yml b/roles/custom/matrix-bridge-mautrix-twitter/tasks/setup_install.yml index d5c26c2b5..04fc858d2 100644 --- a/roles/custom/matrix-bridge-mautrix-twitter/tasks/setup_install.yml +++ b/roles/custom/matrix-bridge-mautrix-twitter/tasks/setup_install.yml @@ -67,6 +67,21 @@ owner: "{{ matrix_user_username }}" group: "{{ matrix_user_groupname }}" +- name: Ensure mautrix-twitter support files installed + ansible.builtin.template: + src: "{{ role_path }}/templates/{{ item }}.j2" + dest: "{{ matrix_mautrix_twitter_base_path }}/{{ item }}" + mode: 0640 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - labels + +- name: Ensure matrix-mautrix-twitter container network is created + community.general.docker_network: + name: "{{ matrix_mautrix_twitter_container_network }}" + driver: bridge + - name: Ensure matrix-mautrix-twitter.service installed ansible.builtin.template: src: "{{ role_path }}/templates/systemd/matrix-mautrix-twitter.service.j2" diff --git a/roles/custom/matrix-bridge-mautrix-twitter/tasks/validate_config.yml b/roles/custom/matrix-bridge-mautrix-twitter/tasks/validate_config.yml index bfe3fd21c..0edaa29bb 100644 --- a/roles/custom/matrix-bridge-mautrix-twitter/tasks/validate_config.yml +++ b/roles/custom/matrix-bridge-mautrix-twitter/tasks/validate_config.yml @@ -10,3 +10,6 @@ - {'name': 'matrix_mautrix_twitter_homeserver_address', when: true} - {'name': 'matrix_mautrix_twitter_homeserver_token', when: true} - {'name': 'matrix_mautrix_twitter_database_hostname', when: "{{ matrix_mautrix_twitter_database_engine == 'postgres' }}"} + - {'name': 'matrix_mautrix_twitter_container_network', when: true} + - {'name': 'matrix_mautrix_twitter_metrics_proxying_hostname', when: "{{ matrix_mautrix_twitter_metrics_proxying_enabled }}"} + - {'name': 'matrix_mautrix_twitter_metrics_proxying_path_prefix', when: "{{ matrix_mautrix_twitter_metrics_proxying_enabled }}"} diff --git a/roles/custom/matrix-bridge-mautrix-twitter/templates/labels.j2 b/roles/custom/matrix-bridge-mautrix-twitter/templates/labels.j2 new file mode 100644 index 000000000..2fc8b833d --- /dev/null +++ b/roles/custom/matrix-bridge-mautrix-twitter/templates/labels.j2 @@ -0,0 +1,30 @@ +{% if matrix_mautrix_twitter_container_labels_traefik_enabled %} +traefik.enable=true + +{% if matrix_mautrix_twitter_container_labels_traefik_docker_network %} +traefik.docker.network={{ matrix_mautrix_twitter_container_labels_traefik_docker_network }} +{% endif %} + +{# Metrics #} +{% if matrix_mautrix_twitter_container_labels_metrics_enabled %} +traefik.http.routers.matrix-mautrix-twitter-metrics.rule={{ matrix_mautrix_twitter_container_labels_metrics_traefik_rule }} +{% if matrix_mautrix_twitter_container_labels_metrics_traefik_priority | int > 0 %} +traefik.http.routers.matrix-mautrix-twitter-metrics.priority={{ matrix_mautrix_twitter_container_labels_metrics_traefik_priority }} +{% endif %} +traefik.http.routers.matrix-mautrix-twitter-metrics.service=matrix-mautrix-twitter-metrics +traefik.http.routers.matrix-mautrix-twitter-metrics.entrypoints={{ matrix_mautrix_twitter_container_labels_metrics_traefik_entrypoints }} +traefik.http.routers.matrix-mautrix-twitter-metrics.tls={{ matrix_mautrix_twitter_container_labels_metrics_traefik_tls | to_json }} +{% if matrix_mautrix_twitter_container_labels_metrics_traefik_tls %} +traefik.http.routers.matrix-mautrix-twitter-metrics.tls.certResolver={{ matrix_mautrix_twitter_container_labels_metrics_traefik_tls_certResolver }} +{% endif %} +{% if matrix_mautrix_twitter_container_labels_metrics_middleware_basic_auth_enabled %} +traefik.http.routers.matrix-mautrix-twitter-metrics.middlewares=matrix-mautrix-twitter-metrics-basic-auth +traefik.http.middlewares.matrix-mautrix-twitter-metrics-basic-auth.basicauth.users={{ matrix_mautrix_twitter_container_labels_metrics_middleware_basic_auth_users }} +{% endif %} + +traefik.http.services.matrix-mautrix-twitter-metrics.loadbalancer.server.port=8000 +{% endif %} + +{% endif %} + +{{ matrix_mautrix_twitter_container_labels_additional_labels }} diff --git a/roles/custom/matrix-bridge-mautrix-twitter/templates/systemd/matrix-mautrix-twitter.service.j2 b/roles/custom/matrix-bridge-mautrix-twitter/templates/systemd/matrix-mautrix-twitter.service.j2 index 114d3a6fa..0c12638a1 100644 --- a/roles/custom/matrix-bridge-mautrix-twitter/templates/systemd/matrix-mautrix-twitter.service.j2 +++ b/roles/custom/matrix-bridge-mautrix-twitter/templates/systemd/matrix-mautrix-twitter.service.j2 @@ -19,19 +19,28 @@ 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-twitter \ +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \ + --rm \ + --name=matrix-mautrix-twitter \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ - --network={{ matrix_docker_network }} \ - -v {{ matrix_mautrix_twitter_config_path }}:/config:z \ - -v {{ matrix_mautrix_twitter_data_path }}:/data:z \ + --network={{ matrix_mautrix_twitter_container_network }} \ + --mount type=bind,src={{ matrix_mautrix_twitter_config_path }},dst=/config,ro \ + --mount type=bind,src={{ matrix_mautrix_twitter_data_path }},dst=/data \ + --label-file={{ matrix_mautrix_twitter_base_path }}/labels \ {% for arg in matrix_mautrix_twitter_container_extra_arguments %} {{ arg }} \ {% endfor %} {{ matrix_mautrix_twitter_docker_image }} \ python3 -m mautrix_twitter -c /config/config.yaml --no-update +{% for network in matrix_mautrix_twitter_container_additional_networks %} +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-mautrix-twitter +{% endfor %} + +ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-mautrix-twitter + 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-twitter 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-mautrix-twitter 2>/dev/null || true' Restart=always