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

177 строки
8.6 KiB

  1. ---
  2. - name: Fail if Postgres not enabled
  3. ansible.builtin.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. ansible.builtin.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. ansible.builtin.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. ansible.builtin.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. ansible.builtin.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. - when: "matrix_postgres_pgloader_container_image_self_build | bool"
  29. block:
  30. - name: Ensure pgloader repository is present on self-build
  31. ansible.builtin.git:
  32. repo: "{{ matrix_postgres_pgloader_container_image_self_build_repo }}"
  33. dest: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
  34. version: "{{ matrix_postgres_pgloader_container_image_self_build_repo_branch }}"
  35. force: "yes"
  36. become: true
  37. become_user: "{{ matrix_user_username }}"
  38. register: matrix_postgres_pgloader_git_pull_results
  39. # 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:
  40. # > the octet sequence #(194) cannot be decoded
  41. #
  42. # The issue is described here and is not getting fixed for months: https://github.com/dimitri/pgloader/pull/1179
  43. #
  44. # Although we're not using the dimitri/pgloader image, the one we're using suffers from the same problem.
  45. - name: Switch pgloader base image from Debian stable (likely 10.x/Buster) to Bullseye
  46. ansible.builtin.lineinfile:
  47. path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}/Dockerfile"
  48. regexp: "{{ item.match }}"
  49. line: "{{ item.replace }}"
  50. with_items:
  51. - match: '^FROM debian:stable-slim as builder$'
  52. replace: 'FROM debian:bullseye-slim as builder'
  53. - match: '^FROM debian:stable-slim$'
  54. replace: 'FROM debian:bullseye-slim'
  55. - name: Ensure pgloader Docker image is built
  56. community.docker.docker_image:
  57. name: "{{ matrix_postgres_pgloader_docker_image }}"
  58. source: build
  59. force_source: "{{ matrix_postgres_pgloader_git_pull_results.changed if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  60. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_pgloader_git_pull_results.changed }}"
  61. build:
  62. dockerfile: Dockerfile
  63. path: "{{ matrix_postgres_pgloader_container_image_self_build_src_path }}"
  64. pull: true
  65. - name: Ensure pgloader Docker image is pulled
  66. community.docker.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. ansible.builtin.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. ansible.builtin.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. ansible.builtin.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. ansible.builtin.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 # noqa name[template]
  106. ansible.builtin.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. register: matrix_postgres_migrate_db_to_postgres_import_result
  119. changed_when: matrix_postgres_migrate_db_to_postgres_import_result.rc == 0
  120. - when: "matrix_postgres_db_migration_request.additional_psql_statements_list | default([]) | length > 0"
  121. block:
  122. - ansible.builtin.import_role:
  123. name: custom/matrix-postgres
  124. tasks_from: detect_existing_postgres_version
  125. - ansible.builtin.set_fact:
  126. 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 }}"
  127. - name: Execute additional Postgres SQL migration statements
  128. ansible.builtin.command:
  129. cmd: >-
  130. {{ matrix_host_command_docker }} run
  131. --rm
  132. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  133. --cap-drop=ALL
  134. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  135. --network={{ matrix_docker_network }}
  136. {{ matrix_postgres_docker_image_to_use }}
  137. psql --host=matrix-postgres --dbname={{ matrix_postgres_db_migration_request.additional_psql_statements_db_name }} --command='{{ item }}'
  138. with_items: "{{ matrix_postgres_db_migration_request.additional_psql_statements_list }}"
  139. register: matrix_postgres_migrate_db_to_postgres_additional_queries_result
  140. changed_when: matrix_postgres_migrate_db_to_postgres_additional_queries_result.rc == 0
  141. - name: Archive {{ matrix_postgres_db_migration_request.engine_old }} database ({{ matrix_postgres_db_migration_request.src }} -> {{ matrix_postgres_db_migration_request.src }}.backup) # noqa name[template]
  142. ansible.builtin.command:
  143. cmd: "mv {{ matrix_postgres_db_migration_request.src }} {{ matrix_postgres_db_migration_request.src }}.backup"
  144. register: matrix_postgres_migrate_db_to_postgres_move_result
  145. changed_when: matrix_postgres_migrate_db_to_postgres_move_result.rc == 0
  146. - name: Inject result
  147. ansible.builtin.set_fact:
  148. devture_playbook_runtime_messages_list: |
  149. {{
  150. devture_playbook_runtime_messages_list | default([])
  151. +
  152. [
  153. "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."
  154. ]
  155. }}