Matrix Docker Ansible eploy
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

299 linhas
16 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Health with safe mode disabled is the main readiness gate. Stub request evidence closes
  6. # Draupnir's remaining blind spot: its health turns green after launching, but not awaiting,
  7. # the SDK's first sync request.
  8. - name: Verify matrix-bot-draupnir
  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. matrix_bot_draupnir_rendered_config: "{{ matrix_bot_draupnir_config_file.content | b64decode | from_yaml }}"
  16. matrix_bot_draupnir_labels_lines: "{{ (matrix_bot_draupnir_labels_file.content | b64decode).splitlines() }}"
  17. matrix_bot_draupnir_runtime: "{{ (matrix_bot_draupnir_container_inspect.stdout | from_json) | first }}"
  18. matrix_bot_draupnir_config_mounts: "{{ matrix_bot_draupnir_runtime.Mounts | selectattr('Destination', 'equalto', '/data/config') | list }}"
  19. matrix_bot_draupnir_data_mounts: "{{ matrix_bot_draupnir_runtime.Mounts | selectattr('Destination', 'equalto', '/data') | list }}"
  20. gather_facts: false
  21. tasks:
  22. # Read the pin from the role rather than defining it in the scenario, so an image bump
  23. # changes the expected value and exercises the newly shipped image.
  24. - name: Load the role's defaults under a separate name
  25. ansible.builtin.include_vars:
  26. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  27. name: matrix_bot_draupnir_role_defaults
  28. - name: Wait for the matrix-bot-draupnir service to become active
  29. ansible.builtin.systemd_service:
  30. name: matrix-bot-draupnir.service
  31. register: matrix_bot_draupnir_service
  32. until: matrix_bot_draupnir_service.status.ActiveState == 'active'
  33. retries: 30
  34. delay: 5
  35. failed_when: false
  36. # Restart=always makes ActiveState alone insufficient for a crash-looping bot.
  37. - name: Assert the service is active and has not restarted
  38. ansible.builtin.assert:
  39. that:
  40. - matrix_bot_draupnir_service.status.ActiveState == 'active'
  41. - matrix_bot_draupnir_service.status.NRestarts is defined
  42. - matrix_bot_draupnir_service.status.NRestarts | int == 0
  43. fail_msg: >-
  44. matrix-bot-draupnir.service is
  45. {{ matrix_bot_draupnir_service.status.ActiveState | default('unknown') }} after
  46. {{ matrix_bot_draupnir_service.status.NRestarts | default('?') }} restart(s)
  47. success_msg: "matrix-bot-draupnir.service is active and has not restarted"
  48. # Probe over the role's own network. The non-default port and status are only reachable
  49. # after normal-mode bootstrap because the scenario explicitly disables recovery safe mode.
  50. - name: Wait for Draupnir to report healthy on the configured port
  51. ansible.builtin.command:
  52. argv:
  53. - docker
  54. - run
  55. - --rm
  56. - --network={{ matrix_bot_draupnir_container_network }}
  57. - "{{ molecule_shared_image_curl }}"
  58. - --silent
  59. - --show-error
  60. - --output
  61. - /dev/null
  62. - --write-out
  63. - "HTTP_STATUS=%{http_code}"
  64. - http://matrix-bot-draupnir:18081/molecule-ready
  65. register: matrix_bot_draupnir_health
  66. changed_when: false
  67. until: matrix_bot_draupnir_health.stdout == 'HTTP_STATUS=201'
  68. retries: 24
  69. delay: 5
  70. failed_when: false
  71. - name: Assert Draupnir reached its normal-mode health gate
  72. ansible.builtin.assert:
  73. that:
  74. - matrix_bot_draupnir_health.rc == 0
  75. - matrix_bot_draupnir_health.stdout == 'HTTP_STATUS=201'
  76. fail_msg: >-
  77. Draupnir did not report the configured healthy status on port 18081
  78. (rc={{ matrix_bot_draupnir_health.rc }}, response={{ matrix_bot_draupnir_health.stdout | default('none') }})
  79. success_msg: "Draupnir reports healthy with the scenario's non-default status"
  80. - name: Wait for Draupnir to request an initial sync from the homeserver stub
  81. ansible.builtin.command:
  82. argv:
  83. - docker
  84. - logs
  85. - matrix-homeserver-stub
  86. register: matrix_bot_draupnir_stub_logs
  87. changed_when: false
  88. until: "'/sync?' in matrix_bot_draupnir_stub_logs.stderr or '/sync?' in matrix_bot_draupnir_stub_logs.stdout"
  89. retries: 12
  90. delay: 5
  91. failed_when: false
  92. # These are observations of Draupnir's outbound bootstrap, not claims about the stub.
  93. - name: Assert Draupnir completed its Matrix identity and management-room bootstrap
  94. ansible.builtin.assert:
  95. that:
  96. - "'/account/whoami' in matrix_bot_draupnir_stub_request_log"
  97. - "'/joined_rooms' in matrix_bot_draupnir_stub_request_log"
  98. - "'/rooms/!draupnir-control%3Amolecule.local/state' in matrix_bot_draupnir_stub_request_log"
  99. fail_msg: "Draupnir did not perform all expected Matrix bootstrap requests"
  100. success_msg: "Draupnir performed its identity and management-room bootstrap"
  101. vars:
  102. matrix_bot_draupnir_stub_request_log: "{{ matrix_bot_draupnir_stub_logs.stdout + matrix_bot_draupnir_stub_logs.stderr }}"
  103. - name: Assert Draupnir entered the Matrix sync loop
  104. ansible.builtin.assert:
  105. that:
  106. - "'/sync?' in matrix_bot_draupnir_stub_request_log"
  107. fail_msg: "Draupnir became healthy without requesting an initial Matrix sync"
  108. success_msg: "Draupnir requested its initial Matrix sync"
  109. vars:
  110. matrix_bot_draupnir_stub_request_log: "{{ matrix_bot_draupnir_stub_logs.stdout + matrix_bot_draupnir_stub_logs.stderr }}"
  111. - name: Read the configuration file the role rendered
  112. ansible.builtin.slurp:
  113. src: "{{ matrix_bot_draupnir_config_path }}/production.yaml"
  114. register: matrix_bot_draupnir_config_file
  115. - name: Assert the rendered Matrix connection and management-room configuration
  116. ansible.builtin.assert:
  117. that:
  118. - matrix_bot_draupnir_rendered_config.homeserverUrl == matrix_bot_draupnir_config_homeserverUrl
  119. - matrix_bot_draupnir_rendered_config.rawHomeserverUrl == matrix_bot_draupnir_config_rawHomeserverUrl
  120. - matrix_bot_draupnir_rendered_config.accessToken == matrix_bot_draupnir_config_accessToken
  121. - matrix_bot_draupnir_rendered_config.managementRoom == matrix_bot_draupnir_config_managementRoom
  122. - matrix_bot_draupnir_rendered_config.experimentalRustCrypto is sameas false
  123. - matrix_bot_draupnir_rendered_config.pantalaimon is not defined
  124. fail_msg: "The rendered configuration does not carry the scenario's Matrix connection"
  125. success_msg: "The rendered configuration carries the scenario's Matrix connection"
  126. - name: Assert the rendered bootstrap and moderation behavior
  127. ansible.builtin.assert:
  128. that:
  129. - matrix_bot_draupnir_rendered_config.logLevel == 'DEBUG'
  130. - matrix_bot_draupnir_rendered_config.verifyPermissionsOnStartup is sameas false
  131. - matrix_bot_draupnir_rendered_config.noop is sameas true
  132. - matrix_bot_draupnir_rendered_config.disableServerACL is sameas true
  133. - matrix_bot_draupnir_rendered_config.backgroundDelayMS == 137
  134. - matrix_bot_draupnir_rendered_config.commands.allowNoPrefix is sameas true
  135. - matrix_bot_draupnir_rendered_config.roomStateBackingStore.enabled is sameas false
  136. fail_msg: "The rendered configuration does not carry the non-default Draupnir behavior"
  137. success_msg: "The rendered configuration carries the non-default Draupnir behavior"
  138. - name: Assert recovery safe mode is disabled in the rendered configuration
  139. ansible.builtin.assert:
  140. that:
  141. - matrix_bot_draupnir_rendered_config.safeMode.bootOption == 'Never'
  142. fail_msg: "Recovery safe mode could mask a failed normal Draupnir bootstrap"
  143. success_msg: "Recovery safe mode cannot mask the scenario's readiness gate"
  144. - name: Assert the rendered health listener configuration
  145. ansible.builtin.assert:
  146. that:
  147. - matrix_bot_draupnir_rendered_config.health.healthz.enabled is sameas true
  148. - matrix_bot_draupnir_rendered_config.health.healthz.port == 18081
  149. - matrix_bot_draupnir_rendered_config.health.healthz.address == '0.0.0.0'
  150. - matrix_bot_draupnir_rendered_config.health.healthz.endpoint == '/molecule-ready'
  151. - matrix_bot_draupnir_rendered_config.health.healthz.healthyStatus == 201
  152. - matrix_bot_draupnir_rendered_config.health.healthz.unhealthyStatus == 503
  153. fail_msg: "The rendered health listener does not carry the scenario's settings"
  154. success_msg: "The rendered health listener carries the scenario's settings"
  155. - name: Assert the rendered abuse-reporting web listener configuration
  156. ansible.builtin.assert:
  157. that:
  158. - matrix_bot_draupnir_rendered_config.web.enabled is sameas true
  159. - matrix_bot_draupnir_rendered_config.web.port == matrix_bot_draupnir_config_web_port
  160. - matrix_bot_draupnir_rendered_config.web.address == '0.0.0.0'
  161. - matrix_bot_draupnir_rendered_config.web.abuseReporting.enabled is sameas true
  162. - matrix_bot_draupnir_rendered_config.web.synapseHTTPAntispam.enabled is sameas false
  163. - matrix_bot_draupnir_rendered_config.displayReports is sameas false
  164. fail_msg: "The rendered web configuration does not carry the abuse-reporting settings"
  165. success_msg: "The rendered web listener carries the abuse-reporting settings"
  166. - name: Read the labels the role rendered
  167. ansible.builtin.slurp:
  168. src: "{{ matrix_bot_draupnir_base_path }}/labels"
  169. register: matrix_bot_draupnir_labels_file
  170. - name: Assert the labels carry the configured public report routing
  171. ansible.builtin.assert:
  172. that:
  173. - "'traefik.enable=true' in matrix_bot_draupnir_labels_lines"
  174. - "'traefik.docker.network=' + matrix_bot_draupnir_container_network in matrix_bot_draupnir_labels_lines"
  175. - "'traefik.http.services.matrix-bot-draupnir.loadbalancer.server.port=18082' in matrix_bot_draupnir_labels_lines"
  176. - "'traefik.http.routers.matrix-bot-draupnir-web-abuseReporting.rule=Host(`draupnir-reports.molecule.local`) && PathRegexp(`^/molecule-report/(v1)/rooms/([^/]*)/event/(.*)$`)' in matrix_bot_draupnir_labels_lines"
  177. - "'traefik.http.routers.matrix-bot-draupnir-web-abuseReporting.priority=743' in matrix_bot_draupnir_labels_lines"
  178. - "'traefik.http.routers.matrix-bot-draupnir-web-abuseReporting.entrypoints=web' in matrix_bot_draupnir_labels_lines"
  179. - "'traefik.http.routers.matrix-bot-draupnir-web-abuseReporting.tls=false' in matrix_bot_draupnir_labels_lines"
  180. - "'molecule.draupnir.coverage=enabled' in matrix_bot_draupnir_labels_lines"
  181. fail_msg: "The role's label file does not carry the scenario's public report routing"
  182. success_msg: "The role's label file carries the scenario's public report routing"
  183. - name: Inspect the running Draupnir container
  184. ansible.builtin.command:
  185. argv:
  186. - docker
  187. - container
  188. - inspect
  189. - matrix-bot-draupnir
  190. register: matrix_bot_draupnir_container_inspect
  191. changed_when: false
  192. - name: Assert the running container uses the exact image pinned by the role
  193. ansible.builtin.assert:
  194. that:
  195. - matrix_bot_draupnir_runtime.Config.Image == 'ghcr.io/the-draupnir-project/draupnir:' + matrix_bot_draupnir_role_defaults.matrix_bot_draupnir_version
  196. fail_msg: "The running Draupnir container does not use the role's exact image pin"
  197. success_msg: "The running Draupnir container uses the role's exact image pin"
  198. - name: Assert the running container uses the playbook-supplied identity
  199. ansible.builtin.assert:
  200. that:
  201. - matrix_bot_draupnir_runtime.Config.User.split(':')[0] == matrix_user_uid | string
  202. - matrix_bot_draupnir_runtime.Config.User.split(':')[1] == matrix_user_gid | string
  203. fail_msg: >-
  204. Draupnir runs as {{ matrix_bot_draupnir_runtime.Config.User }} instead of
  205. {{ matrix_user_uid }}:{{ matrix_user_gid }}
  206. success_msg: "The running container uses the playbook-supplied UID and GID"
  207. - name: Assert the container drops all Linux capabilities
  208. ansible.builtin.assert:
  209. that:
  210. - matrix_bot_draupnir_runtime.HostConfig.CapDrop is sequence
  211. - "'ALL' in matrix_bot_draupnir_runtime.HostConfig.CapDrop"
  212. fail_msg: "The Draupnir container does not drop all Linux capabilities"
  213. success_msg: "The Draupnir container drops all Linux capabilities"
  214. - name: Assert the container root filesystem is read-only
  215. ansible.builtin.assert:
  216. that:
  217. - matrix_bot_draupnir_runtime.HostConfig.ReadonlyRootfs is sameas true
  218. fail_msg: "The Draupnir container root filesystem is writable"
  219. success_msg: "The Draupnir container root filesystem is read-only"
  220. - name: Assert the configuration bind mount is read-only
  221. ansible.builtin.assert:
  222. that:
  223. - matrix_bot_draupnir_config_mounts | length == 1
  224. - matrix_bot_draupnir_config_mounts[0].RW is sameas false
  225. fail_msg: "The Draupnir configuration bind mount is missing or writable"
  226. success_msg: "The Draupnir configuration bind mount is present and read-only"
  227. - name: Assert the data bind mount is writable
  228. ansible.builtin.assert:
  229. that:
  230. - matrix_bot_draupnir_data_mounts | length == 1
  231. - matrix_bot_draupnir_data_mounts[0].RW is sameas true
  232. fail_msg: "The Draupnir data bind mount is missing or read-only"
  233. success_msg: "The Draupnir data bind mount is present and writable"
  234. - name: Assert the container is attached only to its dedicated network
  235. ansible.builtin.assert:
  236. that:
  237. - matrix_bot_draupnir_runtime.NetworkSettings.Networks is mapping
  238. - matrix_bot_draupnir_runtime.NetworkSettings.Networks | length == 1
  239. - matrix_bot_draupnir_container_network in matrix_bot_draupnir_runtime.NetworkSettings.Networks
  240. fail_msg: >-
  241. Draupnir has unexpected networks:
  242. {{ matrix_bot_draupnir_runtime.NetworkSettings.Networks.keys() | list }}
  243. success_msg: "The Draupnir container is attached only to its dedicated network"
  244. - name: Ask Docker for Draupnir's published ports
  245. ansible.builtin.command:
  246. argv:
  247. - docker
  248. - container
  249. - port
  250. - matrix-bot-draupnir
  251. register: matrix_bot_draupnir_published_ports
  252. changed_when: false
  253. failed_when: false
  254. - name: Assert the role did not publish a host port
  255. ansible.builtin.assert:
  256. that:
  257. - matrix_bot_draupnir_runtime.HostConfig.PortBindings | default({}, true) | length == 0
  258. - matrix_bot_draupnir_published_ports.rc == 0
  259. - matrix_bot_draupnir_published_ports.stdout | trim | length == 0
  260. fail_msg: >-
  261. Draupnir unexpectedly publishes a host port:
  262. {{ matrix_bot_draupnir_published_ports.stdout | default('unknown') }}
  263. success_msg: "The role leaves Draupnir's HTTP ports unpublished"
  264. - name: Assert Docker accepted the role's custom label
  265. ansible.builtin.assert:
  266. that:
  267. - matrix_bot_draupnir_runtime.Config.Labels is mapping
  268. - matrix_bot_draupnir_runtime.Config.Labels['molecule.draupnir.coverage'] == 'enabled'
  269. fail_msg: "Docker did not attach the custom label from the role's label file"
  270. success_msg: "Docker accepted the custom label from the role's label file"