Matrix Docker Ansible eploy
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

239 Zeilen
13 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # What this proves: the bridge starts, reads the configuration and registration
  6. # the role rendered, opens its appservice port, and is the version the role
  7. # pins. It also proves the role composed the avatar-proxy labels out of the
  8. # hostname, scheme and path prefix it was given. It does NOT bridge anything -
  9. # there is no Discord on the other side and there is deliberately never going to
  10. # be one, because that would need a Discord account. See
  11. # docs/molecule-testing.md.
  12. - name: Verify mautrix-discord
  13. hosts: all
  14. become: true
  15. vars_files:
  16. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  17. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  18. gather_facts: false
  19. vars:
  20. # The role derives this from scheme + hostname + path prefix, and role
  21. # defaults are out of scope in this play, so it is recomposed here from the
  22. # same three values the scenario pinned in molecule.yml.
  23. mautrix_discord_expected_public_address: >-
  24. {{ matrix_bridge_mautrix_discord_scheme }}://{{ matrix_bridge_mautrix_discord_hostname }}{{ matrix_bridge_mautrix_discord_path_prefix }}
  25. mautrix_discord_expected_avatar_proxy_path_prefix: "{{ matrix_bridge_mautrix_discord_path_prefix }}/mautrix-discord/avatar"
  26. tasks:
  27. # Read from the role's own defaults rather than pinned in molecule.yml, so
  28. # the version assertion below compares the running image against what the
  29. # role ships instead of against the scenario itself.
  30. - name: Load the role's defaults under a separate name
  31. ansible.builtin.include_vars:
  32. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  33. name: mautrix_discord_role_defaults
  34. - name: Wait for the mautrix-discord service to become active
  35. ansible.builtin.systemd_service:
  36. name: matrix-mautrix-discord.service
  37. register: mautrix_discord_service
  38. until: mautrix_discord_service.status.ActiveState == 'active'
  39. retries: 30
  40. delay: 5
  41. failed_when: false
  42. # `Restart=always` means a bridge crash-looping on a configuration it cannot
  43. # read still reports `active`, so the restart counter is checked too. It is
  44. # asserted `is defined` because `| int` turns a missing property into 0 and
  45. # would pass vacuously.
  46. - name: Assert the service is active and has not been restarting
  47. ansible.builtin.assert:
  48. that:
  49. - mautrix_discord_service.status.ActiveState == 'active'
  50. - mautrix_discord_service.status.NRestarts is defined
  51. - mautrix_discord_service.status.NRestarts | int == 0
  52. fail_msg: >-
  53. matrix-mautrix-discord.service is
  54. {{ mautrix_discord_service.status.ActiveState | default('unknown') }}
  55. after {{ mautrix_discord_service.status.NRestarts | default('?') }}
  56. automatic restart(s)
  57. success_msg: "matrix-mautrix-discord.service is active and has not restarted"
  58. # The appservice port is the bridge's own listener, the one a homeserver
  59. # would push transactions to. It opening at all means the bridge got through
  60. # reading its configuration and setting itself up.
  61. - name: Wait for the bridge to open its appservice port
  62. ansible.builtin.command:
  63. argv:
  64. - docker
  65. - run
  66. - --rm
  67. - --network={{ matrix_bridge_mautrix_discord_container_network }}
  68. - "{{ molecule_shared_image_curl }}"
  69. - --silent
  70. - --output
  71. - /dev/null
  72. - --write-out
  73. - "HTTP_STATUS=%{http_code}"
  74. - "http://matrix-mautrix-discord:8080/_matrix/mau/live"
  75. register: mautrix_discord_live
  76. changed_when: false
  77. until: "'HTTP_STATUS=000' not in mautrix_discord_live.stdout"
  78. retries: 24
  79. delay: 5
  80. failed_when: false
  81. - name: Assert the bridge answers on its appservice port
  82. ansible.builtin.assert:
  83. that:
  84. - "'HTTP_STATUS=000' not in mautrix_discord_live.stdout"
  85. fail_msg: >-
  86. The bridge did not answer on its appservice port
  87. ({{ mautrix_discord_live.stdout | default('no output') }})
  88. success_msg: "The bridge answers on its appservice port"
  89. - name: Read the configuration the role rendered
  90. ansible.builtin.slurp:
  91. src: "{{ matrix_bridge_mautrix_discord_config_path }}/config.yaml"
  92. register: mautrix_discord_config_file
  93. # Each of these differs from what the bridge would use on its own, so their
  94. # presence means the role's configuration is what the bridge is running on
  95. # rather than something that happened to agree with it. The public address
  96. # in particular is composed by the role out of three separate variables.
  97. #
  98. # Asserted against the parsed document rather than by substring, so a value
  99. # landing under the wrong key cannot pass.
  100. - name: Assert the rendered configuration carries this scenario's values
  101. ansible.builtin.assert:
  102. that:
  103. - mautrix_discord_config.homeserver.address == matrix_bridge_mautrix_discord_homeserver_address
  104. - mautrix_discord_config.homeserver.domain == matrix_bridge_mautrix_discord_homeserver_domain
  105. - mautrix_discord_config.appservice.bot.username == matrix_bridge_mautrix_discord_appservice_bot_username
  106. - mautrix_discord_config.appservice.as_token == matrix_bridge_mautrix_discord_appservice_token
  107. - mautrix_discord_config.appservice.hs_token == matrix_bridge_mautrix_discord_homeserver_token
  108. - mautrix_discord_config.appservice.database.type == 'sqlite3'
  109. - mautrix_discord_config.bridge.command_prefix == matrix_bridge_mautrix_discord_bridge_command_prefix
  110. - mautrix_discord_config.bridge.avatar_proxy_key == matrix_bridge_mautrix_discord_bridge_avatar_proxy_key
  111. - mautrix_discord_config.bridge.public_address == mautrix_discord_expected_public_address | trim
  112. - mautrix_discord_config.logging.min_level == matrix_bridge_mautrix_discord_logging_level
  113. fail_msg: "The rendered configuration does not carry the scenario's values"
  114. success_msg: "The rendered configuration carries the scenario's values"
  115. vars:
  116. mautrix_discord_config: "{{ mautrix_discord_config_file.content | b64decode | from_yaml }}"
  117. # The registration file is the half of the appservice handshake the
  118. # homeserver reads, and it is generated by the role rather than by the
  119. # bridge, so it is worth checking on its own. `sender_localpart` is the
  120. # role's own `_bot_` prefixing, not something the bridge would produce.
  121. - name: Read the appservice registration the role rendered
  122. ansible.builtin.slurp:
  123. src: "{{ matrix_bridge_mautrix_discord_config_path }}/registration.yaml"
  124. register: mautrix_discord_registration_file
  125. - name: Assert the registration carries the scenario's tokens and bot user
  126. ansible.builtin.assert:
  127. that:
  128. - mautrix_discord_registration.as_token == matrix_bridge_mautrix_discord_appservice_token
  129. - mautrix_discord_registration.hs_token == matrix_bridge_mautrix_discord_homeserver_token
  130. - mautrix_discord_registration.sender_localpart == '_bot_' ~ matrix_bridge_mautrix_discord_appservice_bot_username
  131. - mautrix_discord_registration.url == 'http://matrix-mautrix-discord:8080'
  132. - mautrix_discord_bot_user_regex in (mautrix_discord_registration.namespaces.users | map(attribute='regex') | list)
  133. fail_msg: "The appservice registration does not carry the scenario's tokens and bot user"
  134. success_msg: "The appservice registration carries the scenario's tokens and bot user"
  135. vars:
  136. mautrix_discord_registration: "{{ mautrix_discord_registration_file.content | b64decode | from_yaml }}"
  137. mautrix_discord_bot_user_regex: "^@{{ matrix_bridge_mautrix_discord_appservice_bot_username | regex_escape }}:{{ matrix_bridge_mautrix_discord_homeserver_domain | regex_escape }}$"
  138. # sqlite was chosen in molecule.yml, so the bridge should have created its
  139. # database under the role's data path. This is the cheap proof that the data
  140. # path reached the process and is writable by the uid the role runs it as.
  141. - name: Look for the bridge's sqlite database under the role's data path
  142. ansible.builtin.stat:
  143. path: "{{ matrix_bridge_mautrix_discord_data_path }}/mautrix-discord.db"
  144. register: mautrix_discord_database
  145. - name: Assert the bridge created its database where the role put its data path
  146. ansible.builtin.assert:
  147. that:
  148. - mautrix_discord_database.stat.exists
  149. - mautrix_discord_database.stat.uid | int == matrix_user_uid | int
  150. fail_msg: >-
  151. The bridge did not create its database under
  152. {{ matrix_bridge_mautrix_discord_data_path }}, or it is not owned by
  153. uid {{ matrix_user_uid }}
  154. success_msg: "The bridge created its database under the role's data path, as the role's uid"
  155. - name: Read the image of the running container
  156. ansible.builtin.command:
  157. argv:
  158. - docker
  159. - container
  160. - inspect
  161. - matrix-mautrix-discord
  162. - --format
  163. - "{{ '{{' }} .Config.Image {{ '}}' }}"
  164. register: mautrix_discord_image
  165. changed_when: false
  166. - name: Assert the running container is the version defaults/main.yml pins
  167. ansible.builtin.assert:
  168. that:
  169. - mautrix_discord_role_defaults.matrix_bridge_mautrix_discord_version in mautrix_discord_image.stdout
  170. fail_msg: >-
  171. The running container is {{ mautrix_discord_image.stdout }}, which does
  172. not carry the pinned version
  173. {{ mautrix_discord_role_defaults.matrix_bridge_mautrix_discord_version }}
  174. success_msg: "The running container is the version defaults/main.yml pins"
  175. # The avatar proxy is this role's own reverse-proxy wiring: the labels only
  176. # appear because a public address was configured, and their hostname and
  177. # path prefix are composed by the role rather than copied from a variable.
  178. - name: Read the labels the role rendered
  179. ansible.builtin.slurp:
  180. src: "{{ matrix_bridge_mautrix_discord_base_path }}/labels"
  181. register: mautrix_discord_labels
  182. - name: Assert the avatar-proxy labels were composed from the scenario's hostname and path prefix
  183. ansible.builtin.assert:
  184. that:
  185. - "'traefik.enable=true' in mautrix_discord_labels_rendered"
  186. - "'traefik.docker.network=' ~ matrix_bridge_mautrix_discord_container_network in mautrix_discord_labels_rendered"
  187. - "'traefik.http.routers.matrix-mautrix-discord-avatar-proxy.rule=Host(`' ~ matrix_bridge_mautrix_discord_hostname ~ '`) && PathPrefix(`' ~ mautrix_discord_expected_avatar_proxy_path_prefix ~ '`)' in mautrix_discord_labels_rendered"
  188. - "'traefik.http.middlewares.matrix-mautrix-discord-strip-prefix.stripprefix.prefixes=' ~ matrix_bridge_mautrix_discord_path_prefix in mautrix_discord_labels_rendered"
  189. fail_msg: >-
  190. The avatar-proxy labels do not carry the scenario's hostname
  191. ({{ matrix_bridge_mautrix_discord_hostname }}) and path prefix
  192. ({{ mautrix_discord_expected_avatar_proxy_path_prefix }})
  193. success_msg: "The avatar-proxy labels were composed from the scenario's hostname and path prefix"
  194. vars:
  195. mautrix_discord_labels_rendered: "{{ mautrix_discord_labels.content | b64decode }}"
  196. # The label file is fed to `docker create --label-file`, so a label the role
  197. # renders wrongly is not merely cosmetic - it would stop the container from
  198. # being created at all. Reading them back off the running container proves
  199. # Docker accepted them.
  200. - name: Read the labels Docker attached to the running container
  201. ansible.builtin.command:
  202. argv:
  203. - docker
  204. - container
  205. - inspect
  206. - matrix-mautrix-discord
  207. - --format
  208. - "{{ '{{' }} index .Config.Labels \"traefik.http.routers.matrix-mautrix-discord-avatar-proxy.rule\" {{ '}}' }}"
  209. register: mautrix_discord_container_label
  210. changed_when: false
  211. - name: Assert Docker carries the avatar-proxy router label the role rendered
  212. ansible.builtin.assert:
  213. that:
  214. - matrix_bridge_mautrix_discord_hostname in mautrix_discord_container_label.stdout
  215. - mautrix_discord_expected_avatar_proxy_path_prefix in mautrix_discord_container_label.stdout
  216. fail_msg: >-
  217. The running container's avatar-proxy router label is
  218. "{{ mautrix_discord_container_label.stdout }}"
  219. success_msg: "The running container carries the avatar-proxy router label the role rendered"