Matrix Docker Ansible eploy
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

296 líneas
14 KiB

  1. # SPDX-FileCopyrightText: 2019 - 2022 MDAD project contributors
  2. # SPDX-FileCopyrightText: 2019 - 2026 Slavi Pantaleev
  3. # SPDX-FileCopyrightText: 2025 - 2026 Thom Wiggers
  4. # SPDX-FileCopyrightText: 2019 Dan Arnfield
  5. # SPDX-FileCopyrightText: 2020 Chris van Dijk
  6. # SPDX-FileCopyrightText: 2021 Panagiotis Georgiadis
  7. # SPDX-FileCopyrightText: 2022 Jim Myhrberg
  8. # SPDX-FileCopyrightText: 2022 Marko Weltzer
  9. # SPDX-FileCopyrightText: 2022 Nikita Chernyi
  10. # SPDX-FileCopyrightText: 2022 Sebastian Gumprich
  11. # SPDX-FileCopyrightText: 2024 David Mehren
  12. #
  13. # SPDX-License-Identifier: AGPL-3.0-or-later
  14. ---
  15. - ansible.builtin.include_role:
  16. name: custom/matrix-base
  17. tasks_from: ensure_openssl_installed
  18. - name: Ensure Appservice IRC paths exist
  19. ansible.builtin.file:
  20. path: "{{ item.path }}"
  21. state: directory
  22. mode: '0750'
  23. owner: "{{ matrix_user_name }}"
  24. group: "{{ matrix_group_name }}"
  25. with_items:
  26. - {path: "{{ matrix_bridge_appservice_irc_base_path }}", when: true}
  27. - {path: "{{ matrix_bridge_appservice_irc_config_path }}", when: true}
  28. - {path: "{{ matrix_bridge_appservice_irc_data_path }}", when: true}
  29. - {path: "{{ matrix_bridge_appservice_irc_container_src_files_path }}", when: "{{ matrix_bridge_appservice_irc_container_image_self_build }}"}
  30. when: item.when | bool
  31. - name: Check if an old passkey file already exists
  32. ansible.builtin.stat:
  33. path: "{{ matrix_bridge_appservice_irc_base_path }}/passkey.pem"
  34. register: matrix_bridge_appservice_irc_stat_passkey
  35. - when: "matrix_bridge_appservice_irc_stat_passkey.stat.exists"
  36. block:
  37. - name: (Data relocation) Ensure matrix-appservice-irc.service is stopped
  38. ansible.builtin.service:
  39. name: matrix-appservice-irc
  40. state: stopped
  41. daemon_reload: true
  42. failed_when: false
  43. - name: (Data relocation) Move AppService IRC passkey.pem file to ./data directory
  44. ansible.builtin.command:
  45. cmd: "mv {{ matrix_bridge_appservice_irc_base_path }}/passkey.pem {{ matrix_bridge_appservice_irc_data_path }}/passkey.pem"
  46. register: matrix_bridge_appservice_irc_move_passkey_result
  47. changed_when: matrix_bridge_appservice_irc_move_passkey_result.rc == 0
  48. - name: (Data relocation) Move AppService IRC database files to ./data directory
  49. ansible.builtin.command:
  50. cmd: "mv {{ matrix_bridge_appservice_irc_base_path }}/{{ item }} {{ matrix_bridge_appservice_irc_data_path }}/{{ item }}"
  51. register: matrix_bridge_appservice_irc_move_dbs_result
  52. changed_when: matrix_bridge_appservice_irc_move_dbs_result.rc == 0
  53. with_items:
  54. - rooms.db
  55. - users.db
  56. failed_when: false
  57. - ansible.builtin.set_fact:
  58. matrix_bridge_appservice_irc_migration_requires_restart: false
  59. - when: "matrix_bridge_appservice_irc_database_engine == 'postgres'"
  60. block:
  61. - name: Check if a nedb database already exists
  62. ansible.builtin.stat:
  63. path: "{{ matrix_bridge_appservice_irc_data_path }}/users.db"
  64. register: matrix_bridge_appservice_irc_nedb_database_path_local_stat_result
  65. - when: "matrix_bridge_appservice_irc_nedb_database_path_local_stat_result.stat.exists | bool"
  66. block:
  67. - ansible.builtin.include_tasks: "{{ role_path }}/tasks/migrate_nedb_to_postgres.yml"
  68. - ansible.builtin.set_fact:
  69. matrix_bridge_appservice_irc_migration_requires_restart: true
  70. - name: Ensure Appservice IRC image is pulled
  71. community.docker.docker_image_pull:
  72. name: "{{ matrix_bridge_appservice_irc_container_image }}"
  73. pull: always
  74. when: "matrix_bridge_appservice_irc_enabled | bool and not matrix_bridge_appservice_irc_container_image_self_build | bool"
  75. register: matrix_bridge_appservice_irc_container_image_pull_result
  76. retries: "{{ devture_playbook_help_container_retries_count }}"
  77. delay: "{{ devture_playbook_help_container_retries_delay }}"
  78. until: matrix_bridge_appservice_irc_container_image_pull_result is not failed
  79. # 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.
  80. - name: Ensure matrix-appservice-irc repository ownership is correct when self-building
  81. ansible.builtin.file:
  82. path: "{{ matrix_bridge_appservice_irc_container_src_files_path }}"
  83. state: directory
  84. owner: "{{ matrix_user_name }}"
  85. group: "{{ matrix_group_name }}"
  86. recurse: true
  87. when: "matrix_bridge_appservice_irc_enabled | bool and matrix_bridge_appservice_irc_container_image_self_build | bool"
  88. - name: Ensure matrix-appservice-irc repository is present when self-building
  89. ansible.builtin.git:
  90. repo: "{{ matrix_bridge_appservice_irc_container_repo }}"
  91. version: "{{ matrix_bridge_appservice_irc_container_repo_version }}"
  92. dest: "{{ matrix_bridge_appservice_irc_container_src_files_path }}"
  93. force: "yes"
  94. become: true
  95. become_user: "{{ matrix_user_name }}"
  96. # Keep this even though the task above normalizes ownership. Git may still see an ownership mismatch when Ansible becomes an unprivileged user (see #5065).
  97. environment:
  98. GIT_CONFIG_COUNT: "1"
  99. GIT_CONFIG_KEY_0: safe.directory
  100. GIT_CONFIG_VALUE_0: "{{ matrix_bridge_appservice_irc_container_src_files_path }}"
  101. register: matrix_bridge_appservice_irc_git_pull_results
  102. when: "matrix_bridge_appservice_irc_enabled | bool and matrix_bridge_appservice_irc_container_image_self_build | bool"
  103. - name: Ensure matrix-appservice-irc Docker image is built
  104. community.docker.docker_image_build:
  105. name: "{{ matrix_bridge_appservice_irc_container_image }}"
  106. dockerfile: Dockerfile
  107. path: "{{ matrix_bridge_appservice_irc_container_src_files_path }}"
  108. pull: true
  109. rebuild: "{{ 'always' if matrix_bridge_appservice_irc_git_pull_results.changed | bool else 'never' }}"
  110. when: "matrix_bridge_appservice_irc_enabled | bool and matrix_bridge_appservice_irc_container_image_self_build | bool and matrix_bridge_appservice_irc_git_pull_results.changed"
  111. register: matrix_bridge_appservice_irc_container_image_build_result
  112. - name: Ensure Matrix Appservice IRC config installed
  113. ansible.builtin.copy:
  114. content: "{{ matrix_bridge_appservice_irc_configuration | to_nice_yaml(indent=2, width=999999) }}"
  115. dest: "{{ matrix_bridge_appservice_irc_config_path }}/config.yaml"
  116. mode: '0644'
  117. owner: "{{ matrix_user_name }}"
  118. group: "{{ matrix_group_name }}"
  119. register: matrix_bridge_appservice_irc_config_result
  120. - name: Ensure Matrix Appservice IRC labels file installed
  121. ansible.builtin.template:
  122. src: "{{ role_path }}/templates/labels.j2"
  123. dest: "{{ matrix_bridge_appservice_irc_base_path }}/labels"
  124. mode: '0644'
  125. owner: "{{ matrix_user_name }}"
  126. group: "{{ matrix_group_name }}"
  127. register: matrix_bridge_appservice_irc_labels_result
  128. - name: Generate Appservice IRC passkey if it doesn't exist
  129. ansible.builtin.shell:
  130. cmd: "{{ matrix_host_command_openssl }} genpkey -out {{ matrix_bridge_appservice_irc_data_path }}/passkey.pem -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048"
  131. creates: "{{ matrix_bridge_appservice_irc_data_path }}/passkey.pem"
  132. become: true
  133. become_user: "{{ matrix_user_name }}"
  134. - name: Check if an authenticated media signing key exists
  135. ansible.builtin.stat:
  136. path: "{{ matrix_bridge_appservice_irc_data_path }}/auth-media.jwk"
  137. register: matrix_bridge_appservice_irc_stat_auth_media_key
  138. - when: not matrix_bridge_appservice_irc_stat_auth_media_key.stat.exists
  139. block:
  140. - name: Generate IRC appservice signing key for authenticated media
  141. community.docker.docker_container:
  142. name: "create-auth-media-jwk-key"
  143. image: "{{ matrix_bridge_appservice_irc_container_image }}"
  144. cleanup: true
  145. network_mode: none
  146. entrypoint: "/usr/local/bin/node"
  147. command: >
  148. -e "const webcrypto = require('node:crypto');
  149. async function main() {
  150. const key = await webcrypto.subtle.generateKey({
  151. name: 'HMAC',
  152. hash: 'SHA-512',
  153. }, true, ['sign', 'verify']);
  154. console.log(JSON.stringify(await webcrypto.subtle.exportKey('jwk', key), undefined, 4));
  155. }
  156. main().then(() => process.exit(0)).catch(err => { throw err });"
  157. detach: false
  158. register: matrix_bridge_appservice_irc_jwk_result
  159. - name: Write auth media signing key to file
  160. ansible.builtin.copy:
  161. content: "{{ matrix_bridge_appservice_irc_jwk_result.container.Output }}"
  162. dest: "{{ matrix_bridge_appservice_irc_data_path }}/auth-media.jwk"
  163. mode: "0644"
  164. owner: "{{ matrix_user_name }}"
  165. group: "{{ matrix_group_name }}"
  166. # In the past, we used to generate the passkey.pem file with root, so permissions may not be okay.
  167. # Fix it.
  168. - name: (Migration) Ensure Appservice IRC passkey permissions are okay
  169. ansible.builtin.file:
  170. path: "{{ matrix_bridge_appservice_irc_data_path }}/passkey.pem"
  171. mode: '0644'
  172. owner: "{{ matrix_user_name }}"
  173. group: "{{ matrix_group_name }}"
  174. # Ideally, we'd like to generate the final registration.yaml file by ourselves.
  175. #
  176. # However, the IRC bridge supports multiple servers, which leads to multiple
  177. # users/aliases/rooms rules in the registration file.
  178. #
  179. # Generating a proper file by ourselves is complicated and may lead to deviation
  180. # from what the bridge is doing.
  181. #
  182. # Instead, we do another hacky thing - asking the bridge to generate a template,
  183. # and then we parse it and fix it up with our own AS/HS token.
  184. # We need to do this, because:
  185. # - we'd like to have an up-to-date registration file
  186. # - we can achieve this by asking the bridge to rebuild it each time
  187. # - however, the bridge insists on regenerating all tokens each time
  188. # - .. which is not friendly for integrating with the homeserver
  189. #
  190. # So we have a hybrid approach. We ask the bridge to always generate
  191. # an up-to-date file, and we fix it up with some static values later on,
  192. # to produce a final registration.yaml file, as we desire.
  193. - name: Generate Appservice IRC registration-template.yaml
  194. ansible.builtin.shell: >-
  195. {{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-irc-gen
  196. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  197. --cap-drop=ALL
  198. --mount type=bind,src={{ matrix_bridge_appservice_irc_config_path }},dst=/config
  199. --mount type=bind,src={{ matrix_bridge_appservice_irc_data_path }},dst=/data
  200. --entrypoint=/bin/bash
  201. {{ matrix_bridge_appservice_irc_container_image }}
  202. -c
  203. 'node app.js
  204. -r
  205. -f /config/registration-template.yaml
  206. -u "http://matrix-appservice-irc:9999"
  207. -c /config/config.yaml
  208. -l irc_bot'
  209. changed_when: false
  210. - name: Read Appservice IRC registration-template.yaml
  211. ansible.builtin.slurp:
  212. src: "{{ matrix_bridge_appservice_irc_config_path }}/registration-template.yaml"
  213. register: matrix_bridge_appservice_irc_registration_template_slurp
  214. - name: Remove unnecessary Appservice IRC registration-template.yaml
  215. ansible.builtin.file:
  216. path: "{{ matrix_bridge_appservice_irc_config_path }}/registration-template.yaml"
  217. state: absent
  218. changed_when: false
  219. - name: Parse registration-template.yaml
  220. ansible.builtin.set_fact:
  221. matrix_bridge_appservice_irc_registration_template: "{{ matrix_bridge_appservice_irc_registration_template_slurp['content'] | b64decode | from_yaml }}"
  222. - name: Combine registration-template.yaml and own registration override config
  223. ansible.builtin.set_fact:
  224. matrix_bridge_appservice_irc_registration: "{{ matrix_bridge_appservice_irc_registration_template | combine(matrix_bridge_appservice_irc_registration_override, recursive=True) }}"
  225. - name: Ensure Appservice IRC registration.yaml installed
  226. ansible.builtin.copy:
  227. content: "{{ matrix_bridge_appservice_irc_registration | to_nice_yaml(indent=2, width=999999) }}"
  228. dest: "{{ matrix_bridge_appservice_irc_config_path }}/registration.yaml"
  229. mode: '0644'
  230. owner: "{{ matrix_user_name }}"
  231. group: "{{ matrix_group_name }}"
  232. register: matrix_bridge_appservice_irc_registration_result
  233. - name: Ensure matrix-appservice-irc container network is created
  234. when: matrix_bridge_appservice_irc_container_network != 'host'
  235. community.general.docker_network:
  236. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  237. name: "{{ matrix_bridge_appservice_irc_container_network }}"
  238. driver: bridge
  239. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  240. - name: Ensure matrix-appservice-irc.service installed
  241. ansible.builtin.template:
  242. src: "{{ role_path }}/templates/systemd/matrix-appservice-irc.service.j2"
  243. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-irc.service"
  244. mode: '0644'
  245. register: matrix_bridge_appservice_irc_systemd_service_result
  246. - name: Determine whether matrix-appservice-irc needs a restart
  247. ansible.builtin.set_fact:
  248. matrix_bridge_appservice_irc_restart_necessary: >-
  249. {{
  250. matrix_bridge_appservice_irc_migration_requires_restart | default(false)
  251. or matrix_bridge_appservice_irc_config_result.changed | default(false)
  252. or matrix_bridge_appservice_irc_labels_result.changed | default(false)
  253. or matrix_bridge_appservice_irc_registration_result.changed | default(false)
  254. or matrix_bridge_appservice_irc_systemd_service_result.changed | default(false)
  255. or matrix_bridge_appservice_irc_container_image_pull_result.changed | default(false)
  256. or matrix_bridge_appservice_irc_container_image_build_result.changed | default(false)
  257. }}
  258. - name: Ensure matrix-appservice-irc.service restarted, if necessary
  259. ansible.builtin.service:
  260. name: "matrix-appservice-irc.service"
  261. state: restarted
  262. daemon_reload: true
  263. when: "matrix_bridge_appservice_irc_migration_requires_restart | bool"