1
0
mirror of https://github.com/spantaleev/matrix-docker-ansible-deploy.git synced 2024-12-12 08:43:55 +02:00

Move matrix-registration service to its own network and add native Traefik support

This commit is contained in:
Slavi Pantaleev 2024-01-12 17:17:12 +02:00
parent 13e47fc3f5
commit 4018aa38b3
8 changed files with 164 additions and 91 deletions

View File

@ -379,7 +379,7 @@ devture_systemd_service_manager_services_list_auto: |
+
([{'name': (redis_identifier + '.service'), 'priority': 750, 'groups': ['matrix', 'redis']}] if redis_enabled else [])
+
([{'name': 'matrix-registration.service', 'priority': 4000, 'groups': ['matrix', 'registration']}] if matrix_registration_enabled else [])
([{'name': 'matrix-registration.service', 'priority': 4000, 'groups': ['matrix', 'registration', 'matrix-registration']}] if matrix_registration_enabled else [])
+
([{'name': 'matrix-sliding-sync.service', 'priority': 4000, 'groups': ['matrix', 'sliding-sync']}] if matrix_sliding_sync_enabled else [])
+
@ -4697,12 +4697,38 @@ grafana_default_home_dashboard_path: |-
matrix_registration_enabled: false
# Normally, matrix-nginx-proxy is enabled and nginx can reach matrix-registration over the container network.
# If matrix-nginx-proxy is not enabled, or you otherwise have a need for it, you can expose
# matrix-registration's HTTP port to the local host.
matrix_registration_scheme: "{{ 'https' if matrix_playbook_ssl_enabled else 'http' }}"
matrix_registration_hostname: "{{ matrix_server_fqn_matrix }}"
matrix_registration_path_prefix: /matrix-registration
matrix_registration_systemd_required_services_list_auto: |
{{
([devture_postgres_identifier ~ '.service'] if (devture_postgres_enabled and matrix_registration_database_hostname == devture_postgres_connection_hostname) else [])
}}
matrix_registration_container_image_self_build: "{{ matrix_architecture != 'amd64' }}"
matrix_registration_container_http_host_bind_port: "{{ (matrix_playbook_service_host_bind_interface_prefix ~ '8767') if matrix_playbook_service_host_bind_interface_prefix else '' }}"
matrix_registration_riot_instance: "{{ ('https://' + matrix_server_fqn_element) if matrix_client_element_enabled else 'https://riot.im/app/' }}"
matrix_registration_container_network: "{{ matrix_addons_container_network }}"
matrix_registration_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_registration_database_hostname == devture_postgres_connection_hostname and matrix_mautrix_facebook_container_network != devture_postgres_container_network) else [])
+
([matrix_playbook_reverse_proxyable_services_additional_network] if (matrix_playbook_reverse_proxyable_services_additional_network and matrix_registration_container_labels_traefik_enabled) else [])
) | unique
}}
matrix_registration_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}"
matrix_registration_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}"
matrix_registration_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}"
matrix_registration_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}"
matrix_registration_riot_instance: "{{ ('https://' + matrix_server_fqn_element) if matrix_client_element_enabled else 'https://element.io/app/' }}"
matrix_registration_shared_secret: |-
{{
@ -4713,19 +4739,10 @@ matrix_registration_shared_secret: |-
}[matrix_homeserver_implementation]
}}
matrix_registration_server_location: "{{ matrix_homeserver_container_url }}"
matrix_registration_server_location: "{{ matrix_addons_homeserver_client_api_url }}"
matrix_registration_api_validate_certs: "{{ false if matrix_playbook_ssl_retrieval_method == 'self-signed' else true }}"
matrix_registration_container_image_self_build: "{{ matrix_architecture != 'amd64' }}"
matrix_registration_systemd_required_services_list: |
{{
['docker.service']
+
([devture_postgres_identifier ~ '.service'] if devture_postgres_enabled else [])
}}
# Postgres is the default, except if not using internal Postgres server
matrix_registration_database_engine: "{{ 'postgres' if devture_postgres_enabled else 'sqlite' }}"
matrix_registration_database_hostname: "{{ devture_postgres_connection_hostname if devture_postgres_enabled else '' }}"

