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

397 строки
20 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Proves the logged-out bridge accepts the role-rendered configuration and registration,
  6. # migrates real Postgres, opens its appservice listener, and authenticates transactions.
  7. # A LINE login is deliberately outside this scenario's third-party boundary.
  8. - name: Verify beeper-line
  9. hosts: all
  10. become: true
  11. vars_files:
  12. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  13. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  14. vars:
  15. beeper_line_config: "{{ beeper_line_config_file.content | b64decode | from_yaml }}"
  16. beeper_line_registration: "{{ beeper_line_registration_file.content | b64decode | from_yaml }}"
  17. beeper_line_labels_rendered: "{{ beeper_line_labels_file.content | b64decode }}"
  18. beeper_line_expected_public_address: http://line-api.molecule.local/bridges/line-api
  19. gather_facts: false
  20. tasks:
  21. # A Renovate digest bump changes this source of truth and therefore the image expectation.
  22. - name: Load the role's defaults under a separate name
  23. ansible.builtin.include_vars:
  24. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  25. name: beeper_line_role_defaults
  26. - name: Wait for the beeper-line service to become active
  27. ansible.builtin.systemd_service:
  28. name: matrix-beeper-line.service
  29. register: beeper_line_service
  30. until: beeper_line_service.status.ActiveState == 'active'
  31. retries: 30
  32. delay: 5
  33. failed_when: false
  34. # Restart=always makes ActiveState insufficient for detecting a crash loop.
  35. - name: Assert the service is active and has not been restarting
  36. ansible.builtin.assert:
  37. that:
  38. - beeper_line_service.status.ActiveState == 'active'
  39. - beeper_line_service.status.NRestarts is defined
  40. - beeper_line_service.status.NRestarts | int == 0
  41. fail_msg: >-
  42. matrix-beeper-line.service is {{ beeper_line_service.status.ActiveState | default('unknown') }}
  43. after {{ beeper_line_service.status.NRestarts | default('?') }} restart(s)
  44. success_msg: "matrix-beeper-line.service is active and has not restarted"
  45. - name: Wait for the bridge liveness endpoint
  46. ansible.builtin.command:
  47. argv:
  48. - docker
  49. - run
  50. - --rm
  51. - --network={{ matrix_bridge_beeper_line_container_network }}
  52. - "{{ molecule_shared_image_curl }}"
  53. - --silent
  54. - --output
  55. - /dev/null
  56. - --write-out
  57. - "HTTP_STATUS=%{http_code}"
  58. - http://matrix-beeper-line:29322/_matrix/mau/live
  59. register: beeper_line_live
  60. changed_when: false
  61. until: beeper_line_live.rc == 0 and beeper_line_live.stdout == 'HTTP_STATUS=200'
  62. retries: 24
  63. delay: 5
  64. failed_when: false
  65. - name: Wait for the bridge readiness endpoint
  66. ansible.builtin.command:
  67. argv:
  68. - docker
  69. - run
  70. - --rm
  71. - --network={{ matrix_bridge_beeper_line_container_network }}
  72. - "{{ molecule_shared_image_curl }}"
  73. - --silent
  74. - --output
  75. - /dev/null
  76. - --write-out
  77. - "HTTP_STATUS=%{http_code}"
  78. - http://matrix-beeper-line:29322/_matrix/mau/ready
  79. register: beeper_line_ready
  80. changed_when: false
  81. until: beeper_line_ready.rc == 0 and beeper_line_ready.stdout == 'HTTP_STATUS=200'
  82. retries: 24
  83. delay: 5
  84. failed_when: false
  85. - name: Assert the bridge is live and ready on the configured appservice port
  86. ansible.builtin.assert:
  87. that:
  88. - beeper_line_live.rc == 0
  89. - beeper_line_live.stdout == 'HTTP_STATUS=200'
  90. - beeper_line_ready.rc == 0
  91. - beeper_line_ready.stdout == 'HTTP_STATUS=200'
  92. fail_msg: >-
  93. Health probes returned live={{ beeper_line_live.stdout | default('none') }} and
  94. ready={{ beeper_line_ready.stdout | default('none') }}
  95. success_msg: "The bridge is live and ready on its appservice port"
  96. # This is a real inbound appservice handshake using the role-rendered hs_token.
  97. - name: Submit an empty appservice transaction with the configured token
  98. ansible.builtin.command:
  99. argv:
  100. - docker
  101. - run
  102. - --rm
  103. - --network={{ matrix_bridge_beeper_line_container_network }}
  104. - "{{ molecule_shared_image_curl }}"
  105. - --silent
  106. - --show-error
  107. - --fail-with-body
  108. - --request
  109. - PUT
  110. - --header
  111. - "Content-Type: application/json"
  112. - --header
  113. - "Authorization: Bearer {{ matrix_bridge_beeper_line_homeserver_token }}"
  114. - --data-binary
  115. - '{"events": [], "ephemeral": []}'
  116. - http://matrix-beeper-line:29322/_matrix/app/v1/transactions/molecule-coverage
  117. register: beeper_line_transaction
  118. changed_when: false
  119. - name: Submit an appservice transaction with an invalid token
  120. ansible.builtin.command:
  121. argv:
  122. - docker
  123. - run
  124. - --rm
  125. - --network={{ matrix_bridge_beeper_line_container_network }}
  126. - "{{ molecule_shared_image_curl }}"
  127. - --silent
  128. - --output
  129. - /dev/null
  130. - --write-out
  131. - "HTTP_STATUS=%{http_code}"
  132. - --request
  133. - PUT
  134. - --header
  135. - "Content-Type: application/json"
  136. - --header
  137. - "Authorization: Bearer definitely-wrong-token"
  138. - --data-binary
  139. - '{"events": [], "ephemeral": []}'
  140. - http://matrix-beeper-line:29322/_matrix/app/v1/transactions/molecule-unauthorized
  141. register: beeper_line_transaction_unauthorized
  142. changed_when: false
  143. failed_when: false
  144. - name: Assert the live appservice accepts only the configured handshake token
  145. ansible.builtin.assert:
  146. that:
  147. - beeper_line_transaction.rc == 0
  148. - beeper_line_transaction.stdout | from_json == {}
  149. - beeper_line_transaction_unauthorized.rc == 0
  150. - beeper_line_transaction_unauthorized.stdout == 'HTTP_STATUS=401'
  151. fail_msg: "The appservice transaction endpoint did not enforce the rendered hs_token"
  152. success_msg: "The live appservice accepts the configured hs_token and rejects another"
  153. - name: Read the configuration the role rendered
  154. ansible.builtin.slurp:
  155. src: "{{ matrix_bridge_beeper_line_config_path }}/config.yaml"
  156. register: beeper_line_config_file
  157. - name: Assert the rendered configuration carries the appservice identity
  158. ansible.builtin.assert:
  159. that:
  160. - beeper_line_config.homeserver.address == matrix_bridge_beeper_line_homeserver_address
  161. - beeper_line_config.homeserver.domain == matrix_bridge_beeper_line_homeserver_domain
  162. - beeper_line_config.appservice.id == 'line'
  163. - beeper_line_config.appservice.address == 'http://matrix-beeper-line:29322'
  164. - beeper_line_config.appservice.public_address == beeper_line_expected_public_address
  165. - beeper_line_config.appservice.bot.username == matrix_bridge_beeper_line_appservice_bot_username
  166. - beeper_line_config.appservice.bot.displayname == matrix_bridge_beeper_line_appservice_bot_displayname
  167. - beeper_line_config.appservice.bot.avatar == matrix_bridge_beeper_line_appservice_bot_avatar
  168. - beeper_line_config.appservice.as_token == matrix_bridge_beeper_line_appservice_token
  169. - beeper_line_config.appservice.hs_token == matrix_bridge_beeper_line_homeserver_token
  170. fail_msg: "The rendered configuration does not carry the scenario's appservice identity"
  171. success_msg: "The rendered configuration carries the scenario's appservice identity"
  172. - name: Assert the rendered configuration carries non-default bridge behavior
  173. ansible.builtin.assert:
  174. that:
  175. - beeper_line_config.bridge.command_prefix == matrix_bridge_beeper_line_command_prefix
  176. - not beeper_line_config.bridge.personal_filtering_spaces
  177. - beeper_line_config.bridge.permissions['*'] == 'relay'
  178. - beeper_line_config.bridge.permissions[matrix_bridge_beeper_line_homeserver_domain] == 'admin'
  179. - not beeper_line_config.matrix.federate_rooms
  180. - beeper_line_config.provisioning.shared_secret == matrix_bridge_beeper_line_provisioning_shared_secret
  181. - beeper_line_config.logging.min_level == matrix_bridge_beeper_line_logging_level
  182. fail_msg: "The rendered configuration does not carry the scenario's bridge behavior"
  183. success_msg: "The rendered configuration carries the scenario's bridge behavior"
  184. - name: Assert the rendered configuration carries the non-default backfill policy
  185. ansible.builtin.assert:
  186. that:
  187. - beeper_line_config.backfill.enabled
  188. - beeper_line_config.backfill.max_initial_messages == 27
  189. - beeper_line_config.backfill.max_catchup_messages == 43
  190. - beeper_line_config.backfill.unread_hours_threshold == 168
  191. fail_msg: "The rendered configuration does not carry the scenario's backfill policy"
  192. success_msg: "The rendered configuration carries the scenario's backfill policy"
  193. - name: Assert the configuration points at the scenario's Postgres database
  194. ansible.builtin.assert:
  195. that:
  196. - beeper_line_config.database.type == 'postgres'
  197. - matrix_bridge_beeper_line_database_username in beeper_line_config.database.uri
  198. - matrix_bridge_beeper_line_database_hostname in beeper_line_config.database.uri
  199. - matrix_bridge_beeper_line_database_name in beeper_line_config.database.uri
  200. fail_msg: >-
  201. database.uri is {{ beeper_line_config.database.uri | default('unset') }}, which was not
  202. composed from the scenario's connection settings
  203. success_msg: "The configuration points at the scenario's Postgres database"
  204. - name: Read the appservice registration the role rendered
  205. ansible.builtin.slurp:
  206. src: "{{ matrix_bridge_beeper_line_config_path }}/registration.yaml"
  207. register: beeper_line_registration_file
  208. - name: Assert the registration carries the configured handshake values
  209. ansible.builtin.assert:
  210. that:
  211. - beeper_line_registration.id == 'line'
  212. - beeper_line_registration.as_token == matrix_bridge_beeper_line_appservice_token
  213. - beeper_line_registration.hs_token == matrix_bridge_beeper_line_homeserver_token
  214. - beeper_line_registration.sender_localpart == '_bot_' + matrix_bridge_beeper_line_appservice_bot_username
  215. - beeper_line_registration.url == 'http://matrix-beeper-line:29322'
  216. fail_msg: "The registration does not carry the configured handshake values"
  217. success_msg: "The registration carries the configured handshake values"
  218. - name: Assert the registration namespaces cover only LINE ghosts and the bridge bot
  219. ansible.builtin.assert:
  220. that:
  221. - beeper_line_ghost_regex | length > 0
  222. - beeper_line_bot_regex | length > 0
  223. - beeper_line_ghost_mxid is match(beeper_line_ghost_regex)
  224. - beeper_line_wrong_ghost_mxid is not match(beeper_line_ghost_regex)
  225. - beeper_line_bot_mxid is match(beeper_line_bot_regex)
  226. fail_msg: "The registration namespaces do not cover only LINE ghosts and the bot"
  227. success_msg: "The registration namespaces cover only LINE ghosts and the bot"
  228. vars:
  229. beeper_line_user_regexes: "{{ beeper_line_registration.namespaces.users | map(attribute='regex') | list }}"
  230. beeper_line_ghost_regex: "{{ beeper_line_user_regexes | select('search', 'line_') | first | default('') }}"
  231. beeper_line_bot_regex: "{{ beeper_line_user_regexes | reject('search', 'line_') | first | default('') }}"
  232. beeper_line_ghost_mxid: "@line_moleculefixture:{{ matrix_bridge_beeper_line_homeserver_domain }}"
  233. beeper_line_wrong_ghost_mxid: "@twitter_moleculefixture:{{ matrix_bridge_beeper_line_homeserver_domain }}"
  234. beeper_line_bot_mxid: "@{{ matrix_bridge_beeper_line_appservice_bot_username }}:{{ matrix_bridge_beeper_line_homeserver_domain }}"
  235. # Tables can appear only after hostname resolution, authentication, and real migrations.
  236. - name: List the tables the bridge created in Postgres
  237. ansible.builtin.command:
  238. argv:
  239. - docker
  240. - exec
  241. - matrix-postgres-molecule
  242. - psql
  243. - --username={{ matrix_bridge_beeper_line_database_username }}
  244. - --dbname={{ matrix_bridge_beeper_line_database_name }}
  245. - --tuples-only
  246. - --no-align
  247. - "--command=SELECT tablename FROM pg_tables WHERE schemaname = 'public'"
  248. register: beeper_line_tables
  249. changed_when: false
  250. - name: Assert the bridge migrated its schema into the configured database
  251. ansible.builtin.assert:
  252. that:
  253. - beeper_line_tables.rc == 0
  254. - "'version' in beeper_line_table_names"
  255. - "'portal' in beeper_line_table_names"
  256. - "'user_login' in beeper_line_table_names"
  257. - "'message' in beeper_line_table_names"
  258. - beeper_line_table_names | length > 10
  259. fail_msg: >-
  260. The bridge did not migrate its schema into {{ matrix_bridge_beeper_line_database_name }}
  261. (found {{ beeper_line_table_names | length }} table(s))
  262. success_msg: "The bridge migrated its schema into the configured database"
  263. vars:
  264. beeper_line_table_names: "{{ beeper_line_tables.stdout_lines | select | list }}"
  265. - name: Read the labels the role rendered
  266. ansible.builtin.slurp:
  267. src: "{{ matrix_bridge_beeper_line_base_path }}/labels"
  268. register: beeper_line_labels_file
  269. - name: Assert the labels route the public endpoint to the appservice port
  270. ansible.builtin.assert:
  271. that:
  272. - "'traefik.enable=true' in beeper_line_labels_rendered"
  273. - "'traefik.docker.network=' + matrix_bridge_beeper_line_container_network in beeper_line_labels_rendered"
  274. - "'traefik.http.services.matrix-beeper-line-exposure.loadbalancer.server.port=29322' in beeper_line_labels_rendered"
  275. - "'traefik.http.routers.matrix-beeper-line-exposure.rule=Host(`line-api.molecule.local`) && PathPrefix(`/bridges/line-api`)' in beeper_line_labels_rendered"
  276. - "'traefik.http.middlewares.matrix-beeper-line-exposure-strip-prefix.stripprefix.prefixes=/bridges/line-api' in beeper_line_labels_rendered"
  277. - "'traefik.http.routers.matrix-beeper-line-exposure.priority=1722' in beeper_line_labels_rendered"
  278. - "'traefik.http.routers.matrix-beeper-line-exposure.entrypoints=web' in beeper_line_labels_rendered"
  279. - "'traefik.http.routers.matrix-beeper-line-exposure.tls=false' in beeper_line_labels_rendered"
  280. - "'molecule.beeper-line.coverage=enabled' in beeper_line_labels_rendered"
  281. fail_msg: "The rendered labels do not carry the scenario's exposure configuration"
  282. success_msg: "The rendered labels carry the scenario's exposure configuration"
  283. - name: Inspect the running bridge container
  284. ansible.builtin.command:
  285. argv:
  286. - docker
  287. - container
  288. - inspect
  289. - matrix-beeper-line
  290. register: beeper_line_container_inspect
  291. changed_when: false
  292. - name: Parse the running bridge container inspection
  293. ansible.builtin.set_fact:
  294. beeper_line_container: "{{ (beeper_line_container_inspect.stdout | from_json) | first }}"
  295. - name: Assert the running container uses the exact image pinned by the role
  296. ansible.builtin.assert:
  297. that:
  298. - beeper_line_container.Config.Image == beeper_line_expected_image
  299. fail_msg: >-
  300. The running container uses {{ beeper_line_container.Config.Image }}, expected
  301. {{ beeper_line_expected_image }}
  302. success_msg: "The running container uses the exact image pinned by the role"
  303. vars:
  304. beeper_line_expected_image: >-
  305. {{ beeper_line_role_defaults.matrix_bridge_beeper_line_container_image_registry_prefix_upstream_default }}crispyduck/beeper-line:{{ beeper_line_role_defaults.matrix_bridge_beeper_line_version }}
  306. - name: Assert the running container uses the playbook-supplied runtime
  307. ansible.builtin.assert:
  308. that:
  309. - beeper_line_container.Config.User == (matrix_user_uid | string) + ':' + (matrix_user_gid | string)
  310. - beeper_line_container.Config.WorkingDir == '/data'
  311. - beeper_line_container.Config.Cmd == ['/usr/bin/matrix-line', '-c', '/config/config.yaml', '-r', '/config/registration.yaml', '--no-update']
  312. - beeper_line_container.Config.Env is contains('MOLECULE_BEEPER_LINE_COVERAGE=enabled')
  313. - beeper_line_container.HostConfig.RestartPolicy.Name == 'no'
  314. - beeper_line_container.HostConfig.AutoRemove
  315. - beeper_line_container.HostConfig.LogConfig.Type == 'none'
  316. fail_msg: "The running container does not use the role's exact identity and command"
  317. success_msg: "The running container uses the role's exact identity, command, and extra argument"
  318. - name: Assert the running container has the intended privilege isolation
  319. ansible.builtin.assert:
  320. that:
  321. - "'ALL' in beeper_line_container.HostConfig.CapDrop"
  322. - not beeper_line_container.HostConfig.Privileged
  323. fail_msg: "The running container does not have the intended privilege isolation"
  324. success_msg: "The running container drops all capabilities and is unprivileged"
  325. - name: Assert the running container carries the role's exact bind mounts
  326. ansible.builtin.assert:
  327. that:
  328. - beeper_line_config_mount | length > 0
  329. - beeper_line_config_mount.Source == matrix_bridge_beeper_line_config_path
  330. - not beeper_line_config_mount.RW
  331. - beeper_line_data_mount | length > 0
  332. - beeper_line_data_mount.Source == matrix_bridge_beeper_line_data_path
  333. - beeper_line_data_mount.RW
  334. - beeper_line_container.Mounts | length == 2
  335. fail_msg: "The running container does not carry the role's exact config and data mounts"
  336. success_msg: "The running container carries read-only config and writable data mounts"
  337. vars:
  338. beeper_line_config_mount: >-
  339. {{ beeper_line_container.Mounts
  340. | selectattr('Destination', 'equalto', '/config')
  341. | first | default({}) }}
  342. beeper_line_data_mount: >-
  343. {{ beeper_line_container.Mounts
  344. | selectattr('Destination', 'equalto', '/data')
  345. | first | default({}) }}
  346. - name: Assert the rendered labels are attached to the running container
  347. ansible.builtin.assert:
  348. that:
  349. - beeper_line_container.Config.Labels['traefik.enable'] == 'true'
  350. - beeper_line_container.Config.Labels['traefik.docker.network'] == matrix_bridge_beeper_line_container_network
  351. - beeper_line_container.Config.Labels['traefik.http.services.matrix-beeper-line-exposure.loadbalancer.server.port'] == '29322'
  352. - beeper_line_container.Config.Labels['molecule.beeper-line.coverage'] == 'enabled'
  353. fail_msg: "The running container does not carry the labels the role rendered"
  354. success_msg: "The running container carries the labels the role rendered"
  355. - name: Assert the running container has the exact network and port exposure
  356. ansible.builtin.assert:
  357. that:
  358. - beeper_line_container.HostConfig.NetworkMode == matrix_bridge_beeper_line_container_network
  359. - matrix_bridge_beeper_line_container_network in beeper_line_container.NetworkSettings.Networks
  360. - beeper_line_container.NetworkSettings.Networks | length == 1
  361. - beeper_line_container.HostConfig.PortBindings | default({}, true) | length == 0
  362. fail_msg: >-
  363. The container has unexpected networks or host ports:
  364. networks={{ beeper_line_container.NetworkSettings.Networks.keys() | list }},
  365. ports={{ beeper_line_container.HostConfig.PortBindings | default({}) }}
  366. success_msg: "The container uses only its dedicated network and publishes no host ports"