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.
 
 
 

174 regels
8.1 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. - name: Verify matrix-alertmanager-receiver
  6. hosts: all
  7. become: true
  8. vars_files:
  9. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  10. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  11. gather_facts: false
  12. tasks:
  13. # Read from the role's own defaults rather than pinned in molecule.yml, so the version
  14. # assertion compares the running image against what defaults/main.yml ships.
  15. # Pinning it here would make that assertion compare the scenario with itself.
  16. - name: Load the role's defaults under a separate name
  17. ansible.builtin.include_vars:
  18. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  19. name: matrix_alertmanager_receiver_role_defaults
  20. - name: Wait for the matrix-alertmanager-receiver service to become active
  21. ansible.builtin.systemd_service:
  22. name: matrix-alertmanager-receiver.service
  23. register: matrix_alertmanager_receiver_service
  24. until: matrix_alertmanager_receiver_service.status.ActiveState == 'active'
  25. retries: 30
  26. delay: 5
  27. failed_when: false
  28. # `Restart=always` means a crash-looping container still reports `active`, so the restart
  29. # counter is checked too. Asserted `is defined` because `| int` turns a missing property
  30. # into 0 and would pass vacuously.
  31. - name: Assert the service is active and has not been restarting
  32. ansible.builtin.assert:
  33. that:
  34. - matrix_alertmanager_receiver_service.status.ActiveState == 'active'
  35. - matrix_alertmanager_receiver_service.status.NRestarts is defined
  36. - matrix_alertmanager_receiver_service.status.NRestarts | int == 0
  37. fail_msg: >-
  38. matrix-alertmanager-receiver.service is
  39. {{ matrix_alertmanager_receiver_service.status.ActiveState | default('unknown') }}
  40. after {{ matrix_alertmanager_receiver_service.status.NRestarts | default('?') }}
  41. automatic restart(s)
  42. success_msg: "matrix-alertmanager-receiver.service is active and has not restarted"
  43. # Probed from inside the container network rather than the host, because the role
  44. # publishes no host port - exactly as in a real deployment, where Traefik reaches it
  45. # over the network.
  46. - name: Wait for matrix-alertmanager-receiver to answer on the port the role configured
  47. ansible.builtin.command:
  48. argv:
  49. - docker
  50. - run
  51. - --rm
  52. - --network={{ matrix_alertmanager_receiver_container_network }}
  53. - "{{ molecule_shared_image_curl }}"
  54. - --silent
  55. - --show-error
  56. - --write-out
  57. - "\nHTTP_STATUS=%{http_code}"
  58. - "http://matrix-alertmanager-receiver:{{ matrix_alertmanager_receiver_config_http_port }}{{ matrix_alertmanager_receiver_config_http_metrics_path }}"
  59. register: matrix_alertmanager_receiver_metrics
  60. changed_when: false
  61. until: "'HTTP_STATUS=200' in matrix_alertmanager_receiver_metrics.stdout"
  62. retries: 24
  63. delay: 5
  64. failed_when: false
  65. # Port and metrics path are both non-default here, so a 200 is only reachable if what
  66. # the role rendered is what the process is running on.
  67. - name: Assert the configured port and metrics path reached the process
  68. ansible.builtin.assert:
  69. that:
  70. - "'HTTP_STATUS=200' in matrix_alertmanager_receiver_metrics.stdout"
  71. fail_msg: >-
  72. matrix-alertmanager-receiver did not serve metrics on port
  73. {{ matrix_alertmanager_receiver_config_http_port }} at
  74. {{ matrix_alertmanager_receiver_config_http_metrics_path }}
  75. ({{ matrix_alertmanager_receiver_metrics.stdout | default('no output') }})
  76. success_msg: >-
  77. matrix-alertmanager-receiver serves metrics on the configured port and path
  78. - name: Assert the metrics endpoint is really Prometheus metrics
  79. ansible.builtin.assert:
  80. that:
  81. - "'# HELP' in matrix_alertmanager_receiver_metrics.stdout"
  82. fail_msg: >-
  83. The metrics endpoint answered, but did not return Prometheus metrics
  84. success_msg: "The metrics endpoint returns Prometheus metrics"
  85. # Negative control for the assertion above: the role's own default metrics path must NOT
  86. # answer, or a 200 on the configured path would prove nothing.
  87. - name: Ask for the role's default metrics path, which this scenario moved away from
  88. ansible.builtin.command:
  89. argv:
  90. - docker
  91. - run
  92. - --rm
  93. - --network={{ matrix_alertmanager_receiver_container_network }}
  94. - "{{ molecule_shared_image_curl }}"
  95. - --silent
  96. - --output
  97. - /dev/null
  98. - --write-out
  99. - "HTTP_STATUS=%{http_code}"
  100. - "http://matrix-alertmanager-receiver:{{ matrix_alertmanager_receiver_config_http_port }}/metrics"
  101. register: matrix_alertmanager_receiver_default_path
  102. changed_when: false
  103. failed_when: false
  104. - name: Assert the default metrics path does not answer
  105. ansible.builtin.assert:
  106. that:
  107. - "'HTTP_STATUS=200' not in matrix_alertmanager_receiver_default_path.stdout"
  108. fail_msg: >-
  109. /metrics answered as well, so serving on
  110. {{ matrix_alertmanager_receiver_config_http_metrics_path }} does not
  111. prove the role's configuration reached the process
  112. ({{ matrix_alertmanager_receiver_default_path.stdout | default('no output') }})
  113. success_msg: "Only the configured metrics path answers"
  114. - name: Read the configuration file the role rendered
  115. ansible.builtin.slurp:
  116. src: "{{ matrix_alertmanager_receiver_config_path }}/config.yml"
  117. register: matrix_alertmanager_receiver_config_file
  118. - name: Assert the rendered configuration carries this scenario's values
  119. ansible.builtin.assert:
  120. that:
  121. - matrix_alertmanager_receiver_config_matrix_user_id in matrix_alertmanager_receiver_config_rendered
  122. - matrix_alertmanager_receiver_config_matrix_access_token in matrix_alertmanager_receiver_config_rendered
  123. - "'molecule-room' in matrix_alertmanager_receiver_config_rendered"
  124. fail_msg: "The rendered configuration does not carry the scenario's Matrix settings"
  125. success_msg: "The rendered configuration carries the scenario's Matrix settings"
  126. vars:
  127. matrix_alertmanager_receiver_config_rendered: "{{ matrix_alertmanager_receiver_config_file.content | b64decode }}"
  128. - name: Assert the running container is the image the role pins
  129. ansible.builtin.command:
  130. argv:
  131. - docker
  132. - container
  133. - inspect
  134. - matrix-alertmanager-receiver
  135. - --format
  136. - "{{ '{{' }} .Config.Image {{ '}}' }}"
  137. register: matrix_alertmanager_receiver_image
  138. changed_when: false
  139. - name: Assert the image carries the version defaults/main.yml pins
  140. ansible.builtin.assert:
  141. that:
  142. - matrix_alertmanager_receiver_role_defaults.matrix_alertmanager_receiver_version in matrix_alertmanager_receiver_image.stdout
  143. fail_msg: >-
  144. The running container is {{ matrix_alertmanager_receiver_image.stdout }},
  145. which does not carry the pinned version {{ matrix_alertmanager_receiver_role_defaults.matrix_alertmanager_receiver_version }}
  146. success_msg: "The running container is the version defaults/main.yml pins"
  147. - name: Read the labels the role rendered
  148. ansible.builtin.slurp:
  149. src: "{{ matrix_alertmanager_receiver_base_path }}/labels"
  150. register: matrix_alertmanager_receiver_labels
  151. - name: Assert no Traefik labels are emitted while Traefik support is disabled
  152. ansible.builtin.assert:
  153. that:
  154. - "'traefik.' not in (matrix_alertmanager_receiver_labels.content | b64decode)"
  155. fail_msg: >-
  156. Traefik labels were emitted even though
  157. matrix_alertmanager_receiver_container_labels_traefik_enabled is false
  158. success_msg: "No Traefik labels are emitted while Traefik support is disabled"