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.
 
 

177 lines
8.1 KiB

  1. # SPDX-FileCopyrightText: 2023 - 2024 Michael Hollister
  2. # SPDX-FileCopyrightText: 2024 Daniel A. Maierhofer
  3. # SPDX-FileCopyrightText: 2024 David Mehren
  4. # SPDX-FileCopyrightText: 2024 Slavi Pantaleev
  5. #
  6. # SPDX-License-Identifier: AGPL-3.0-or-later
  7. ---
  8. - name: Ensure media-repo paths exist
  9. ansible.builtin.file:
  10. path: "{{ item.path }}"
  11. state: directory
  12. mode: '0750'
  13. owner: "{{ matrix_user_name }}"
  14. group: "{{ matrix_group_name }}"
  15. with_items:
  16. - path: "{{ matrix_media_repo_base_path }}"
  17. when: true
  18. - path: "{{ matrix_media_repo_config_path }}"
  19. when: true
  20. - path: "{{ matrix_media_repo_data_path }}"
  21. when: true
  22. - path: "{{ matrix_media_repo_container_src_files_path }}"
  23. when: "{{ matrix_media_repo_container_image_self_build }}"
  24. when: "item.when | bool"
  25. - name: Ensure media-repo support files installed
  26. ansible.builtin.template:
  27. src: "{{ role_path }}/templates/media-repo/{{ item }}.j2"
  28. dest: "{{ matrix_media_repo_base_path }}/{{ item }}"
  29. mode: '0640'
  30. owner: "{{ matrix_user_name }}"
  31. group: "{{ matrix_group_name }}"
  32. with_items:
  33. - env
  34. - labels
  35. register: matrix_media_repo_support_files_result
  36. - name: Ensure media-repo configuration installed
  37. ansible.builtin.template:
  38. src: "{{ role_path }}/templates/media-repo/media-repo.yaml.j2"
  39. dest: "{{ matrix_media_repo_config_path }}/media-repo.yaml"
  40. mode: '0640'
  41. owner: "{{ matrix_user_name }}"
  42. group: "{{ matrix_group_name }}"
  43. register: matrix_media_repo_config_result
  44. - name: Ensure media-repo Docker image is pulled
  45. community.docker.docker_image_pull:
  46. name: "{{ matrix_media_repo_container_image }}"
  47. pull: always
  48. when: "not matrix_media_repo_container_image_self_build | bool"
  49. register: matrix_media_repo_container_image_pull_result
  50. retries: "{{ devture_playbook_help_container_retries_count }}"
  51. delay: "{{ devture_playbook_help_container_retries_delay }}"
  52. until: matrix_media_repo_container_image_pull_result is not failed
  53. - when: "matrix_media_repo_container_image_self_build | bool"
  54. block:
  55. # 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.
  56. - name: Ensure media-repo repository ownership is correct on self-build
  57. ansible.builtin.file:
  58. path: "{{ matrix_media_repo_container_src_files_path }}"
  59. state: directory
  60. owner: "{{ matrix_user_name }}"
  61. group: "{{ matrix_group_name }}"
  62. recurse: true
  63. - name: Ensure media-repo repository is present on self-build
  64. ansible.builtin.git:
  65. repo: "{{ matrix_media_repo_container_image_self_build_repo }}"
  66. dest: "{{ matrix_media_repo_container_src_files_path }}"
  67. version: "{{ matrix_media_repo_container_image.split(':')[1] }}"
  68. force: "yes"
  69. become: true
  70. become_user: "{{ matrix_user_name }}"
  71. register: matrix_media_repo_git_pull_results
  72. - name: Check if media-repo Docker image exists
  73. ansible.builtin.command: "{{ devture_systemd_docker_base_host_command_docker }} images --quiet --filter 'reference={{ matrix_media_repo_container_image }}'"
  74. register: matrix_media_repo_container_image_check_result
  75. changed_when: false
  76. # Invoking the `docker build` command here, instead of calling the `docker_image` Ansible module,
  77. # because the latter does not support BuildKit.
  78. # See: https://github.com/ansible-collections/community.general/issues/514
  79. - name: Ensure media-repo Docker image is built
  80. ansible.builtin.command:
  81. cmd: "{{ devture_systemd_docker_base_host_command_docker }} build -t {{ matrix_media_repo_container_image }} {{ matrix_media_repo_container_src_files_path }}"
  82. environment:
  83. DOCKER_BUILDKIT: 1
  84. changed_when: true
  85. when: "matrix_media_repo_git_pull_results.changed | bool or matrix_media_repo_container_image_check_result.stdout == ''"
  86. - name: Check existence of media-repo signing key
  87. ansible.builtin.stat:
  88. path: "{{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key"
  89. register: matrix_media_repo_signing_key_stat
  90. - when: "matrix_media_repo_generate_signing_key | bool and not (matrix_media_repo_signing_key_stat.stat.exists | bool)"
  91. block:
  92. - name: Generate media-repo signing key
  93. ansible.builtin.command:
  94. cmd: |
  95. {{ devture_systemd_docker_base_host_command_docker }} run
  96. --rm
  97. --name={{ matrix_media_repo_identifier }}-temp
  98. --user={{ matrix_synapse_uid }}:{{ matrix_synapse_gid }}
  99. --cap-drop=ALL
  100. --mount type=bind,src={{ matrix_media_repo_config_path }},dst=/config
  101. --workdir='/config'
  102. --entrypoint='generate_signing_key'
  103. {{ matrix_media_repo_container_image }}
  104. -output {{ matrix_media_repo_identifier }}.signing.key.TEMP
  105. creates: "{{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key.TEMP"
  106. - name: Merge media-repo signing key with homeserver signing key
  107. ansible.builtin.command:
  108. cmd: |
  109. {{ devture_systemd_docker_base_host_command_docker }} run
  110. --rm
  111. --name={{ matrix_media_repo_identifier }}-temp
  112. --user={{ matrix_synapse_uid }}:{{ matrix_synapse_gid }}
  113. --cap-drop=ALL
  114. --mount type=bind,src={{ matrix_media_repo_config_path }},dst=/config
  115. --mount type=bind,src={{ matrix_media_repo_homeserver_signing_key | dirname }},dst=/homeserver-signing-key-dir
  116. --workdir='/config'
  117. --entrypoint='combine_signing_keys'
  118. {{ matrix_media_repo_container_image }}
  119. -format {{ matrix_homeserver_implementation }} -output /homeserver-signing-key-dir/{{ matrix_media_repo_homeserver_signing_key | basename }}.merged /homeserver-signing-key-dir/{{ matrix_media_repo_homeserver_signing_key | basename }} {{ matrix_media_repo_identifier }}.signing.key.TEMP
  120. creates: "{{ matrix_media_repo_homeserver_signing_key }}.merged"
  121. - name: Backup existing homeserver signing key before replacing it
  122. ansible.builtin.copy:
  123. remote_src: true
  124. src: "{{ matrix_media_repo_homeserver_signing_key }}"
  125. dest: "{{ matrix_media_repo_homeserver_signing_key }}.{{ matrix_homeserver_implementation }}.backup"
  126. mode: '0644'
  127. owner: "{{ matrix_user_name }}"
  128. group: "{{ matrix_group_name }}"
  129. - name: Replace homeserver signing key with merged signing key
  130. ansible.builtin.command:
  131. cmd: "mv {{ matrix_media_repo_homeserver_signing_key }}.merged {{ matrix_media_repo_homeserver_signing_key }}"
  132. removes: "{{ matrix_media_repo_homeserver_signing_key }}.merged"
  133. - name: Finalize media-repo signing key setup
  134. ansible.builtin.command:
  135. cmd: "mv {{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key.TEMP {{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key"
  136. removes: "{{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key.TEMP"
  137. - name: Ensure media-repo container network is created
  138. when: matrix_media_repo_container_network != 'host'
  139. community.general.docker_network:
  140. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  141. name: "{{ matrix_media_repo_container_network }}"
  142. driver: bridge
  143. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  144. - name: Ensure media-repo service installed
  145. ansible.builtin.template:
  146. src: "{{ role_path }}/templates/media-repo/systemd/matrix-media-repo.service.j2"
  147. dest: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_media_repo_identifier }}.service"
  148. mode: '0640'
  149. register: matrix_media_repo_systemd_service_result
  150. - name: Determine whether media-repo needs a restart
  151. ansible.builtin.set_fact:
  152. matrix_media_repo_restart_necessary: >-
  153. {{
  154. matrix_media_repo_config_result.changed | default(false)
  155. or matrix_media_repo_support_files_result.changed | default(false)
  156. or matrix_media_repo_systemd_service_result.changed | default(false)
  157. or matrix_media_repo_container_image_pull_result.changed | default(false)
  158. }}