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.
 
 

170 rivejä
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. 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 if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  57. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_pgloader_git_pull_results.changed }}"
  58. build:
  59. dockerfile: Dockerfile
  60. path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
  61. pull: yes
  62. when: "matrix_postgres_pgloader_container_image_self_build|bool"
  63. - name: Ensure pgloader Docker image is pulled
  64. docker_image:
  65. name: "{{ matrix_postgres_pgloader_docker_image }}"
  66. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  67. force_source: "{{ matrix_postgres_pgloader_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  68. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_pgloader_docker_image_force_pull }}"
  69. when: "not matrix_postgres_pgloader_container_image_self_build"
  70. # Defaults
  71. - name: Set postgres_start_wait_time, if not provided
  72. set_fact:
  73. postgres_start_wait_time: 15
  74. when: "postgres_start_wait_time|default('') == ''"
  75. # Actual import work
  76. # matrix-postgres is most likely started already
  77. - name: Ensure matrix-postgres is started
  78. service:
  79. name: matrix-postgres
  80. state: started
  81. daemon_reload: yes
  82. register: matrix_postgres_service_start_result
  83. - name: Wait a bit, so that Postgres can start
  84. wait_for:
  85. timeout: "{{ postgres_start_wait_time }}"
  86. delegate_to: 127.0.0.1
  87. become: false
  88. when: "matrix_postgres_service_start_result.changed|bool"
  89. # We only stop services here, leaving it to the caller to start them later.
  90. #
  91. # We can't start them, because they probably need to be reconfigured too (changing the configuration from using SQLite to Postgres, etc.),
  92. # before starting.
  93. #
  94. # Since the caller will be starting them, it might make sense to leave stopping to it as well.
  95. # 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.
  96. # If we bailed out (somewhere above), nothing would have gotten stopped. It's nice to leave this running in such cases.
  97. - name: Ensure systemd services blocking the database import are stopped
  98. service:
  99. name: "{{ item }}"
  100. state: stopped
  101. failed_when: false
  102. with_items: "{{ matrix_postgres_db_migration_request.systemd_services_to_stop }}"
  103. - name: Import {{ matrix_postgres_db_migration_request.engine_old }} database from {{ matrix_postgres_db_migration_request.src }} into Postgres
  104. command:
  105. cmd: >-
  106. {{ matrix_host_command_docker }} run
  107. --rm
  108. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  109. --cap-drop=ALL
  110. --network={{ matrix_docker_network }}
  111. --mount type=bind,src={{ matrix_postgres_db_migration_request.src }},dst=/in.db,ro
  112. --entrypoint=/bin/sh
  113. {{ matrix_postgres_pgloader_docker_image }}
  114. -c
  115. 'pgloader {{ matrix_postgres_db_migration_request.pgloader_options|default([])|join(' ') }} /in.db {{ matrix_postgres_db_migration_request.dst }}'
  116. - block:
  117. # We can't use `{{ role_path }}` here, neither with `import_tasks`, nor with `include_tasks`,
  118. # because it refers to the role that included this util, and not to the role this file belongs to.
  119. - import_tasks: "{{ role_path }}/../matrix-postgres/tasks/util/detect_existing_postgres_version.yml"
  120. - set_fact:
  121. 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 }}"
  122. - name: Execute additional Postgres SQL migration statements
  123. command:
  124. cmd: >-
  125. {{ matrix_host_command_docker }} run
  126. --rm
  127. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  128. --cap-drop=ALL
  129. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  130. --network={{ matrix_docker_network }}
  131. {{ matrix_postgres_docker_image_to_use }}
  132. psql --host=matrix-postgres --dbname={{ matrix_postgres_db_migration_request.additional_psql_statements_db_name }} --command='{{ item }}'
  133. with_items: "{{ matrix_postgres_db_migration_request.additional_psql_statements_list }}"
  134. when: "matrix_postgres_db_migration_request.additional_psql_statements_list|default([])|length > 0"
  135. - name: Archive {{ matrix_postgres_db_migration_request.engine_old }} database ({{ matrix_postgres_db_migration_request.src }} -> {{ matrix_postgres_db_migration_request.src }}.backup)
  136. command:
  137. cmd: "mv {{ matrix_postgres_db_migration_request.src }} {{ matrix_postgres_db_migration_request.src }}.backup"
  138. - name: Inject result
  139. set_fact:
  140. matrix_playbook_runtime_results: |
  141. {{
  142. matrix_playbook_runtime_results|default([])
  143. +
  144. [
  145. "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."
  146. ]
  147. }}