Matrix Docker Ansible eploy
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

210 wiersze
9.1 KiB

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