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.
 
 

283 lines
12 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_appservice_irc_base_path }}", when: true}
  27. - {path: "{{ matrix_appservice_irc_config_path }}", when: true}
  28. - {path: "{{ matrix_appservice_irc_data_path }}", when: true}
  29. - {path: "{{ matrix_appservice_irc_docker_src_files_path }}", when: "{{ matrix_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_appservice_irc_base_path }}/passkey.pem"
  34. register: matrix_appservice_irc_stat_passkey
  35. - when: "matrix_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_appservice_irc_base_path }}/passkey.pem {{ matrix_appservice_irc_data_path }}/passkey.pem"
  46. register: matrix_appservice_irc_move_passkey_result
  47. changed_when: matrix_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_appservice_irc_base_path }}/{{ item }} {{ matrix_appservice_irc_data_path }}/{{ item }}"
  51. register: matrix_appservice_irc_move_dbs_result
  52. changed_when: matrix_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_appservice_irc_migration_requires_restart: false
  59. - when: "matrix_appservice_irc_database_engine == 'postgres'"
  60. block:
  61. - name: Check if a nedb database already exists
  62. ansible.builtin.stat:
  63. path: "{{ matrix_appservice_irc_data_path }}/users.db"
  64. register: matrix_appservice_irc_nedb_database_path_local_stat_result
  65. - when: "matrix_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_appservice_irc_migration_requires_restart: true
  70. - name: Ensure Appservice IRC image is pulled
  71. community.docker.docker_image:
  72. name: "{{ matrix_appservice_irc_docker_image }}"
  73. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  74. force_source: "{{ matrix_appservice_irc_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  75. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_irc_docker_image_force_pull }}"
  76. when: "matrix_appservice_irc_enabled | bool and not matrix_appservice_irc_container_image_self_build | bool"
  77. register: matrix_appservice_irc_container_image_pull_result
  78. retries: "{{ devture_playbook_help_container_retries_count }}"
  79. delay: "{{ devture_playbook_help_container_retries_delay }}"
  80. until: matrix_appservice_irc_container_image_pull_result is not failed
  81. - name: Ensure matrix-appservice-irc repository is present when self-building
  82. ansible.builtin.git:
  83. repo: "{{ matrix_appservice_irc_docker_repo }}"
  84. version: "{{ matrix_appservice_irc_docker_repo_version }}"
  85. dest: "{{ matrix_appservice_irc_docker_src_files_path }}"
  86. force: "yes"
  87. become: true
  88. become_user: "{{ matrix_user_name }}"
  89. register: matrix_appservice_irc_git_pull_results
  90. when: "matrix_appservice_irc_enabled | bool and matrix_appservice_irc_container_image_self_build | bool"
  91. - name: Ensure matrix-appservice-irc Docker image is built
  92. community.docker.docker_image:
  93. name: "{{ matrix_appservice_irc_docker_image }}"
  94. source: build
  95. force_source: "{{ matrix_appservice_irc_git_pull_results.changed if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  96. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_irc_git_pull_results.changed }}"
  97. build:
  98. dockerfile: Dockerfile
  99. path: "{{ matrix_appservice_irc_docker_src_files_path }}"
  100. pull: true
  101. when: "matrix_appservice_irc_enabled | bool and matrix_appservice_irc_container_image_self_build | bool and matrix_appservice_irc_git_pull_results.changed"
  102. - name: Ensure Matrix Appservice IRC config installed
  103. ansible.builtin.copy:
  104. content: "{{ matrix_appservice_irc_configuration | to_nice_yaml(indent=2, width=999999) }}"
  105. dest: "{{ matrix_appservice_irc_config_path }}/config.yaml"
  106. mode: '0644'
  107. owner: "{{ matrix_user_name }}"
  108. group: "{{ matrix_group_name }}"
  109. register: matrix_appservice_irc_config_result
  110. - name: Ensure Matrix Appservice IRC labels file installed
  111. ansible.builtin.template:
  112. src: "{{ role_path }}/templates/labels.j2"
  113. dest: "{{ matrix_appservice_irc_base_path }}/labels"
  114. mode: '0644'
  115. owner: "{{ matrix_user_name }}"
  116. group: "{{ matrix_group_name }}"
  117. register: matrix_appservice_irc_labels_result
  118. - name: Generate Appservice IRC passkey if it doesn't exist
  119. ansible.builtin.shell:
  120. cmd: "{{ matrix_host_command_openssl }} genpkey -out {{ matrix_appservice_irc_data_path }}/passkey.pem -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048"
  121. creates: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  122. become: true
  123. become_user: "{{ matrix_user_name }}"
  124. - name: Check if an authenticated media signing key exists
  125. ansible.builtin.stat:
  126. path: "{{ matrix_appservice_irc_data_path }}/auth-media.jwk"
  127. register: matrix_appservice_irc_stat_auth_media_key
  128. - when: not matrix_appservice_irc_stat_auth_media_key.stat.exists
  129. block:
  130. - name: Generate IRC appservice signing key for authenticated media
  131. community.docker.docker_container:
  132. name: "create-auth-media-jwk-key"
  133. image: "{{ matrix_appservice_irc_docker_image }}"
  134. cleanup: true
  135. network_mode: none
  136. entrypoint: "/usr/local/bin/node"
  137. command: >
  138. -e "const webcrypto = require('node:crypto');
  139. async function main() {
  140. const key = await webcrypto.subtle.generateKey({
  141. name: 'HMAC',
  142. hash: 'SHA-512',
  143. }, true, ['sign', 'verify']);
  144. console.log(JSON.stringify(await webcrypto.subtle.exportKey('jwk', key), undefined, 4));
  145. }
  146. main().then(() => process.exit(0)).catch(err => { throw err });"
  147. detach: false
  148. register: matrix_appservice_irc_jwk_result
  149. - name: Write auth media signing key to file
  150. ansible.builtin.copy:
  151. content: "{{ matrix_appservice_irc_jwk_result.container.Output }}"
  152. dest: "{{ matrix_appservice_irc_data_path }}/auth-media.jwk"
  153. mode: "0644"
  154. owner: "{{ matrix_user_name }}"
  155. group: "{{ matrix_group_name }}"
  156. # In the past, we used to generate the passkey.pem file with root, so permissions may not be okay.
  157. # Fix it.
  158. - name: (Migration) Ensure Appservice IRC passkey permissions are okay
  159. ansible.builtin.file:
  160. path: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  161. mode: '0644'
  162. owner: "{{ matrix_user_name }}"
  163. group: "{{ matrix_group_name }}"
  164. # Ideally, we'd like to generate the final registration.yaml file by ourselves.
  165. #
  166. # However, the IRC bridge supports multiple servers, which leads to multiple
  167. # users/aliases/rooms rules in the registration file.
  168. #
  169. # Generating a proper file by ourselves is complicated and may lead to deviation
  170. # from what the bridge is doing.
  171. #
  172. # Instead, we do another hacky thing - asking the bridge to generate a template,
  173. # and then we parse it and fix it up with our own AS/HS token.
  174. # We need to do this, because:
  175. # - we'd like to have an up-to-date registration file
  176. # - we can achieve this by asking the bridge to rebuild it each time
  177. # - however, the bridge insists on regenerating all tokens each time
  178. # - .. which is not friendly for integrating with the homeserver
  179. #
  180. # So we have a hybrid approach. We ask the bridge to always generate
  181. # an up-to-date file, and we fix it up with some static values later on,
  182. # to produce a final registration.yaml file, as we desire.
  183. - name: Generate Appservice IRC registration-template.yaml
  184. ansible.builtin.shell: >-
  185. {{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-irc-gen
  186. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  187. --cap-drop=ALL
  188. --mount type=bind,src={{ matrix_appservice_irc_config_path }},dst=/config
  189. --mount type=bind,src={{ matrix_appservice_irc_data_path }},dst=/data
  190. --entrypoint=/bin/bash
  191. {{ matrix_appservice_irc_docker_image }}
  192. -c
  193. 'node app.js
  194. -r
  195. -f /config/registration-template.yaml
  196. -u "http://matrix-appservice-irc:9999"
  197. -c /config/config.yaml
  198. -l irc_bot'
  199. changed_when: false
  200. - name: Read Appservice IRC registration-template.yaml
  201. ansible.builtin.slurp:
  202. src: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  203. register: matrix_appservice_irc_registration_template_slurp
  204. - name: Remove unnecessary Appservice IRC registration-template.yaml
  205. ansible.builtin.file:
  206. path: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  207. state: absent
  208. changed_when: false
  209. - name: Parse registration-template.yaml
  210. ansible.builtin.set_fact:
  211. matrix_appservice_irc_registration_template: "{{ matrix_appservice_irc_registration_template_slurp['content'] | b64decode | from_yaml }}"
  212. - name: Combine registration-template.yaml and own registration override config
  213. ansible.builtin.set_fact:
  214. matrix_appservice_irc_registration: "{{ matrix_appservice_irc_registration_template | combine(matrix_appservice_irc_registration_override, recursive=True) }}"
  215. - name: Ensure Appservice IRC registration.yaml installed
  216. ansible.builtin.copy:
  217. content: "{{ matrix_appservice_irc_registration | to_nice_yaml(indent=2, width=999999) }}"
  218. dest: "{{ matrix_appservice_irc_config_path }}/registration.yaml"
  219. mode: '0644'
  220. owner: "{{ matrix_user_name }}"
  221. group: "{{ matrix_group_name }}"
  222. register: matrix_appservice_irc_registration_result
  223. - name: Ensure matrix-appservice-irc container network is created
  224. community.general.docker_network:
  225. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  226. name: "{{ matrix_appservice_irc_container_network }}"
  227. driver: bridge
  228. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  229. - name: Ensure matrix-appservice-irc.service installed
  230. ansible.builtin.template:
  231. src: "{{ role_path }}/templates/systemd/matrix-appservice-irc.service.j2"
  232. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-irc.service"
  233. mode: '0644'
  234. register: matrix_appservice_irc_systemd_service_result
  235. - name: Determine whether matrix-appservice-irc needs a restart
  236. ansible.builtin.set_fact:
  237. matrix_appservice_irc_restart_necessary: >-
  238. {{
  239. matrix_appservice_irc_migration_requires_restart | default(false)
  240. or matrix_appservice_irc_config_result.changed | default(false)
  241. or matrix_appservice_irc_labels_result.changed | default(false)
  242. or matrix_appservice_irc_registration_result.changed | default(false)
  243. or matrix_appservice_irc_systemd_service_result.changed | default(false)
  244. or matrix_appservice_irc_container_image_pull_result.changed | default(false)
  245. }}
  246. - name: Ensure matrix-appservice-irc.service restarted, if necessary
  247. ansible.builtin.service:
  248. name: "matrix-appservice-irc.service"
  249. state: restarted
  250. daemon_reload: true
  251. when: "matrix_appservice_irc_migration_requires_restart | bool"