View File

@ -5,6 +5,10 @@
matrix_registration_enabled: true
matrix_registration_scheme: https
matrix_registration_hostname: ''
matrix_registration_path_prefix: /
matrix_registration_container_image_self_build: false
matrix_registration_container_image_self_build_repo: "https://github.com/ZerataX/matrix-registration"
matrix_registration_container_image_self_build_branch: "{{ 'master' if matrix_registration_version == 'latest' else matrix_registration_version }}"
@ -25,11 +29,46 @@ matrix_registration_docker_image: "{{ matrix_registration_docker_image_name_pref
matrix_registration_docker_image_name_prefix: "{{ 'localhost/' if matrix_registration_container_image_self_build else matrix_container_global_registry_prefix }}"
matrix_registration_docker_image_force_pull: "{{ matrix_registration_docker_image.endswith(':latest') }}"
matrix_registration_container_network: ""
matrix_registration_container_additional_networks: "{{ matrix_registration_container_additional_networks_auto + matrix_registration_container_additional_networks_custom }}"
matrix_registration_container_additional_networks_auto: []
matrix_registration_container_additional_networks_custom: []
# matrix_registration_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_registration_container_labels_additional_labels`.
matrix_registration_container_labels_traefik_enabled: true
matrix_registration_container_labels_traefik_docker_network: "{{ matrix_registration_container_network }}"
matrix_registration_container_labels_traefik_entrypoints: web-secure
matrix_registration_container_labels_traefik_tls_certResolver: default # noqa var-naming
# Controls whether labels will be added that expose matrix-registration's public endpoints
matrix_registration_container_labels_public_endpoint_enabled: true
matrix_registration_container_labels_public_endpoint_traefik_rule: "Host(`{{ matrix_registration_hostname }}`){% if matrix_registration_path_prefix != '/' %} && PathPrefix(`{{ matrix_registration_path_prefix }}`){% endif %}"
matrix_registration_container_labels_public_endpoint_traefik_priority: 0
matrix_registration_container_labels_public_endpoint_traefik_entrypoints: "{{ matrix_registration_container_labels_traefik_entrypoints }}"
matrix_registration_container_labels_public_endpoint_traefik_tls: "{{ matrix_registration_container_labels_public_endpoint_traefik_entrypoints != 'web' }}"
matrix_registration_container_labels_public_endpoint_traefik_tls_certResolver: "{{ matrix_registration_container_labels_traefik_tls_certResolver }}" # noqa var-naming
# matrix_registration_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_registration_container_labels_additional_labels: |
# my.label=1
# another.label="here"
matrix_registration_container_labels_additional_labels: ''
# A list of extra arguments to pass to the container
matrix_registration_container_extra_arguments: []
# List of systemd services that matrix-registration.service depends on
matrix_registration_systemd_required_services_list: ['docker.service']
matrix_registration_systemd_required_services_list: "{{ matrix_registration_systemd_required_services_list_default + matrix_registration_systemd_required_services_list_auto + matrix_registration_systemd_required_services_list_custom }}"
matrix_registration_systemd_required_services_list_default: ['docker.service']
matrix_registration_systemd_required_services_list_auto: []
matrix_registration_systemd_required_services_list_custom: []
# List of systemd services that matrix-registration.service wants
matrix_registration_systemd_wanted_services_list: []
@ -68,15 +107,10 @@ matrix_registration_db: "{{
}[matrix_registration_database_engine]
}}"
matrix_registration_base_url: "{{ matrix_registration_path_prefix }}"
# The path at which Matrix Registration will be exposed on `matrix.DOMAIN`
# (only applies when matrix-nginx-proxy is used).
matrix_registration_public_endpoint: /matrix-registration
matrix_registration_base_url: "{{ matrix_registration_public_endpoint }}"
matrix_registration_api_register_endpoint: "{{ matrix_homeserver_url }}{{ matrix_registration_public_endpoint }}/register"
matrix_registration_api_token_endpoint: "{{ matrix_homeserver_url }}{{ matrix_registration_public_endpoint }}/token"
matrix_registration_api_register_endpoint: "{{ matrix_registration_scheme }}://{{ matrix_registration_hostname }}{{ matrix_registration_base_url }}{{ '' if matrix_registration_base_url == '/' else '/' }}register"
matrix_registration_api_token_endpoint: "{{ matrix_registration_scheme }}://{{ matrix_registration_hostname }}{{ matrix_registration_base_url }}{{ '' if matrix_registration_base_url == '/' else '/' }}token"
matrix_registration_api_validate_certs: true
@ -93,7 +127,7 @@ matrix_registration_shared_secret: ""
# matrix_registration_admin_secret is your own admin secret for using matrix-registration (creating new tokens, etc.)
matrix_registration_admin_secret: ""
matrix_registration_riot_instance: "https://riot.im/app/"
matrix_registration_riot_instance: "https://element.io/app/"
# Default matrix-registration configuration template which covers the generic use case.
# You can customize it by controlling the various variables inside it.

