Matrix Docker Ansible eploy
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

208 righe
9.1 KiB

  1. ---
  2. - ansible.builtin.include_role:
  3. name: custom/matrix-base
  4. tasks_from: ensure_openssl_installed
  5. - name: Ensure Appservice IRC paths exist
  6. ansible.builtin.file:
  7. path: "{{ item.path }}"
  8. state: directory
  9. mode: 0750
  10. owner: "{{ matrix_user_username }}"
  11. group: "{{ matrix_user_groupname }}"
  12. with_items:
  13. - {path: "{{ matrix_appservice_irc_base_path }}", when: true}
  14. - {path: "{{ matrix_appservice_irc_config_path }}", when: true}
  15. - {path: "{{ matrix_appservice_irc_data_path }}", when: true}
  16. - {path: "{{ matrix_appservice_irc_docker_src_files_path }}", when: "{{ matrix_appservice_irc_container_image_self_build }}"}
  17. when: item.when | bool
  18. - name: Check if an old passkey file already exists
  19. ansible.builtin.stat:
  20. path: "{{ matrix_appservice_irc_base_path }}/passkey.pem"
  21. register: matrix_appservice_irc_stat_passkey
  22. - when: "matrix_appservice_irc_stat_passkey.stat.exists"
  23. block:
  24. - name: (Data relocation) Ensure matrix-appservice-irc.service is stopped
  25. ansible.builtin.service:
  26. name: matrix-appservice-irc
  27. state: stopped
  28. daemon_reload: true
  29. failed_when: false
  30. - name: (Data relocation) Move AppService IRC passkey.pem file to ./data directory
  31. ansible.builtin.command:
  32. cmd: "mv {{ matrix_appservice_irc_base_path }}/passkey.pem {{ matrix_appservice_irc_data_path }}/passkey.pem"
  33. register: matrix_appservice_irc_move_passkey_result
  34. changed_when: matrix_appservice_irc_move_passkey_result.rc == 0
  35. - name: (Data relocation) Move AppService IRC database files to ./data directory
  36. ansible.builtin.command:
  37. cmd: "mv {{ matrix_appservice_irc_base_path }}/{{ item }} {{ matrix_appservice_irc_data_path }}/{{ item }}"
  38. register: matrix_appservice_irc_move_dbs_result
  39. changed_when: matrix_appservice_irc_move_dbs_result.rc == 0
  40. with_items:
  41. - rooms.db
  42. - users.db
  43. failed_when: false
  44. - ansible.builtin.set_fact:
  45. matrix_appservice_irc_requires_restart: false
  46. - when: "matrix_appservice_irc_database_engine == 'postgres'"
  47. block:
  48. - name: Check if a nedb database already exists
  49. ansible.builtin.stat:
  50. path: "{{ matrix_appservice_irc_data_path }}/users.db"
  51. register: matrix_appservice_irc_nedb_database_path_local_stat_result
  52. - when: "matrix_appservice_irc_nedb_database_path_local_stat_result.stat.exists | bool"
  53. block:
  54. - ansible.builtin.include_tasks: "{{ role_path }}/tasks/migrate_nedb_to_postgres.yml"
  55. - ansible.builtin.set_fact:
  56. matrix_appservice_irc_requires_restart: true
  57. - name: Ensure Appservice IRC image is pulled
  58. community.docker.docker_image:
  59. name: "{{ matrix_appservice_irc_docker_image }}"
  60. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  61. force_source: "{{ matrix_appservice_irc_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  62. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_irc_docker_image_force_pull }}"
  63. when: "matrix_appservice_irc_enabled | bool and not matrix_appservice_irc_container_image_self_build | bool"
  64. register: result
  65. retries: "{{ devture_playbook_help_container_retries_count }}"
  66. delay: "{{ devture_playbook_help_container_retries_delay }}"
  67. until: result is not failed
  68. - name: Ensure matrix-appservice-irc repository is present when self-building
  69. ansible.builtin.git:
  70. repo: "{{ matrix_appservice_irc_docker_repo }}"
  71. version: "{{ matrix_appservice_irc_docker_repo_version }}"
  72. dest: "{{ matrix_appservice_irc_docker_src_files_path }}"
  73. force: "yes"
  74. become: true
  75. become_user: "{{ matrix_user_username }}"
  76. register: matrix_appservice_irc_git_pull_results
  77. when: "matrix_appservice_irc_enabled | bool and matrix_appservice_irc_container_image_self_build | bool"
  78. - name: Ensure matrix-appservice-irc Docker image is built
  79. community.docker.docker_image:
  80. name: "{{ matrix_appservice_irc_docker_image }}"
  81. source: build
  82. force_source: "{{ matrix_appservice_irc_git_pull_results.changed if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  83. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_irc_git_pull_results.changed }}"
  84. build:
  85. dockerfile: Dockerfile
  86. path: "{{ matrix_appservice_irc_docker_src_files_path }}"
  87. pull: true
  88. when: "matrix_appservice_irc_enabled | bool and matrix_appservice_irc_container_image_self_build | bool and matrix_appservice_irc_git_pull_results.changed"
  89. - name: Ensure Matrix Appservice IRC config installed
  90. ansible.builtin.copy:
  91. content: "{{ matrix_appservice_irc_configuration | to_nice_yaml(indent=2, width=999999) }}"
  92. dest: "{{ matrix_appservice_irc_config_path }}/config.yaml"
  93. mode: 0644
  94. owner: "{{ matrix_user_username }}"
  95. group: "{{ matrix_user_groupname }}"
  96. - name: Check if Appservice IRC passkey exists
  97. ansible.builtin.stat:
  98. path: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  99. register: irc_passkey_file
  100. - name: Generate Appservice IRC passkey if it doesn't exist
  101. ansible.builtin.shell: "{{ matrix_host_command_openssl }} genpkey -out {{ matrix_appservice_irc_data_path }}/passkey.pem -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048"
  102. become: true
  103. become_user: "{{ matrix_user_username }}"
  104. when: "not irc_passkey_file.stat.exists"
  105. # In the past, we used to generate the passkey.pem file with root, so permissions may not be okay.
  106. # Fix it.
  107. - name: (Migration) Ensure Appservice IRC passkey permissions are okay
  108. ansible.builtin.file:
  109. path: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  110. mode: 0644
  111. owner: "{{ matrix_user_username }}"
  112. group: "{{ matrix_user_groupname }}"
  113. # Ideally, we'd like to generate the final registration.yaml file by ourselves.
  114. #
  115. # However, the IRC bridge supports multiple servers, which leads to multiple
  116. # users/aliases/rooms rules in the registration file.
  117. #
  118. # Generating a proper file by ourselves is complicated and may lead to deviation
  119. # from what the bridge is doing.
  120. #
  121. # Instead, we do another hacky thing - asking the bridge to generate a template,
  122. # and then we parse it and fix it up with our own AS/HS token.
  123. # We need to do this, because:
  124. # - we'd like to have an up-to-date registration file
  125. # - we can achieve this by asking the bridge to rebuild it each time
  126. # - however, the bridge insists on regenerating all tokens each time
  127. # - .. which is not friendly for integrating with the homeserver
  128. #
  129. # So we have a hybrid approach. We ask the bridge to always generate
  130. # an up-to-date file, and we fix it up with some static values later on,
  131. # to produce a final registration.yaml file, as we desire.
  132. - name: Generate Appservice IRC registration-template.yaml
  133. ansible.builtin.shell: >-
  134. {{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-irc-gen
  135. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  136. --cap-drop=ALL
  137. -v {{ matrix_appservice_irc_config_path }}:/config:z
  138. -v {{ matrix_appservice_irc_data_path }}:/data:z
  139. --entrypoint=/bin/bash
  140. {{ matrix_appservice_irc_docker_image }}
  141. -c
  142. 'node app.js
  143. -r
  144. -f /config/registration-template.yaml
  145. -u "http://matrix-appservice-irc:9999"
  146. -c /config/config.yaml
  147. -l irc_bot'
  148. changed_when: false
  149. - name: Read Appservice IRC registration-template.yaml
  150. ansible.builtin.slurp:
  151. src: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  152. register: matrix_appservice_irc_registration_template_slurp
  153. - name: Remove unnecessary Appservice IRC registration-template.yaml
  154. ansible.builtin.file:
  155. path: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  156. state: absent
  157. changed_when: false
  158. - name: Parse registration-template.yaml
  159. ansible.builtin.set_fact:
  160. matrix_appservice_irc_registration_template: "{{ matrix_appservice_irc_registration_template_slurp['content'] | b64decode | from_yaml }}"
  161. - name: Combine registration-template.yaml and own registration override config
  162. ansible.builtin.set_fact:
  163. matrix_appservice_irc_registration: "{{ matrix_appservice_irc_registration_template | combine(matrix_appservice_irc_registration_override, recursive=True) }}"
  164. - name: Ensure Appservice IRC registration.yaml installed
  165. ansible.builtin.copy:
  166. content: "{{ matrix_appservice_irc_registration | to_nice_yaml(indent=2, width=999999) }}"
  167. dest: "{{ matrix_appservice_irc_config_path }}/registration.yaml"
  168. mode: 0644
  169. owner: "{{ matrix_user_username }}"
  170. group: "{{ matrix_user_groupname }}"
  171. - name: Ensure matrix-appservice-irc.service installed
  172. ansible.builtin.template:
  173. src: "{{ role_path }}/templates/systemd/matrix-appservice-irc.service.j2"
  174. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-irc.service"
  175. mode: 0644
  176. register: matrix_appservice_irc_systemd_service_result
  177. - name: Ensure matrix-appservice-irc.service restarted, if necessary
  178. ansible.builtin.service:
  179. name: "matrix-appservice-irc.service"
  180. state: restarted
  181. daemon_reload: true
  182. when: "matrix_appservice_irc_requires_restart | bool"