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.
 
 

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