Matrix Docker Ansible eploy
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

186 lignes
7.7 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 matrix-synapse is stopped
  46. ansible.builtin.service:
  47. name: matrix-synapse
  48. state: stopped
  49. - name: Ensure matrix-postgres is started
  50. ansible.builtin.service:
  51. name: matrix-postgres
  52. state: started
  53. daemon_reload: true
  54. - name: Wait a bit, so that Postgres can start
  55. ansible.builtin.wait_for:
  56. timeout: "{{ postgres_start_wait_time }}"
  57. delegate_to: 127.0.0.1
  58. become: false
  59. # We dump all databases, roles, etc.
  60. #
  61. # Because we'll be importing into a new container which initializes the default
  62. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`) by itself on startup,
  63. # we need to remove these from the dump, or we'll get errors saying these already exist.
  64. - name: Perform Postgres database dump
  65. ansible.builtin.command:
  66. cmd: >-
  67. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-dump
  68. --log-driver=none
  69. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  70. --network={{ matrix_docker_network }}
  71. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  72. --entrypoint=/bin/sh
  73. --mount type=bind,src={{ postgres_dump_dir }},dst=/out
  74. {{ matrix_postgres_detected_version_corresponding_docker_image }}
  75. -c "pg_dumpall -h matrix-postgres
  76. {{ '| gzip -c ' if postgres_dump_name.endswith('.gz') else '' }}
  77. > /out/{{ postgres_dump_name }}"
  78. register: matrix_postgres_upgrade_postgres_dump_command_result
  79. changed_when: matrix_postgres_upgrade_postgres_dump_command_result.rc == 0
  80. tags:
  81. - skip_ansible_lint
  82. - name: Ensure matrix-postgres is stopped
  83. ansible.builtin.service:
  84. name: matrix-postgres
  85. state: stopped
  86. - name: Rename existing Postgres data directory
  87. ansible.builtin.command:
  88. cmd: "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
  89. register: matrix_postgres_upgrade_postgres_move_command_result
  90. changed_when: matrix_postgres_upgrade_postgres_move_command_result.rc == 0
  91. - ansible.builtin.debug:
  92. 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."
  93. - ansible.builtin.import_tasks: tasks/setup_postgres.yml
  94. - name: Ensure matrix-postgres autoruns and is restarted
  95. ansible.builtin.service:
  96. name: matrix-postgres
  97. enabled: true
  98. state: restarted
  99. daemon_reload: true
  100. - name: Wait a bit, so that Postgres can start
  101. ansible.builtin.wait_for:
  102. timeout: "{{ postgres_start_wait_time }}"
  103. delegate_to: 127.0.0.1
  104. become: false
  105. # Starting the database container had automatically created the default
  106. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  107. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  108. # We need to skip over those lines.
  109. - name: Generate Postgres database import command
  110. ansible.builtin.set_fact:
  111. matrix_postgres_import_command: >-
  112. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
  113. --log-driver=none
  114. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  115. --cap-drop=ALL
  116. --network={{ matrix_docker_network }}
  117. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  118. --entrypoint=/bin/sh
  119. --mount type=bind,src={{ postgres_dump_dir }},dst=/in,ro
  120. {{ matrix_postgres_docker_image_latest }}
  121. -c "cat /in/{{ postgres_dump_name }} |
  122. {{ 'gunzip |' if postgres_dump_name.endswith('.gz') else '' }}
  123. grep -vE '{{ matrix_postgres_import_roles_ignore_regex }}' |
  124. grep -vE '{{ matrix_postgres_import_databases_ignore_regex }}' |
  125. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  126. tags:
  127. - skip_ansible_lint
  128. # This is a hack.
  129. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  130. #
  131. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  132. # which ruins the command (`matrix_postgres_import_command`)
  133. - name: Note about Postgres importing
  134. ansible.builtin.set_fact:
  135. dummy: true
  136. with_items:
  137. - >-
  138. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  139. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  140. delete the new database data (`rm -rf {{ matrix_postgres_data_path }}`)
  141. and restore the automatically-made backup (`mv {{ postgres_auto_upgrade_backup_data_path }} {{ matrix_postgres_data_path }}`).
  142. - name: Perform Postgres database import
  143. ansible.builtin.command:
  144. cmd: "{{ matrix_postgres_import_command }}"
  145. register: matrix_postgres_upgrade_postgres_import_command_result
  146. changed_when: matrix_postgres_upgrade_postgres_import_command_result.rc == 0
  147. - name: Delete Postgres database dump file
  148. ansible.builtin.file:
  149. path: "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
  150. state: absent
  151. - name: Ensure matrix-synapse is started
  152. ansible.builtin.service:
  153. name: matrix-synapse
  154. state: started
  155. daemon_reload: true
  156. - ansible.builtin.debug:
  157. 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."