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

138 строки
6.3 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. register: matrix_bot_draupnir_git_pull_results
  62. when: "matrix_bot_draupnir_container_image_self_build | bool"
  63. - name: Ensure Draupnir Docker image is built
  64. # Using docker_image_build with BuildKit for modern, efficient builds.
  65. # Rebuild when the git checkout advanced to a new commit; otherwise keep the build idempotent.
  66. # Technically the idempotency of rebuilds is more that if a build has already been executed for that name:tag
  67. # then we won't rebuild while in idempotent mode even if git moved. That's what the force rebuild logic is for.
  68. community.docker.docker_image_build:
  69. name: "{{ matrix_bot_draupnir_container_image }}"
  70. dockerfile: Dockerfile
  71. path: "{{ matrix_bot_draupnir_container_src_files_path }}"
  72. pull: true
  73. rebuild: "{{ 'always' if matrix_bot_draupnir_git_pull_results.changed | bool else 'never' }}"
  74. when: "matrix_bot_draupnir_container_image_self_build | bool"
  75. register: matrix_bot_draupnir_container_image_build_result
  76. - name: Ensure matrix-bot-draupnir config installed
  77. ansible.builtin.copy:
  78. content: "{{ matrix_bot_draupnir_configuration | to_nice_yaml(indent=2, width=999999) }}"
  79. dest: "{{ matrix_bot_draupnir_config_path }}/production.yaml"
  80. mode: '0644'
  81. owner: "{{ matrix_user_name }}"
  82. group: "{{ matrix_group_name }}"
  83. register: matrix_bot_draupnir_config_result
  84. - name: Ensure matrix-bot-draupnir container network is created
  85. when: matrix_bot_draupnir_container_network != 'host'
  86. community.general.docker_network:
  87. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  88. name: "{{ matrix_bot_draupnir_container_network }}"
  89. driver: bridge
  90. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  91. - name: Ensure matrix-bot-draupnir.service installed
  92. ansible.builtin.template:
  93. src: "{{ role_path }}/templates/systemd/matrix-bot-draupnir.service.j2"
  94. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-bot-draupnir.service"
  95. mode: '0644'
  96. register: matrix_bot_draupnir_systemd_service_result
  97. # matrix-bot-draupnir and matrix-appservice-draupnir-for-all share the
  98. # same upstream container image. When both are enabled and force-pull is
  99. # on, the second role to run sees the image as already up-to-date (the
  100. # first role just pulled it), so its pull_result.changed is false and
  101. # conditional restart would skip it. To avoid that, we also treat
  102. # force-pull itself as a restart trigger for this role. The downside is
  103. # that both Draupnir services restart on every run when force-pull is
  104. # enabled (e.g. with rolling tags like `latest` or `main`), even when the
  105. # upstream image has not moved. That is wasteful but acceptable.
  106. # See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/5186
  107. - name: Determine whether Draupnir needs a restart
  108. ansible.builtin.set_fact:
  109. matrix_bot_draupnir_restart_necessary: >-
  110. {{
  111. matrix_bot_draupnir_migration_requires_restart | default(false)
  112. or matrix_bot_draupnir_support_files_result.changed | default(false)
  113. or matrix_bot_draupnir_config_result.changed | default(false)
  114. or matrix_bot_draupnir_systemd_service_result.changed | default(false)
  115. or matrix_bot_draupnir_container_image_pull_result.changed | default(false)
  116. or matrix_bot_draupnir_container_image_build_result.changed | default(false)
  117. or matrix_bot_draupnir_force_restart | bool
  118. }}
  119. - name: Ensure matrix-bot-draupnir.service restarted, if necessary
  120. ansible.builtin.service:
  121. name: "matrix-bot-draupnir.service"
  122. state: restarted
  123. daemon_reload: true
  124. when: "matrix_bot_draupnir_migration_requires_restart | bool"