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

267 строки
12 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: Ensure Matrix Appservice IRC labels file installed
  109. ansible.builtin.template:
  110. src: "{{ role_path }}/templates/labels.j2"
  111. dest: "{{ matrix_appservice_irc_base_path }}/labels"
  112. mode: 0644
  113. owner: "{{ matrix_user_name }}"
  114. group: "{{ matrix_group_name }}"
  115. - name: Generate Appservice IRC passkey if it doesn't exist
  116. ansible.builtin.shell:
  117. cmd: "{{ matrix_host_command_openssl }} genpkey -out {{ matrix_appservice_irc_data_path }}/passkey.pem -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048"
  118. creates: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  119. become: true
  120. become_user: "{{ matrix_user_name }}"
  121. - name: Check if an authenticated media signing key exists
  122. ansible.builtin.stat:
  123. path: "{{ matrix_appservice_irc_data_path }}/auth-media.jwk"
  124. register: matrix_appservice_irc_stat_auth_media_key
  125. - when: not matrix_appservice_irc_stat_auth_media_key.stat.exists
  126. block:
  127. - name: Generate IRC appservice signing key for authenticated media
  128. community.docker.docker_container:
  129. name: "create-auth-media-jwk-key"
  130. image: "{{ matrix_appservice_irc_docker_image }}"
  131. cleanup: true
  132. network_mode: none
  133. entrypoint: "/usr/local/bin/node"
  134. command: >
  135. -e "const webcrypto = require('node:crypto');
  136. async function main() {
  137. const key = await webcrypto.subtle.generateKey({
  138. name: 'HMAC',
  139. hash: 'SHA-512',
  140. }, true, ['sign', 'verify']);
  141. console.log(JSON.stringify(await webcrypto.subtle.exportKey('jwk', key), undefined, 4));
  142. }
  143. main().then(() => process.exit(0)).catch(err => { throw err });"
  144. detach: false
  145. register: matrix_appservice_irc_jwk_result
  146. - name: Write auth media signing key to file
  147. ansible.builtin.copy:
  148. content: "{{ matrix_appservice_irc_jwk_result.container.Output }}"
  149. dest: "{{ matrix_appservice_irc_data_path }}/auth-media.jwk"
  150. mode: "0644"
  151. owner: "{{ matrix_user_name }}"
  152. group: "{{ matrix_group_name }}"
  153. # In the past, we used to generate the passkey.pem file with root, so permissions may not be okay.
  154. # Fix it.
  155. - name: (Migration) Ensure Appservice IRC passkey permissions are okay
  156. ansible.builtin.file:
  157. path: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  158. mode: 0644
  159. owner: "{{ matrix_user_name }}"
  160. group: "{{ matrix_group_name }}"
  161. # Ideally, we'd like to generate the final registration.yaml file by ourselves.
  162. #
  163. # However, the IRC bridge supports multiple servers, which leads to multiple
  164. # users/aliases/rooms rules in the registration file.
  165. #
  166. # Generating a proper file by ourselves is complicated and may lead to deviation
  167. # from what the bridge is doing.
  168. #
  169. # Instead, we do another hacky thing - asking the bridge to generate a template,
  170. # and then we parse it and fix it up with our own AS/HS token.
  171. # We need to do this, because:
  172. # - we'd like to have an up-to-date registration file
  173. # - we can achieve this by asking the bridge to rebuild it each time
  174. # - however, the bridge insists on regenerating all tokens each time
  175. # - .. which is not friendly for integrating with the homeserver
  176. #
  177. # So we have a hybrid approach. We ask the bridge to always generate
  178. # an up-to-date file, and we fix it up with some static values later on,
  179. # to produce a final registration.yaml file, as we desire.
  180. - name: Generate Appservice IRC registration-template.yaml
  181. ansible.builtin.shell: >-
  182. {{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-irc-gen
  183. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  184. --cap-drop=ALL
  185. --mount type=bind,src={{ matrix_appservice_irc_config_path }},dst=/config
  186. --mount type=bind,src={{ matrix_appservice_irc_data_path }},dst=/data
  187. --entrypoint=/bin/bash
  188. {{ matrix_appservice_irc_docker_image }}
  189. -c
  190. 'node app.js
  191. -r
  192. -f /config/registration-template.yaml
  193. -u "http://matrix-appservice-irc:9999"
  194. -c /config/config.yaml
  195. -l irc_bot'
  196. changed_when: false
  197. - name: Read Appservice IRC registration-template.yaml
  198. ansible.builtin.slurp:
  199. src: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  200. register: matrix_appservice_irc_registration_template_slurp
  201. - name: Remove unnecessary Appservice IRC registration-template.yaml
  202. ansible.builtin.file:
  203. path: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  204. state: absent
  205. changed_when: false
  206. - name: Parse registration-template.yaml
  207. ansible.builtin.set_fact:
  208. matrix_appservice_irc_registration_template: "{{ matrix_appservice_irc_registration_template_slurp['content'] | b64decode | from_yaml }}"
  209. - name: Combine registration-template.yaml and own registration override config
  210. ansible.builtin.set_fact:
  211. matrix_appservice_irc_registration: "{{ matrix_appservice_irc_registration_template | combine(matrix_appservice_irc_registration_override, recursive=True) }}"
  212. - name: Ensure Appservice IRC registration.yaml installed
  213. ansible.builtin.copy:
  214. content: "{{ matrix_appservice_irc_registration | to_nice_yaml(indent=2, width=999999) }}"
  215. dest: "{{ matrix_appservice_irc_config_path }}/registration.yaml"
  216. mode: 0644
  217. owner: "{{ matrix_user_name }}"
  218. group: "{{ matrix_group_name }}"
  219. - name: Ensure matrix-appservice-irc container network is created
  220. community.general.docker_network:
  221. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  222. name: "{{ matrix_appservice_irc_container_network }}"
  223. driver: bridge
  224. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  225. - name: Ensure matrix-appservice-irc.service installed
  226. ansible.builtin.template:
  227. src: "{{ role_path }}/templates/systemd/matrix-appservice-irc.service.j2"
  228. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-irc.service"
  229. mode: 0644
  230. register: matrix_appservice_irc_systemd_service_result
  231. - name: Ensure matrix-appservice-irc.service restarted, if necessary
  232. ansible.builtin.service:
  233. name: "matrix-appservice-irc.service"
  234. state: restarted
  235. daemon_reload: true
  236. when: "matrix_appservice_irc_requires_restart | bool"