Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

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