Matrix Docker Ansible eploy
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

143 строки
6.6 KiB

  1. # SPDX-FileCopyrightText: 2023 - 2024 MDAD project contributors
  2. # SPDX-FileCopyrightText: 2023 Catalan Lover <catalanlover@protonmail.com>
  3. # SPDX-FileCopyrightText: 2024 David Mehren
  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_bot_draupnir_migration_requires_restart: false
  11. - name: Ensure matrix-bot-draupnir 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_bot_draupnir_base_path }}", when: true}
  20. - {path: "{{ matrix_bot_draupnir_config_path }}", when: true}
  21. - {path: "{{ matrix_bot_draupnir_data_path }}", when: true}
  22. - {path: "{{ matrix_bot_draupnir_container_src_files_path }}", when: "{{ matrix_bot_draupnir_container_image_self_build }}"}
  23. when: "item.when | bool"
  24. - name: Ensure matrix-bot-draupnir support files installed
  25. ansible.builtin.template:
  26. src: "{{ item.src }}"
  27. dest: "{{ item.dest }}"
  28. owner: "{{ matrix_user_name }}"
  29. group: "{{ matrix_group_name }}"
  30. mode: '0644'
  31. with_items:
  32. - src: "{{ role_path }}/templates/labels.j2"
  33. dest: "{{ matrix_bot_draupnir_base_path }}/labels"
  34. register: matrix_bot_draupnir_support_files_result
  35. - name: Ensure Draupnir Docker image is pulled
  36. community.docker.docker_image_pull:
  37. name: "{{ matrix_bot_draupnir_container_image }}"
  38. pull: always
  39. when: "not matrix_bot_draupnir_container_image_self_build | bool"
  40. register: matrix_bot_draupnir_container_image_pull_result
  41. retries: "{{ devture_playbook_help_container_retries_count }}"
  42. delay: "{{ devture_playbook_help_container_retries_delay }}"
  43. until: matrix_bot_draupnir_container_image_pull_result is not failed
  44. # 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.
  45. - name: Ensure Draupnir repository ownership is correct on self-build
  46. ansible.builtin.file:
  47. path: "{{ matrix_bot_draupnir_container_src_files_path }}"
  48. state: directory
  49. owner: "{{ matrix_user_name }}"
  50. group: "{{ matrix_group_name }}"
  51. recurse: true
  52. when: "matrix_bot_draupnir_container_image_self_build | bool"
  53. - name: Ensure Draupnir repository is present on self-build
  54. ansible.builtin.git:
  55. repo: "{{ matrix_bot_draupnir_container_image_self_build_repo }}"
  56. dest: "{{ matrix_bot_draupnir_container_src_files_path }}"
  57. version: "{{ matrix_bot_draupnir_container_image.split(':')[1] }}"
  58. force: "yes"
  59. become: true
  60. become_user: "{{ matrix_user_name }}"
  61. # Keep this even though the task above normalizes ownership. Git may still see an ownership mismatch when Ansible becomes an unprivileged user (see #5065).
  62. environment:
  63. GIT_CONFIG_COUNT: "1"
  64. GIT_CONFIG_KEY_0: safe.directory
  65. GIT_CONFIG_VALUE_0: "{{ matrix_bot_draupnir_container_src_files_path }}"
  66. register: matrix_bot_draupnir_git_pull_results
  67. when: "matrix_bot_draupnir_container_image_self_build | bool"
  68. - name: Ensure Draupnir Docker image is built
  69. # Using docker_image_build with BuildKit for modern, efficient builds.
  70. # Rebuild when the git checkout advanced to a new commit; otherwise keep the build idempotent.
  71. # Technically the idempotency of rebuilds is more that if a build has already been executed for that name:tag
  72. # then we won't rebuild while in idempotent mode even if git moved. That's what the force rebuild logic is for.
  73. community.docker.docker_image_build:
  74. name: "{{ matrix_bot_draupnir_container_image }}"
  75. dockerfile: Dockerfile
  76. path: "{{ matrix_bot_draupnir_container_src_files_path }}"
  77. pull: true
  78. rebuild: "{{ 'always' if matrix_bot_draupnir_git_pull_results.changed | bool else 'never' }}"
  79. when: "matrix_bot_draupnir_container_image_self_build | bool"
  80. register: matrix_bot_draupnir_container_image_build_result
  81. - name: Ensure matrix-bot-draupnir config installed
  82. ansible.builtin.copy:
  83. content: "{{ matrix_bot_draupnir_configuration | to_nice_yaml(indent=2, width=999999) }}"
  84. dest: "{{ matrix_bot_draupnir_config_path }}/production.yaml"
  85. mode: '0644'
  86. owner: "{{ matrix_user_name }}"
  87. group: "{{ matrix_group_name }}"
  88. register: matrix_bot_draupnir_config_result
  89. - name: Ensure matrix-bot-draupnir container network is created
  90. when: matrix_bot_draupnir_container_network != 'host'
  91. community.general.docker_network:
  92. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  93. name: "{{ matrix_bot_draupnir_container_network }}"
  94. driver: bridge
  95. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  96. - name: Ensure matrix-bot-draupnir.service installed
  97. ansible.builtin.template:
  98. src: "{{ role_path }}/templates/systemd/matrix-bot-draupnir.service.j2"
  99. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-bot-draupnir.service"
  100. mode: '0644'
  101. register: matrix_bot_draupnir_systemd_service_result
  102. # matrix-bot-draupnir and matrix-appservice-draupnir-for-all share the
  103. # same upstream container image. When both are enabled and force-pull is
  104. # on, the second role to run sees the image as already up-to-date (the
  105. # first role just pulled it), so its pull_result.changed is false and
  106. # conditional restart would skip it. To avoid that, we also treat
  107. # force-pull itself as a restart trigger for this role. The downside is
  108. # that both Draupnir services restart on every run when force-pull is
  109. # enabled (e.g. with rolling tags like `latest` or `main`), even when the
  110. # upstream image has not moved. That is wasteful but acceptable.
  111. # See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/5186
  112. - name: Determine whether Draupnir needs a restart
  113. ansible.builtin.set_fact:
  114. matrix_bot_draupnir_restart_necessary: >-
  115. {{
  116. matrix_bot_draupnir_migration_requires_restart | default(false)
  117. or matrix_bot_draupnir_support_files_result.changed | default(false)
  118. or matrix_bot_draupnir_config_result.changed | default(false)
  119. or matrix_bot_draupnir_systemd_service_result.changed | default(false)
  120. or matrix_bot_draupnir_container_image_pull_result.changed | default(false)
  121. or matrix_bot_draupnir_container_image_build_result.changed | default(false)
  122. or matrix_bot_draupnir_force_restart | bool
  123. }}
  124. - name: Ensure matrix-bot-draupnir.service restarted, if necessary
  125. ansible.builtin.service:
  126. name: "matrix-bot-draupnir.service"
  127. state: restarted
  128. daemon_reload: true
  129. when: "matrix_bot_draupnir_migration_requires_restart | bool"