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

226 строки
12 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Proves matrix-reminder-bot starts on the configuration the role rendered, logs in as the
  6. # user the role gave it, opens the database at the path the role gave it, and is the version
  7. # the role pins.
  8. #
  9. # The bot has no HTTP surface to probe, so the evidence is what it says about itself in the
  10. # journal plus what it left on disk. It does NOT set real reminders. See docs/molecule-testing.md.
  11. - name: Verify matrix-reminder-bot
  12. hosts: all
  13. become: true
  14. vars_files:
  15. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  16. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  17. gather_facts: false
  18. vars:
  19. matrix_bot_matrix_reminder_bot_molecule_user_id: "@{{ matrix_bot_matrix_reminder_bot_matrix_user_id_localpart }}:{{ matrix_domain }}"
  20. matrix_bot_matrix_reminder_bot_molecule_container_user: "{{ matrix_user_uid }}:{{ matrix_user_gid }}"
  21. tasks:
  22. # Read from the role's own defaults rather than pinned in molecule.yml, so the version
  23. # assertion compares the running image against what defaults/main.yml ships.
  24. # Pinning it here would make that assertion compare the scenario with itself.
  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_bot_matrix_reminder_bot_role_defaults
  29. - name: Wait for the matrix-reminder-bot service to become active
  30. ansible.builtin.systemd_service:
  31. name: matrix-bot-matrix-reminder-bot.service
  32. register: matrix_bot_matrix_reminder_bot_service
  33. until: matrix_bot_matrix_reminder_bot_service.status.ActiveState == 'active'
  34. retries: 30
  35. delay: 5
  36. failed_when: false
  37. # `Restart=always` means a bot crash-looping on unreadable config still reports `active`,
  38. # so the restart counter is checked too. The config file is parsed before the bot's own
  39. # catch-all retry loop starts, so anything wrong in what the role rendered shows up here
  40. # as restarts. Asserted `is defined` because `| int` turns a missing property into 0.
  41. - name: Assert the service is active and has not been restarting
  42. ansible.builtin.assert:
  43. that:
  44. - matrix_bot_matrix_reminder_bot_service.status.ActiveState == 'active'
  45. - matrix_bot_matrix_reminder_bot_service.status.NRestarts is defined
  46. - matrix_bot_matrix_reminder_bot_service.status.NRestarts | int == 0
  47. fail_msg: >-
  48. matrix-bot-matrix-reminder-bot.service is
  49. {{ matrix_bot_matrix_reminder_bot_service.status.ActiveState | default('unknown') }}
  50. after {{ matrix_bot_matrix_reminder_bot_service.status.NRestarts | default('?') }}
  51. automatic restart(s)
  52. success_msg: "matrix-bot-matrix-reminder-bot.service is active and has not restarted"
  53. # The unit runs `docker start --attach`, so the container's output is in the journal
  54. # despite `--log-driver=none`. It is the only thing this bot reports about itself.
  55. #
  56. # Filtered rather than tailed: startup lines are the OLDEST in the journal, so a
  57. # `--lines=N` tail loses them behind anything logged later, and reading it whole pulls
  58. # unbounded text into a variable. The filter keeps the failure line too, so the
  59. # "did not fail to log in" assertion below still has something to see.
  60. - name: Wait for the bot to report that it finished starting up
  61. ansible.builtin.shell:
  62. cmd: >-
  63. set -o pipefail;
  64. journalctl --unit=matrix-bot-matrix-reminder-bot.service --no-pager --output=cat --lines=all
  65. | grep -E 'Logged in as|Startup complete|Failed to login|Database initialization' | head -n 50 || true
  66. executable: /bin/bash
  67. register: matrix_bot_matrix_reminder_bot_journal
  68. changed_when: false
  69. until: "'Startup complete' in matrix_bot_matrix_reminder_bot_journal.stdout"
  70. retries: 24
  71. delay: 5
  72. failed_when: false
  73. # "Logged in as ..." is only reached once the login call returned something other than a
  74. # LoginError, so this covers the whole chain at once: homeserver URL, user ID and password
  75. # were all good enough for a real login round-trip.
  76. - name: Assert the bot logged in as the user the role configured
  77. ansible.builtin.assert:
  78. that:
  79. - "'Startup complete' in matrix_bot_matrix_reminder_bot_journal.stdout"
  80. - "'Logged in as ' + matrix_bot_matrix_reminder_bot_molecule_user_id in matrix_bot_matrix_reminder_bot_journal.stdout"
  81. - "'Failed to login' not in matrix_bot_matrix_reminder_bot_journal.stdout"
  82. fail_msg: >-
  83. The bot did not log in as {{ matrix_bot_matrix_reminder_bot_molecule_user_id }}
  84. and reach startup
  85. success_msg: >-
  86. The bot logged in as {{ matrix_bot_matrix_reminder_bot_molecule_user_id }} and finished starting up
  87. # The role picks the storage engine by building the connection string the bot parses,
  88. # and the bot names the type it settled on once the database is open.
  89. - name: Assert the bot opened the database engine the role selected
  90. ansible.builtin.assert:
  91. that:
  92. - "\"Database initialization of type 'postgres' complete\" in matrix_bot_matrix_reminder_bot_journal.stdout"
  93. fail_msg: "The bot did not report a completed Postgres database initialization"
  94. success_msg: "The bot initialized the Postgres database the role pointed it at"
  95. - name: Read the configuration file the role rendered
  96. ansible.builtin.slurp:
  97. src: "{{ matrix_bot_matrix_reminder_bot_config_path }}/config.yaml"
  98. register: matrix_bot_matrix_reminder_bot_config_file
  99. # Every one differs from both the role's defaults and the bot's own fallbacks, so their
  100. # presence means the role rendered this file rather than coinciding with it.
  101. - name: Assert the rendered configuration carries this scenario's values
  102. ansible.builtin.assert:
  103. that:
  104. - matrix_bot_matrix_reminder_bot_molecule_user_id in matrix_bot_matrix_reminder_bot_config_rendered
  105. - matrix_bot_matrix_reminder_bot_matrix_user_password in matrix_bot_matrix_reminder_bot_config_rendered
  106. - matrix_bot_matrix_reminder_bot_matrix_homeserver_url in matrix_bot_matrix_reminder_bot_config_rendered
  107. - matrix_bot_matrix_reminder_bot_reminders_timezone in matrix_bot_matrix_reminder_bot_config_rendered
  108. - matrix_bot_matrix_reminder_bot_command_prefix in matrix_bot_matrix_reminder_bot_config_rendered
  109. - matrix_bot_matrix_reminder_bot_database_username in matrix_bot_matrix_reminder_bot_config_rendered
  110. - matrix_bot_matrix_reminder_bot_database_name in matrix_bot_matrix_reminder_bot_config_rendered
  111. - matrix_bot_matrix_reminder_bot_database_hostname in matrix_bot_matrix_reminder_bot_config_rendered
  112. - "'@molecule-allowed:molecule.local' in matrix_bot_matrix_reminder_bot_config_rendered"
  113. - "'.*:blocked.molecule.local' in matrix_bot_matrix_reminder_bot_config_rendered"
  114. fail_msg: "The rendered configuration does not carry the scenario's settings"
  115. success_msg: "The rendered configuration carries the scenario's settings"
  116. vars:
  117. matrix_bot_matrix_reminder_bot_config_rendered: "{{ matrix_bot_matrix_reminder_bot_config_file.content | b64decode }}"
  118. # `device_name` is hardcoded in the role's template, so this value can only be here if
  119. # `..._configuration_extension_yaml` was merged over it rather than ignored.
  120. - name: Assert the configuration extension was merged over the template
  121. ansible.builtin.assert:
  122. that:
  123. - "'device_name: Molecule Reminder Bot' in matrix_bot_matrix_reminder_bot_config_rendered"
  124. - "'device_name: Reminder Bot' not in matrix_bot_matrix_reminder_bot_config_rendered"
  125. fail_msg: >-
  126. The configuration extension did not override the device name the
  127. role's template hardcodes
  128. success_msg: "The configuration extension was merged over the role's template"
  129. vars:
  130. matrix_bot_matrix_reminder_bot_config_rendered: "{{ matrix_bot_matrix_reminder_bot_config_file.content | b64decode }}"
  131. # With no HTTP surface, the schema in Postgres is the evidence that the storage
  132. # configuration reached the running process rather than only the file on disk. The bot can
  133. # only have created tables by resolving the hostname, authenticating with the credentials
  134. # the role rendered, and running its migrations.
  135. - name: List the tables the bot created in Postgres
  136. ansible.builtin.command:
  137. argv:
  138. - docker
  139. - exec
  140. - matrix-postgres-molecule
  141. - psql
  142. - --username={{ matrix_bot_matrix_reminder_bot_database_username }}
  143. - --dbname={{ matrix_bot_matrix_reminder_bot_database_name }}
  144. - --tuples-only
  145. - --no-align
  146. - --command=SELECT tablename FROM pg_tables WHERE schemaname = 'public'
  147. register: matrix_bot_matrix_reminder_bot_tables
  148. changed_when: false
  149. - name: Assert the bot created its schema in the database the role pointed it at
  150. ansible.builtin.assert:
  151. that:
  152. - matrix_bot_matrix_reminder_bot_tables.rc == 0
  153. - matrix_bot_matrix_reminder_bot_table_names | length > 0
  154. fail_msg: >-
  155. The bot created no tables in {{ matrix_bot_matrix_reminder_bot_database_name }}
  156. success_msg: "The bot created its schema in the database the role pointed it at"
  157. vars:
  158. matrix_bot_matrix_reminder_bot_table_names: "{{ matrix_bot_matrix_reminder_bot_tables.stdout_lines | select | list }}"
  159. # matrix-nio writes its encryption store here once login succeeds, so a populated directory
  160. # means the bot could use the store path the role created inside a read-only container.
  161. - name: List the encryption store the role created
  162. ansible.builtin.find:
  163. paths: "{{ matrix_bot_matrix_reminder_bot_data_store_path }}"
  164. file_type: file
  165. register: matrix_bot_matrix_reminder_bot_store_files
  166. - name: Assert the bot wrote its encryption store where the role put it
  167. ansible.builtin.assert:
  168. that:
  169. - matrix_bot_matrix_reminder_bot_store_files.matched | int > 0
  170. fail_msg: >-
  171. {{ matrix_bot_matrix_reminder_bot_data_store_path }} is empty, so the bot never
  172. got far enough to open its encryption store
  173. success_msg: "The bot wrote its encryption store under the role's data path"
  174. - name: Read the running container's user and environment
  175. ansible.builtin.command:
  176. argv:
  177. - docker
  178. - container
  179. - inspect
  180. - matrix-bot-matrix-reminder-bot
  181. - --format
  182. - "{{ '{{' }} .Config.User {{ '}}' }} {{ '{{' }} json .Config.Env {{ '}}' }} {{ '{{' }} .Config.Image {{ '}}' }}"
  183. register: matrix_bot_matrix_reminder_bot_container
  184. changed_when: false
  185. # The timezone reaches the container twice, through the config file checked above and
  186. # through TZ on the unit. The uid/gid come from the playbook context, not from the image.
  187. - name: Assert the container runs as the role's user with the configured timezone
  188. ansible.builtin.assert:
  189. that:
  190. - "'\"TZ=' + matrix_bot_matrix_reminder_bot_reminders_timezone + '\"' in matrix_bot_matrix_reminder_bot_container.stdout"
  191. - matrix_bot_matrix_reminder_bot_molecule_container_user in matrix_bot_matrix_reminder_bot_container.stdout
  192. fail_msg: >-
  193. The container does not run as {{ matrix_user_uid }}:{{ matrix_user_gid }} with
  194. TZ={{ matrix_bot_matrix_reminder_bot_reminders_timezone }}
  195. ({{ matrix_bot_matrix_reminder_bot_container.stdout }})
  196. success_msg: >-
  197. The container runs as {{ matrix_user_uid }}:{{ matrix_user_gid }} with
  198. TZ={{ matrix_bot_matrix_reminder_bot_reminders_timezone }}
  199. - name: Assert the running container is the version defaults/main.yml pins
  200. ansible.builtin.assert:
  201. that:
  202. - matrix_bot_matrix_reminder_bot_role_defaults.matrix_bot_matrix_reminder_bot_version | string in matrix_bot_matrix_reminder_bot_container.stdout
  203. fail_msg: >-
  204. The running container is {{ matrix_bot_matrix_reminder_bot_container.stdout }}, which
  205. does not carry the pinned version
  206. {{ matrix_bot_matrix_reminder_bot_role_defaults.matrix_bot_matrix_reminder_bot_version }}
  207. success_msg: "The running container is the version defaults/main.yml pins"