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.
 
 

192 lines
7.6 KiB

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