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

250 строки
12 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Ketesa is a static client. The weight-bearing checks are its real HTTP root and the
  6. # config.json served from the bind mount; no live Synapse or credentials are involved.
  7. - name: Verify matrix-ketesa
  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. matrix_ketesa_rendered_config: "{{ matrix_ketesa_config_file.content | b64decode | from_json }}"
  15. matrix_ketesa_root_body: "{{ matrix_ketesa_root_http.stdout_lines[:-1] | join('\n') }}"
  16. matrix_ketesa_served_config_body: "{{ matrix_ketesa_config_http.stdout_lines[:-1] | join('\n') }}"
  17. matrix_ketesa_served_config: "{{ matrix_ketesa_served_config_body | from_json }}"
  18. matrix_ketesa_labels_lines: "{{ (matrix_ketesa_labels_file.content | b64decode).splitlines() }}"
  19. matrix_ketesa_runtime: "{{ (matrix_ketesa_container_inspect.stdout | from_json) | first }}"
  20. matrix_ketesa_config_mounts: "{{ matrix_ketesa_runtime.Mounts | selectattr('Destination', 'equalto', '/var/public/config.json') | list }}"
  21. gather_facts: false
  22. tasks:
  23. # Read the pin from the role rather than defining it in the scenario, so an image bump
  24. # changes the expected value and tests the image that the role actually ships.
  25. - name: Load the role's defaults under a separate name
  26. ansible.builtin.include_vars:
  27. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  28. name: matrix_ketesa_role_defaults
  29. - name: Wait for the matrix-ketesa service to become active
  30. ansible.builtin.systemd_service:
  31. name: matrix-ketesa.service
  32. register: matrix_ketesa_service
  33. until: matrix_ketesa_service.status.ActiveState == 'active'
  34. retries: 30
  35. delay: 5
  36. failed_when: false
  37. # Restart=always makes ActiveState alone insufficient: a container can crash-loop while
  38. # systemd continues to report its unit as active.
  39. - name: Assert the service is active and has not restarted
  40. ansible.builtin.assert:
  41. that:
  42. - matrix_ketesa_service.status.ActiveState == 'active'
  43. - matrix_ketesa_service.status.NRestarts is defined
  44. - matrix_ketesa_service.status.NRestarts | int == 0
  45. fail_msg: >-
  46. matrix-ketesa.service is
  47. {{ matrix_ketesa_service.status.ActiveState | default('unknown') }} after
  48. {{ matrix_ketesa_service.status.NRestarts | default('?') }} restart(s)
  49. success_msg: "matrix-ketesa.service is active and has not restarted"
  50. - name: Read the configuration file the role rendered
  51. ansible.builtin.slurp:
  52. src: "{{ matrix_ketesa_config_path }}/config.json"
  53. register: matrix_ketesa_config_file
  54. - name: Assert the rendered JSON carries the scenario's Ketesa configuration
  55. ansible.builtin.assert:
  56. that:
  57. - matrix_ketesa_rendered_config.keys() | sort == ['asManagedUsers', 'corsCredentials', 'externalAuthProvider', 'menu', 'restrictBaseUrl', 'wellKnownDiscovery'] | sort
  58. - matrix_ketesa_rendered_config.restrictBaseUrl == matrix_ketesa_config_restrictBaseUrl
  59. - matrix_ketesa_rendered_config.externalAuthProvider is sameas true
  60. - matrix_ketesa_rendered_config.wellKnownDiscovery is sameas false
  61. - matrix_ketesa_rendered_config.corsCredentials == matrix_ketesa_config_corsCredentials
  62. - matrix_ketesa_rendered_config.asManagedUsers == matrix_ketesa_config_asManagedUsers_custom
  63. - matrix_ketesa_rendered_config.menu | length == 1
  64. - matrix_ketesa_rendered_config.menu[0].label == 'Molecule support'
  65. - matrix_ketesa_rendered_config.menu[0].i18n.de == 'Molekül-Support'
  66. - matrix_ketesa_rendered_config.menu[0].i18n.fr == 'Assistance Molécule'
  67. - matrix_ketesa_rendered_config.menu[0].icon == 'SupportAgent'
  68. - matrix_ketesa_rendered_config.menu[0].url == 'https://support.molecule.local/ketesa'
  69. fail_msg: "The rendered JSON does not carry the scenario's non-default Ketesa settings"
  70. success_msg: "The rendered JSON carries the scenario's non-default Ketesa settings"
  71. # Probe over the role's own network. Publishing a test-only host port would diverge from
  72. # the deployment path and would collide when scenarios run concurrently.
  73. - name: Wait for Ketesa to serve its application root
  74. ansible.builtin.command:
  75. argv:
  76. - docker
  77. - run
  78. - --rm
  79. - --network={{ matrix_ketesa_container_network }}
  80. - "{{ molecule_shared_image_curl }}"
  81. - --silent
  82. - --show-error
  83. - --write-out
  84. - "\nHTTP_STATUS=%{http_code}"
  85. - http://matrix-ketesa:8080/
  86. register: matrix_ketesa_root_http
  87. changed_when: false
  88. until: "'HTTP_STATUS=200' in matrix_ketesa_root_http.stdout"
  89. retries: 24
  90. delay: 5
  91. failed_when: false
  92. - name: Assert the application root is real Ketesa HTML
  93. ansible.builtin.assert:
  94. that:
  95. - matrix_ketesa_root_http.rc == 0
  96. - matrix_ketesa_root_http.stdout_lines[-1] == 'HTTP_STATUS=200'
  97. - matrix_ketesa_root_body | trim | length > 100
  98. - "'<!doctype html' in matrix_ketesa_root_body | lower"
  99. - "'ketesa' in matrix_ketesa_root_body | lower"
  100. fail_msg: >-
  101. Ketesa did not return its application HTML from the container network
  102. (rc={{ matrix_ketesa_root_http.rc }}, status={{ matrix_ketesa_root_http.stdout_lines[-1] | default('missing') }})
  103. success_msg: "Ketesa serves its application root over HTTP"
  104. - name: Fetch the config.json that Ketesa serves
  105. ansible.builtin.command:
  106. argv:
  107. - docker
  108. - run
  109. - --rm
  110. - --network={{ matrix_ketesa_container_network }}
  111. - "{{ molecule_shared_image_curl }}"
  112. - --silent
  113. - --show-error
  114. - --write-out
  115. - "\nHTTP_STATUS=%{http_code}"
  116. - http://matrix-ketesa:8080/config.json
  117. register: matrix_ketesa_config_http
  118. changed_when: false
  119. until: "'HTTP_STATUS=200' in matrix_ketesa_config_http.stdout"
  120. retries: 24
  121. delay: 5
  122. failed_when: false
  123. - name: Assert Ketesa serves the parsed JSON that the role rendered
  124. ansible.builtin.assert:
  125. that:
  126. - matrix_ketesa_config_http.rc == 0
  127. - matrix_ketesa_config_http.stdout_lines[-1] == 'HTTP_STATUS=200'
  128. - matrix_ketesa_served_config is mapping
  129. - matrix_ketesa_served_config == matrix_ketesa_rendered_config
  130. fail_msg: "The config.json served by Ketesa does not match the role's rendered JSON"
  131. success_msg: "Ketesa serves the exact parsed JSON that the role rendered"
  132. - name: Read the labels the role rendered
  133. ansible.builtin.slurp:
  134. src: "{{ matrix_ketesa_base_path }}/labels"
  135. register: matrix_ketesa_labels_file
  136. - name: Assert the labels carry the configured public exposure
  137. ansible.builtin.assert:
  138. that:
  139. - "'traefik.enable=true' in matrix_ketesa_labels_lines"
  140. - "'traefik.docker.network=matrix-ketesa-molecule' in matrix_ketesa_labels_lines"
  141. - "'traefik.http.services.matrix-ketesa.loadbalancer.server.port=8080' in matrix_ketesa_labels_lines"
  142. - "'traefik.http.middlewares.matrix-ketesa-ipallowlist.ipallowlist.sourcerange=192.0.2.0/24,2001:db8::/32' in matrix_ketesa_labels_lines"
  143. - "'traefik.http.middlewares.matrix-ketesa-strip-prefix.stripprefix.prefixes=/molecule-ketesa' in matrix_ketesa_labels_lines"
  144. - "'traefik.http.middlewares.matrix-ketesa-add-headers.headers.customresponseheaders.X-Molecule-Ketesa=role-scenario' in matrix_ketesa_labels_lines"
  145. - "'traefik.http.routers.matrix-ketesa.rule=Host(`ketesa-admin.molecule.local`) && PathPrefix(`/molecule-ketesa`)' in matrix_ketesa_labels_lines"
  146. - "'traefik.http.routers.matrix-ketesa.priority=731' in matrix_ketesa_labels_lines"
  147. - "'traefik.http.routers.matrix-ketesa.middlewares=matrix-ketesa-ipallowlist,matrix-ketesa-slashless-redirect,matrix-ketesa-strip-prefix,matrix-ketesa-add-headers' in matrix_ketesa_labels_lines"
  148. - "'traefik.http.routers.matrix-ketesa.entrypoints=web' in matrix_ketesa_labels_lines"
  149. - "'traefik.http.routers.matrix-ketesa.tls=false' in matrix_ketesa_labels_lines"
  150. - "'molecule.ketesa.coverage=enabled' in matrix_ketesa_labels_lines"
  151. fail_msg: "The role's label file does not carry the scenario's exposure settings"
  152. success_msg: "The role's label file carries the scenario's exposure settings"
  153. - name: Inspect the running Ketesa container
  154. ansible.builtin.command:
  155. argv:
  156. - docker
  157. - container
  158. - inspect
  159. - matrix-ketesa
  160. register: matrix_ketesa_container_inspect
  161. changed_when: false
  162. - name: Assert the running container uses the exact image pinned by the role
  163. ansible.builtin.assert:
  164. that:
  165. - matrix_ketesa_runtime.Config.Image == 'ghcr.io/etkecc/ketesa:' + matrix_ketesa_role_defaults.matrix_ketesa_version
  166. fail_msg: >-
  167. The running image {{ matrix_ketesa_runtime.Config.Image }} is not the exact
  168. ghcr.io/etkecc/ketesa:{{ matrix_ketesa_role_defaults.matrix_ketesa_version }} pin
  169. success_msg: "The running container uses the exact image pinned by the role"
  170. - name: Assert the running container uses the dedicated Ketesa identity
  171. ansible.builtin.assert:
  172. that:
  173. - matrix_ketesa_runtime.Config.User.split(':')[0] == matrix_ketesa_container_uid | string
  174. - matrix_ketesa_runtime.Config.User.split(':')[1] == matrix_ketesa_container_gid | string
  175. fail_msg: >-
  176. The running container uses {{ matrix_ketesa_runtime.Config.User }} instead of
  177. {{ matrix_ketesa_container_uid }}:{{ matrix_ketesa_container_gid }}
  178. success_msg: "The running container uses the dedicated Ketesa UID and GID"
  179. - name: Assert the container root filesystem is read-only
  180. ansible.builtin.assert:
  181. that:
  182. - matrix_ketesa_runtime.HostConfig.ReadonlyRootfs is sameas true
  183. fail_msg: "The Ketesa container root filesystem is writable"
  184. success_msg: "The Ketesa container root filesystem is read-only"
  185. - name: Assert the configuration bind mount is read-only
  186. ansible.builtin.assert:
  187. that:
  188. - matrix_ketesa_config_mounts | length == 1
  189. - matrix_ketesa_config_mounts[0].RW is sameas false
  190. fail_msg: "The Ketesa configuration bind mount is missing or writable"
  191. success_msg: "The Ketesa configuration bind mount is present and read-only"
  192. - name: Assert the container is attached only to its dedicated network
  193. ansible.builtin.assert:
  194. that:
  195. - matrix_ketesa_runtime.NetworkSettings.Networks is mapping
  196. - matrix_ketesa_runtime.NetworkSettings.Networks | length == 1
  197. - matrix_ketesa_container_network in matrix_ketesa_runtime.NetworkSettings.Networks
  198. fail_msg: >-
  199. Ketesa has unexpected network attachments:
  200. {{ matrix_ketesa_runtime.NetworkSettings.Networks.keys() | list }}
  201. success_msg: "The container is attached only to its dedicated network"
  202. - name: Ask Docker for Ketesa's published ports
  203. ansible.builtin.command:
  204. argv:
  205. - docker
  206. - container
  207. - port
  208. - matrix-ketesa
  209. register: matrix_ketesa_published_ports
  210. changed_when: false
  211. failed_when: false
  212. - name: Assert the role did not publish a host port
  213. ansible.builtin.assert:
  214. that:
  215. - matrix_ketesa_runtime.HostConfig.PortBindings | default({}, true) | length == 0
  216. - matrix_ketesa_published_ports.rc == 0
  217. - matrix_ketesa_published_ports.stdout | trim | length == 0
  218. fail_msg: >-
  219. Ketesa unexpectedly publishes a host port:
  220. {{ matrix_ketesa_published_ports.stdout | default('unknown') }}
  221. success_msg: "The role leaves Ketesa's HTTP port unpublished"
  222. - name: Assert Docker accepted the role's custom label
  223. ansible.builtin.assert:
  224. that:
  225. - matrix_ketesa_runtime.Config.Labels is mapping
  226. - matrix_ketesa_runtime.Config.Labels['molecule.ketesa.coverage'] == 'enabled'
  227. fail_msg: "Docker did not attach the custom label from the role's label file"
  228. success_msg: "Docker accepted the custom label from the role's label file"