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

168 строки
7.7 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. register: matrix_postgres_pgloader_git_pull_results
  36. # 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:
  37. # > the octet sequence #(194) cannot be decoded
  38. #
  39. # The issue is described here and is not getting fixed for months: https://github.com/dimitri/pgloader/pull/1179
  40. #
  41. # Although we're not using the dimitri/pgloader image, the one we're using suffers from the same problem.
  42. - name: Switch pgloader base image from Debian stable (likely 10.x/Buster) to Bullseye
  43. lineinfile:
  44. path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}/Dockerfile"
  45. regexp: "{{ item.match }}"
  46. line: "{{ item.replace }}"
  47. with_items:
  48. - match: '^FROM debian:stable-slim as builder$'
  49. replace: 'FROM debian:bullseye-slim as builder'
  50. - match: '^FROM debian:stable-slim$'
  51. replace: 'FROM debian:bullseye-slim'
  52. - name: Ensure pgloader Docker image is built
  53. docker_image:
  54. name: "{{ matrix_postgres_pgloader_docker_image }}"
  55. source: build
  56. force_source: "{{ matrix_postgres_pgloader_git_pull_results.changed }}"
  57. build:
  58. dockerfile: Dockerfile
  59. path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
  60. pull: yes
  61. when: "matrix_postgres_pgloader_container_image_self_build|bool"
  62. - name: Ensure pgloader Docker image is pulled
  63. docker_image:
  64. name: "{{ matrix_postgres_pgloader_docker_image }}"
  65. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  66. force_source: "{{ matrix_postgres_pgloader_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  67. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_pgloader_docker_image_force_pull }}"
  68. when: "not matrix_postgres_pgloader_container_image_self_build"
  69. # Defaults
  70. - name: Set postgres_start_wait_time, if not provided
  71. set_fact:
  72. postgres_start_wait_time: 15
  73. when: "postgres_start_wait_time|default('') == ''"
  74. # Actual import work
  75. # matrix-postgres is most likely started already
  76. - name: Ensure matrix-postgres is started
  77. service:
  78. name: matrix-postgres
  79. state: started
  80. daemon_reload: yes
  81. register: matrix_postgres_service_start_result
  82. - name: Wait a bit, so that Postgres can start
  83. wait_for:
  84. timeout: "{{ postgres_start_wait_time }}"
  85. delegate_to: 127.0.0.1
  86. become: false
  87. when: "matrix_postgres_service_start_result.changed|bool"
  88. # We only stop services here, leaving it to the caller to start them later.
  89. #
  90. # We can't start them, because they probably need to be reconfigured too (changing the configuration from using SQLite to Postgres, etc.),
  91. # before starting.
  92. #
  93. # Since the caller will be starting them, it might make sense to leave stopping to it as well.
  94. # 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.
  95. # If we bailed out (somewhere above), nothing would have gotten stopped. It's nice to leave this running in such cases.
  96. - name: Ensure systemd services blocking the database import are stopped
  97. service:
  98. name: "{{ item }}"
  99. state: stopped
  100. with_items: "{{ matrix_postgres_db_migration_request.systemd_services_to_stop }}"
  101. - name: Import {{ matrix_postgres_db_migration_request.engine_old }} database from {{ matrix_postgres_db_migration_request.src }} into Postgres
  102. command:
  103. cmd: >-
  104. {{ matrix_host_command_docker }} run
  105. --rm
  106. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  107. --cap-drop=ALL
  108. --network={{ matrix_docker_network }}
  109. --mount type=bind,src={{ matrix_postgres_db_migration_request.src }},dst=/in.db,ro
  110. --entrypoint=/bin/sh
  111. {{ matrix_postgres_pgloader_docker_image }}
  112. -c
  113. 'pgloader {{ matrix_postgres_db_migration_request.pgloader_options|default([])|join(' ') }} /in.db {{ matrix_postgres_db_migration_request.dst }}'
  114. - name: Archive {{ matrix_postgres_db_migration_request.engine_old }} database ({{ matrix_postgres_db_migration_request.src }} -> {{ matrix_postgres_db_migration_request.src }}.backup)
  115. command:
  116. cmd: "mv {{ matrix_postgres_db_migration_request.src }} {{ matrix_postgres_db_migration_request.src }}.backup"
  117. - block:
  118. # We can't use `{{ role_path }}` here, neither with `import_tasks`, nor with `include_tasks`,
  119. # because it refers to the role that included this util, and not to the role this file belongs to.
  120. - import_tasks: "{{ role_path }}/../matrix-postgres/tasks/util/detect_existing_postgres_version.yml"
  121. - set_fact:
  122. 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 }}"
  123. - name: Execute additional Postgres SQL migration statements
  124. command:
  125. cmd: >-
  126. {{ matrix_host_command_docker }} run
  127. --rm
  128. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  129. --cap-drop=ALL
  130. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  131. --network={{ matrix_docker_network }}
  132. {{ matrix_postgres_docker_image_to_use }}
  133. psql --host=matrix-postgres --dbname={{ matrix_postgres_db_migration_request.additional_psql_statements_db_name }} --command='{{ item }}'
  134. with_items: "{{ matrix_postgres_db_migration_request.additional_psql_statements_list }}"
  135. when: "matrix_postgres_db_migration_request.additional_psql_statements_list|default([])|length > 0"
  136. - name: Inject result
  137. set_fact:
  138. matrix_playbook_runtime_results: |
  139. {{
  140. matrix_playbook_runtime_results|default([])
  141. +
  142. [
  143. "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."
  144. ]
  145. }}