Matrix Docker Ansible eploy
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

195 řádky
8.0 KiB

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