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.
 
 
 

150 lines
7.9 KiB

  1. # SPDX-FileCopyrightText: 2024 David Mehren
  2. # SPDX-FileCopyrightText: 2024 MDAD project contributors
  3. # SPDX-FileCopyrightText: 2024 - 2026 Catalan Lover <catalanlover@protonmail.com>
  4. # SPDX-FileCopyrightText: 2024 Slavi Pantaleev
  5. # SPDX-FileCopyrightText: 2024 Suguru Hirahara
  6. #
  7. # SPDX-License-Identifier: AGPL-3.0-or-later
  8. ---
  9. - ansible.builtin.set_fact:
  10. matrix_appservice_draupnir_for_all_migration_requires_restart: false
  11. - name: Ensure matrix-appservice-draupnir-for-all paths exist
  12. ansible.builtin.file:
  13. path: "{{ item.path }}"
  14. state: directory
  15. mode: '0750'
  16. owner: "{{ matrix_user_name }}"
  17. group: "{{ matrix_group_name }}"
  18. with_items:
  19. - {path: "{{ matrix_appservice_draupnir_for_all_base_path }}", when: true}
  20. - {path: "{{ matrix_appservice_draupnir_for_all_config_path }}", when: true}
  21. - {path: "{{ matrix_appservice_draupnir_for_all_data_path }}", when: true}
  22. - {path: "{{ matrix_appservice_draupnir_for_all_container_src_files_path }}", when: "{{ matrix_appservice_draupnir_for_all_container_image_self_build }}"}
  23. when: "item.when | bool"
  24. - name: Ensure Draupnir Docker image is pulled
  25. community.docker.docker_image_pull:
  26. name: "{{ matrix_appservice_draupnir_for_all_container_image }}"
  27. pull: always
  28. when: "not matrix_appservice_draupnir_for_all_container_image_self_build | bool"
  29. register: matrix_appservice_draupnir_for_all_container_image_pull_result
  30. retries: "{{ devture_playbook_help_container_retries_count }}"
  31. delay: "{{ devture_playbook_help_container_retries_delay }}"
  32. until: matrix_appservice_draupnir_for_all_container_image_pull_result is not failed
  33. # 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.
  34. - name: Ensure Draupnir repository ownership is correct on self-build
  35. ansible.builtin.file:
  36. path: "{{ matrix_appservice_draupnir_for_all_container_src_files_path }}"
  37. state: directory
  38. owner: "{{ matrix_user_name }}"
  39. group: "{{ matrix_group_name }}"
  40. recurse: true
  41. when: "matrix_appservice_draupnir_for_all_container_image_self_build | bool"
  42. - name: Ensure Draupnir repository is present on self-build
  43. ansible.builtin.git:
  44. repo: "{{ matrix_appservice_draupnir_for_all_container_image_self_build_repo }}"
  45. dest: "{{ matrix_appservice_draupnir_for_all_container_src_files_path }}"
  46. version: "{{ matrix_appservice_draupnir_for_all_container_image.split(':')[1] }}"
  47. force: "yes"
  48. become: true
  49. become_user: "{{ matrix_user_name }}"
  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_appservice_draupnir_for_all_container_src_files_path }}"
  55. register: matrix_appservice_draupnir_for_all_git_pull_results
  56. when: "matrix_appservice_draupnir_for_all_container_image_self_build | bool"
  57. - name: Ensure Draupnir Docker image is built
  58. # Using docker_image_build with BuildKit for modern, efficient builds.
  59. # Rebuild when the git checkout advanced to a new commit; otherwise keep the build idempotent.
  60. # Technically the idempotency of rebuilds is more that if a build has already been executed for that name:tag
  61. # then we won't rebuild while in idempotent mode even if git moved. That's what the force rebuild logic is for.
  62. community.docker.docker_image_build:
  63. name: "{{ matrix_appservice_draupnir_for_all_container_image }}"
  64. dockerfile: Dockerfile
  65. path: "{{ matrix_appservice_draupnir_for_all_container_src_files_path }}"
  66. pull: true
  67. rebuild: "{{ 'always' if matrix_appservice_draupnir_for_all_git_pull_results.changed | bool else 'never' }}"
  68. when: "matrix_appservice_draupnir_for_all_container_image_self_build | bool"
  69. register: matrix_appservice_draupnir_for_all_container_image_build_result
  70. - name: Ensure matrix-appservice-draupnir-for-all appservice config installed
  71. ansible.builtin.copy:
  72. content: "{{ matrix_appservice_draupnir_for_all_configuration_appservice | to_nice_yaml(indent=2, width=999999) }}"
  73. dest: "{{ matrix_appservice_draupnir_for_all_config_path }}/production-appservice.yaml"
  74. mode: '0644'
  75. owner: "{{ matrix_user_name }}"
  76. group: "{{ matrix_group_name }}"
  77. register: matrix_appservice_draupnir_for_all_appservice_config_result
  78. - name: Ensure matrix-appservice-draupnir-for-all bot config installed
  79. ansible.builtin.copy:
  80. content: "{{ matrix_appservice_draupnir_for_all_configuration | to_nice_yaml(indent=2, width=999999) }}"
  81. dest: "{{ matrix_appservice_draupnir_for_all_config_path }}/production-bots.yaml"
  82. mode: '0644'
  83. owner: "{{ matrix_user_name }}"
  84. group: "{{ matrix_group_name }}"
  85. register: matrix_appservice_draupnir_for_all_bot_config_result
  86. - name: Ensure matrix-appservice-draupnir-for-all registration.yaml installed
  87. ansible.builtin.copy:
  88. content: "{{ matrix_appservice_draupnir_for_all_registration | to_nice_yaml(indent=2, width=999999) }}"
  89. dest: "{{ matrix_appservice_draupnir_for_all_config_path }}/draupnir-for-all-registration.yaml"
  90. mode: '0644'
  91. owner: "{{ matrix_user_name }}"
  92. group: "{{ matrix_group_name }}"
  93. register: matrix_appservice_draupnir_for_all_registration_config_result
  94. - name: Ensure matrix-appservice-draupnir-for-all container network is created
  95. when: matrix_appservice_draupnir_for_all_container_network != 'host'
  96. community.general.docker_network:
  97. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  98. name: "{{ matrix_appservice_draupnir_for_all_container_network }}"
  99. driver: bridge
  100. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  101. - name: Ensure matrix-appservice-draupnir-for-all.service installed
  102. ansible.builtin.template:
  103. src: "{{ role_path }}/templates/systemd/matrix-appservice-draupnir-for-all.service.j2"
  104. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-draupnir-for-all.service"
  105. mode: '0644'
  106. register: matrix_appservice_draupnir_for_all_systemd_service_result
  107. # matrix-appservice-draupnir-for-all and matrix-bot-draupnir share the
  108. # same upstream container image. When both are enabled and force-pull is
  109. # on, the second role to run sees the image as already up-to-date (the
  110. # first role just pulled it), so its pull_result.changed is false and
  111. # conditional restart would skip it. To avoid that, we also treat
  112. # force-pull itself as a restart trigger for this role. The downside is
  113. # that both Draupnir services restart on every run when force-pull is
  114. # enabled (e.g. with rolling tags like `latest` or `main`), even when the
  115. # upstream image has not moved. That is wasteful but acceptable.
  116. # See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/5186
  117. - name: Determine whether Draupnir for All needs a restart
  118. ansible.builtin.set_fact:
  119. matrix_appservice_draupnir_for_all_restart_necessary: >-
  120. {{
  121. matrix_appservice_draupnir_for_all_migration_requires_restart | default(false)
  122. or matrix_appservice_draupnir_for_all_appservice_config_result.changed | default(false)
  123. or matrix_appservice_draupnir_for_all_bot_config_result.changed | default(false)
  124. or matrix_appservice_draupnir_for_all_registration_config_result.changed | default(false)
  125. or matrix_appservice_draupnir_for_all_systemd_service_result.changed | default(false)
  126. or matrix_appservice_draupnir_for_all_container_image_pull_result.changed | default(false)
  127. or matrix_appservice_draupnir_for_all_container_image_build_result.changed | default(false)
  128. or matrix_appservice_draupnir_for_all_force_restart | bool
  129. }}
  130. - name: Ensure matrix-appservice-draupnir-for-all.service restarted, if necessary
  131. ansible.builtin.service:
  132. name: "matrix-appservice-draupnir-for-all.service"
  133. state: restarted
  134. daemon_reload: true
  135. when: "matrix_appservice_draupnir_for_all_migration_requires_restart | bool"