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

224 строки
9.8 KiB

  1. # SPDX-FileCopyrightText: 2019 - 2022 MDAD project contributors
  2. # SPDX-FileCopyrightText: 2019 - 2024 Slavi Pantaleev
  3. # SPDX-FileCopyrightText: 2019 Dan Arnfield
  4. # SPDX-FileCopyrightText: 2020 Chris van Dijk
  5. # SPDX-FileCopyrightText: 2021 Panagiotis Georgiadis
  6. # SPDX-FileCopyrightText: 2022 Jim Myhrberg
  7. # SPDX-FileCopyrightText: 2022 Marko Weltzer
  8. # SPDX-FileCopyrightText: 2022 Nikita Chernyi
  9. # SPDX-FileCopyrightText: 2022 Sebastian Gumprich
  10. # SPDX-FileCopyrightText: 2024 David Mehren
  11. #
  12. # SPDX-License-Identifier: AGPL-3.0-or-later
  13. ---
  14. - ansible.builtin.include_role:
  15. name: custom/matrix-base
  16. tasks_from: ensure_openssl_installed
  17. - name: Ensure Appservice IRC paths exist
  18. ansible.builtin.file:
  19. path: "{{ item.path }}"
  20. state: directory
  21. mode: 0750
  22. owner: "{{ matrix_user_name }}"
  23. group: "{{ matrix_group_name }}"
  24. with_items:
  25. - {path: "{{ matrix_appservice_irc_base_path }}", when: true}
  26. - {path: "{{ matrix_appservice_irc_config_path }}", when: true}
  27. - {path: "{{ matrix_appservice_irc_data_path }}", when: true}
  28. - {path: "{{ matrix_appservice_irc_docker_src_files_path }}", when: "{{ matrix_appservice_irc_container_image_self_build }}"}
  29. when: item.when | bool
  30. - name: Check if an old passkey file already exists
  31. ansible.builtin.stat:
  32. path: "{{ matrix_appservice_irc_base_path }}/passkey.pem"
  33. register: matrix_appservice_irc_stat_passkey
  34. - when: "matrix_appservice_irc_stat_passkey.stat.exists"
  35. block:
  36. - name: (Data relocation) Ensure matrix-appservice-irc.service is stopped
  37. ansible.builtin.service:
  38. name: matrix-appservice-irc
  39. state: stopped
  40. daemon_reload: true
  41. failed_when: false
  42. - name: (Data relocation) Move AppService IRC passkey.pem file to ./data directory
  43. ansible.builtin.command:
  44. cmd: "mv {{ matrix_appservice_irc_base_path }}/passkey.pem {{ matrix_appservice_irc_data_path }}/passkey.pem"
  45. register: matrix_appservice_irc_move_passkey_result
  46. changed_when: matrix_appservice_irc_move_passkey_result.rc == 0
  47. - name: (Data relocation) Move AppService IRC database files to ./data directory
  48. ansible.builtin.command:
  49. cmd: "mv {{ matrix_appservice_irc_base_path }}/{{ item }} {{ matrix_appservice_irc_data_path }}/{{ item }}"
  50. register: matrix_appservice_irc_move_dbs_result
  51. changed_when: matrix_appservice_irc_move_dbs_result.rc == 0
  52. with_items:
  53. - rooms.db
  54. - users.db
  55. failed_when: false
  56. - ansible.builtin.set_fact:
  57. matrix_appservice_irc_requires_restart: false
  58. - when: "matrix_appservice_irc_database_engine == 'postgres'"
  59. block:
  60. - name: Check if a nedb database already exists
  61. ansible.builtin.stat:
  62. path: "{{ matrix_appservice_irc_data_path }}/users.db"
  63. register: matrix_appservice_irc_nedb_database_path_local_stat_result
  64. - when: "matrix_appservice_irc_nedb_database_path_local_stat_result.stat.exists | bool"
  65. block:
  66. - ansible.builtin.include_tasks: "{{ role_path }}/tasks/migrate_nedb_to_postgres.yml"
  67. - ansible.builtin.set_fact:
  68. matrix_appservice_irc_requires_restart: true
  69. - name: Ensure Appservice IRC image is pulled
  70. community.docker.docker_image:
  71. name: "{{ matrix_appservice_irc_docker_image }}"
  72. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  73. force_source: "{{ matrix_appservice_irc_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  74. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_irc_docker_image_force_pull }}"
  75. when: "matrix_appservice_irc_enabled | bool and not matrix_appservice_irc_container_image_self_build | bool"
  76. register: result
  77. retries: "{{ devture_playbook_help_container_retries_count }}"
  78. delay: "{{ devture_playbook_help_container_retries_delay }}"
  79. until: result is not failed
  80. - name: Ensure matrix-appservice-irc repository is present when self-building
  81. ansible.builtin.git:
  82. repo: "{{ matrix_appservice_irc_docker_repo }}"
  83. version: "{{ matrix_appservice_irc_docker_repo_version }}"
  84. dest: "{{ matrix_appservice_irc_docker_src_files_path }}"
  85. force: "yes"
  86. become: true
  87. become_user: "{{ matrix_user_name }}"
  88. register: matrix_appservice_irc_git_pull_results
  89. when: "matrix_appservice_irc_enabled | bool and matrix_appservice_irc_container_image_self_build | bool"
  90. - name: Ensure matrix-appservice-irc Docker image is built
  91. community.docker.docker_image:
  92. name: "{{ matrix_appservice_irc_docker_image }}"
  93. source: build
  94. force_source: "{{ matrix_appservice_irc_git_pull_results.changed if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  95. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_irc_git_pull_results.changed }}"
  96. build:
  97. dockerfile: Dockerfile
  98. path: "{{ matrix_appservice_irc_docker_src_files_path }}"
  99. pull: true
  100. when: "matrix_appservice_irc_enabled | bool and matrix_appservice_irc_container_image_self_build | bool and matrix_appservice_irc_git_pull_results.changed"
  101. - name: Ensure Matrix Appservice IRC config installed
  102. ansible.builtin.copy:
  103. content: "{{ matrix_appservice_irc_configuration | to_nice_yaml(indent=2, width=999999) }}"
  104. dest: "{{ matrix_appservice_irc_config_path }}/config.yaml"
  105. mode: 0644
  106. owner: "{{ matrix_user_name }}"
  107. group: "{{ matrix_group_name }}"
  108. - name: Generate Appservice IRC passkey if it doesn't exist
  109. ansible.builtin.shell:
  110. cmd: "{{ matrix_host_command_openssl }} genpkey -out {{ matrix_appservice_irc_data_path }}/passkey.pem -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048"
  111. creates: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  112. become: true
  113. become_user: "{{ matrix_user_name }}"
  114. # In the past, we used to generate the passkey.pem file with root, so permissions may not be okay.
  115. # Fix it.
  116. - name: (Migration) Ensure Appservice IRC passkey permissions are okay
  117. ansible.builtin.file:
  118. path: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  119. mode: 0644
  120. owner: "{{ matrix_user_name }}"
  121. group: "{{ matrix_group_name }}"
  122. # Ideally, we'd like to generate the final registration.yaml file by ourselves.
  123. #
  124. # However, the IRC bridge supports multiple servers, which leads to multiple
  125. # users/aliases/rooms rules in the registration file.
  126. #
  127. # Generating a proper file by ourselves is complicated and may lead to deviation
  128. # from what the bridge is doing.
  129. #
  130. # Instead, we do another hacky thing - asking the bridge to generate a template,
  131. # and then we parse it and fix it up with our own AS/HS token.
  132. # We need to do this, because:
  133. # - we'd like to have an up-to-date registration file
  134. # - we can achieve this by asking the bridge to rebuild it each time
  135. # - however, the bridge insists on regenerating all tokens each time
  136. # - .. which is not friendly for integrating with the homeserver
  137. #
  138. # So we have a hybrid approach. We ask the bridge to always generate
  139. # an up-to-date file, and we fix it up with some static values later on,
  140. # to produce a final registration.yaml file, as we desire.
  141. - name: Generate Appservice IRC registration-template.yaml
  142. ansible.builtin.shell: >-
  143. {{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-irc-gen
  144. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  145. --cap-drop=ALL
  146. --mount type=bind,src={{ matrix_appservice_irc_config_path }},dst=/config
  147. --mount type=bind,src={{ matrix_appservice_irc_data_path }},dst=/data
  148. --entrypoint=/bin/bash
  149. {{ matrix_appservice_irc_docker_image }}
  150. -c
  151. 'node app.js
  152. -r
  153. -f /config/registration-template.yaml
  154. -u "http://matrix-appservice-irc:9999"
  155. -c /config/config.yaml
  156. -l irc_bot'
  157. changed_when: false
  158. - name: Read Appservice IRC registration-template.yaml
  159. ansible.builtin.slurp:
  160. src: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  161. register: matrix_appservice_irc_registration_template_slurp
  162. - name: Remove unnecessary Appservice IRC registration-template.yaml
  163. ansible.builtin.file:
  164. path: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  165. state: absent
  166. changed_when: false
  167. - name: Parse registration-template.yaml
  168. ansible.builtin.set_fact:
  169. matrix_appservice_irc_registration_template: "{{ matrix_appservice_irc_registration_template_slurp['content'] | b64decode | from_yaml }}"
  170. - name: Combine registration-template.yaml and own registration override config
  171. ansible.builtin.set_fact:
  172. matrix_appservice_irc_registration: "{{ matrix_appservice_irc_registration_template | combine(matrix_appservice_irc_registration_override, recursive=True) }}"
  173. - name: Ensure Appservice IRC registration.yaml installed
  174. ansible.builtin.copy:
  175. content: "{{ matrix_appservice_irc_registration | to_nice_yaml(indent=2, width=999999) }}"
  176. dest: "{{ matrix_appservice_irc_config_path }}/registration.yaml"
  177. mode: 0644
  178. owner: "{{ matrix_user_name }}"
  179. group: "{{ matrix_group_name }}"
  180. - name: Ensure matrix-appservice-irc container network is created
  181. community.general.docker_network:
  182. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  183. name: "{{ matrix_appservice_irc_container_network }}"
  184. driver: bridge
  185. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  186. - name: Ensure matrix-appservice-irc.service installed
  187. ansible.builtin.template:
  188. src: "{{ role_path }}/templates/systemd/matrix-appservice-irc.service.j2"
  189. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-irc.service"
  190. mode: 0644
  191. register: matrix_appservice_irc_systemd_service_result
  192. - name: Ensure matrix-appservice-irc.service restarted, if necessary
  193. ansible.builtin.service:
  194. name: "matrix-appservice-irc.service"
  195. state: restarted
  196. daemon_reload: true
  197. when: "matrix_appservice_irc_requires_restart | bool"