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

278 строки
16 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, and is the Messenger-side image at the version the role pins.
  7. #
  8. # The extra thing worth proving here is that `..._meta_mode` reaches every place it feeds: one
  9. # upstream codebase serves several Meta networks, and the mode picks which. The scenario runs
  10. # `facebook-tor` rather than the role's default, so the assertions can tell the two apart.
  11. #
  12. # It does NOT bridge anything: no Facebook or Messenger account is on the other side, and
  13. # deliberately never will be. See docs/molecule-testing.md.
  14. - name: Verify mautrix-meta-messenger
  15. hosts: all
  16. become: true
  17. vars_files:
  18. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  19. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  20. # Lazily evaluated, so they resolve only in the tasks after the matching slurp.
  21. # Deliberately not set_fact: the rendered configuration contains the bridge's own Go
  22. # templates, and a stored fact gets templated again on every lookup, which would try to
  23. # evaluate those as Jinja.
  24. vars:
  25. mautrix_meta_messenger_config: "{{ mautrix_meta_messenger_config_file.content | b64decode | from_yaml }}"
  26. mautrix_meta_messenger_registration: "{{ mautrix_meta_messenger_registration_file.content | b64decode | from_yaml }}"
  27. mautrix_meta_messenger_labels_rendered: "{{ mautrix_meta_messenger_labels.content | b64decode }}"
  28. gather_facts: false
  29. tasks:
  30. # From the role's own defaults rather than pinned in molecule.yml, so the version
  31. # assertion compares the running image against what the role ships, not the scenario.
  32. - name: Load the role's defaults under a separate name
  33. ansible.builtin.include_vars:
  34. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  35. name: mautrix_meta_messenger_role_defaults
  36. - name: Wait for the mautrix-meta-messenger service to become active
  37. ansible.builtin.systemd_service:
  38. name: matrix-mautrix-meta-messenger.service
  39. register: mautrix_meta_messenger_service
  40. until: mautrix_meta_messenger_service.status.ActiveState == 'active'
  41. retries: 30
  42. delay: 5
  43. failed_when: false
  44. # `Restart=always` means a bridge crash-looping on unreadable config still reports
  45. # `active`, so the restart counter is checked too. Asserted `is defined` because
  46. # `| int` turns a missing property into 0 and would pass vacuously.
  47. - name: Assert the service is active and has not been restarting
  48. ansible.builtin.assert:
  49. that:
  50. - mautrix_meta_messenger_service.status.ActiveState == 'active'
  51. - mautrix_meta_messenger_service.status.NRestarts is defined
  52. - mautrix_meta_messenger_service.status.NRestarts | int == 0
  53. fail_msg: >-
  54. matrix-mautrix-meta-messenger.service is
  55. {{ mautrix_meta_messenger_service.status.ActiveState | default('unknown') }}
  56. after {{ mautrix_meta_messenger_service.status.NRestarts | default('?') }}
  57. automatic restart(s)
  58. success_msg: "matrix-mautrix-meta-messenger.service is active and has not restarted"
  59. # The appservice listener is where a homeserver would push transactions. It opening at all
  60. # means the bridge got through reading its configuration, through its /whoami check, and
  61. # through setting itself up.
  62. - name: Wait for the bridge to open its appservice port
  63. ansible.builtin.command:
  64. argv:
  65. - docker
  66. - run
  67. - --rm
  68. - --network={{ matrix_bridge_mautrix_meta_messenger_container_network }}
  69. - "{{ molecule_shared_image_curl }}"
  70. - --silent
  71. - --output
  72. - /dev/null
  73. - --write-out
  74. - "HTTP_STATUS=%{http_code}"
  75. - "http://matrix-mautrix-meta-messenger:29319/_matrix/mau/live"
  76. register: mautrix_meta_messenger_live
  77. changed_when: false
  78. until: "'HTTP_STATUS=000' not in mautrix_meta_messenger_live.stdout"
  79. retries: 24
  80. delay: 5
  81. failed_when: false
  82. - name: Assert the bridge answers on its appservice port
  83. ansible.builtin.assert:
  84. that:
  85. - "'HTTP_STATUS=000' not in mautrix_meta_messenger_live.stdout"
  86. fail_msg: >-
  87. The bridge did not answer on its appservice port
  88. ({{ mautrix_meta_messenger_live.stdout | default('no output') }})
  89. success_msg: "The bridge answers on its appservice port"
  90. - name: Read the configuration the role rendered
  91. ansible.builtin.slurp:
  92. src: "{{ matrix_bridge_mautrix_meta_messenger_config_path }}/config.yaml"
  93. register: mautrix_meta_messenger_config_file
  94. # Each differs from what the bridge would use on its own, so their presence rules out
  95. # a coincidence.
  96. - name: Assert the rendered configuration carries this scenario's values
  97. ansible.builtin.assert:
  98. that:
  99. - mautrix_meta_messenger_config.homeserver.address == matrix_bridge_mautrix_meta_messenger_homeserver_address
  100. - mautrix_meta_messenger_config.homeserver.domain == matrix_bridge_mautrix_meta_messenger_homeserver_domain
  101. - mautrix_meta_messenger_config.appservice.bot.username == matrix_bridge_mautrix_meta_messenger_appservice_username
  102. - mautrix_meta_messenger_config.appservice.as_token == matrix_bridge_mautrix_meta_messenger_appservice_token
  103. - mautrix_meta_messenger_config.appservice.hs_token == matrix_bridge_mautrix_meta_messenger_homeserver_token
  104. - mautrix_meta_messenger_config.bridge.command_prefix == matrix_bridge_mautrix_meta_messenger_bridge_command_prefix
  105. - mautrix_meta_messenger_config.logging.min_level == matrix_bridge_mautrix_meta_messenger_logging_min_level
  106. # matrix_admin is empty in the shared context, so the only per-domain permission
  107. # left is the one the role derives from the homeserver domain.
  108. - mautrix_meta_messenger_config.bridge.permissions[matrix_bridge_mautrix_meta_messenger_homeserver_domain] == 'user'
  109. # From the shared context rather than this scenario, but the role is what has to
  110. # carry it into the configuration.
  111. - mautrix_meta_messenger_config.encryption.allow == matrix_bridges_encryption_enabled
  112. fail_msg: "The rendered configuration does not carry the scenario's values"
  113. success_msg: "The rendered configuration carries the scenario's values"
  114. # The mode is not written into the configuration as-is: the role expands it into an
  115. # appservice id, a ghost username prefix, a bot displayname and the bridge's `tor` switch.
  116. # Each holds a different value under the role's default mode, so together they prove the
  117. # choice propagated rather than being ignored.
  118. - name: Assert the configuration reflects the Meta mode the scenario selected
  119. ansible.builtin.assert:
  120. that:
  121. - mautrix_meta_messenger_config.appservice.id == 'facebook-tor'
  122. - mautrix_meta_messenger_config.appservice.username_template.startswith('facebook_')
  123. - not mautrix_meta_messenger_config.appservice.username_template.startswith('messenger_')
  124. - mautrix_meta_messenger_config.network.tor
  125. - mautrix_meta_messenger_config.appservice.bot.displayname == 'Facebook-tor bridge bot'
  126. - matrix_bridge_mautrix_meta_messenger_bridge_displayname_suffix in mautrix_meta_messenger_config.network.displayname_template
  127. fail_msg: >-
  128. The rendered configuration does not reflect
  129. matrix_bridge_mautrix_meta_messenger_meta_mode=facebook-tor
  130. success_msg: "The rendered configuration reflects the Meta mode the scenario selected"
  131. - name: Assert the configuration points the bridge at the database the role derived
  132. ansible.builtin.assert:
  133. that:
  134. - mautrix_meta_messenger_config.database.type == matrix_bridge_mautrix_meta_messenger_database_engine
  135. - matrix_bridge_mautrix_meta_messenger_database_username in mautrix_meta_messenger_config.database.uri
  136. - matrix_bridge_mautrix_meta_messenger_database_name in mautrix_meta_messenger_config.database.uri
  137. - matrix_bridge_mautrix_meta_messenger_database_hostname in mautrix_meta_messenger_config.database.uri
  138. fail_msg: >-
  139. database.uri is {{ mautrix_meta_messenger_config.database.uri | default('unset') }},
  140. which was not built from the scenario's connection settings
  141. success_msg: "The rendered configuration points the bridge at the database the role derived"
  142. # The public address is what the role builds out of the three exposure
  143. # variables; it is the same endpoint the Traefik labels below route to.
  144. - name: Assert the configuration carries the public address the exposure settings imply
  145. ansible.builtin.assert:
  146. that:
  147. - mautrix_meta_messenger_config.appservice.public_address == 'https://bridges.molecule.local/bridges/meta-messenger'
  148. fail_msg: >-
  149. appservice.public_address is
  150. {{ mautrix_meta_messenger_config.appservice.public_address | default('unset') }}
  151. success_msg: "The rendered configuration carries the public address the exposure settings imply"
  152. # The role generates the registration; the bridge only consumes it. Worth checking on its
  153. # own, as it is the half of the handshake the homeserver reads.
  154. - name: Read the appservice registration the role rendered
  155. ansible.builtin.slurp:
  156. src: "{{ matrix_bridge_mautrix_meta_messenger_config_path }}/registration.yaml"
  157. register: mautrix_meta_messenger_registration_file
  158. - name: Assert the registration carries the scenario's tokens, id and bot user
  159. ansible.builtin.assert:
  160. that:
  161. - mautrix_meta_messenger_registration.as_token == matrix_bridge_mautrix_meta_messenger_appservice_token
  162. - mautrix_meta_messenger_registration.hs_token == matrix_bridge_mautrix_meta_messenger_homeserver_token
  163. - mautrix_meta_messenger_registration.id == 'facebook-tor'
  164. - mautrix_meta_messenger_registration.sender_localpart == '_bot_' + matrix_bridge_mautrix_meta_messenger_appservice_username
  165. - mautrix_meta_messenger_registration.url == 'http://matrix-mautrix-meta-messenger:29319'
  166. fail_msg: "The appservice registration does not carry the scenario's tokens, id and bot user"
  167. success_msg: "The appservice registration carries the scenario's tokens, id and bot user"
  168. # The namespaces are regexes the role assembles. Comparing them as strings would only
  169. # re-derive the role's own escaping, so they are checked by what they match: the bot,
  170. # this mode's ghosts, and not the ghosts of the mode the role would have defaulted to.
  171. - name: Assert the registration namespaces cover the bot and this mode's ghost users
  172. ansible.builtin.assert:
  173. that:
  174. - mautrix_meta_messenger_ghost_regex | length > 0
  175. - mautrix_meta_messenger_bot_regex | length > 0
  176. - mautrix_meta_messenger_facebook_ghost_mxid is match(mautrix_meta_messenger_ghost_regex)
  177. - mautrix_meta_messenger_messenger_ghost_mxid is not match(mautrix_meta_messenger_ghost_regex)
  178. - mautrix_meta_messenger_bot_mxid is match(mautrix_meta_messenger_bot_regex)
  179. fail_msg: "The appservice registration namespaces do not cover the bot and this mode's ghost users"
  180. success_msg: "The appservice registration namespaces cover the bot and this mode's ghost users"
  181. vars:
  182. mautrix_meta_messenger_user_regexes: "{{ mautrix_meta_messenger_registration.namespaces.users | map(attribute='regex') | list }}"
  183. mautrix_meta_messenger_ghost_regex: "{{ mautrix_meta_messenger_user_regexes | select('search', 'facebook_') | first | default('') }}"
  184. mautrix_meta_messenger_bot_regex: "{{ mautrix_meta_messenger_user_regexes | reject('search', 'facebook_') | first | default('') }}"
  185. mautrix_meta_messenger_facebook_ghost_mxid: "@facebook_1234567890:{{ matrix_bridge_mautrix_meta_messenger_homeserver_domain }}"
  186. mautrix_meta_messenger_messenger_ghost_mxid: "@messenger_1234567890:{{ matrix_bridge_mautrix_meta_messenger_homeserver_domain }}"
  187. mautrix_meta_messenger_bot_mxid: "@{{ matrix_bridge_mautrix_meta_messenger_appservice_username }}:{{ matrix_bridge_mautrix_meta_messenger_homeserver_domain }}"
  188. # The bridge can only have created tables here by resolving the hostname, authenticating
  189. # with the credentials the role rendered, and running its migrations to completion.
  190. - name: List the tables the bridge created in Postgres
  191. ansible.builtin.command:
  192. argv:
  193. - docker
  194. - exec
  195. - matrix-postgres-molecule
  196. - psql
  197. - --username={{ matrix_bridge_mautrix_meta_messenger_database_username }}
  198. - --dbname={{ matrix_bridge_mautrix_meta_messenger_database_name }}
  199. - --tuples-only
  200. - --no-align
  201. - --command=SELECT tablename FROM pg_tables WHERE schemaname = 'public'
  202. register: mautrix_meta_messenger_tables
  203. changed_when: false
  204. - name: Assert the bridge migrated its schema into the database the role pointed it at
  205. ansible.builtin.assert:
  206. that:
  207. - mautrix_meta_messenger_tables.rc == 0
  208. - "'version' in mautrix_meta_messenger_table_names"
  209. - mautrix_meta_messenger_table_names | length > 5
  210. fail_msg: >-
  211. The bridge did not create its schema in
  212. {{ matrix_bridge_mautrix_meta_messenger_database_name }}
  213. (found {{ mautrix_meta_messenger_table_names | length }} table(s))
  214. success_msg: "The bridge migrated its schema into the database the role pointed it at"
  215. vars:
  216. mautrix_meta_messenger_table_names: "{{ mautrix_meta_messenger_tables.stdout_lines | select | list }}"
  217. - name: Read the image of the running container
  218. ansible.builtin.command:
  219. argv:
  220. - docker
  221. - container
  222. - inspect
  223. - matrix-mautrix-meta-messenger
  224. - --format
  225. - "{{ '{{' }} .Config.Image {{ '}}' }}"
  226. register: mautrix_meta_messenger_image
  227. changed_when: false
  228. # Both Meta bridges publish to the same image repository, with Instagram's tags carrying
  229. # an `ig-` prefix. Compared whole rather than by substring, so pulling the other bridge's
  230. # image would fail here.
  231. - name: Assert the running container is the Messenger image at the version defaults/main.yml pins
  232. ansible.builtin.assert:
  233. that:
  234. - mautrix_meta_messenger_image.stdout.endswith(':' + mautrix_meta_messenger_role_defaults.matrix_bridge_mautrix_meta_messenger_version)
  235. fail_msg: >-
  236. The running container is {{ mautrix_meta_messenger_image.stdout }}, which is not
  237. the Messenger image at the pinned version
  238. {{ mautrix_meta_messenger_role_defaults.matrix_bridge_mautrix_meta_messenger_version }}
  239. success_msg: "The running container is the Messenger image at the version defaults/main.yml pins"
  240. - name: Read the labels the role rendered
  241. ansible.builtin.slurp:
  242. src: "{{ matrix_bridge_mautrix_meta_messenger_base_path }}/labels"
  243. register: mautrix_meta_messenger_labels
  244. # Traefik is not running here, so this checks what the role wrote, not what a reverse
  245. # proxy would do with it. The port matters: it is hardcoded in the role's templates rather
  246. # than derived from a variable, so nothing else here would catch it drifting from the port
  247. # the bridge actually listens on.
  248. - name: Assert the labels route the exposure hostname and path prefix to the appservice port
  249. ansible.builtin.assert:
  250. that:
  251. - "'traefik.enable=true' in mautrix_meta_messenger_labels_rendered"
  252. - "'traefik.http.services.matrix-mautrix-meta-messenger-appservice.loadbalancer.server.port=29319' in mautrix_meta_messenger_labels_rendered"
  253. - "'traefik.http.routers.matrix-mautrix-meta-messenger-exposure.rule=Host(`bridges.molecule.local`) && PathPrefix(`/bridges/meta-messenger`)' in mautrix_meta_messenger_labels_rendered"
  254. - "'traefik.http.middlewares.matrix-mautrix-meta-messenger-exposure-strip-prefix.stripprefix.prefixes=/bridges/meta-messenger' in mautrix_meta_messenger_labels_rendered"
  255. - "'traefik.docker.network=' + matrix_bridge_mautrix_meta_messenger_container_network in mautrix_meta_messenger_labels_rendered"
  256. fail_msg: "The rendered labels do not route the exposure hostname and path prefix to the appservice port"
  257. success_msg: "The rendered labels route the exposure hostname and path prefix to the appservice port"