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.
 
 
 

345 regels
19 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Proves the old Python bridge accepts the role-rendered configuration, completes its
  6. # Matrix-side startup, migrates real Postgres and exposes its appservice and metrics.
  7. - name: Verify mautrix-googlechat
  8. hosts: all
  9. become: true
  10. vars_files:
  11. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  12. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  13. vars:
  14. mautrix_googlechat_config: "{{ mautrix_googlechat_config_file.content | b64decode | from_yaml }}"
  15. mautrix_googlechat_registration: "{{ mautrix_googlechat_registration_file.content | b64decode | from_yaml }}"
  16. mautrix_googlechat_labels_rendered: "{{ mautrix_googlechat_labels_file.content | b64decode }}"
  17. gather_facts: false
  18. tasks:
  19. # Read the version from the role so a Renovate bump changes the expected image.
  20. - name: Load the role's defaults under a separate name
  21. ansible.builtin.include_vars:
  22. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  23. name: mautrix_googlechat_role_defaults
  24. - name: Wait for the mautrix-googlechat service to become active
  25. ansible.builtin.systemd_service:
  26. name: matrix-mautrix-googlechat.service
  27. register: mautrix_googlechat_service
  28. until: mautrix_googlechat_service.status.ActiveState == 'active'
  29. retries: 30
  30. delay: 5
  31. failed_when: false
  32. - name: Assert the service is active and has not been restarting
  33. ansible.builtin.assert:
  34. that:
  35. - mautrix_googlechat_service.status.ActiveState == 'active'
  36. - mautrix_googlechat_service.status.NRestarts is defined
  37. - mautrix_googlechat_service.status.NRestarts | int == 0
  38. fail_msg: >-
  39. matrix-mautrix-googlechat.service is
  40. {{ mautrix_googlechat_service.status.ActiveState | default('unknown') }} after
  41. {{ mautrix_googlechat_service.status.NRestarts | default('?') }} restart(s)
  42. success_msg: "matrix-mautrix-googlechat.service is active and has not restarted"
  43. - name: Wait for the bridge liveness endpoint
  44. ansible.builtin.command:
  45. argv:
  46. - docker
  47. - run
  48. - --rm
  49. - --network={{ matrix_bridge_mautrix_googlechat_container_network }}
  50. - "{{ molecule_shared_image_curl }}"
  51. - --silent
  52. - --output
  53. - /dev/null
  54. - --write-out
  55. - "HTTP_STATUS=%{http_code}"
  56. - http://matrix-mautrix-googlechat:8080/_matrix/mau/live
  57. register: mautrix_googlechat_live
  58. changed_when: false
  59. until: mautrix_googlechat_live.rc == 0 and mautrix_googlechat_live.stdout == 'HTTP_STATUS=200'
  60. retries: 24
  61. delay: 5
  62. failed_when: false
  63. - name: Wait for the bridge readiness endpoint
  64. ansible.builtin.command:
  65. argv:
  66. - docker
  67. - run
  68. - --rm
  69. - --network={{ matrix_bridge_mautrix_googlechat_container_network }}
  70. - "{{ molecule_shared_image_curl }}"
  71. - --silent
  72. - --output
  73. - /dev/null
  74. - --write-out
  75. - "HTTP_STATUS=%{http_code}"
  76. - http://matrix-mautrix-googlechat:8080/_matrix/mau/ready
  77. register: mautrix_googlechat_ready
  78. changed_when: false
  79. until: mautrix_googlechat_ready.rc == 0 and mautrix_googlechat_ready.stdout == 'HTTP_STATUS=200'
  80. retries: 24
  81. delay: 5
  82. failed_when: false
  83. - name: Assert the bridge is live and ready on its appservice listener
  84. ansible.builtin.assert:
  85. that:
  86. - mautrix_googlechat_live.rc == 0
  87. - mautrix_googlechat_live.stdout == 'HTTP_STATUS=200'
  88. - mautrix_googlechat_ready.rc == 0
  89. - mautrix_googlechat_ready.stdout == 'HTTP_STATUS=200'
  90. fail_msg: >-
  91. Health probes returned live={{ mautrix_googlechat_live.stdout | default('none') }} and
  92. ready={{ mautrix_googlechat_ready.stdout | default('none') }}
  93. success_msg: "The bridge is live and ready on its appservice listener"
  94. - name: Query the enabled metrics listener
  95. ansible.builtin.command:
  96. argv:
  97. - docker
  98. - run
  99. - --rm
  100. - --network={{ matrix_bridge_mautrix_googlechat_container_network }}
  101. - "{{ molecule_shared_image_curl }}"
  102. - --silent
  103. - --show-error
  104. - --fail
  105. - http://matrix-mautrix-googlechat:8000/metrics
  106. register: mautrix_googlechat_metrics
  107. changed_when: false
  108. - name: Assert the live metrics surface comes from the running Python bridge
  109. ansible.builtin.assert:
  110. that:
  111. - mautrix_googlechat_metrics.rc == 0
  112. - "'# HELP python_gc_objects_collected_total' in mautrix_googlechat_metrics.stdout"
  113. - "'# HELP process_start_time_seconds' in mautrix_googlechat_metrics.stdout"
  114. - "'# HELP python_info' in mautrix_googlechat_metrics.stdout"
  115. fail_msg: "The enabled metrics listener did not expose the Python process metrics"
  116. success_msg: "The enabled metrics listener exposes the Python process metrics"
  117. - name: Read the configuration the role rendered
  118. ansible.builtin.slurp:
  119. src: "{{ matrix_bridge_mautrix_googlechat_config_path }}/config.yaml"
  120. register: mautrix_googlechat_config_file
  121. - name: Assert the rendered configuration carries the appservice identity
  122. ansible.builtin.assert:
  123. that:
  124. - mautrix_googlechat_config.homeserver.address == matrix_bridge_mautrix_googlechat_homeserver_address
  125. - mautrix_googlechat_config.homeserver.domain == matrix_bridge_mautrix_googlechat_homeserver_domain
  126. - mautrix_googlechat_config.appservice.id == 'googlechat'
  127. - mautrix_googlechat_config.appservice.address == 'http://matrix-mautrix-googlechat:8080'
  128. - mautrix_googlechat_config.appservice.bot_username == matrix_bridge_mautrix_googlechat_appservice_bot_username
  129. - mautrix_googlechat_config.appservice.as_token == matrix_bridge_mautrix_googlechat_appservice_token
  130. - mautrix_googlechat_config.appservice.hs_token == matrix_bridge_mautrix_googlechat_homeserver_token
  131. fail_msg: "The rendered configuration does not carry the scenario's appservice identity"
  132. success_msg: "The rendered configuration carries the scenario's appservice identity"
  133. - name: Assert the rendered configuration carries non-default bridge behavior
  134. ansible.builtin.assert:
  135. that:
  136. - mautrix_googlechat_config.bridge.command_prefix == matrix_bridge_mautrix_googlechat_command_prefix
  137. - not mautrix_googlechat_config.bridge.federate_rooms
  138. - mautrix_googlechat_config.bridge.login_shared_secret == matrix_bridge_mautrix_googlechat_login_shared_secret
  139. - mautrix_googlechat_config.bridge.web.auth.public == 'http://matrix.molecule.local:8008/molecule-googlechat/login'
  140. - mautrix_googlechat_config.bridge.web.auth.prefix == '/molecule-googlechat/login'
  141. - mautrix_googlechat_config.metrics.enabled
  142. - mautrix_googlechat_config.logging.root.level == matrix_bridge_mautrix_googlechat_logging_level
  143. fail_msg: "The rendered configuration does not carry the scenario's bridge behavior"
  144. success_msg: "The rendered configuration carries the scenario's bridge behavior"
  145. - name: Assert the configuration points at the scenario's Postgres database
  146. ansible.builtin.assert:
  147. that:
  148. - mautrix_googlechat_config.appservice.database is string
  149. - mautrix_googlechat_config.appservice.database is match('^postgres://')
  150. - matrix_bridge_mautrix_googlechat_database_username in mautrix_googlechat_config.appservice.database
  151. - matrix_bridge_mautrix_googlechat_database_hostname in mautrix_googlechat_config.appservice.database
  152. - matrix_bridge_mautrix_googlechat_database_name in mautrix_googlechat_config.appservice.database
  153. fail_msg: >-
  154. appservice.database is {{ mautrix_googlechat_config.appservice.database | default('unset') }},
  155. which was not composed from the scenario's Postgres settings
  156. success_msg: "The configuration points at the scenario's Postgres database"
  157. - name: Read the appservice registration the role rendered
  158. ansible.builtin.slurp:
  159. src: "{{ matrix_bridge_mautrix_googlechat_config_path }}/registration.yaml"
  160. register: mautrix_googlechat_registration_file
  161. - name: Assert the registration carries the configured handshake values
  162. ansible.builtin.assert:
  163. that:
  164. - mautrix_googlechat_registration.id == 'googlechat'
  165. - mautrix_googlechat_registration.as_token == matrix_bridge_mautrix_googlechat_appservice_token
  166. - mautrix_googlechat_registration.hs_token == matrix_bridge_mautrix_googlechat_homeserver_token
  167. - mautrix_googlechat_registration.sender_localpart == '_bot_' + matrix_bridge_mautrix_googlechat_appservice_bot_username
  168. - mautrix_googlechat_registration.url == 'http://matrix-mautrix-googlechat:8080'
  169. fail_msg: "The registration does not carry the configured handshake values"
  170. success_msg: "The registration carries the configured handshake values"
  171. - name: Assert the registration namespaces cover only Google Chat ghosts and the bot
  172. ansible.builtin.assert:
  173. that:
  174. - mautrix_googlechat_ghost_regex | length > 0
  175. - mautrix_googlechat_bot_regex | length > 0
  176. - mautrix_googlechat_ghost_mxid is match(mautrix_googlechat_ghost_regex)
  177. - mautrix_googlechat_wrong_ghost_mxid is not match(mautrix_googlechat_ghost_regex)
  178. - mautrix_googlechat_bot_mxid is match(mautrix_googlechat_bot_regex)
  179. fail_msg: "The registration namespaces do not cover only Google Chat ghosts and the bot"
  180. success_msg: "The registration namespaces cover only Google Chat ghosts and the bot"
  181. vars:
  182. mautrix_googlechat_user_regexes: "{{ mautrix_googlechat_registration.namespaces.users | map(attribute='regex') | list }}"
  183. mautrix_googlechat_ghost_regex: "{{ mautrix_googlechat_user_regexes | select('search', 'googlechat_') | first | default('') }}"
  184. mautrix_googlechat_bot_regex: "{{ mautrix_googlechat_user_regexes | reject('search', 'googlechat_') | first | default('') }}"
  185. mautrix_googlechat_ghost_mxid: "@googlechat_123456:{{ matrix_bridge_mautrix_googlechat_homeserver_domain }}"
  186. mautrix_googlechat_wrong_ghost_mxid: "@gvoice_123456:{{ matrix_bridge_mautrix_googlechat_homeserver_domain }}"
  187. mautrix_googlechat_bot_mxid: "@{{ matrix_bridge_mautrix_googlechat_appservice_bot_username }}:{{ matrix_bridge_mautrix_googlechat_homeserver_domain }}"
  188. - name: List the tables the bridge created in Postgres
  189. ansible.builtin.command:
  190. argv:
  191. - docker
  192. - exec
  193. - matrix-postgres-molecule
  194. - psql
  195. - --username={{ matrix_bridge_mautrix_googlechat_database_username }}
  196. - --dbname={{ matrix_bridge_mautrix_googlechat_database_name }}
  197. - --tuples-only
  198. - --no-align
  199. - "--command=SELECT tablename FROM pg_tables WHERE schemaname = 'public'"
  200. register: mautrix_googlechat_tables
  201. changed_when: false
  202. - name: Assert the bridge migrated its schema into the configured database
  203. ansible.builtin.assert:
  204. that:
  205. - mautrix_googlechat_tables.rc == 0
  206. - "'version' in mautrix_googlechat_table_names"
  207. - "'portal' in mautrix_googlechat_table_names"
  208. - "'puppet' in mautrix_googlechat_table_names"
  209. - "'message' in mautrix_googlechat_table_names"
  210. - "'mx_version' in mautrix_googlechat_table_names"
  211. - mautrix_googlechat_table_names | length >= 10
  212. fail_msg: >-
  213. The bridge did not migrate its schema into {{ matrix_bridge_mautrix_googlechat_database_name }}
  214. (found {{ mautrix_googlechat_table_names | length }} table(s))
  215. success_msg: "The bridge migrated its schema into the configured database"
  216. vars:
  217. mautrix_googlechat_table_names: "{{ mautrix_googlechat_tables.stdout_lines | select | list }}"
  218. - name: Read the labels the role rendered
  219. ansible.builtin.slurp:
  220. src: "{{ matrix_bridge_mautrix_googlechat_base_path }}/labels"
  221. register: mautrix_googlechat_labels_file
  222. - name: Assert the labels route both HTTP surfaces to their configured ports
  223. ansible.builtin.assert:
  224. that:
  225. - "'traefik.enable=true' in mautrix_googlechat_labels_rendered"
  226. - "'traefik.docker.network=' + matrix_bridge_mautrix_googlechat_container_network in mautrix_googlechat_labels_rendered"
  227. - "'traefik.http.services.matrix-mautrix-googlechat-public.loadbalancer.server.port=8080' in mautrix_googlechat_labels_rendered"
  228. - "'traefik.http.routers.matrix-mautrix-googlechat-public.rule=Host(`googlechat.molecule.local`) && PathPrefix(`/molecule-googlechat`)' in mautrix_googlechat_labels_rendered"
  229. - "'traefik.http.services.matrix-mautrix-googlechat-metrics.loadbalancer.server.port=8000' in mautrix_googlechat_labels_rendered"
  230. - "'traefik.http.routers.matrix-mautrix-googlechat-metrics.rule=Host(`metrics.molecule.local`) && PathPrefix(`/metrics/googlechat`)' in mautrix_googlechat_labels_rendered"
  231. - "'molecule.googlechat.coverage=enabled' in mautrix_googlechat_labels_rendered"
  232. fail_msg: "The rendered labels do not carry both HTTP routing contracts"
  233. success_msg: "The rendered labels carry both HTTP routing contracts"
  234. - name: Inspect the running bridge container
  235. ansible.builtin.command:
  236. argv:
  237. - docker
  238. - container
  239. - inspect
  240. - matrix-mautrix-googlechat
  241. register: mautrix_googlechat_container_inspect
  242. changed_when: false
  243. - name: Parse the running bridge container inspection
  244. ansible.builtin.set_fact:
  245. mautrix_googlechat_container: "{{ (mautrix_googlechat_container_inspect.stdout | from_json) | first }}"
  246. - name: Assert the running container uses the exact image pinned by the role
  247. ansible.builtin.assert:
  248. that:
  249. - mautrix_googlechat_container.Config.Image == mautrix_googlechat_expected_image
  250. fail_msg: >-
  251. The running container uses {{ mautrix_googlechat_container.Config.Image }}, expected
  252. {{ mautrix_googlechat_expected_image }}
  253. success_msg: "The running container uses the exact image pinned by the role"
  254. vars:
  255. mautrix_googlechat_expected_image: >-
  256. {{ mautrix_googlechat_role_defaults.matrix_bridge_mautrix_googlechat_container_image_registry_prefix_upstream_default }}mautrix/googlechat:{{ mautrix_googlechat_role_defaults.matrix_bridge_mautrix_googlechat_version }}
  257. - name: Assert the running container uses the playbook-supplied runtime
  258. ansible.builtin.assert:
  259. that:
  260. - mautrix_googlechat_container.Config.User == (matrix_user_uid | string) + ':' + (matrix_user_gid | string)
  261. - mautrix_googlechat_container.Config.WorkingDir == '/opt/mautrix-googlechat'
  262. - mautrix_googlechat_container.Config.Cmd == ['python3', '-m', 'mautrix_googlechat', '-c', '/config/config.yaml', '--no-update']
  263. - mautrix_googlechat_container.HostConfig.RestartPolicy.Name == 'no'
  264. - mautrix_googlechat_container.HostConfig.AutoRemove
  265. - mautrix_googlechat_container.HostConfig.LogConfig.Type == 'none'
  266. fail_msg: "The running container does not use the role's exact identity, command and workdir"
  267. success_msg: "The running container uses the role's exact identity, command and workdir"
  268. - name: Assert the running container has the intended privilege isolation
  269. ansible.builtin.assert:
  270. that:
  271. - "'ALL' in mautrix_googlechat_container.HostConfig.CapDrop"
  272. - not mautrix_googlechat_container.HostConfig.Privileged
  273. fail_msg: "The running container does not have the intended privilege isolation"
  274. success_msg: "The running container drops all capabilities and is unprivileged"
  275. - name: Assert the running container carries the role's two bind mounts
  276. ansible.builtin.assert:
  277. that:
  278. - mautrix_googlechat_config_mount | length > 0
  279. - mautrix_googlechat_config_mount.Source == matrix_bridge_mautrix_googlechat_config_path
  280. - not mautrix_googlechat_config_mount.RW
  281. - mautrix_googlechat_data_mount | length > 0
  282. - mautrix_googlechat_data_mount.Source == matrix_bridge_mautrix_googlechat_data_path
  283. - mautrix_googlechat_data_mount.RW
  284. - mautrix_googlechat_container.Mounts | length == 2
  285. fail_msg: "The running container does not carry the role's exact config and data mounts"
  286. success_msg: "The running container carries its read-only config and writable data mounts"
  287. vars:
  288. mautrix_googlechat_config_mount: >-
  289. {{ mautrix_googlechat_container.Mounts
  290. | selectattr('Destination', 'equalto', '/config')
  291. | first | default({}) }}
  292. mautrix_googlechat_data_mount: >-
  293. {{ mautrix_googlechat_container.Mounts
  294. | selectattr('Destination', 'equalto', '/data')
  295. | first | default({}) }}
  296. - name: Assert the rendered labels are attached to the running container
  297. ansible.builtin.assert:
  298. that:
  299. - mautrix_googlechat_container.Config.Labels['traefik.enable'] == 'true'
  300. - mautrix_googlechat_container.Config.Labels['traefik.docker.network'] == matrix_bridge_mautrix_googlechat_container_network
  301. - mautrix_googlechat_container.Config.Labels['traefik.http.services.matrix-mautrix-googlechat-public.loadbalancer.server.port'] == '8080'
  302. - mautrix_googlechat_container.Config.Labels['traefik.http.services.matrix-mautrix-googlechat-metrics.loadbalancer.server.port'] == '8000'
  303. - mautrix_googlechat_container.Config.Labels['molecule.googlechat.coverage'] == 'enabled'
  304. fail_msg: "The running container does not carry the labels the role rendered"
  305. success_msg: "The running container carries the labels the role rendered"
  306. - name: Assert the running container has the exact network and port exposure
  307. ansible.builtin.assert:
  308. that:
  309. - mautrix_googlechat_container.HostConfig.NetworkMode == matrix_bridge_mautrix_googlechat_container_network
  310. - matrix_bridge_mautrix_googlechat_container_network in mautrix_googlechat_container.NetworkSettings.Networks
  311. - mautrix_googlechat_container.NetworkSettings.Networks | length == 1
  312. - mautrix_googlechat_container.HostConfig.PortBindings | default({}, true) | length == 0
  313. fail_msg: >-
  314. The container has unexpected networks or host ports:
  315. networks={{ mautrix_googlechat_container.NetworkSettings.Networks.keys() | list }},
  316. ports={{ mautrix_googlechat_container.HostConfig.PortBindings | default({}) }}
  317. success_msg: "The container uses only its dedicated network and publishes no host ports"