Matrix Docker Ansible eploy
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

189 wiersze
7.9 KiB

  1. ---
  2. - name: Set default postgres_dump_dir, if not provided
  3. ansible.builtin.set_fact:
  4. postgres_dump_dir: "/tmp"
  5. when: "postgres_dump_dir | default('') == ''"
  6. - name: Set postgres_dump_name, if not provided
  7. ansible.builtin.set_fact:
  8. postgres_dump_name: "matrix-postgres-dump.sql.gz"
  9. when: "postgres_dump_name | default('') == ''"
  10. - name: Set postgres_auto_upgrade_backup_data_path, if not provided
  11. ansible.builtin.set_fact:
  12. postgres_auto_upgrade_backup_data_path: "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
  13. when: "postgres_auto_upgrade_backup_data_path | default('') == ''"
  14. - name: Set postgres_start_wait_time, if not provided
  15. ansible.builtin.set_fact:
  16. postgres_start_wait_time: 15
  17. when: "postgres_start_wait_time | default('') == ''"
  18. - name: Set postgres_force_upgrade, if not provided
  19. ansible.builtin.set_fact:
  20. postgres_force_upgrade: false
  21. when: "postgres_force_upgrade | default('') == ''"
  22. - name: Fail, if trying to upgrade external Postgres database
  23. ansible.builtin.fail:
  24. msg: "Your configuration indicates that you're not using Postgres from this role. There is nothing to upgrade."
  25. when: "not matrix_postgres_enabled | bool"
  26. - name: Check Postgres auto-upgrade backup data directory
  27. ansible.builtin.stat:
  28. path: "{{ postgres_auto_upgrade_backup_data_path }}"
  29. register: result_auto_upgrade_path
  30. - name: Abort, if existing Postgres auto-upgrade data path detected
  31. ansible.builtin.fail:
  32. msg: "Detected that a left-over {{ postgres_auto_upgrade_backup_data_path }} exists. You should rename it to {{ matrix_postgres_data_path }} if the previous upgrade went wrong, or delete it if it went well."
  33. when: "result_auto_upgrade_path.stat.exists"
  34. - ansible.builtin.import_tasks: tasks/detect_existing_postgres_version.yml
  35. - name: Abort, if no existing Postgres version detected
  36. ansible.builtin.fail:
  37. msg: "Could not find existing Postgres installation"
  38. when: "not matrix_postgres_detected_existing | bool"
  39. - name: Abort, if already at latest Postgres version
  40. ansible.builtin.fail:
  41. msg: "You are already running the latest Postgres version supported ({{ matrix_postgres_docker_image_latest }}). Nothing to do"
  42. when: "matrix_postgres_detected_version_corresponding_docker_image == matrix_postgres_docker_image_latest and not postgres_force_upgrade"
  43. - ansible.builtin.debug:
  44. msg: "Upgrading database from {{ matrix_postgres_detected_version_corresponding_docker_image }} to {{ matrix_postgres_docker_image_latest }}"
  45. - name: Ensure services are stopped
  46. ansible.builtin.service:
  47. name: "{{ item }}"
  48. state: stopped
  49. daemon_reload: true
  50. with_items: "{{ matrix_postgres_systemd_services_to_stop_for_maintenance_list }}"
  51. - name: Ensure matrix-postgres is started
  52. ansible.builtin.service:
  53. name: matrix-postgres
  54. state: started
  55. daemon_reload: true
  56. - name: Wait a bit, so that Postgres can start
  57. ansible.builtin.wait_for:
  58. timeout: "{{ postgres_start_wait_time }}"
  59. delegate_to: 127.0.0.1
  60. become: false
  61. # We dump all databases, roles, etc.
  62. #
  63. # Because we'll be importing into a new container which initializes the default
  64. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`) by itself on startup,
  65. # we need to remove these from the dump, or we'll get errors saying these already exist.
  66. - name: Perform Postgres database dump
  67. ansible.builtin.command:
  68. cmd: >-
  69. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-dump
  70. --log-driver=none
  71. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  72. --network={{ matrix_docker_network }}
  73. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  74. --entrypoint=/bin/sh
  75. --mount type=bind,src={{ postgres_dump_dir }},dst=/out
  76. {{ matrix_postgres_detected_version_corresponding_docker_image }}
  77. -c "pg_dumpall -h matrix-postgres
  78. {{ '| gzip -c ' if postgres_dump_name.endswith('.gz') else '' }}
  79. > /out/{{ postgres_dump_name }}"
  80. register: matrix_postgres_upgrade_postgres_dump_command_result
  81. changed_when: matrix_postgres_upgrade_postgres_dump_command_result.rc == 0
  82. tags:
  83. - skip_ansible_lint
  84. - name: Ensure matrix-postgres is stopped
  85. ansible.builtin.service:
  86. name: matrix-postgres
  87. state: stopped
  88. - name: Rename existing Postgres data directory
  89. ansible.builtin.command:
  90. cmd: "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
  91. register: matrix_postgres_upgrade_postgres_move_command_result
  92. changed_when: matrix_postgres_upgrade_postgres_move_command_result.rc == 0
  93. - ansible.builtin.debug:
  94. msg: "NOTE: Your Postgres data directory has been moved from `{{ matrix_postgres_data_path }}` to `{{ postgres_auto_upgrade_backup_data_path }}`. In the event of failure, you can move it back and run the playbook with --tags=setup-postgres to restore operation."
  95. - ansible.builtin.import_tasks: tasks/setup_postgres.yml
  96. - name: Ensure matrix-postgres autoruns and is restarted
  97. ansible.builtin.service:
  98. name: matrix-postgres
  99. enabled: true
  100. state: restarted
  101. daemon_reload: true
  102. - name: Wait a bit, so that Postgres can start
  103. ansible.builtin.wait_for:
  104. timeout: "{{ postgres_start_wait_time }}"
  105. delegate_to: 127.0.0.1
  106. become: false
  107. # Starting the database container had automatically created the default
  108. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  109. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  110. # We need to skip over those lines.
  111. - name: Generate Postgres database import command
  112. ansible.builtin.set_fact:
  113. matrix_postgres_import_command: >-
  114. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
  115. --log-driver=none
  116. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  117. --cap-drop=ALL
  118. --network={{ matrix_docker_network }}
  119. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  120. --entrypoint=/bin/sh
  121. --mount type=bind,src={{ postgres_dump_dir }},dst=/in,ro
  122. {{ matrix_postgres_docker_image_latest }}
  123. -c "cat /in/{{ postgres_dump_name }} |
  124. {{ 'gunzip |' if postgres_dump_name.endswith('.gz') else '' }}
  125. grep -vE '{{ matrix_postgres_import_roles_ignore_regex }}' |
  126. grep -vE '{{ matrix_postgres_import_databases_ignore_regex }}' |
  127. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  128. tags:
  129. - skip_ansible_lint
  130. # This is a hack.
  131. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  132. #
  133. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  134. # which ruins the command (`matrix_postgres_import_command`)
  135. - name: Note about Postgres importing
  136. ansible.builtin.set_fact:
  137. dummy: true
  138. with_items:
  139. - >-
  140. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  141. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  142. delete the new database data (`rm -rf {{ matrix_postgres_data_path }}`)
  143. and restore the automatically-made backup (`mv {{ postgres_auto_upgrade_backup_data_path }} {{ matrix_postgres_data_path }}`).
  144. - name: Perform Postgres database import
  145. ansible.builtin.command:
  146. cmd: "{{ matrix_postgres_import_command }}"
  147. register: matrix_postgres_upgrade_postgres_import_command_result
  148. changed_when: matrix_postgres_upgrade_postgres_import_command_result.rc == 0
  149. - name: Delete Postgres database dump file
  150. ansible.builtin.file:
  151. path: "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
  152. state: absent
  153. - name: Ensure services are started
  154. ansible.builtin.service:
  155. name: "{{ item }}"
  156. state: started
  157. daemon_reload: true
  158. with_items: "{{ matrix_postgres_systemd_services_to_stop_for_maintenance_list }}"
  159. - ansible.builtin.debug:
  160. msg: "NOTE: Your old Postgres data directory is preserved at `{{ postgres_auto_upgrade_backup_data_path }}`. You might want to get rid of it once you've confirmed that all is well."