diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index e7ef6da9a..0dac50599 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -1297,19 +1297,36 @@ matrix_mautrix_twitter_database_password: "{{ '%s' | format(matrix_homeserver_ge # We don't enable bridges by default. matrix_mautrix_gmessages_enabled: false +matrix_mautrix_gmessages_systemd_required_services_list_auto: | + {{ + matrix_addons_homeserver_systemd_services_list + + + ([devture_postgres_identifier ~ '.service'] if (devture_postgres_enabled and matrix_mautrix_facebook_database_hostname == devture_postgres_connection_hostname) else []) + }} + matrix_mautrix_gmessages_container_image_self_build: "{{ matrix_architecture not in ['arm64', 'amd64'] }}" -matrix_mautrix_gmessages_systemd_required_services_list: | +matrix_mautrix_gmessages_container_network: "{{ matrix_addons_container_network }}" + +matrix_mautrix_gmessages_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_gmessages_database_hostname == devture_postgres_connection_hostname and matrix_mautrix_gmessages_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_gmessages_container_labels_traefik_enabled) else []) + ) | unique }} +matrix_mautrix_gmessages_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}" +matrix_mautrix_gmessages_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}" +matrix_mautrix_gmessages_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}" +matrix_mautrix_gmessages_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}" + +matrix_mautrix_gmessages_container_labels_metrics_middleware_basic_auth_enabled: "{{ matrix_metrics_exposure_http_basic_auth_enabled }}" +matrix_mautrix_gmessages_container_labels_metrics_middleware_basic_auth_users: "{{ matrix_metrics_exposure_http_basic_auth_users }}" + matrix_mautrix_gmessages_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'gmessa.as.token', rounds=655555) | to_uuid }}" matrix_mautrix_gmessages_homeserver_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'gmessa.hs.token', rounds=655555) | to_uuid }}" @@ -1318,7 +1335,9 @@ matrix_mautrix_gmessages_login_shared_secret: "{{ matrix_synapse_ext_password_pr matrix_mautrix_gmessages_metrics_enabled: "{{ prometheus_enabled }}" -# TODO - add support for exposing metrics +matrix_mautrix_gmessages_metrics_proxying_enabled: "{{ matrix_mautrix_gmessages_metrics_enabled and matrix_metrics_exposure_enabled }}" +matrix_mautrix_gmessages_metrics_proxying_hostname: "{{ matrix_metrics_exposure_hostname }}" +matrix_mautrix_gmessages_metrics_proxying_path_prefix: "{{ matrix_metrics_exposure_path_prefix }}/mautrix-gmessages" # Postgres is the default, except if not using internal Postgres server matrix_mautrix_gmessages_database_engine: "{{ 'postgres' if devture_postgres_enabled else 'sqlite' }}" diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/defaults/main.yml b/roles/custom/matrix-bridge-mautrix-gmessages/defaults/main.yml index 4a279e31c..59086e958 100644 --- a/roles/custom/matrix-bridge-mautrix-gmessages/defaults/main.yml +++ b/roles/custom/matrix-bridge-mautrix-gmessages/defaults/main.yml @@ -27,11 +27,49 @@ matrix_mautrix_gmessages_appservice_address: "http://matrix-mautrix-gmessages:80 matrix_mautrix_gmessages_command_prefix: "!gm" +matrix_mautrix_gmessages_container_network: "" + +matrix_mautrix_gmessages_container_additional_networks: "{{ matrix_mautrix_gmessages_container_additional_networks_auto + matrix_mautrix_gmessages_container_additional_networks_custom }}" +matrix_mautrix_gmessages_container_additional_networks_auto: [] +matrix_mautrix_gmessages_container_additional_networks_custom: [] + +# matrix_mautrix_gmessages_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_gmessages_container_labels_additional_labels`. +matrix_mautrix_gmessages_container_labels_traefik_enabled: true +matrix_mautrix_gmessages_container_labels_traefik_docker_network: "{{ matrix_mautrix_gmessages_container_network }}" +matrix_mautrix_gmessages_container_labels_traefik_entrypoints: web-secure +matrix_mautrix_gmessages_container_labels_traefik_tls_certResolver: default # noqa var-naming + +# Controls whether labels will be added that expose mautrix-gmessages' metrics +matrix_mautrix_gmessages_container_labels_metrics_enabled: "{{ matrix_mautrix_gmessages_metrics_enabled and matrix_mautrix_gmessages_metrics_proxying_enabled }}" +matrix_mautrix_gmessages_container_labels_metrics_traefik_rule: "Host(`{{ matrix_mautrix_gmessages_metrics_proxying_hostname }}`) && PathPrefix(`{{ matrix_mautrix_gmessages_metrics_proxying_path_prefix }}`)" +matrix_mautrix_gmessages_container_labels_metrics_traefik_priority: 0 +matrix_mautrix_gmessages_container_labels_metrics_traefik_entrypoints: "{{ matrix_mautrix_gmessages_container_labels_traefik_entrypoints }}" +matrix_mautrix_gmessages_container_labels_metrics_traefik_tls: "{{ matrix_mautrix_gmessages_container_labels_metrics_traefik_entrypoints != 'web' }}" +matrix_mautrix_gmessages_container_labels_metrics_traefik_tls_certResolver: "{{ matrix_mautrix_gmessages_container_labels_traefik_tls_certResolver }}" # noqa var-naming +matrix_mautrix_gmessages_container_labels_metrics_middleware_basic_auth_enabled: false +# See: https://doc.traefik.io/traefik/middlewares/http/basicauth/#users +matrix_mautrix_gmessages_container_labels_metrics_middleware_basic_auth_users: '' + +# matrix_mautrix_gmessages_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_gmessages_container_labels_additional_labels: | +# my.label=1 +# another.label="here" +matrix_mautrix_gmessages_container_labels_additional_labels: '' + # A list of extra arguments to pass to the container matrix_mautrix_gmessages_container_extra_arguments: [] # List of systemd services that matrix-mautrix-gmessages.service depends on. -matrix_mautrix_gmessages_systemd_required_services_list: ['docker.service'] +matrix_mautrix_gmessages_systemd_required_services_list: "{{ matrix_mautrix_gmessages_systemd_required_services_list_default + matrix_mautrix_gmessages_systemd_required_services_list_auto + matrix_mautrix_gmessages_systemd_required_services_list_custom }}" +matrix_mautrix_gmessages_systemd_required_services_list_default: ['docker.service'] +matrix_mautrix_gmessages_systemd_required_services_list_auto: [] +matrix_mautrix_gmessages_systemd_required_services_list_custom: [] # List of systemd services that matrix-mautrix-gmessages.service wants matrix_mautrix_gmessages_systemd_wanted_services_list: [] @@ -56,7 +94,8 @@ matrix_mautrix_gmessages_metrics_enabled: false # Controls whether metrics should be proxied (exposed) on a public URL. matrix_mautrix_gmessages_metrics_proxying_enabled: false -# TODO - add more variables for controlling the hostname and prefix, etc. +matrix_mautrix_gmessages_metrics_proxying_hostname: '' +matrix_mautrix_gmessages_metrics_proxying_path_prefix: '' # Database-related configuration fields. # diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/tasks/inject_into_nginx_proxy.yml b/roles/custom/matrix-bridge-mautrix-gmessages/tasks/inject_into_nginx_proxy.yml deleted file mode 100644 index 7e4a528f2..000000000 --- a/roles/custom/matrix-bridge-mautrix-gmessages/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-gmessages-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-gmessages role. - when: matrix_nginx_proxy_role_executed | default(False) | bool - -- when: matrix_mautrix_gmessages_metrics_proxying_enabled | bool - block: - - name: Generate mautrix-gmessages metrics proxying configuration for matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-gmessages) - ansible.builtin.set_fact: - matrix_mautrix_gmessages_nginx_metrics_configuration_block: | - location /metrics/mautrix-gmessages { - {% 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-gmessages: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 %} - } - -# Injection code was here diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/tasks/main.yml b/roles/custom/matrix-bridge-mautrix-gmessages/tasks/main.yml index 45da31da8..4ca9e19fe 100644 --- a/roles/custom/matrix-bridge-mautrix-gmessages/tasks/main.yml +++ b/roles/custom/matrix-bridge-mautrix-gmessages/tasks/main.yml @@ -1,14 +1,5 @@ --- -- tags: - - setup-all - - setup-nginx-proxy - - install-all - - install-nginx-proxy - block: - - when: matrix_mautrix_gmessages_enabled | bool and matrix_mautrix_gmessages_metrics_enabled | bool - ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml" - - tags: - setup-all - setup-mautrix-gmessages diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/tasks/setup_install.yml b/roles/custom/matrix-bridge-mautrix-gmessages/tasks/setup_install.yml index 73038c4c1..88a26cfde 100644 --- a/roles/custom/matrix-bridge-mautrix-gmessages/tasks/setup_install.yml +++ b/roles/custom/matrix-bridge-mautrix-gmessages/tasks/setup_install.yml @@ -132,6 +132,21 @@ dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-mautrix-gmessages.service" mode: 0644 +- name: Ensure mautrix-gmessages support files installed + ansible.builtin.template: + src: "{{ role_path }}/templates/{{ item }}.j2" + dest: "{{ matrix_mautrix_gmessages_base_path }}/{{ item }}" + mode: 0640 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - labels + +- name: Ensure matrix-mautrix-gmessages container network is created + community.general.docker_network: + name: "{{ matrix_mautrix_gmessages_container_network }}" + driver: bridge + - name: Ensure matrix-mautrix-gmessages.service restarted, if necessary ansible.builtin.service: name: "matrix-mautrix-gmessages.service" diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/tasks/validate_config.yml b/roles/custom/matrix-bridge-mautrix-gmessages/tasks/validate_config.yml index acfffa75e..a763b89c9 100644 --- a/roles/custom/matrix-bridge-mautrix-gmessages/tasks/validate_config.yml +++ b/roles/custom/matrix-bridge-mautrix-gmessages/tasks/validate_config.yml @@ -9,6 +9,9 @@ - {'name': 'matrix_mautrix_gmessages_appservice_token', when: true} - {'name': 'matrix_mautrix_gmessages_homeserver_token', when: true} - {'name': 'matrix_mautrix_gmessages_database_hostname', when: "{{ matrix_mautrix_gmessages_database_engine == 'postgres' }}"} + - {'name': 'matrix_mautrix_gmessages_metrics_proxying_hostname', when: "{{ matrix_mautrix_gmessages_metrics_enabled }}"} + - {'name': 'matrix_mautrix_gmessages_metrics_proxying_path_prefix', when: "{{ matrix_mautrix_gmessages_metrics_enabled }}"} + - {'name': 'matrix_mautrix_gmessages_container_network', when: true} - name: (Deprecation) Catch and report renamed settings ansible.builtin.fail: diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/templates/config.yaml.j2 b/roles/custom/matrix-bridge-mautrix-gmessages/templates/config.yaml.j2 index 6dc136bb4..e49998dd9 100644 --- a/roles/custom/matrix-bridge-mautrix-gmessages/templates/config.yaml.j2 +++ b/roles/custom/matrix-bridge-mautrix-gmessages/templates/config.yaml.j2 @@ -87,7 +87,7 @@ metrics: # Enable prometheus metrics? enabled: {{ matrix_mautrix_gmessages_metrics_enabled | to_json }} # IP and port where the metrics listener should be. The path is always /metrics - listen: 127.0.0.1:8001 + listen: 0.0.0.0:8001 google_messages: # OS name to tell the phone. This is the name that shows up in the paired devices list. diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/templates/labels.j2 b/roles/custom/matrix-bridge-mautrix-gmessages/templates/labels.j2 new file mode 100644 index 000000000..a44eedc25 --- /dev/null +++ b/roles/custom/matrix-bridge-mautrix-gmessages/templates/labels.j2 @@ -0,0 +1,35 @@ +{% if matrix_mautrix_gmessages_container_labels_traefik_enabled %} +traefik.enable=true + +{% if matrix_mautrix_gmessages_container_labels_traefik_docker_network %} +traefik.docker.network={{ matrix_mautrix_gmessages_container_labels_traefik_docker_network }} +{% endif %} + +{# + Metrics +#} +{% if matrix_mautrix_gmessages_container_labels_metrics_enabled %} +traefik.http.routers.matrix-mautrix-gmessages-metrics.rule={{ matrix_mautrix_gmessages_container_labels_metrics_traefik_rule }} +{% if matrix_mautrix_gmessages_container_labels_metrics_traefik_priority | int > 0 %} +traefik.http.routers.matrix-mautrix-gmessages-metrics.priority={{ matrix_mautrix_gmessages_container_labels_metrics_traefik_priority }} +{% endif %} +traefik.http.routers.matrix-mautrix-gmessages-metrics.service=matrix-mautrix-gmessages-metrics +traefik.http.routers.matrix-mautrix-gmessages-metrics.entrypoints={{ matrix_mautrix_gmessages_container_labels_metrics_traefik_entrypoints }} +traefik.http.routers.matrix-mautrix-gmessages-metrics.tls={{ matrix_mautrix_gmessages_container_labels_metrics_traefik_tls | to_json }} +{% if matrix_mautrix_gmessages_container_labels_metrics_traefik_tls %} +traefik.http.routers.matrix-mautrix-gmessages-metrics.tls.certResolver={{ matrix_mautrix_gmessages_container_labels_metrics_traefik_tls_certResolver }} +{% endif %} +{% if matrix_mautrix_gmessages_container_labels_metrics_middleware_basic_auth_enabled %} +traefik.http.routers.matrix-mautrix-gmessages-metrics.middlewares=matrix-mautrix-gmessages-metrics-basic-auth +traefik.http.middlewares.matrix-mautrix-gmessages-metrics-basic-auth.basicauth.users={{ matrix_mautrix_gmessages_container_labels_metrics_middleware_basic_auth_users }} +{% endif %} + +traefik.http.services.matrix-mautrix-gmessages-metrics.loadbalancer.server.port=8001 +{% endif %} +{# + /Metrics +#} + +{% endif %} + +{{ matrix_mautrix_gmessages_container_labels_additional_labels }} diff --git a/roles/custom/matrix-bridge-mautrix-gmessages/templates/systemd/matrix-mautrix-gmessages.service.j2 b/roles/custom/matrix-bridge-mautrix-gmessages/templates/systemd/matrix-mautrix-gmessages.service.j2 index 885fa8ada..3c4fdd44f 100644 --- a/roles/custom/matrix-bridge-mautrix-gmessages/templates/systemd/matrix-mautrix-gmessages.service.j2 +++ b/roles/custom/matrix-bridge-mautrix-gmessages/templates/systemd/matrix-mautrix-gmessages.service.j2 @@ -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-gmessages \ +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \ + --rm \ + --name=matrix-mautrix-gmessages \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ - --network={{ matrix_docker_network }} \ - -v {{ matrix_mautrix_gmessages_config_path }}:/config:z \ - -v {{ matrix_mautrix_gmessages_data_path }}:/data:z \ + --network={{ matrix_mautrix_gmessages_container_network }} \ + --mount type=bind,src={{ matrix_mautrix_gmessages_config_path }},dst=/config \ + --mount type=bind,src={{ matrix_mautrix_gmessages_data_path }},dst=/data \ + --label-file={{ matrix_mautrix_gmessages_base_path }}/labels \ --workdir=/data \ {% for arg in matrix_mautrix_gmessages_container_extra_arguments %} {{ arg }} \ @@ -33,6 +36,12 @@ ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name {{ matrix_mautrix_gmessages_docker_image }} \ /usr/bin/mautrix-gmessages -c /config/config.yaml -r /config/registration.yaml +{% for network in matrix_mautrix_gmessages_container_additional_networks %} +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-mautrix-gmessages +{% endfor %} + +ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-mautrix-gmessages + 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-gmessages 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-mautrix-gmessages 2>/dev/null || true' Restart=always