Matrix Docker Ansible eploy
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

178 líneas
8.5 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. - block:
  29. - name: Ensure pgloader repository is present on self-build
  30. ansible.builtin.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. ansible.builtin.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. 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
  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. - block:
  121. - ansible.builtin.import_role:
  122. name: matrix-postgres
  123. tasks_from: detect_existing_postgres_version
  124. - ansible.builtin.set_fact:
  125. 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 }}"
  126. - name: Execute additional Postgres SQL migration statements
  127. ansible.builtin.command:
  128. cmd: >-
  129. {{ matrix_host_command_docker }} run
  130. --rm
  131. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  132. --cap-drop=ALL
  133. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  134. --network={{ matrix_docker_network }}
  135. {{ matrix_postgres_docker_image_to_use }}
  136. psql --host=matrix-postgres --dbname={{ matrix_postgres_db_migration_request.additional_psql_statements_db_name }} --command='{{ item }}'
  137. with_items: "{{ matrix_postgres_db_migration_request.additional_psql_statements_list }}"
  138. register: matrix_postgres_migrate_db_to_postgres_additional_queries_result
  139. changed_when: matrix_postgres_migrate_db_to_postgres_additional_queries_result.rc == 0
  140. when: "matrix_postgres_db_migration_request.additional_psql_statements_list | default([])|length > 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)
  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. matrix_playbook_runtime_results: |
  149. {{
  150. matrix_playbook_runtime_results | 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. }}