From 0f7b89523fb0bf633a1e36bb90aec12a7b1d43a8 Mon Sep 17 00:00:00 2001 From: Pierre 'McFly' Marty Date: Thu, 14 Dec 2023 18:20:02 +0100 Subject: [PATCH] feat: enroll signalgo to nginx proxy --- group_vars/matrix_servers | 59 +++++++++++++++++++ .../defaults/main.yml | 10 ++++ .../tasks/inject_into_nginx_proxy.yml | 35 +++++++++++ .../tasks/main.yml | 9 +++ .../tasks/setup_install.yml | 34 ++++++++++- .../tasks/validate_config.yml | 17 ++++-- .../templates/config.yaml.j2 | 26 ++++---- .../tasks/validate_config.yml | 10 ++-- 8 files changed, 176 insertions(+), 24 deletions(-) create mode 100644 roles/custom/matrix-bridge-mautrix-signalgo/tasks/inject_into_nginx_proxy.yml diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index f044f6d4d..f786a9865 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -97,6 +97,8 @@ matrix_homeserver_container_extra_arguments_auto: | + (['--mount type=bind,src=' + matrix_mautrix_signal_config_path + '/registration.yaml,dst=/matrix-mautrix-signal-registration.yaml,ro'] if matrix_mautrix_signal_enabled else []) + + (['--mount type=bind,src=' + matrix_mautrix_signalgo_config_path + '/registration.yaml,dst=/matrix-mautrix-signalgo-registration.yaml,ro'] if matrix_mautrix_signalgo_enabled else []) + + (['--mount type=bind,src=' + matrix_mautrix_telegram_config_path + '/registration.yaml,dst=/matrix-mautrix-telegram-registration.yaml,ro'] if matrix_mautrix_telegram_enabled else []) + (['--mount type=bind,src=' + matrix_mautrix_twitter_config_path + '/registration.yaml,dst=/matrix-mautrix-twitter-registration.yaml,ro'] if matrix_mautrix_twitter_enabled else []) @@ -160,6 +162,8 @@ matrix_homeserver_app_service_config_files_auto: | + (['/matrix-mautrix-signal-registration.yaml'] if matrix_mautrix_signal_enabled else []) + + (['/matrix-mautrix-signalgo-registration.yaml'] if matrix_mautrix_signalgo_enabled else []) + + (['/matrix-mautrix-telegram-registration.yaml'] if matrix_mautrix_telegram_enabled else []) + (['/matrix-mautrix-twitter-registration.yaml'] if matrix_mautrix_twitter_enabled else []) @@ -276,8 +280,12 @@ devture_systemd_service_manager_services_list_auto: | + ([{'name': 'matrix-mautrix-signal.service', 'priority': 2000, 'groups': ['matrix', 'bridges', 'mautrix-signal']}] if matrix_mautrix_signal_enabled else []) + + ([{'name': 'matrix-mautrix-signalgo.service', 'priority': 2000, 'groups': ['matrix', 'bridges', 'mautrix-signalgo']}] if matrix_mautrix_signalgo_enabled else []) + + ([{'name': 'matrix-mautrix-signal-daemon.service', 'priority': 1900, 'groups': ['matrix', 'bridges', 'mautrix-signal', 'mautrix-signal-daemon']}] if matrix_mautrix_signal_enabled else []) + + ([{'name': 'matrix-mautrix-signalgo-daemon.service', 'priority': 1900, 'groups': ['matrix', 'bridges', 'mautrix-signalgo', 'mautrix-signalgo-daemon']}] if matrix_mautrix_signalgo_enabled else []) + + ([{'name': 'matrix-mautrix-telegram.service', 'priority': 2000, 'groups': ['matrix', 'bridges', 'mautrix-telegram']}] if matrix_mautrix_telegram_enabled else []) + ([{'name': 'matrix-mautrix-twitter.service', 'priority': 2000, 'groups': ['matrix', 'bridges', 'mautrix-twitter']}] if matrix_mautrix_twitter_enabled else []) @@ -1090,6 +1098,51 @@ matrix_mautrix_signal_daemon_container_image_self_build: "{{ matrix_architecture # ###################################################################### +###################################################################### +# +# matrix-bridge-mautrix-signalgo +# +###################################################################### + +# We don't enable bridges by default. +matrix_mautrix_signalgo_enabled: false + +matrix_mautrix_signalgo_container_image_self_build: "{{ matrix_architecture not in ['arm64', 'amd64'] }}" + +matrix_mautrix_signalgo_systemd_required_services_list: | + {{ + ['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 []) + }} + +matrix_mautrix_signalgo_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'sigo.as.token', rounds=655555) | to_uuid }}" + +matrix_mautrix_signalgo_homeserver_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'sigo.hs.token', rounds=655555) | to_uuid }}" + +matrix_mautrix_signalgo_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_signalgo_metrics_enabled` +# - `matrix_mautrix_signalgo_proxying_metrics_enabled` +# - `matrix_nginx_proxy_proxy_matrix_metrics_enabled` +matrix_mautrix_signalgo_metrics_enabled: "{{ prometheus_enabled }}" + +# Postgres is the default, except if not using internal Postgres server +matrix_mautrix_signalgo_database_engine: "{{ 'postgres' if devture_postgres_enabled else 'sqlite' }}" +matrix_mautrix_signalgo_database_hostname: "{{ devture_postgres_connection_hostname if devture_postgres_enabled else '' }}" +matrix_mautrix_signalgo_database_password: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'mausignalgo.db', rounds=655555) | to_uuid }}" + +###################################################################### +# +# /matrix-bridge-mautrix-signalgo +# +###################################################################### + ###################################################################### # # matrix-bridge-mautrix-telegram @@ -3129,6 +3182,12 @@ devture_postgres_managed_databases_auto: | 'password': matrix_mautrix_signal_database_password, }] if (matrix_mautrix_signal_enabled and matrix_mautrix_signal_database_engine == 'postgres' and matrix_mautrix_signal_database_hostname == devture_postgres_connection_hostname) else []) + + ([{ + 'name': matrix_mautrix_signalgo_database_name, + 'username': matrix_mautrix_signalgo_database_username, + 'password': matrix_mautrix_signalgo_database_password, + }] if (matrix_mautrix_signalgo_enabled and matrix_mautrix_signalgo_database_engine == 'postgres' and matrix_mautrix_signalgo_database_hostname == devture_postgres_connection_hostname) else []) + + ([{ 'name': matrix_mautrix_wsproxy_syncproxy_database_name, 'username': matrix_mautrix_wsproxy_syncproxy_database_username, diff --git a/roles/custom/matrix-bridge-mautrix-signalgo/defaults/main.yml b/roles/custom/matrix-bridge-mautrix-signalgo/defaults/main.yml index 45ef0789a..a08917bcc 100644 --- a/roles/custom/matrix-bridge-mautrix-signalgo/defaults/main.yml +++ b/roles/custom/matrix-bridge-mautrix-signalgo/defaults/main.yml @@ -55,6 +55,16 @@ matrix_mautrix_signalgo_logging_level: 'warn' # If false, created portal rooms will never be federated. matrix_mautrix_signalgo_federate_rooms: true +# 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_signal_metrics_proxying_enabled`. +matrix_mautrix_signalgo_metrics_enabled: false + +# Controls whether metrics should be proxied (exposed) on `matrix.DOMAIN/metrics/mautrix-signal`. +# 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`. +matrix_mautrix_signalgo_metrics_proxying_enabled: false + # Database-related configuration fields. # # To use SQLite, stick to these defaults. diff --git a/roles/custom/matrix-bridge-mautrix-signalgo/tasks/inject_into_nginx_proxy.yml b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/inject_into_nginx_proxy.yml new file mode 100644 index 000000000..5806e624b --- /dev/null +++ b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/inject_into_nginx_proxy.yml @@ -0,0 +1,35 @@ +--- + +- name: Fail if matrix-nginx-proxy role already executed + ansible.builtin.fail: + msg: >- + Trying to append mautrix-signalgo-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-signalgo role. + when: matrix_nginx_proxy_role_executed | default(False) | bool + +- when: matrix_mautrix_signalgo_metrics_proxying_enabled | bool + block: + - name: Generate mautrix-signalgo metrics proxying configuration for matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-signalgo) + ansible.builtin.set_fact: + matrix_mautrix_signalgo_nginx_metrics_configuration_block: | + location /metrics/mautrix-signalgo { + {% 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-signalgo: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-signalgo metrics proxying configuration with matrix-nginx-proxy (matrix.DOMAIN/metrics/mautrix-signalgo) + 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_signalgo_nginx_metrics_configuration_block] + }} diff --git a/roles/custom/matrix-bridge-mautrix-signalgo/tasks/main.yml b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/main.yml index dc4450047..2507467b6 100644 --- a/roles/custom/matrix-bridge-mautrix-signalgo/tasks/main.yml +++ b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/main.yml @@ -1,5 +1,14 @@ --- +- tags: + - setup-all + - setup-nginx-proxy + - install-all + - install-nginx-proxy + block: + - when: matrix_mautrix_signalgo_enabled | bool and matrix_mautrix_signalgo_metrics_enabled | bool + ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml" + - tags: - setup-all - setup-mautrix-signalgo diff --git a/roles/custom/matrix-bridge-mautrix-signalgo/tasks/setup_install.yml b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/setup_install.yml index 61db5a575..99c219981 100644 --- a/roles/custom/matrix-bridge-mautrix-signalgo/tasks/setup_install.yml +++ b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/setup_install.yml @@ -77,6 +77,39 @@ pull: true when: "matrix_mautrix_signalgo_container_image_self_build | bool" +- name: Check if an old database file exists + ansible.builtin.stat: + path: "{{ matrix_mautrix_signalgo_base_path }}/mautrix-signalgo.db" + register: matrix_mautrix_signalgo_stat_database + +- name: Check if an old matrix state file exists + ansible.builtin.stat: + path: "{{ matrix_mautrix_signalgo_base_path }}/mx-state.json" + register: matrix_mautrix_signalgo_stat_mx_state + +- name: (Data relocation) Ensure matrix-mautrix-signalgo.service is stopped + ansible.builtin.service: + name: matrix-mautrix-signalgo + state: stopped + enabled: false + daemon_reload: true + failed_when: false + when: "matrix_mautrix_signalgo_stat_database.stat.exists" + +- name: (Data relocation) Move mautrix-signalgo database file to ./data directory + ansible.builtin.command: + cmd: "mv {{ matrix_mautrix_signalgo_base_path }}/mautrix-signalgo.db {{ matrix_mautrix_signalgo_data_path }}/mautrix-signalgo.db" + creates: "{{ matrix_mautrix_signalgo_data_path }}/mautrix-signalgo.db" + removes: "{{ matrix_mautrix_signalgo_base_path }}/mautrix-signalgo.db" + when: "matrix_mautrix_signalgo_stat_database.stat.exists" + +- name: (Data relocation) Move mautrix-signalgo mx-state file to ./data directory + ansible.builtin.command: + cmd: "mv {{ matrix_mautrix_signalgo_base_path }}/mx-state.json {{ matrix_mautrix_signalgo_data_path }}/mx-state.json" + creates: "{{ matrix_mautrix_signalgo_data_path }}/mx-state.json" + removes: "{{ matrix_mautrix_signalgo_base_path }}/mx-state.json" + when: "matrix_mautrix_signalgo_stat_mx_state.stat.exists" + - name: Ensure mautrix-signalgo config.yaml installed ansible.builtin.copy: content: "{{ matrix_mautrix_signalgo_configuration | to_nice_yaml(indent=2, width=999999) }}" @@ -98,7 +131,6 @@ src: "{{ role_path }}/templates/systemd/matrix-mautrix-signalgo.service.j2" dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-mautrix-signalgo.service" mode: 0644 - register: matrix_mautrix_signalgo_systemd_service_result - name: Ensure matrix-mautrix-signalgo.service restarted, if necessary ansible.builtin.service: diff --git a/roles/custom/matrix-bridge-mautrix-signalgo/tasks/validate_config.yml b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/validate_config.yml index 1fc0d0cec..0a6a38cd7 100644 --- a/roles/custom/matrix-bridge-mautrix-signalgo/tasks/validate_config.yml +++ b/roles/custom/matrix-bridge-mautrix-signalgo/tasks/validate_config.yml @@ -1,11 +1,20 @@ --- -- name: Fail if required mautrix-signalgo settings not defined +- name: Fail if required mautrix-whatsapp settings not defined ansible.builtin.fail: msg: >- You need to define a required configuration setting (`{{ item.name }}`). when: "item.when | bool and vars[item.name] == ''" with_items: - - {'name': 'matrix_mautrix_signalgo_appservice_token', when: true} - - {'name': 'matrix_mautrix_signalgo_homeserver_token', when: true} - - {'name': 'matrix_mautrix_signalgo_database_hostname', when: "{{ matrix_mautrix_signalgo_database_engine == 'postgres' }}"} + - {'name': 'matrix_mautrix_whatsapp_appservice_token', when: true} + - {'name': 'matrix_mautrix_whatsapp_homeserver_token', when: true} + - {'name': 'matrix_mautrix_whatsapp_database_hostname', when: "{{ matrix_mautrix_whatsapp_database_engine == 'postgres' }}"} + +- name: (Deprecation) Catch and report renamed settings + ansible.builtin.fail: + msg: >- + Your configuration contains a variable, which now has a different name. + Please change your configuration to rename the variable (`{{ item.old }}` -> `{{ item.new }}`). + when: "item.old in vars" + with_items: + - {'old': 'matrix_mautrix_whatsapp_log_level', 'new': 'matrix_mautrix_whatsapp_logging_level'} diff --git a/roles/custom/matrix-bridge-mautrix-signalgo/templates/config.yaml.j2 b/roles/custom/matrix-bridge-mautrix-signalgo/templates/config.yaml.j2 index 0d708d743..7c0a6a52d 100644 --- a/roles/custom/matrix-bridge-mautrix-signalgo/templates/config.yaml.j2 +++ b/roles/custom/matrix-bridge-mautrix-signalgo/templates/config.yaml.j2 @@ -1,10 +1,10 @@ #jinja2: lstrip_blocks: "True" -# Homeserver details. +# Homeserver details homeserver: # The address that this appservice can use to connect to the homeserver. - address: {{ matrix_mautrix_signalgo_homeserver_address | to_json }} + address: {{ matrix_mautrix_signalgo_homeserver_address }} # The domain of the homeserver (for MXIDs, etc). - domain: {{ matrix_mautrix_signalgo_homeserver_domain | to_json }} + domain: {{ matrix_mautrix_signalgo_homeserver_domain }} # What software is the homeserver running? # Standard Matrix homeservers like Synapse, Dendrite and Conduit should just use "standard" here. @@ -91,7 +91,7 @@ bridge: username_template: signal_{{.}} # Displayname template for Signal users. This is also used as the room name in DMs if private_chat_portal_meta is enabled. # Available variables: TODO - displayname_template: '{{.ProfileName}} (Signal)' + displayname_template: '{{.ProfileName}} (Signalgo)' # Whether to explicitly set the avatar and room name for private chat portal rooms. # If set to `default`, this will be enabled in encrypted rooms and disabled in unencrypted rooms. # If set to `always`, all DM rooms will have explicit names and avatars set. @@ -252,13 +252,11 @@ bridge: # Logging config. See https://github.com/tulir/zeroconfig for details. logging: - min_level: {{ matrix_mautrix_signalgo_logging_level | to_json }} - writers: - - type: stdout - format: pretty-colored - - type: file - format: json - filename: ./logs/mautrix-signalgo.log - max_size: 100 - max_backups: 10 - compress: true + directory: ./logs + file_name_format: '' + file_date_format: "2006-01-02" + file_mode: 384 + timestamp_format: Jan _2, 2006 15:04:05 + print_level: {{ matrix_mautrix_signalgo_logging_level | to_json }} + print_json: false + file_json: false diff --git a/roles/custom/matrix-bridge-mautrix-whatsapp/tasks/validate_config.yml b/roles/custom/matrix-bridge-mautrix-whatsapp/tasks/validate_config.yml index 0a6a38cd7..eb785b4a7 100644 --- a/roles/custom/matrix-bridge-mautrix-whatsapp/tasks/validate_config.yml +++ b/roles/custom/matrix-bridge-mautrix-whatsapp/tasks/validate_config.yml @@ -1,14 +1,14 @@ --- -- name: Fail if required mautrix-whatsapp settings not defined +- name: Fail if required mautrix-signalgo settings not defined ansible.builtin.fail: msg: >- You need to define a required configuration setting (`{{ item.name }}`). when: "item.when | bool and vars[item.name] == ''" with_items: - - {'name': 'matrix_mautrix_whatsapp_appservice_token', when: true} - - {'name': 'matrix_mautrix_whatsapp_homeserver_token', when: true} - - {'name': 'matrix_mautrix_whatsapp_database_hostname', when: "{{ matrix_mautrix_whatsapp_database_engine == 'postgres' }}"} + - {'name': 'matrix_mautrix_signalgo_appservice_token', when: true} + - {'name': 'matrix_mautrix_signalgo_homeserver_token', when: true} + - {'name': 'matrix_mautrix_signalgo_database_hostname', when: "{{ matrix_mautrix_signalgo_database_engine == 'postgres' }}"} - name: (Deprecation) Catch and report renamed settings ansible.builtin.fail: @@ -17,4 +17,4 @@ Please change your configuration to rename the variable (`{{ item.old }}` -> `{{ item.new }}`). when: "item.old in vars" with_items: - - {'old': 'matrix_mautrix_whatsapp_log_level', 'new': 'matrix_mautrix_whatsapp_logging_level'} + - {'old': 'matrix_mautrix_signalgo_log_level', 'new': 'matrix_mautrix_signalgo_logging_level'}