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.
 
 

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