Matrix Docker Ansible eploy
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

308 líneas
16 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Element Admin is a static browser client. Its real application root and the live
  6. # configuration embedded from the role's env file are the weight-bearing checks; no
  7. # homeserver or third-party credentials are needed for the process to start.
  8. - name: Verify matrix-element-admin
  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_element_admin_root_body: "{{ matrix_element_admin_root_http.stdout_lines[:-1] | join('\n') }}"
  16. matrix_element_admin_runtime: "{{ (matrix_element_admin_container_inspect.stdout | from_json) | first }}"
  17. gather_facts: false
  18. tasks:
  19. # Loading the pin from the role keeps this scenario coupled to the shipped image
  20. # without duplicating a version which could silently drift.
  21. - name: Load the role's defaults under a separate name
  22. ansible.builtin.include_vars:
  23. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  24. name: matrix_element_admin_role_defaults
  25. - name: Wait for the matrix-element-admin service to become active
  26. ansible.builtin.systemd_service:
  27. name: matrix-element-admin.service
  28. register: matrix_element_admin_service
  29. until: matrix_element_admin_service.status.ActiveState == 'active'
  30. retries: 30
  31. delay: 5
  32. failed_when: false
  33. # Restart=always means ActiveState alone can be green while the container crash-loops.
  34. - name: Assert the service is active and has not restarted
  35. ansible.builtin.assert:
  36. that:
  37. - matrix_element_admin_service.status.ActiveState == 'active'
  38. - matrix_element_admin_service.status.NRestarts is defined
  39. - matrix_element_admin_service.status.NRestarts | int == 0
  40. fail_msg: >-
  41. matrix-element-admin.service is
  42. {{ matrix_element_admin_service.status.ActiveState | default('unknown') }} after
  43. {{ matrix_element_admin_service.status.NRestarts | default('?') }} restart(s)
  44. success_msg: "matrix-element-admin.service is active and has not restarted"
  45. # Probe over the dedicated container network. Publishing a host port only for the
  46. # test would diverge from how Traefik reaches the service in real installations.
  47. - name: Wait for Element Admin to serve its application root
  48. ansible.builtin.command:
  49. argv:
  50. - docker
  51. - run
  52. - --rm
  53. - --network={{ matrix_element_admin_container_network }}
  54. - "{{ molecule_shared_image_curl }}"
  55. - --silent
  56. - --show-error
  57. - --write-out
  58. - "\nHTTP_STATUS=%{http_code}"
  59. - http://matrix-element-admin:8080/
  60. register: matrix_element_admin_root_http
  61. changed_when: false
  62. until: "'HTTP_STATUS=200' in matrix_element_admin_root_http.stdout"
  63. retries: 24
  64. delay: 5
  65. failed_when: false
  66. - name: Assert the application root is real Element Admin HTML
  67. ansible.builtin.assert:
  68. that:
  69. - matrix_element_admin_root_http.rc == 0
  70. - matrix_element_admin_root_http.stdout_lines[-1] == 'HTTP_STATUS=200'
  71. - matrix_element_admin_root_body | trim | length > 100
  72. - "'<!doctype html' in matrix_element_admin_root_body | lower"
  73. - "'element admin' in matrix_element_admin_root_body | lower"
  74. fail_msg: >-
  75. Element Admin did not return its application HTML from the container network
  76. (rc={{ matrix_element_admin_root_http.rc }}, status={{ matrix_element_admin_root_http.stdout_lines[-1] | default('missing') }})
  77. success_msg: "Element Admin serves its real application root over HTTP"
  78. # The image entrypoint base64-embeds JSON in index.html from SERVER_NAME. Decode
  79. # that exact live value instead of trusting that the environment merely exists.
  80. - name: Extract the live configuration and application asset path
  81. ansible.builtin.set_fact:
  82. matrix_element_admin_embedded_config_base64: >-
  83. {{ matrix_element_admin_root_body | regex_search('(?<=window.APP_CONFIG_BASE64 = ")[^"]+(?=";)') }}
  84. matrix_element_admin_application_asset_path: >-
  85. {{ matrix_element_admin_root_body | regex_search('(?<=src=")[/]assets[/][^"]+(?=")') }}
  86. - name: Decode the live Element Admin configuration
  87. ansible.builtin.set_fact:
  88. matrix_element_admin_embedded_config: >-
  89. {{ matrix_element_admin_embedded_config_base64 | b64decode | from_json }}
  90. - name: Assert the live configuration carries the configured homeserver name
  91. ansible.builtin.assert:
  92. that:
  93. - matrix_element_admin_embedded_config is mapping
  94. - matrix_element_admin_embedded_config.keys() | list == ['serverName']
  95. - matrix_element_admin_embedded_config.serverName == matrix_element_admin_environment_variable_server_name
  96. fail_msg: >-
  97. Element Admin's live embedded configuration does not carry the SERVER_NAME value rendered by the role
  98. success_msg: "The live application configuration carries the configured homeserver name"
  99. # Fetch a hashed application bundle referenced by the live HTML. This exercises a
  100. # real non-root HTTP path without claiming support for an invalid public subpath.
  101. - name: Fetch a live Element Admin application asset
  102. ansible.builtin.command:
  103. argv:
  104. - docker
  105. - run
  106. - --rm
  107. - --network={{ matrix_element_admin_container_network }}
  108. - "{{ molecule_shared_image_curl }}"
  109. - --silent
  110. - --show-error
  111. - --output
  112. - /dev/null
  113. - --write-out
  114. - "HTTP_STATUS=%{http_code}\nCONTENT_TYPE=%{content_type}\nSIZE=%{size_download}"
  115. - "http://matrix-element-admin:8080{{ matrix_element_admin_application_asset_path }}"
  116. register: matrix_element_admin_asset_http
  117. changed_when: false
  118. failed_when: false
  119. - name: Assert the non-root application asset is served
  120. ansible.builtin.assert:
  121. that:
  122. - matrix_element_admin_asset_http.rc == 0
  123. - "'HTTP_STATUS=200' in matrix_element_admin_asset_http.stdout_lines"
  124. - matrix_element_admin_asset_http.stdout | regex_search('(?m)^CONTENT_TYPE=application/javascript(?:;|$)') is not none
  125. - matrix_element_admin_asset_http.stdout | regex_search('SIZE=([1-9][0-9]{2,})') is not none
  126. fail_msg: "Element Admin did not serve the application bundle referenced by its live HTML"
  127. success_msg: "Element Admin serves a real non-root application asset"
  128. - name: Read the environment file the role rendered
  129. ansible.builtin.slurp:
  130. src: "{{ matrix_element_admin_base_path }}/env"
  131. register: matrix_element_admin_env_file
  132. - name: Initialize the parsed environment
  133. ansible.builtin.set_fact:
  134. matrix_element_admin_env_parsed: {}
  135. - name: Parse the rendered environment
  136. ansible.builtin.set_fact:
  137. matrix_element_admin_env_parsed: >-
  138. {{ matrix_element_admin_env_parsed | combine({item.split('=', 1)[0]: item.split('=', 1)[1]}) }}
  139. loop: "{{ (matrix_element_admin_env_file.content | b64decode).splitlines() | reject('equalto', '') }}"
  140. when: "'=' in item"
  141. no_log: true
  142. - name: Assert the rendered environment carries the scenario settings
  143. ansible.builtin.assert:
  144. that:
  145. - matrix_element_admin_env_parsed.SERVER_NAME == matrix_element_admin_environment_variable_server_name
  146. - matrix_element_admin_env_parsed.MOLECULE_ELEMENT_ADMIN_MARKER == 'environment-reached'
  147. fail_msg: "The parsed environment does not carry the scenario's values"
  148. success_msg: "The parsed environment carries the scenario's values"
  149. - name: Read the labels file the role rendered
  150. ansible.builtin.slurp:
  151. src: "{{ matrix_element_admin_base_path }}/labels"
  152. register: matrix_element_admin_labels_file
  153. - name: Initialize the parsed labels
  154. ansible.builtin.set_fact:
  155. matrix_element_admin_labels_parsed: {}
  156. - name: Parse the rendered labels
  157. ansible.builtin.set_fact:
  158. matrix_element_admin_labels_parsed: >-
  159. {{ matrix_element_admin_labels_parsed | combine({item.split('=', 1)[0]: item.split('=', 1)[1]}) }}
  160. loop: "{{ (matrix_element_admin_labels_file.content | b64decode).splitlines() | reject('equalto', '') }}"
  161. when: "'=' in item"
  162. no_log: true
  163. - name: Assert the rendered labels carry the public routing contract
  164. ansible.builtin.assert:
  165. that:
  166. - matrix_element_admin_labels_parsed['traefik.enable'] == 'true'
  167. - matrix_element_admin_labels_parsed['traefik.docker.network'] == matrix_element_admin_container_network
  168. - matrix_element_admin_labels_parsed['traefik.http.services.matrix-element-admin.loadbalancer.server.port'] == '8080'
  169. - matrix_element_admin_labels_parsed['traefik.http.routers.matrix-element-admin.rule'] == 'Host(`element-admin-console.molecule.local`)'
  170. - matrix_element_admin_labels_parsed['traefik.http.routers.matrix-element-admin.priority'] == '619'
  171. - matrix_element_admin_labels_parsed['traefik.http.routers.matrix-element-admin.entrypoints'] == 'web'
  172. - matrix_element_admin_labels_parsed['traefik.http.routers.matrix-element-admin.tls'] == 'false'
  173. - matrix_element_admin_labels_parsed['molecule.element-admin.coverage'] == 'enabled'
  174. fail_msg: "The rendered labels do not carry the scenario's routing values"
  175. success_msg: "The rendered labels carry the scenario's routing values"
  176. - name: Inspect the running Element Admin container
  177. ansible.builtin.command:
  178. argv:
  179. - docker
  180. - container
  181. - inspect
  182. - matrix-element-admin
  183. register: matrix_element_admin_container_inspect
  184. changed_when: false
  185. - name: Assert the configured environment reached the running container
  186. ansible.builtin.assert:
  187. that:
  188. - "('SERVER_NAME=' ~ matrix_element_admin_environment_variable_server_name) in matrix_element_admin_runtime.Config.Env"
  189. - "'MOLECULE_ELEMENT_ADMIN_MARKER=environment-reached' in matrix_element_admin_runtime.Config.Env"
  190. fail_msg: "The running container environment does not carry the scenario's settings"
  191. success_msg: "The configured environment reached the running container"
  192. - name: Assert the rendered labels reached the running container
  193. ansible.builtin.assert:
  194. that:
  195. - matrix_element_admin_runtime.Config.Labels['traefik.enable'] == 'true'
  196. - matrix_element_admin_runtime.Config.Labels['traefik.docker.network'] == matrix_element_admin_container_network
  197. - matrix_element_admin_runtime.Config.Labels['traefik.http.routers.matrix-element-admin.priority'] == '619'
  198. - matrix_element_admin_runtime.Config.Labels['molecule.element-admin.coverage'] == 'enabled'
  199. fail_msg: "The running container does not carry the labels the role rendered"
  200. success_msg: "The rendered labels reached the running container"
  201. - name: Assert the running container uses the exact image and version the role pins
  202. ansible.builtin.assert:
  203. that:
  204. - matrix_element_admin_runtime.Config.Image == matrix_element_admin_expected_image
  205. fail_msg: >-
  206. The running container uses {{ matrix_element_admin_runtime.Config.Image }},
  207. expected {{ matrix_element_admin_expected_image }}
  208. success_msg: "The running container uses the exact image and version defaults/main.yml pins"
  209. vars:
  210. matrix_element_admin_expected_image: >-
  211. {{ matrix_element_admin_role_defaults.matrix_element_admin_container_image_registry_prefix_upstream_default }}element-admin:{{ matrix_element_admin_role_defaults.matrix_element_admin_version }}
  212. - name: Assert the running container uses the configured UID and GID
  213. ansible.builtin.assert:
  214. that:
  215. - matrix_element_admin_runtime.Config.User == (matrix_user_uid | string) ~ ':' ~ (matrix_user_gid | string)
  216. fail_msg: >-
  217. The running container uses {{ matrix_element_admin_runtime.Config.User }},
  218. expected {{ matrix_user_uid }}:{{ matrix_user_gid }}
  219. success_msg: "The running container uses the configured UID and GID"
  220. - name: Assert the running container command contract
  221. ansible.builtin.assert:
  222. that:
  223. - matrix_element_admin_runtime.Config.Entrypoint == ['/docker-entrypoint.sh']
  224. - matrix_element_admin_runtime.Config.Cmd == ['nginx', '-g', 'daemon off;']
  225. fail_msg: >-
  226. The running container has unexpected entrypoint/command values:
  227. {{ matrix_element_admin_runtime.Config.Entrypoint }} {{ matrix_element_admin_runtime.Config.Cmd }}
  228. success_msg: "The role preserves the image's Element Admin entrypoint and command"
  229. - name: Assert the running container security contract
  230. ansible.builtin.assert:
  231. that:
  232. - matrix_element_admin_runtime.HostConfig.ReadonlyRootfs is sameas true
  233. - matrix_element_admin_runtime.HostConfig.Privileged is sameas false
  234. - matrix_element_admin_runtime.HostConfig.CapAdd | default([], true) | length == 0
  235. - matrix_element_admin_runtime.HostConfig.CapDrop == ['ALL']
  236. fail_msg: "The Element Admin container is missing its read-only root or dropped capabilities"
  237. success_msg: "The Element Admin container has a read-only root and all capabilities dropped"
  238. - name: Assert the intended writable path is a constrained tmpfs mount
  239. ansible.builtin.assert:
  240. that:
  241. - matrix_element_admin_runtime.Mounts | length == 0
  242. - matrix_element_admin_runtime.HostConfig.Binds | default([], true) | length == 0
  243. - matrix_element_admin_runtime.HostConfig.Tmpfs.keys() | list == ['/tmp']
  244. - matrix_element_admin_runtime.HostConfig.Tmpfs['/tmp'] == 'rw,noexec,nosuid,size=1024m'
  245. fail_msg: "The running container does not have the role's exact /tmp tmpfs contract"
  246. success_msg: "The running container has the exact constrained /tmp tmpfs mount"
  247. - name: Assert the running container is attached only to its dedicated network
  248. ansible.builtin.assert:
  249. that:
  250. - matrix_element_admin_runtime.NetworkSettings.Networks is mapping
  251. - matrix_element_admin_runtime.NetworkSettings.Networks | length == 1
  252. - matrix_element_admin_container_network in matrix_element_admin_runtime.NetworkSettings.Networks
  253. fail_msg: >-
  254. Element Admin has unexpected network attachments:
  255. {{ matrix_element_admin_runtime.NetworkSettings.Networks.keys() | list }}
  256. success_msg: "The running container is attached only to its dedicated network"
  257. - name: Ask Docker for Element Admin's published ports
  258. ansible.builtin.command:
  259. argv:
  260. - docker
  261. - container
  262. - port
  263. - matrix-element-admin
  264. register: matrix_element_admin_published_ports
  265. changed_when: false
  266. failed_when: false
  267. - name: Assert the role publishes no host ports
  268. ansible.builtin.assert:
  269. that:
  270. - matrix_element_admin_runtime.Config.ExposedPorts.keys() | list == ['8080/tcp']
  271. - matrix_element_admin_runtime.HostConfig.PortBindings | default({}, true) | length == 0
  272. - matrix_element_admin_published_ports.rc == 0
  273. - matrix_element_admin_published_ports.stdout | trim | length == 0
  274. fail_msg: >-
  275. Element Admin unexpectedly publishes a host port:
  276. {{ matrix_element_admin_published_ports.stdout | default('unknown') }}
  277. success_msg: "The role leaves Element Admin's HTTP port unpublished"