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.
 
 

169 lines
7.9 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:
  46. name: "{{ matrix_media_repo_container_image }}"
  47. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  48. force_source: "{{ matrix_media_repo_container_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  49. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_media_repo_container_image_force_pull }}"
  50. when: "not matrix_media_repo_container_image_self_build | bool"
  51. register: matrix_media_repo_container_image_pull_result
  52. retries: "{{ devture_playbook_help_container_retries_count }}"
  53. delay: "{{ devture_playbook_help_container_retries_delay }}"
  54. until: matrix_media_repo_container_image_pull_result is not failed
  55. - when: "matrix_media_repo_container_image_self_build | bool"
  56. block:
  57. - name: Ensure media-repo repository is present on self-build
  58. ansible.builtin.git:
  59. repo: "{{ matrix_media_repo_container_image_self_build_repo }}"
  60. dest: "{{ matrix_media_repo_container_src_files_path }}"
  61. version: "{{ matrix_media_repo_container_image.split(':')[1] }}"
  62. force: "yes"
  63. become: true
  64. become_user: "{{ matrix_user_name }}"
  65. register: matrix_media_repo_git_pull_results
  66. - name: Check if media-repo Docker image exists
  67. ansible.builtin.command: "{{ devture_systemd_docker_base_host_command_docker }} images --quiet --filter 'reference={{ matrix_media_repo_container_image }}'"
  68. register: matrix_media_repo_container_image_check_result
  69. changed_when: false
  70. # Invoking the `docker build` command here, instead of calling the `docker_image` Ansible module,
  71. # because the latter does not support BuildKit.
  72. # See: https://github.com/ansible-collections/community.general/issues/514
  73. - name: Ensure media-repo Docker image is built
  74. ansible.builtin.command:
  75. cmd: "{{ devture_systemd_docker_base_host_command_docker }} build -t {{ matrix_media_repo_container_image }} {{ matrix_media_repo_container_src_files_path }}"
  76. environment:
  77. DOCKER_BUILDKIT: 1
  78. changed_when: true
  79. when: "matrix_media_repo_git_pull_results.changed | bool or matrix_media_repo_container_image_check_result.stdout == ''"
  80. - name: Check existence of media-repo signing key
  81. ansible.builtin.stat:
  82. path: "{{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key"
  83. register: matrix_media_repo_signing_key_stat
  84. - when: "matrix_media_repo_generate_signing_key | bool and not (matrix_media_repo_signing_key_stat.stat.exists | bool)"
  85. block:
  86. - name: Generate media-repo signing key
  87. ansible.builtin.command:
  88. cmd: |
  89. {{ devture_systemd_docker_base_host_command_docker }} run
  90. --rm
  91. --name={{ matrix_media_repo_identifier }}-temp
  92. --user={{ matrix_synapse_uid }}:{{ matrix_synapse_gid }}
  93. --cap-drop=ALL
  94. --mount type=bind,src={{ matrix_media_repo_config_path }},dst=/config
  95. --workdir='/config'
  96. --entrypoint='generate_signing_key'
  97. {{ matrix_media_repo_container_image }}
  98. -output {{ matrix_media_repo_identifier }}.signing.key.TEMP
  99. creates: "{{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key.TEMP"
  100. - name: Merge media-repo signing key with homeserver signing key
  101. ansible.builtin.command:
  102. cmd: |
  103. {{ devture_systemd_docker_base_host_command_docker }} run
  104. --rm
  105. --name={{ matrix_media_repo_identifier }}-temp
  106. --user={{ matrix_synapse_uid }}:{{ matrix_synapse_gid }}
  107. --cap-drop=ALL
  108. --mount type=bind,src={{ matrix_media_repo_config_path }},dst=/config
  109. --mount type=bind,src={{ matrix_media_repo_homeserver_signing_key | dirname }},dst=/homeserver-signing-key-dir
  110. --workdir='/config'
  111. --entrypoint='combine_signing_keys'
  112. {{ matrix_media_repo_container_image }}
  113. -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
  114. creates: "{{ matrix_media_repo_homeserver_signing_key }}.merged"
  115. - name: Backup existing homeserver signing key before replacing it
  116. ansible.builtin.copy:
  117. remote_src: true
  118. src: "{{ matrix_media_repo_homeserver_signing_key }}"
  119. dest: "{{ matrix_media_repo_homeserver_signing_key }}.{{ matrix_homeserver_implementation }}.backup"
  120. mode: '0644'
  121. owner: "{{ matrix_user_name }}"
  122. group: "{{ matrix_group_name }}"
  123. - name: Replace homeserver signing key with merged signing key
  124. ansible.builtin.command:
  125. cmd: "mv {{ matrix_media_repo_homeserver_signing_key }}.merged {{ matrix_media_repo_homeserver_signing_key }}"
  126. removes: "{{ matrix_media_repo_homeserver_signing_key }}.merged"
  127. - name: Finalize media-repo signing key setup
  128. ansible.builtin.command:
  129. 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"
  130. removes: "{{ matrix_media_repo_config_path }}/{{ matrix_media_repo_identifier }}.signing.key.TEMP"
  131. - name: Ensure media-repo container network is created
  132. community.general.docker_network:
  133. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  134. name: "{{ matrix_media_repo_container_network }}"
  135. driver: bridge
  136. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  137. - name: Ensure media-repo service installed
  138. ansible.builtin.template:
  139. src: "{{ role_path }}/templates/media-repo/systemd/matrix-media-repo.service.j2"
  140. dest: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_media_repo_identifier }}.service"
  141. mode: '0640'
  142. register: matrix_media_repo_systemd_service_result
  143. - name: Determine whether media-repo needs a restart
  144. ansible.builtin.set_fact:
  145. matrix_media_repo_restart_necessary: >-
  146. {{
  147. matrix_media_repo_config_result.changed | default(false)
  148. or matrix_media_repo_support_files_result.changed | default(false)
  149. or matrix_media_repo_systemd_service_result.changed | default(false)
  150. or matrix_media_repo_container_image_pull_result.changed | default(false)
  151. }}