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.
 
 
 

243 lines
13 KiB

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