Matrix Docker Ansible eploy
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

194 строки
7.8 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 build
  63. docker_image:
  64. name: "{{ matrix_appservice_irc_docker_image }}"
  65. source: build
  66. force_source: yes
  67. build:
  68. dockerfile: Dockerfile
  69. path: "{{ matrix_appservice_irc_docker_src_files_path }}"
  70. pull: yes
  71. when: "matrix_appservice_irc_enabled|bool and matrix_appservice_irc_container_self_build|bool and matrix_appservice_irc_git_pull_results.changed"
  72. - name: Ensure Matrix Appservice IRC config installed
  73. copy:
  74. content: "{{ matrix_appservice_irc_configuration|to_nice_yaml }}"
  75. dest: "{{ matrix_appservice_irc_config_path }}/config.yaml"
  76. mode: 0644
  77. owner: "{{ matrix_user_username }}"
  78. group: "{{ matrix_user_groupname }}"
  79. - name: Check if Appservice IRC passkey exists
  80. stat:
  81. path: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  82. register: irc_passkey_file
  83. - name: Generate Appservice IRC passkey if it doesn't exist
  84. shell: "{{ matrix_host_command_openssl }} genpkey -out {{ matrix_appservice_irc_data_path }}/passkey.pem -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048"
  85. become: true
  86. become_user: "{{ matrix_user_username }}"
  87. when: "not irc_passkey_file.stat.exists"
  88. # In the past, we used to generate the passkey.pem file with root, so permissions may not be okay.
  89. # Fix it.
  90. - name: (Migration) Ensure Appservice IRC passkey permissions are okay
  91. file:
  92. path: "{{ matrix_appservice_irc_data_path }}/passkey.pem"
  93. mode: 0644
  94. owner: "{{ matrix_user_username }}"
  95. group: "{{ matrix_user_groupname }}"
  96. # Ideally, we'd like to generate the final registration.yaml file by ourselves.
  97. #
  98. # However, the IRC bridge supports multiple servers, which leads to multiple
  99. # users/aliases/rooms rules in the registration file.
  100. #
  101. # Generating a proper file by ourselves is complicated and may lead to deviation
  102. # from what the bridge is doing.
  103. #
  104. # Instead, we do another hacky thing - asking the bridge to generate a template,
  105. # and then we parse it and fix it up with our own AS/HS token.
  106. # We need to do this, because:
  107. # - we'd like to have an up-to-date registration file
  108. # - we can achieve this by asking the bridge to rebuild it each time
  109. # - however, the bridge insists on regenerating all tokens each time
  110. # - .. which is not friendly for integrating with the homeserver
  111. #
  112. # So we have a hybrid approach. We ask the bridge to always generate
  113. # an up-to-date file, and we fix it up with some static values later on,
  114. # to produce a final registration.yaml file, as we desire.
  115. - name: Generate Appservice IRC registration-template.yaml
  116. shell: >-
  117. {{ matrix_host_command_docker }} run --rm --name matrix-appservice-irc-gen
  118. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  119. --cap-drop=ALL
  120. -v {{ matrix_appservice_irc_config_path }}:/config:z
  121. -v {{ matrix_appservice_irc_data_path }}:/data:z
  122. --entrypoint=/bin/bash
  123. {{ matrix_appservice_irc_docker_image }}
  124. -c
  125. 'node app.js
  126. -r
  127. -f /config/registration-template.yaml
  128. -u "http://matrix-appservice-irc:9999"
  129. -c /config/config.yaml
  130. -l irc_bot'
  131. changed_when: false
  132. - name: Read Appservice IRC registration-template.yaml
  133. slurp:
  134. src: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  135. register: matrix_appservice_irc_registration_template_slurp
  136. - name: Remove unnecessary Appservice IRC registration-template.yaml
  137. file:
  138. path: "{{ matrix_appservice_irc_config_path }}/registration-template.yaml"
  139. state: absent
  140. changed_when: false
  141. - name: Parse registration-template.yaml
  142. set_fact:
  143. matrix_appservice_irc_registration_template: "{{ matrix_appservice_irc_registration_template_slurp['content'] | b64decode | from_yaml }}"
  144. - name: Combine registration-template.yaml and own registration override config
  145. set_fact:
  146. matrix_appservice_irc_registration: "{{ matrix_appservice_irc_registration_template|combine(matrix_appservice_irc_registration_override, recursive=True) }}"
  147. - name: Ensure Appservice IRC registration.yaml installed
  148. copy:
  149. content: "{{ matrix_appservice_irc_registration|to_nice_yaml }}"
  150. dest: "{{ matrix_appservice_irc_config_path }}/registration.yaml"
  151. mode: 0644
  152. owner: "{{ matrix_user_username }}"
  153. group: "{{ matrix_user_groupname }}"
  154. - name: Ensure matrix-appservice-irc.service installed
  155. template:
  156. src: "{{ role_path }}/templates/systemd/matrix-appservice-irc.service.j2"
  157. dest: "{{ matrix_systemd_path }}/matrix-appservice-irc.service"
  158. mode: 0644
  159. register: matrix_appservice_irc_systemd_service_result
  160. - name: Ensure systemd reloaded after matrix-appservice-irc.service installation
  161. service:
  162. daemon_reload: yes
  163. when: "matrix_appservice_irc_systemd_service_result.changed"
  164. - name: Ensure matrix-appservice-irc.service restarted, if necessary
  165. service:
  166. name: "matrix-appservice-irc.service"
  167. state: restarted
  168. when: "matrix_appservice_irc_requires_restart|bool"