From 128a7b82d51b9b3d087c862d15e79477617d0cff Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Wed, 3 Jan 2024 09:24:40 +0200 Subject: [PATCH] Switch mautrix-instagram from matrix-nginx-proxy to matrix-homeserver-proxy This is completely untested. --- group_vars/matrix_servers | 57 +++++++++++++------ .../defaults/main.yml | 47 +++++++++++++-- .../tasks/inject_into_nginx_proxy.yml | 35 ------------ .../tasks/main.yml | 9 --- .../tasks/setup_install.yml | 15 +++++ .../templates/labels.j2 | 30 ++++++++++ .../matrix-mautrix-instagram.service.j2 | 16 ++++-- 7 files changed, 139 insertions(+), 70 deletions(-) delete mode 100644 roles/custom/matrix-bridge-mautrix-instagram/tasks/inject_into_nginx_proxy.yml create mode 100644 roles/custom/matrix-bridge-mautrix-instagram/templates/labels.j2 diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 519a8314a..ba6f83042 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -915,6 +915,15 @@ matrix_mautrix_facebook_systemd_required_services_list_auto: | ([devture_postgres_identifier ~ '.service'] if devture_postgres_enabled and matrix_mautrix_facebook_database_hostname == devture_postgres_connection_hostname else []) }} + +matrix_mautrix_facebook_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}" +matrix_mautrix_facebook_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}" +matrix_mautrix_facebook_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}" +matrix_mautrix_facebook_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}" + +matrix_mautrix_facebook_container_labels_metrics_middleware_basic_auth_enabled: "{{ matrix_metrics_exposure_http_basic_auth_enabled }}" +matrix_mautrix_facebook_container_labels_metrics_middleware_basic_auth_users: "{{ matrix_metrics_exposure_http_basic_auth_users }}" + matrix_mautrix_facebook_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'fb.as.token', rounds=655555) | to_uuid }}" matrix_mautrix_facebook_homeserver_address: "{{ matrix_bridges_homeserver_client_api_url }}" @@ -936,8 +945,6 @@ matrix_mautrix_facebook_metrics_enabled: "{{ prometheus_enabled }}" matrix_mautrix_facebook_metrics_proxying_enabled: "{{ matrix_mautrix_facebook_metrics_enabled and matrix_metrics_exposure_enabled }}" matrix_mautrix_facebook_metrics_proxying_hostname: "{{ matrix_metrics_exposure_hostname }}" matrix_mautrix_facebook_metrics_proxying_path_prefix: "{{ matrix_metrics_exposure_path_prefix }}/mautrix-facebook" -matrix_mautrix_facebook_container_labels_metrics_middleware_basic_auth_enabled: "{{ matrix_metrics_exposure_http_basic_auth_enabled }}" -matrix_mautrix_facebook_container_labels_metrics_middleware_basic_auth_users: "{{ matrix_metrics_exposure_http_basic_auth_users }}" # We'd like to force-set people with external Postgres to SQLite, so the bridge role can complain # and point them to a migration path. @@ -945,11 +952,6 @@ matrix_mautrix_facebook_database_engine: "{{ 'postgres' if devture_postgres_enab matrix_mautrix_facebook_database_hostname: "{{ devture_postgres_connection_hostname if devture_postgres_enabled else '' }}" matrix_mautrix_facebook_database_password: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'mau.fb.db', rounds=655555) | to_uuid }}" -matrix_mautrix_facebook_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}" -matrix_mautrix_facebook_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}" -matrix_mautrix_facebook_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}" -matrix_mautrix_facebook_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}" - ###################################################################### # # /matrix-bridge-mautrix-facebook @@ -1052,31 +1054,50 @@ matrix_mautrix_instagram_enabled: false matrix_mautrix_instagram_container_image_self_build: "{{ matrix_architecture not in ['amd64', 'arm64'] }}" -matrix_mautrix_instagram_systemd_required_services_list: | +matrix_mautrix_instagram_container_network: "{{ matrix_bridges_container_network }}" + +matrix_mautrix_instagram_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_instagram_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_instagram_container_labels_traefik_enabled else []) + ) | unique + }} + +matrix_mautrix_instagram_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}" +matrix_mautrix_instagram_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}" +matrix_mautrix_instagram_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}" +matrix_mautrix_instagram_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}" + +matrix_mautrix_instagram_container_labels_metrics_middleware_basic_auth_enabled: "{{ matrix_metrics_exposure_http_basic_auth_enabled }}" +matrix_mautrix_instagram_container_labels_metrics_middleware_basic_auth_users: "{{ matrix_metrics_exposure_http_basic_auth_users }}" + +matrix_mautrix_instagram_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_instagram_database_hostname == devture_postgres_connection_hostname else []) }} matrix_mautrix_instagram_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'ig.as.token', rounds=655555) | to_uuid }}" +matrix_mautrix_instagram_homeserver_address: "{{ matrix_bridges_homeserver_client_api_url }}" + matrix_mautrix_instagram_homeserver_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'ig.hs.token', rounds=655555) | to_uuid }}" matrix_mautrix_instagram_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_instagram_bridge_presence: "{{ matrix_synapse_presence_enabled if matrix_synapse_enabled else true }}" -# People using an external Prometheus server will need to toggle all of these to be able to consume metrics remotely: -# - `matrix_mautrix_instagram_metrics_enabled` -# - `matrix_mautrix_instagram_proxying_metrics_enabled` -# - `matrix_nginx_proxy_proxy_matrix_metrics_enabled` matrix_mautrix_instagram_metrics_enabled: "{{ prometheus_enabled }}" +matrix_mautrix_instagram_metrics_proxying_enabled: "{{ matrix_mautrix_instagram_metrics_enabled and matrix_metrics_exposure_enabled }}" +matrix_mautrix_instagram_metrics_proxying_hostname: "{{ matrix_metrics_exposure_hostname }}" +matrix_mautrix_instagram_metrics_proxying_path_prefix: "{{ matrix_metrics_exposure_path_prefix }}/mautrix-instagram" + # We'd like to force-set people with external Postgres to SQLite, so the bridge role can complain # and point them to a migration path. matrix_mautrix_instagram_database_engine: "{{ 'postgres' if devture_postgres_enabled else 'sqlite' }}" diff --git a/roles/custom/matrix-bridge-mautrix-instagram/defaults/main.yml b/roles/custom/matrix-bridge-mautrix-instagram/defaults/main.yml index 36d7a7026..d8fcaab41 100644 --- a/roles/custom/matrix-bridge-mautrix-instagram/defaults/main.yml +++ b/roles/custom/matrix-bridge-mautrix-instagram/defaults/main.yml @@ -32,11 +32,50 @@ matrix_mautrix_instagram_bridge_permissions: | | combine({matrix_admin: 'admin'} if matrix_admin else {}) }} +matrix_mautrix_instagram_container_network: "" + +matrix_mautrix_instagram_container_additional_networks: "{{ matrix_mautrix_instagram_container_additional_networks_auto + matrix_mautrix_instagram_container_additional_networks_custom }}" +matrix_mautrix_instagram_container_additional_networks_auto: [] +matrix_mautrix_instagram_container_additional_networks_custom: [] + + +# matrix_mautrix_instagram_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_instagram_container_labels_additional_labels`. +matrix_mautrix_instagram_container_labels_traefik_enabled: true +matrix_mautrix_instagram_container_labels_traefik_docker_network: "{{ matrix_mautrix_instagram_container_network }}" +matrix_mautrix_instagram_container_labels_traefik_entrypoints: web-secure +matrix_mautrix_instagram_container_labels_traefik_tls_certResolver: default + +# Controls whether labels will be added that expose mautrix-instagram's metrics +matrix_mautrix_instagram_container_labels_metrics_enabled: "{{ matrix_mautrix_instagram_metrics_enabled and matrix_mautrix_instagram_metrics_proxying_enabled }}" +matrix_mautrix_instagram_container_labels_metrics_traefik_rule: "Host(`{{ matrix_mautrix_instagram_metrics_proxying_hostname }}`) && PathPrefix(`{{ matrix_mautrix_instagram_metrics_proxying_path_prefix }}`)" +matrix_mautrix_instagram_container_labels_metrics_traefik_priority: 0 +matrix_mautrix_instagram_container_labels_metrics_traefik_entrypoints: "{{ matrix_mautrix_instagram_container_labels_traefik_entrypoints }}" +matrix_mautrix_instagram_container_labels_metrics_traefik_tls: "{{ matrix_mautrix_instagram_container_labels_metrics_traefik_entrypoints != 'web' }}" +matrix_mautrix_instagram_container_labels_metrics_traefik_tls_certResolver: "{{ matrix_mautrix_instagram_container_labels_traefik_tls_certResolver }}" # noqa var-naming +matrix_mautrix_instagram_container_labels_metrics_middleware_basic_auth_enabled: false +# See: https://doc.traefik.io/traefik/middlewares/http/basicauth/#users +matrix_mautrix_instagram_container_labels_metrics_middleware_basic_auth_users: '' + +# matrix_mautrix_instagram_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_instagram_container_labels_additional_labels: | +# my.label=1 +# another.label="here" +matrix_mautrix_instagram_container_labels_additional_labels: '' + # A list of extra arguments to pass to the container matrix_mautrix_instagram_container_extra_arguments: [] # List of systemd services that matrix-mautrix-instagram.service depends on. -matrix_mautrix_instagram_systemd_required_services_list: ['docker.service'] +matrix_mautrix_instagram_systemd_required_services_list: "{{ matrix_mautrix_instagram_systemd_required_services_list_default + matrix_mautrix_instagram_systemd_required_services_list_auto + matrix_mautrix_instagram_systemd_required_services_list_custom }}" +matrix_mautrix_instagram_systemd_required_services_list_default: ['docker.service'] +matrix_mautrix_instagram_systemd_required_services_list_auto: [] +matrix_mautrix_instagram_systemd_required_services_list_custom: [] # List of systemd services that matrix-mautrix-instagram.service wants matrix_mautrix_instagram_systemd_wanted_services_list: [] @@ -53,10 +92,10 @@ matrix_mautrix_instagram_federate_rooms: true # If metrics need to be consumed by another (external) Prometheus server, consider exposing them via `matrix_mautrix_instagram_metrics_proxying_enabled`. matrix_mautrix_instagram_metrics_enabled: false -# Controls whether metrics should be proxied (exposed) on `matrix.DOMAIN/metrics/mautrix-instagram`. -# 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 exposed on a public URL. matrix_mautrix_instagram_metrics_proxying_enabled: false +matrix_mautrix_instagram_metrics_proxying_hostname: '' +matrix_mautrix_instagram_metrics_proxying_path_prefix: '' # Database-related configuration fields. # diff --git a/roles/custom/matrix-bridge-mautrix-instagram/tasks/inject_into_nginx_proxy.yml b/roles/custom/matrix-bridge-mautrix-instagram/tasks/inject_into_nginx_proxy.yml deleted file mode 100644 index 99a36c141..000000000 --- a/roles/custom/matrix-bridge-mautrix-instagram/tasks/inject_into_nginx_proxy.yml +++ /dev/null @@ -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-instagram role. - when: matrix_nginx_proxy_role_executed | default(False) | bool - -- when: matrix_mautrix_instagram_metrics_proxying_enabled | bool - block: - - name: Generate mautrix-instagram metrics proxying configuration for matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-instagram) - ansible.builtin.set_fact: - matrix_mautrix_instagram_nginx_metrics_configuration_block: | - location /metrics/mautrix-instagram { - {% 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-instagram: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 %} - } - - - name: Register mautrix-instagram metrics proxying configuration with matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-instagram) - 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_instagram_nginx_metrics_configuration_block] - }} diff --git a/roles/custom/matrix-bridge-mautrix-instagram/tasks/main.yml b/roles/custom/matrix-bridge-mautrix-instagram/tasks/main.yml index 834dec0cc..a9c63922c 100644 --- a/roles/custom/matrix-bridge-mautrix-instagram/tasks/main.yml +++ b/roles/custom/matrix-bridge-mautrix-instagram/tasks/main.yml @@ -1,14 +1,5 @@ --- -- tags: - - setup-all - - setup-nginx-proxy - - install-all - - install-nginx-proxy - block: - - when: matrix_mautrix_instagram_enabled | bool and matrix_mautrix_instagram_metrics_enabled | bool - ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml" - - tags: - setup-all - setup-mautrix-instagram diff --git a/roles/custom/matrix-bridge-mautrix-instagram/tasks/setup_install.yml b/roles/custom/matrix-bridge-mautrix-instagram/tasks/setup_install.yml index 68891c9eb..e0d4da4a1 100644 --- a/roles/custom/matrix-bridge-mautrix-instagram/tasks/setup_install.yml +++ b/roles/custom/matrix-bridge-mautrix-instagram/tasks/setup_install.yml @@ -65,6 +65,21 @@ owner: "{{ matrix_user_username }}" group: "{{ matrix_user_groupname }}" +- name: Ensure mautrix-instagram support files installed + ansible.builtin.template: + src: "{{ role_path }}/templates/{{ item }}.j2" + dest: "{{ matrix_mautrix_instagram_base_path }}/{{ item }}" + mode: 0640 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - labels + +- name: Ensure matrix-mautrix-instagram container network is created + community.general.docker_network: + name: "{{ matrix_mautrix_instagram_container_network }}" + driver: bridge + - name: Ensure matrix-mautrix-instagram.service installed ansible.builtin.template: src: "{{ role_path }}/templates/systemd/matrix-mautrix-instagram.service.j2" diff --git a/roles/custom/matrix-bridge-mautrix-instagram/templates/labels.j2 b/roles/custom/matrix-bridge-mautrix-instagram/templates/labels.j2 new file mode 100644 index 000000000..c45f6ac9b --- /dev/null +++ b/roles/custom/matrix-bridge-mautrix-instagram/templates/labels.j2 @@ -0,0 +1,30 @@ +{% if matrix_mautrix_instagram_container_labels_traefik_enabled %} +traefik.enable=true + +{% if matrix_mautrix_instagram_container_labels_traefik_docker_network %} +traefik.docker.network={{ matrix_mautrix_instagram_container_labels_traefik_docker_network }} +{% endif %} + +{# Metrics #} +{% if matrix_mautrix_instagram_container_labels_metrics_enabled %} +traefik.http.routers.matrix-mautrix-instagram-metrics.rule={{ matrix_mautrix_instagram_container_labels_metrics_traefik_rule }} +{% if matrix_mautrix_instagram_container_labels_metrics_traefik_priority | int > 0 %} +traefik.http.routers.matrix-mautrix-instagram-metrics.priority={{ matrix_mautrix_instagram_container_labels_metrics_traefik_priority }} +{% endif %} +traefik.http.routers.matrix-mautrix-instagram-metrics.service=matrix-mautrix-instagram-metrics +traefik.http.routers.matrix-mautrix-instagram-metrics.entrypoints={{ matrix_mautrix_instagram_container_labels_metrics_traefik_entrypoints }} +traefik.http.routers.matrix-mautrix-instagram-metrics.tls={{ matrix_mautrix_instagram_container_labels_metrics_traefik_tls | to_json }} +{% if matrix_mautrix_instagram_container_labels_metrics_traefik_tls %} +traefik.http.routers.matrix-mautrix-instagram-metrics.tls.certResolver={{ matrix_mautrix_instagram_container_labels_metrics_traefik_tls_certResolver }} +{% endif %} +{% if matrix_mautrix_instagram_container_labels_metrics_middleware_basic_auth_enabled %} +traefik.http.routers.matrix-mautrix-instagram-metrics.middlewares=matrix-mautrix-instagram-metrics-basic-auth +traefik.http.middlewares.matrix-mautrix-instagram-metrics-basic-auth.basicauth.users={{ matrix_mautrix_instagram_container_labels_metrics_middleware_basic_auth_users }} +{% endif %} + +traefik.http.services.matrix-mautrix-instagram-metrics.loadbalancer.server.port=8000 +{% endif %} + +{% endif %} + +{{ matrix_mautrix_instagram_container_labels_additional_labels }} diff --git a/roles/custom/matrix-bridge-mautrix-instagram/templates/systemd/matrix-mautrix-instagram.service.j2 b/roles/custom/matrix-bridge-mautrix-instagram/templates/systemd/matrix-mautrix-instagram.service.j2 index c9b64229e..f980ca237 100644 --- a/roles/custom/matrix-bridge-mautrix-instagram/templates/systemd/matrix-mautrix-instagram.service.j2 +++ b/roles/custom/matrix-bridge-mautrix-instagram/templates/systemd/matrix-mautrix-instagram.service.j2 @@ -19,19 +19,27 @@ 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-instagram \ +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \ + --rm \ + --name=matrix-mautrix-instagram \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ - --network={{ matrix_docker_network }} \ - -v {{ matrix_mautrix_instagram_config_path }}:/config:z \ - -v {{ matrix_mautrix_instagram_data_path }}:/data:z \ + --network={{ matrix_mautrix_instagram_container_network }} \ + --mount type=bind,src={{ matrix_mautrix_instagram_config_path }},dst=/config \ + --mount type=bind,src={{ matrix_mautrix_instagram_data_path }},dst=/data \ {% for arg in matrix_mautrix_instagram_container_extra_arguments %} {{ arg }} \ {% endfor %} {{ matrix_mautrix_instagram_docker_image }} \ python3 -m mautrix_instagram -c /config/config.yaml --no-update +{% for network in matrix_mautrix_instagram_container_additional_networks %} +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-mautrix-instagram +{% endfor %} + +ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-mautrix-instagram + 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-instagram 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-mautrix-instagram 2>/dev/null || true' Restart=always