diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 64d1b9dfc..3e15dd62d 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -592,13 +592,37 @@ matrix_appservice_webhooks_systemd_required_services_list: | # We don't enable bridges by default. matrix_appservice_slack_enabled: false +matrix_appservice_slack_hostname: "{{ matrix_server_fqn_matrix }}" + +matrix_appservice_slack_systemd_required_services_list_auto: | + {{ + matrix_addons_homeserver_systemd_services_list + + + ([devture_postgres_identifier ~ '.service'] if (devture_postgres_enabled and matrix_appservice_slack_database_hostname == devture_postgres_connection_hostname) else []) + }} + matrix_appservice_slack_container_image_self_build: "{{ matrix_architecture not in ['amd64', 'arm64'] }}" -# Normally, matrix-nginx-proxy is enabled and nginx can reach matrix-appservice-slack over the container network. -# If matrix-nginx-proxy is not enabled, or you otherwise have a need for it, you can expose -# matrix-appservice-slack's client-server port to the local host. matrix_appservice_slack_container_http_host_bind_port: "{{ (matrix_playbook_service_host_bind_interface_prefix ~ matrix_appservice_slack_slack_port) if matrix_playbook_service_host_bind_interface_prefix else '' }}" +matrix_appservice_slack_container_network: "{{ matrix_addons_container_network }}" + +matrix_appservice_slack_container_additional_networks_auto: |- + {{ + ( + ([] if matrix_addons_homeserver_container_network == '' else [matrix_addons_homeserver_container_network]) + + + ([devture_postgres_container_network] if (devture_postgres_enabled and matrix_appservice_slack_database_hostname == devture_postgres_connection_hostname and matrix_appservice_slack_container_network != devture_postgres_container_network) else []) + + + ([matrix_playbook_reverse_proxyable_services_additional_network] if (matrix_playbook_reverse_proxyable_services_additional_network and matrix_appservice_slack_container_labels_traefik_enabled) else []) + ) | unique + }} + +matrix_appservice_slack_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}" +matrix_appservice_slack_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}" +matrix_appservice_slack_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}" +matrix_appservice_slack_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}" + matrix_appservice_slack_appservice_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'slack.as.token', rounds=655555) | to_uuid }}" matrix_appservice_slack_homeserver_url: "{{ matrix_homeserver_container_url }}" @@ -606,15 +630,6 @@ matrix_appservice_slack_homeserver_token: "{{ '%s' | format(matrix_homeserver_ge matrix_appservice_slack_id_token: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'slack.id.token', rounds=655555) | to_uuid }}" -matrix_appservice_slack_systemd_required_services_list: | - {{ - ['docker.service'] - + - ['matrix-' + matrix_homeserver_implementation + '.service'] - + - (['matrix-nginx-proxy.service'] if matrix_nginx_proxy_enabled else []) - }} - # Postgres is the default, except if not using internal Postgres server matrix_appservice_slack_database_engine: "{{ 'postgres' if devture_postgres_enabled else 'nedb' }}" matrix_appservice_slack_database_hostname: "{{ devture_postgres_connection_hostname if devture_postgres_enabled else '' }}" diff --git a/roles/custom/matrix-bridge-appservice-slack/defaults/main.yml b/roles/custom/matrix-bridge-appservice-slack/defaults/main.yml index a803dbe96..d3bf4a98f 100644 --- a/roles/custom/matrix-bridge-appservice-slack/defaults/main.yml +++ b/roles/custom/matrix-bridge-appservice-slack/defaults/main.yml @@ -4,6 +4,10 @@ matrix_appservice_slack_enabled: true +matrix_appservice_slack_scheme: https +matrix_appservice_slack_hostname: '' +matrix_appservice_slack_path_prefix: /appservice-slack + matrix_appservice_slack_container_image_self_build: false matrix_appservice_slack_docker_repo: "https://github.com/matrix-org/matrix-appservice-slack.git" matrix_appservice_slack_docker_repo_version: "{{ 'master' if matrix_appservice_slack_version == 'latest' else matrix_appservice_slack_version }}" @@ -21,8 +25,8 @@ matrix_appservice_slack_base_path: "{{ matrix_base_data_path }}/appservice-slack matrix_appservice_slack_config_path: "{{ matrix_appservice_slack_base_path }}/config" matrix_appservice_slack_data_path: "{{ matrix_appservice_slack_base_path }}/data" -matrix_appservice_slack_public_endpoint: /appservice-slack -matrix_appservice_slack_inbound_uri_prefix: "{{ matrix_homeserver_url }}{{ matrix_appservice_slack_public_endpoint }}" +matrix_appservice_slack_public_endpoint: "{{ matrix_appservice_slack_path_prefix }}" +matrix_appservice_slack_inbound_uri_prefix: "{{ matrix_appservice_slack_scheme }}://{{ matrix_appservice_slack_hostname }}{{ matrix_appservice_slack_public_endpoint }}" # Once you make a control room in Matrix, you can get its ID by typing any message and checking its source matrix_appservice_slack_control_room_id: '' @@ -38,20 +42,57 @@ matrix_appservice_slack_slack_port: 9003 # Takes an ":" or "" value (e.g. "127.0.0.1:9999"), or empty string to not expose. matrix_appservice_slack_container_http_host_bind_port: '' -matrix_appservice_slack_homeserver_media_url: "{{ matrix_server_fqn_matrix }}" -matrix_appservice_slack_homeserver_url: "" -matrix_appservice_slack_homeserver_domain: "{{ matrix_domain }}" -matrix_appservice_slack_appservice_url: 'http://matrix-appservice-slack' +matrix_appservice_slack_container_network: "" + +matrix_appservice_slack_container_additional_networks: "{{ matrix_appservice_slack_container_additional_networks_auto + matrix_appservice_slack_container_additional_networks_custom }}" +matrix_appservice_slack_container_additional_networks_auto: [] +matrix_appservice_slack_container_additional_networks_custom: [] + +# matrix_appservice_slack_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_appservice_slack_container_labels_additional_labels`. +matrix_appservice_slack_container_labels_traefik_enabled: true +matrix_appservice_slack_container_labels_traefik_docker_network: "{{ matrix_appservice_slack_container_network }}" +matrix_appservice_slack_container_labels_traefik_entrypoints: web-secure +matrix_appservice_slack_container_labels_traefik_tls_certResolver: default # noqa var-naming + +# Controls whether labels will be added that expose maubot's management endpoints +matrix_appservice_slack_container_labels_public_endpoint_enabled: true +matrix_appservice_slack_container_labels_public_endpoint_hostname: "{{ matrix_appservice_slack_hostname }}" +matrix_appservice_slack_container_labels_public_endpoint_prefix: "{{ matrix_appservice_slack_path_prefix }}" +matrix_appservice_slack_container_labels_public_endpoint_traefik_rule: "Host(`{{ matrix_appservice_slack_container_labels_public_endpoint_hostname }}`) && PathPrefix(`{{ matrix_appservice_slack_path_prefix }}`)" +matrix_appservice_slack_container_labels_public_endpoint_traefik_priority: 0 +matrix_appservice_slack_container_labels_public_endpoint_traefik_entrypoints: "{{ matrix_appservice_slack_container_labels_traefik_entrypoints }}" +matrix_appservice_slack_container_labels_public_endpoint_traefik_tls: "{{ matrix_appservice_slack_container_labels_public_endpoint_traefik_entrypoints != 'web' }}" +matrix_appservice_slack_container_labels_public_endpoint_traefik_tls_certResolver: "{{ matrix_appservice_slack_container_labels_traefik_tls_certResolver }}" # noqa var-naming + +# matrix_appservice_slack_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_appservice_slack_container_labels_additional_labels: | +# my.label=1 +# another.label="here" +matrix_appservice_slack_container_labels_additional_labels: '' # A list of extra arguments to pass to the container matrix_appservice_slack_container_extra_arguments: [] # List of systemd services that matrix-appservice-slack.service depends on. -matrix_appservice_slack_systemd_required_services_list: ['docker.service'] +matrix_appservice_slack_systemd_required_services_list: "{{ matrix_appservice_slack_systemd_required_services_list_default + matrix_appservice_slack_systemd_required_services_list_auto + matrix_appservice_slack_systemd_required_services_list_custom }}" +matrix_appservice_slack_systemd_required_services_list_default: ['docker.service'] +matrix_appservice_slack_systemd_required_services_list_auto: [] +matrix_appservice_slack_systemd_required_services_list_custom: [] # List of systemd services that matrix-appservice-slack.service wants matrix_appservice_slack_systemd_wanted_services_list: [] +matrix_appservice_slack_homeserver_media_url: "{{ matrix_server_fqn_matrix }}" +matrix_appservice_slack_homeserver_url: "" +matrix_appservice_slack_homeserver_domain: "{{ matrix_domain }}" +matrix_appservice_slack_appservice_url: 'http://matrix-appservice-slack' + matrix_appservice_slack_appservice_token: '' matrix_appservice_slack_homeserver_token: '' matrix_appservice_slack_id_token: '' diff --git a/roles/custom/matrix-bridge-appservice-slack/tasks/inject_into_nginx_proxy.yml b/roles/custom/matrix-bridge-appservice-slack/tasks/inject_into_nginx_proxy.yml deleted file mode 100644 index 4ddcb954d..000000000 --- a/roles/custom/matrix-bridge-appservice-slack/tasks/inject_into_nginx_proxy.yml +++ /dev/null @@ -1,44 +0,0 @@ ---- - -- name: Fail if matrix-nginx-proxy role already executed - ansible.builtin.fail: - msg: >- - Trying to append Slack Appservice'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-appservice-slack role. - when: matrix_nginx_proxy_role_executed | default(False) | bool - -- name: Generate Matrix Appservice Slack proxying configuration for matrix-nginx-proxy - ansible.builtin.set_fact: - matrix_appservice_slack_matrix_nginx_proxy_configuration: | - location {{ matrix_appservice_slack_public_endpoint }} { - {% 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_appservice_slack_appservice_url }}:{{ matrix_appservice_slack_slack_port }}"; - proxy_pass $backend; - {% else %} - {# Generic configuration for use outside of our container setup #} - proxy_pass http://127.0.0.1:{{ matrix_appservice_slack_slack_port }}; - {% endif %} - } - -- name: Register Slack Appservice proxying configuration with matrix-nginx-proxy - ansible.builtin.set_fact: - matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks: | - {{ - matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks | default([]) - + - [matrix_appservice_slack_matrix_nginx_proxy_configuration] - }} - -- name: Warn about reverse-proxying if matrix-nginx-proxy not used - ansible.builtin.debug: - msg: >- - NOTE: You've enabled the Matrix Slack bridge but are not using the matrix-nginx-proxy - reverse proxy. - Please make sure that you're proxying the `{{ matrix_appservice_slack_public_endpoint }}` - URL endpoint to the matrix-appservice-slack container. - You can expose the container's port using the `matrix_appservice_slack_container_http_host_bind_port` variable. - when: "matrix_appservice_slack_enabled | bool and not matrix_nginx_proxy_enabled | default(False) | bool" diff --git a/roles/custom/matrix-bridge-appservice-slack/tasks/main.yml b/roles/custom/matrix-bridge-appservice-slack/tasks/main.yml index 6fb8ac7c9..c98dc5198 100644 --- a/roles/custom/matrix-bridge-appservice-slack/tasks/main.yml +++ b/roles/custom/matrix-bridge-appservice-slack/tasks/main.yml @@ -1,14 +1,5 @@ --- -- tags: - - setup-all - - setup-nginx-proxy - - install-all - - install-nginx-proxy - block: - - when: matrix_appservice_slack_enabled | bool - ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml" - - tags: - setup-all - setup-appservice-slack diff --git a/roles/custom/matrix-bridge-appservice-slack/tasks/setup_install.yml b/roles/custom/matrix-bridge-appservice-slack/tasks/setup_install.yml index 9a72f69d0..740918337 100644 --- a/roles/custom/matrix-bridge-appservice-slack/tasks/setup_install.yml +++ b/roles/custom/matrix-bridge-appservice-slack/tasks/setup_install.yml @@ -82,6 +82,21 @@ owner: "{{ matrix_user_username }}" group: "{{ matrix_user_groupname }}" +- name: Ensure matrix-appservice-slack container network is created + community.general.docker_network: + name: "{{ matrix_appservice_slack_container_network }}" + driver: bridge + +- name: Ensure matrix-appservice-slack support files installed + ansible.builtin.template: + src: "{{ role_path }}/templates/{{ item }}.j2" + dest: "{{ matrix_appservice_slack_base_path }}/{{ item }}" + mode: 0640 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - labels + - name: Ensure matrix-appservice-slack.service installed ansible.builtin.template: src: "{{ role_path }}/templates/systemd/matrix-appservice-slack.service.j2" diff --git a/roles/custom/matrix-bridge-appservice-slack/tasks/validate_config.yml b/roles/custom/matrix-bridge-appservice-slack/tasks/validate_config.yml index ef97635ec..d6d802dd3 100644 --- a/roles/custom/matrix-bridge-appservice-slack/tasks/validate_config.yml +++ b/roles/custom/matrix-bridge-appservice-slack/tasks/validate_config.yml @@ -12,6 +12,9 @@ - {'name': 'matrix_appservice_slack_homeserver_token', when: true} - {'name': 'matrix_appservice_slack_id_token', when: true} - {'name': 'matrix_appservice_slack_database_hostname', when: "{{ matrix_appservice_slack_database_engine == 'postgres' }}"} + - {'name': 'matrix_appservice_slack_container_network', when: true} + - {'name': 'matrix_appservice_slack_hostname', when: true} + - {'name': 'matrix_appservice_slack_path_prefix', when: true} - name: (Deprecation) Catch and report renamed settings ansible.builtin.fail: diff --git a/roles/custom/matrix-bridge-appservice-slack/templates/labels.j2 b/roles/custom/matrix-bridge-appservice-slack/templates/labels.j2 new file mode 100644 index 000000000..3046c01ec --- /dev/null +++ b/roles/custom/matrix-bridge-appservice-slack/templates/labels.j2 @@ -0,0 +1,25 @@ +{% if matrix_appservice_slack_container_labels_traefik_enabled %} +traefik.enable=true + +{% if matrix_appservice_slack_container_labels_traefik_docker_network %} +traefik.docker.network={{ matrix_appservice_slack_container_labels_traefik_docker_network }} +{% endif %} + +{# Management #} +{% if matrix_appservice_slack_container_labels_public_endpoint_enabled %} +traefik.http.routers.matrix-appservice-slack-public-endpoint.rule={{ matrix_appservice_slack_container_labels_public_endpoint_traefik_rule }} +{% if matrix_appservice_slack_container_labels_public_endpoint_traefik_priority | int > 0 %} +traefik.http.routers.matrix-appservice-slack-public-endpoint.priority={{ matrix_appservice_slack_container_labels_public_endpoint_traefik_priority }} +{% endif %} +traefik.http.routers.matrix-appservice-slack-public-endpoint.service=matrix-mautrix-facebook-appservice +traefik.http.routers.matrix-appservice-slack-public-endpoint.entrypoints={{ matrix_appservice_slack_container_labels_public_endpoint_traefik_entrypoints }} +traefik.http.routers.matrix-appservice-slack-public-endpoint.tls={{ matrix_appservice_slack_container_labels_public_endpoint_traefik_tls | to_json }} +{% if matrix_appservice_slack_container_labels_public_endpoint_traefik_tls %} +traefik.http.routers.matrix-appservice-slack-public-endpoint.tls.certResolver={{ matrix_appservice_slack_container_labels_public_endpoint_traefik_tls_certResolver }} +{% endif %} +traefik.http.services.matrix-mautrix-facebook-appservice.loadbalancer.server.port={{ matrix_appservice_slack_slack_port }} +{% endif %} + +{% endif %} + +{{ matrix_appservice_slack_container_labels_additional_labels }} diff --git a/roles/custom/matrix-bridge-appservice-slack/templates/systemd/matrix-appservice-slack.service.j2 b/roles/custom/matrix-bridge-appservice-slack/templates/systemd/matrix-appservice-slack.service.j2 index 5503522bd..c97a227c5 100644 --- a/roles/custom/matrix-bridge-appservice-slack/templates/systemd/matrix-appservice-slack.service.j2 +++ b/roles/custom/matrix-bridge-appservice-slack/templates/systemd/matrix-appservice-slack.service.j2 @@ -19,22 +19,30 @@ 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-appservice-slack \ +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \ + --rm \ + --name=matrix-appservice-slack \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ - --network={{ matrix_docker_network }} \ + --network={{ matrix_appservice_slack_container_network }} \ {% if matrix_appservice_slack_container_http_host_bind_port %} -p {{ matrix_appservice_slack_container_http_host_bind_port }}:{{matrix_appservice_slack_slack_port}} \ {% endif %} - -v {{ matrix_appservice_slack_config_path }}:/config:z \ - -v {{ matrix_appservice_slack_data_path }}:/data:z \ + --mount type=bind,src={{ matrix_appservice_slack_config_path }},dst=/config \ + --mount type=bind,src={{ matrix_appservice_slack_data_path }},dst=/data \ {% for arg in matrix_appservice_slack_container_extra_arguments %} {{ arg }} \ {% endfor %} {{ matrix_appservice_slack_docker_image }} \ node app.js -p {{matrix_appservice_slack_matrix_port}} -c /config/config.yaml -f /config/slack-registration.yaml +{% for network in matrix_appservice_slack_container_additional_networks %} +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-appservice-slack +{% endfor %} + +ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-appservice-slack + 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-appservice-slack 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-appservice-slack 2>/dev/null || true' Restart=always