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.
 
 

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