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

172 строки
8.0 KiB

  1. ---
  2. - name: Fail if Postgres not enabled
  3. fail:
  4. msg: "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot migrate."
  5. when: "not matrix_postgres_enabled|bool"
  6. - name: Fail if util called incorrectly (missing matrix_postgres_db_migration_request)
  7. fail:
  8. msg: "The `matrix_postgres_db_migration_request` variable needs to be provided to this util."
  9. when: "matrix_postgres_db_migration_request is not defined"
  10. - name: Fail if util called incorrectly (invalid matrix_postgres_db_migration_request)
  11. fail:
  12. msg: "The `matrix_postgres_db_migration_request` variable needs to contain `{{ item }}`."
  13. with_items:
  14. - src
  15. - dst
  16. - caller
  17. - engine_variable_name
  18. - systemd_services_to_stop
  19. when: "item not in matrix_postgres_db_migration_request"
  20. - name: Check if the provided source database file exists
  21. stat:
  22. path: "{{ matrix_postgres_db_migration_request.src }}"
  23. register: matrix_postgres_db_migration_request_src_stat_result
  24. - name: Fail if provided source database file doesn't exist
  25. fail:
  26. msg: "File cannot be found on the server at {{ matrix_postgres_db_migration_request.src }}"
  27. when: "not matrix_postgres_db_migration_request_src_stat_result.stat.exists"
  28. - block:
  29. - name: Ensure pgloader repository is present on self-build
  30. git:
  31. repo: "{{ matrix_postgres_pgloader_container_image_self_build_repo }}"
  32. dest: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
  33. version: "{{ matrix_postgres_pgloader_container_image_self_build_repo_branch }}"
  34. force: "yes"
  35. become: true
  36. become_user: "{{ matrix_user_username }}"
  37. register: matrix_postgres_pgloader_git_pull_results
  38. # If `stable` is used, we hit an error when processing /opt/src/pgloader/build/quicklisp/dists/quicklisp/software/uax-15-20201220-git/data/CompositionExclusions.txt:
  39. # > the octet sequence #(194) cannot be decoded
  40. #
  41. # The issue is described here and is not getting fixed for months: https://github.com/dimitri/pgloader/pull/1179
  42. #
  43. # Although we're not using the dimitri/pgloader image, the one we're using suffers from the same problem.
  44. - name: Switch pgloader base image from Debian stable (likely 10.x/Buster) to Bullseye
  45. lineinfile:
  46. path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}/Dockerfile"
  47. regexp: "{{ item.match }}"
  48. line: "{{ item.replace }}"
  49. with_items:
  50. - match: '^FROM debian:stable-slim as builder$'
  51. replace: 'FROM debian:bullseye-slim as builder'
  52. - match: '^FROM debian:stable-slim$'
  53. replace: 'FROM debian:bullseye-slim'
  54. - name: Ensure pgloader Docker image is built
  55. docker_image:
  56. name: "{{ matrix_postgres_pgloader_docker_image }}"
  57. source: build
  58. force_source: "{{ matrix_postgres_pgloader_git_pull_results.changed if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  59. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_pgloader_git_pull_results.changed }}"
  60. build:
  61. dockerfile: Dockerfile
  62. path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
  63. pull: true
  64. when: "matrix_postgres_pgloader_container_image_self_build|bool"
  65. - name: Ensure pgloader Docker image is pulled
  66. docker_image:
  67. name: "{{ matrix_postgres_pgloader_docker_image }}"
  68. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  69. force_source: "{{ matrix_postgres_pgloader_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  70. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_pgloader_docker_image_force_pull }}"
  71. when: "not matrix_postgres_pgloader_container_image_self_build"
  72. # Defaults
  73. - name: Set postgres_start_wait_time, if not provided
  74. set_fact:
  75. postgres_start_wait_time: 15
  76. when: "postgres_start_wait_time|default('') == ''"
  77. # Actual import work
  78. # matrix-postgres is most likely started already
  79. - name: Ensure matrix-postgres is started
  80. service:
  81. name: matrix-postgres
  82. state: started
  83. daemon_reload: true
  84. register: matrix_postgres_service_start_result
  85. - name: Wait a bit, so that Postgres can start
  86. wait_for:
  87. timeout: "{{ postgres_start_wait_time }}"
  88. delegate_to: 127.0.0.1
  89. become: false
  90. when: "matrix_postgres_service_start_result.changed|bool"
  91. # We only stop services here, leaving it to the caller to start them later.
  92. #
  93. # We can't start them, because they probably need to be reconfigured too (changing the configuration from using SQLite to Postgres, etc.),
  94. # before starting.
  95. #
  96. # Since the caller will be starting them, it might make sense to leave stopping to it as well.
  97. # However, we don't do it, because it's simpler having it here, and it also gets to happen only if we'll be doing an import.
  98. # If we bailed out (somewhere above), nothing would have gotten stopped. It's nice to leave this running in such cases.
  99. - name: Ensure systemd services blocking the database import are stopped
  100. service:
  101. name: "{{ item }}"
  102. state: stopped
  103. failed_when: false
  104. with_items: "{{ matrix_postgres_db_migration_request.systemd_services_to_stop }}"
  105. - name: Import {{ matrix_postgres_db_migration_request.engine_old }} database from {{ matrix_postgres_db_migration_request.src }} into Postgres
  106. command:
  107. cmd: >-
  108. {{ matrix_host_command_docker }} run
  109. --rm
  110. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  111. --cap-drop=ALL
  112. --network={{ matrix_docker_network }}
  113. --mount type=bind,src={{ matrix_postgres_db_migration_request.src }},dst=/in.db,ro
  114. --entrypoint=/bin/sh
  115. {{ matrix_postgres_pgloader_docker_image }}
  116. -c
  117. 'pgloader {{ matrix_postgres_db_migration_request.pgloader_options|default([])|join(' ') }} /in.db {{ matrix_postgres_db_migration_request.dst }}'
  118. - block:
  119. # We can't use `{{ role_path }}` here, neither with `import_tasks`, nor with `include_tasks`,
  120. # because it refers to the role that included this util, and not to the role this file belongs to.
  121. - import_tasks: "{{ role_path }}/../matrix-postgres/tasks/util/detect_existing_postgres_version.yml"
  122. - set_fact:
  123. matrix_postgres_docker_image_to_use: "{{ matrix_postgres_docker_image_latest if matrix_postgres_detected_version_corresponding_docker_image == '' else matrix_postgres_detected_version_corresponding_docker_image }}"
  124. - name: Execute additional Postgres SQL migration statements
  125. command:
  126. cmd: >-
  127. {{ matrix_host_command_docker }} run
  128. --rm
  129. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  130. --cap-drop=ALL
  131. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  132. --network={{ matrix_docker_network }}
  133. {{ matrix_postgres_docker_image_to_use }}
  134. psql --host=matrix-postgres --dbname={{ matrix_postgres_db_migration_request.additional_psql_statements_db_name }} --command='{{ item }}'
  135. with_items: "{{ matrix_postgres_db_migration_request.additional_psql_statements_list }}"
  136. when: "matrix_postgres_db_migration_request.additional_psql_statements_list|default([])|length > 0"
  137. - name: Archive {{ matrix_postgres_db_migration_request.engine_old }} database ({{ matrix_postgres_db_migration_request.src }} -> {{ matrix_postgres_db_migration_request.src }}.backup)
  138. command:
  139. cmd: "mv {{ matrix_postgres_db_migration_request.src }} {{ matrix_postgres_db_migration_request.src }}.backup"
  140. - name: Inject result
  141. set_fact:
  142. matrix_playbook_runtime_results: |
  143. {{
  144. matrix_playbook_runtime_results|default([])
  145. +
  146. [
  147. "NOTE: Your {{ matrix_postgres_db_migration_request.engine_old }} database file has been imported into Postgres. The original database file has been moved from `{{ matrix_postgres_db_migration_request.src }}` to `{{ matrix_postgres_db_migration_request.src }}.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete this file."
  148. ]
  149. }}