Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

180 lines
8.0 KiB

  1. # SPDX-FileCopyrightText: 2018 - 2024 Slavi Pantaleev
  2. # SPDX-FileCopyrightText: 2019 Dan Arnfield
  3. # SPDX-FileCopyrightText: 2019 Jan Christian Grünhage
  4. # SPDX-FileCopyrightText: 2019 Lyubomir Popov
  5. # SPDX-FileCopyrightText: 2020 - 2024 MDAD project contributors
  6. # SPDX-FileCopyrightText: 2020 Chris van Dijk
  7. # SPDX-FileCopyrightText: 2020 Horvath Gergely
  8. # SPDX-FileCopyrightText: 2022 Jim Myhrberg
  9. # SPDX-FileCopyrightText: 2022 Marko Weltzer
  10. # SPDX-FileCopyrightText: 2022 Nikita Chernyi
  11. # SPDX-FileCopyrightText: 2022 Sebastian Gumprich
  12. # SPDX-FileCopyrightText: 2024 David Mehren
  13. #
  14. # SPDX-License-Identifier: AGPL-3.0-or-later
  15. ---
  16. # This will throw a Permission Denied error if already mounted using fuse
  17. - name: Check Synapse media store path
  18. ansible.builtin.stat:
  19. path: "{{ matrix_synapse_media_store_path }}"
  20. register: local_path_media_store_stat
  21. ignore_errors: true
  22. # This is separate and conditional, to ensure we don't execute it
  23. # if the path already exists or we failed to check, because it's mounted using fuse.
  24. - name: Ensure Synapse media store path exists
  25. ansible.builtin.file:
  26. path: "{{ matrix_synapse_media_store_path }}"
  27. state: directory
  28. mode: '0750'
  29. owner: "{{ matrix_synapse_uid }}"
  30. group: "{{ matrix_synapse_gid }}"
  31. when: "not local_path_media_store_stat.failed and not local_path_media_store_stat.stat.exists"
  32. - when: "matrix_synapse_container_image_self_build | bool"
  33. block:
  34. # A checkout owned by a different user (a uid change, an earlier clone by another user, etc.) would make the git task below fail on ownership or permissions.
  35. - name: Ensure Synapse repository ownership is correct on self-build
  36. ansible.builtin.file:
  37. path: "{{ matrix_synapse_container_src_files_path }}"
  38. state: directory
  39. owner: "{{ matrix_synapse_uid }}"
  40. group: "{{ matrix_synapse_gid }}"
  41. recurse: true
  42. - name: Ensure Synapse repository is present on self-build
  43. ansible.builtin.git:
  44. repo: "{{ matrix_synapse_container_image_self_build_repo }}"
  45. dest: "{{ matrix_synapse_container_src_files_path }}"
  46. version: "{{ matrix_synapse_container_image.split(':')[1] }}"
  47. force: "yes"
  48. become: true
  49. become_user: "{{ matrix_synapse_username }}"
  50. # Keep this even though the task above normalizes ownership. Git may still see an ownership mismatch when Ansible becomes an unprivileged user (see #5065).
  51. environment:
  52. GIT_CONFIG_COUNT: "1"
  53. GIT_CONFIG_KEY_0: safe.directory
  54. GIT_CONFIG_VALUE_0: "{{ matrix_synapse_container_src_files_path }}"
  55. register: matrix_synapse_git_pull_results
  56. - name: Check if Synapse Docker image exists
  57. ansible.builtin.command: "{{ devture_systemd_docker_base_host_command_docker }} images --quiet --filter 'reference={{ matrix_synapse_container_image }}'"
  58. register: matrix_synapse_container_image_check_result
  59. changed_when: false
  60. # Invoking the `docker build` command here, instead of calling the `docker_image` Ansible module,
  61. # because the latter does not support BuildKit.
  62. # See: https://github.com/ansible-collections/community.general/issues/514
  63. - name: Ensure Synapse Docker image is built
  64. ansible.builtin.shell:
  65. chdir: "{{ matrix_synapse_container_src_files_path }}"
  66. cmd: |
  67. {{ devture_systemd_docker_base_host_command_docker }} build \
  68. -t "{{ matrix_synapse_container_image }}" \
  69. -f docker/Dockerfile \
  70. .
  71. environment:
  72. DOCKER_BUILDKIT: 1
  73. changed_when: true
  74. when: "matrix_synapse_git_pull_results.changed | bool or matrix_synapse_container_image_check_result.stdout == ''"
  75. - name: Ensure Synapse Docker image is pulled
  76. community.docker.docker_image_pull:
  77. name: "{{ matrix_synapse_container_image }}"
  78. pull: always
  79. when: "not matrix_synapse_container_image_self_build"
  80. register: result
  81. retries: "{{ devture_playbook_help_container_retries_count }}"
  82. delay: "{{ devture_playbook_help_container_retries_delay }}"
  83. until: result is not failed
  84. - when: "matrix_synapse_container_image_customizations_enabled | bool"
  85. block:
  86. - name: Ensure customizations Dockerfile is created
  87. ansible.builtin.template:
  88. src: "{{ role_path }}/templates/synapse/customizations/Dockerfile.j2"
  89. dest: "{{ matrix_synapse_customized_container_src_files_path }}/Dockerfile"
  90. owner: "{{ matrix_synapse_uid }}"
  91. group: "{{ matrix_synapse_gid }}"
  92. mode: '0640'
  93. register: matrix_synapse_container_image_customizations_dockerfile_result
  94. - name: Ensure customized Docker image for Synapse is built
  95. community.docker.docker_image_build:
  96. name: "{{ matrix_synapse_container_image_customized }}"
  97. dockerfile: Dockerfile
  98. path: "{{ matrix_synapse_customized_container_src_files_path }}"
  99. pull: false
  100. nocache: "{{ matrix_synapse_container_image_customized_build_nocache }}"
  101. rebuild: "{{ 'always' if (matrix_synapse_container_image_customizations_dockerfile_result.changed | bool or matrix_synapse_container_image_customized_force_source | bool) else 'never' }}"
  102. register: matrix_synapse_container_image_customized_build_result
  103. # We do this so that the signing key would get generated.
  104. #
  105. # This will also generate a default homeserver.yaml configuration file and a log configuration file.
  106. # We don't care about those configuration files, as we replace them with our own anyway (see below).
  107. #
  108. # We don't use the `docker_container` module, because using it with `cap_drop` requires
  109. # a very recent docker-py version, which is not available for a lot of people yet.
  110. - name: Generate initial Synapse config and signing key
  111. ansible.builtin.command:
  112. cmd: |
  113. {{ devture_systemd_docker_base_host_command_docker }} run
  114. --rm
  115. --name=matrix-config
  116. --user={{ matrix_synapse_uid }}:{{ matrix_synapse_gid }}
  117. --cap-drop=ALL
  118. --mount type=bind,src={{ matrix_synapse_config_dir_path }},dst=/data
  119. -e SYNAPSE_CONFIG_PATH=/data/homeserver.yaml
  120. -e SYNAPSE_SERVER_NAME={{ matrix_server_fqn_matrix }}
  121. -e SYNAPSE_REPORT_STATS=no
  122. {{ matrix_synapse_container_image }}
  123. generate
  124. creates: "{{ matrix_synapse_config_dir_path }}/{{ matrix_server_fqn_matrix }}.signing.key"
  125. - name: Ensure Synapse homeserver config installed
  126. ansible.builtin.copy:
  127. content: "{{ matrix_synapse_configuration | to_nice_yaml(indent=2, width=999999) }}"
  128. dest: "{{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  129. mode: '0644'
  130. owner: "{{ matrix_synapse_uid }}"
  131. group: "{{ matrix_synapse_gid }}"
  132. - name: Ensure Synapse container network is created
  133. when: matrix_synapse_container_network != 'host'
  134. community.general.docker_network:
  135. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  136. name: "{{ matrix_synapse_container_network }}"
  137. driver: bridge
  138. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  139. - name: Ensure Synapse support files installed
  140. ansible.builtin.template:
  141. src: "{{ item.src }}"
  142. dest: "{{ item.dest }}"
  143. mode: "{{ item.mode }}"
  144. with_items:
  145. - src: "{{ matrix_synapse_template_synapse_log }}"
  146. dest: "{{ matrix_synapse_config_dir_path }}/{{ matrix_server_fqn_matrix }}.log.config"
  147. mode: '0644'
  148. - src: "{{ role_path }}/templates/synapse/bin/register-user.j2"
  149. dest: "{{ matrix_synapse_bin_path }}/register-user"
  150. mode: '0755'
  151. - src: "{{ role_path }}/templates/synapse/labels.j2"
  152. dest: "{{ matrix_synapse_base_path }}/labels"
  153. mode: '0644'
  154. - src: "{{ role_path }}/templates/synapse/systemd/matrix-synapse.service.j2"
  155. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-synapse.service"
  156. mode: '0644'
  157. - name: Generate sample prometheus.yml for external scraping
  158. ansible.builtin.template:
  159. src: "{{ role_path }}/templates/synapse/prometheus/external_prometheus.yml.example.j2"
  160. dest: "{{ matrix_synapse_base_path }}/external_prometheus.yml.example"
  161. owner: "{{ matrix_synapse_uid }}"
  162. group: "{{ matrix_synapse_gid }}"
  163. mode: '0644'
  164. when: matrix_synapse_metrics_proxying_enabled | bool