From 3f212feb1f59aedcbfe8a2ab6cf5b4b024cd19d2 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Sat, 13 Jan 2024 17:05:58 +0200 Subject: [PATCH] Move matrix-email2matrix to its own container network --- docs/configuring-playbook-email2matrix.md | 3 +-- group_vars/matrix_servers | 12 ++++++++++++ .../matrix-email2matrix/defaults/main.yml | 11 ++++++++++- .../tasks/setup_install.yml | 5 +++++ .../tasks/validate_config.yml | 10 ++++++++++ .../systemd/matrix-email2matrix.service.j2 | 18 ++++++++++++++---- 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/docs/configuring-playbook-email2matrix.md b/docs/configuring-playbook-email2matrix.md index d65d2ccdb..73fcff551 100644 --- a/docs/configuring-playbook-email2matrix.md +++ b/docs/configuring-playbook-email2matrix.md @@ -70,7 +70,6 @@ matrix_email2matrix_matrix_mappings: SkipMarkdown: true ``` -You can also set `MatrixHomeserverUrl` to `http://matrix-synapse-reverse-proxy-companion:8008`, instead of the public `https://matrix.DOMAIN`. -However, that's more likely to break in the future if you switch to another server implementation than Synapse. +You can also set `MatrixHomeserverUrl` to the container URL where your homeserver's Client-Server API lives by using the `{{ matrix_homeserver_container_url }}` variable, instead of the public `https://matrix.DOMAIN` endpoint. Re-run the playbook (`--tags=setup-email2matrix,start`) and try sending an email to `my-mailbox@matrix.DOMAIN`. diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index f7624be6d..f7f9d6bd5 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -2940,8 +2940,20 @@ matrix_dynamic_dns_enabled: false matrix_email2matrix_enabled: false +matrix_email2matrix_systemd_required_services_list_auto: | + {{ + matrix_addons_homeserver_systemd_services_list + }} + matrix_email2matrix_container_image_self_build: "{{ matrix_architecture not in ['amd64', 'arm32', 'arm64'] }}" +matrix_email2matrix_container_network: "{{ matrix_addons_container_network }}" + +matrix_email2matrix_container_additional_networks_auto: |- + {{ + ([] if matrix_addons_homeserver_container_network == '' else [matrix_addons_homeserver_container_network]) + }} + ###################################################################### # # /matrix-email2matrix diff --git a/roles/custom/matrix-email2matrix/defaults/main.yml b/roles/custom/matrix-email2matrix/defaults/main.yml index cdd287a81..8ea7637c7 100644 --- a/roles/custom/matrix-email2matrix/defaults/main.yml +++ b/roles/custom/matrix-email2matrix/defaults/main.yml @@ -17,11 +17,20 @@ matrix_email2matrix_docker_image_prefix: "{{ 'localhost/' if matrix_email2matrix matrix_email2matrix_docker_image: "{{ matrix_email2matrix_docker_image_prefix }}devture/email2matrix:{{ matrix_email2matrix_version }}" matrix_email2matrix_docker_image_force_pull: "{{ matrix_email2matrix_docker_image.endswith(':latest') }}" +matrix_email2matrix_container_network: "" + +matrix_email2matrix_container_additional_networks: "{{ matrix_email2matrix_container_additional_networks_auto + matrix_email2matrix_container_additional_networks_custom }}" +matrix_email2matrix_container_additional_networks_auto: [] +matrix_email2matrix_container_additional_networks_custom: [] + # A list of extra arguments to pass to the container matrix_email2matrix_container_extra_arguments: [] # List of systemd services that matrix-corporal.service depends on -matrix_email2matrix_systemd_required_services_list: ['docker.service'] +matrix_email2matrix_systemd_required_services_list: "{{ matrix_email2matrix_systemd_required_services_list_default + matrix_email2matrix_systemd_required_services_list_auto + matrix_email2matrix_systemd_required_services_list_custom }}" +matrix_email2matrix_systemd_required_services_list_default: ['docker.service'] +matrix_email2matrix_systemd_required_services_list_auto: [] +matrix_email2matrix_systemd_required_services_list_custom: [] # Controls where the matrix-email2matrix container exposes the SMTP (tcp/2525 in the container). # diff --git a/roles/custom/matrix-email2matrix/tasks/setup_install.yml b/roles/custom/matrix-email2matrix/tasks/setup_install.yml index 8cfbc5ea3..f6fe55e23 100644 --- a/roles/custom/matrix-email2matrix/tasks/setup_install.yml +++ b/roles/custom/matrix-email2matrix/tasks/setup_install.yml @@ -56,6 +56,11 @@ pull: true when: "matrix_email2matrix_container_image_self_build | bool" +- name: Ensure matrix-email2matrix container network is created + community.general.docker_network: + name: "{{ matrix_email2matrix_container_network }}" + driver: bridge + - name: Ensure matrix-email2matrix.service installed ansible.builtin.template: src: "{{ role_path }}/templates/systemd/matrix-email2matrix.service.j2" diff --git a/roles/custom/matrix-email2matrix/tasks/validate_config.yml b/roles/custom/matrix-email2matrix/tasks/validate_config.yml index 8d89f1d6a..ab0ced5d0 100644 --- a/roles/custom/matrix-email2matrix/tasks/validate_config.yml +++ b/roles/custom/matrix-email2matrix/tasks/validate_config.yml @@ -5,3 +5,13 @@ msg: > You need to define at least one mapping in `matrix_email2matrix_matrix_mappings` for enabling Email2Matrix. when: "matrix_email2matrix_matrix_mappings | length == 0" + +- name: Fail if required email2matrix 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_email2matrix_container_network', when: true} + + diff --git a/roles/custom/matrix-email2matrix/templates/systemd/matrix-email2matrix.service.j2 b/roles/custom/matrix-email2matrix/templates/systemd/matrix-email2matrix.service.j2 index df0d1f85e..10d4de04d 100644 --- a/roles/custom/matrix-email2matrix/templates/systemd/matrix-email2matrix.service.j2 +++ b/roles/custom/matrix-email2matrix/templates/systemd/matrix-email2matrix.service.j2 @@ -1,8 +1,10 @@ #jinja2: lstrip_blocks: "True" [Unit] Description=Email2Matrix -After=docker.service -Requires=docker.service +{% for service in matrix_email2matrix_systemd_required_services_list %} +Requires={{ service }} +After={{ service }} +{% endfor %} DefaultDependencies=no [Service] @@ -11,12 +13,14 @@ 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-email2matrix 2>/dev/null || true' ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-email2matrix 2>/dev/null || true' -ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-email2matrix \ +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \ + --rm \ + --name=matrix-email2matrix \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ --read-only \ - --network={{ matrix_docker_network }} \ + --network={{ matrix_email2matrix_container_network }} \ -p {{ matrix_email2matrix_smtp_host_bind_port }}:2525 \ --mount type=bind,src={{ matrix_email2matrix_config_dir_path }}/config.json,dst=/config.json,ro \ {% for arg in matrix_email2matrix_container_extra_arguments %} @@ -24,6 +28,12 @@ ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name {% endfor %} {{ matrix_email2matrix_docker_image }} +{% for network in matrix_email2matrix_container_additional_networks %} +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-email2matrix +{% endfor %} + +ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-email2matrix + 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-email2matrix 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-email2matrix 2>/dev/null || true' Restart=always