View File

@ -1,55 +0,0 @@
---
- name: Fail if matrix-nginx-proxy role already executed
ansible.builtin.fail:
msg: >-
Trying to append matrix-registration'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-registration role.
when: matrix_nginx_proxy_role_executed | default(False) | bool
- name: Generate matrix-registration proxying configuration for matrix-nginx-proxy
ansible.builtin.set_fact:
matrix_registration_matrix_nginx_proxy_configuration: |
rewrite ^{{ matrix_registration_public_endpoint }}$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_registration_public_endpoint }}/ permanent;
rewrite ^{{ matrix_registration_public_endpoint }}/$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_registration_public_endpoint }}/register redirect;
location ~ ^{{ matrix_registration_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-registration:5000";
proxy_pass http://$backend/$1;
{% else %}
{# Generic configuration for use outside of our container setup #}
proxy_pass http://127.0.0.1:8767/$1;
{% endif %}
{#
Workaround matrix-registration serving the background image at /static
(see https://github.com/ZerataX/matrix-registration/issues/47)
#}
sub_filter_once off;
sub_filter_types text/css;
sub_filter "/static/" "{{ matrix_registration_public_endpoint }}/static/";
}
- name: Register matrix-registration 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_registration_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-registration tool but are not using the matrix-nginx-proxy
reverse proxy.
Please make sure that you're proxying the `{{ matrix_registration_public_endpoint }}`
URL endpoint to the matrix-registration container.
You can expose the container's port using the `matrix_registration_container_http_host_bind_port` variable.
when: "not matrix_nginx_proxy_enabled | default(False) | bool"

View File

@ -1,14 +1,5 @@
---
- tags:
- setup-all
- setup-nginx-proxy
- install-all
- install-nginx-proxy
block:
- when: matrix_registration_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml"
- tags:
- setup-all
- setup-matrix-registration

View File

@ -97,6 +97,21 @@
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
- name: Ensure matrix-registration support files installed
ansible.builtin.template:
src: "{{ role_path }}/templates/{{ item }}.j2"
dest: "{{ matrix_registration_base_path }}/{{ item }}"
mode: 0640
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
with_items:
- labels
- name: Ensure matrix-registration container network is created
community.general.docker_network:
name: "{{ matrix_registration_container_network }}"
driver: bridge
- name: Ensure matrix-registration.service installed
ansible.builtin.template:
src: "{{ role_path }}/templates/systemd/matrix-registration.service.j2"

View File

@ -6,6 +6,9 @@
You need to define a required configuration setting (`{{ item.name }}`).
when: "item.when | bool and vars[item.name] == ''"
with_items:
- {'name': 'matrix_registration_hostname', when: true}
- {'name': 'matrix_registration_path_prefix', when: true}
- {'name': 'matrix_registration_container_network', when: true}
- {'name': 'matrix_registration_shared_secret', when: true}
- {'name': 'matrix_registration_admin_secret', when: true}
- {'name': 'matrix_registration_server_location', when: true}
@ -19,3 +22,4 @@
when: "item.old in vars"
with_items:
- {'old': 'matrix_registration_docker_repo', 'new': 'matrix_registration_container_image_self_build_repo'}
- {'old': 'matrix_registration_public_endpoint', 'new': 'matrix_registration_path_prefix'}

View File

@ -0,0 +1,58 @@
{% if matrix_registration_container_labels_traefik_enabled %}
traefik.enable=true
{% if matrix_registration_container_labels_traefik_docker_network %}
traefik.docker.network={{ matrix_registration_container_labels_traefik_docker_network }}
{% endif %}
traefik.http.services.matrix-registration.loadbalancer.server.port=5000
{#
Public (e.g. /matrix-registration)
#}
{% if matrix_registration_container_labels_public_endpoint_enabled %}
{% set middlewares = [] %}
{% if matrix_registration_path_prefix != '/' %}
traefik.http.middlewares.matrix-registration-slashless-redirect-slashless.redirectregex.regex=({{ matrix_registration_path_prefix | quote }})$
traefik.http.middlewares.matrix-registration-slashless-redirect-slashless.redirectregex.replacement=${1}/
{% set middlewares = middlewares + ['matrix-registration-slashless-redirect-slashless'] %}
{% endif %}
{# matrix-registration serves nothing at the root URL, so we redirect to /register instead. #}
traefik.http.middlewares.matrix-registration-slashless-redirect-root.redirectregex.regex=({{ matrix_registration_path_prefix | quote }}{{ '' if matrix_registration_path_prefix == '/' else '/' }})$
traefik.http.middlewares.matrix-registration-slashless-redirect-root.redirectregex.replacement=${1}register
{% set middlewares = middlewares + ['matrix-registration-slashless-redirect-root'] %}
{% if matrix_registration_path_prefix != '/' %}
traefik.http.middlewares.matrix-registration-strip-prefix.stripprefix.prefixes={{ matrix_registration_path_prefix }}
{% set middlewares = middlewares + ['matrix-registration-strip-prefix'] %}
{% endif %}
traefik.http.routers.matrix-registration-public.rule={{ matrix_registration_container_labels_public_endpoint_traefik_rule }}
{% if middlewares | length > 0 %}
traefik.http.routers.matrix-registration-public.middlewares={{ middlewares | join(',') }}
{% endif %}
{% if matrix_registration_container_labels_public_endpoint_traefik_priority | int > 0 %}
traefik.http.routers.matrix-registration-public.priority={{ matrix_registration_container_labels_public_endpoint_traefik_priority }}
{% endif %}
traefik.http.routers.matrix-registration-public.service=matrix-registration
traefik.http.routers.matrix-registration-public.entrypoints={{ matrix_registration_container_labels_public_endpoint_traefik_entrypoints }}
traefik.http.routers.matrix-registration-public.tls={{ matrix_registration_container_labels_public_endpoint_traefik_tls | to_json }}
{% if matrix_registration_container_labels_public_endpoint_traefik_tls %}
traefik.http.routers.matrix-registration-public.tls.certResolver={{ matrix_registration_container_labels_public_endpoint_traefik_tls_certResolver }}
{% endif %}
{% endif %}
{#
/Public (e.g. /matrix-registration)
#}
{% endif %}
{{ matrix_registration_container_labels_additional_labels }}

View File

@ -16,22 +16,31 @@ Environment="HOME={{ devture_systemd_docker_base_systemd_unit_home_path }}"
ExecStartPre=-{{ 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-registration 2>/dev/null || true'
ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-registration 2>/dev/null || true'
ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-registration \
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \
--rm \
--name=matrix-registration \
--log-driver=none \
--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
--cap-drop=ALL \
--network={{ matrix_docker_network }} \
--network={{ matrix_registration_container_network }} \
{% if matrix_registration_container_http_host_bind_port %}
-p {{ matrix_registration_container_http_host_bind_port }}:5000 \
{% endif %}
--mount type=bind,src={{ matrix_registration_config_path }},dst=/config,ro \
--mount type=bind,src={{ matrix_registration_data_path }},dst=/data \
--label-file={{ matrix_registration_base_path }}/labels \
{% for arg in matrix_registration_container_extra_arguments %}
{{ arg }} \
{% endfor %}
{{ matrix_registration_docker_image }} \
serve
{% for network in matrix_registration_container_additional_networks %}
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-registration
{% endfor %}
ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-registration
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-registration 2>/dev/null || true'
ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-registration 2>/dev/null || true'
Restart=always