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.
 
 

182 líneas
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/util/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. - name: Ensure matrix-postgres is stopped
  81. ansible.builtin.service:
  82. name: matrix-postgres
  83. state: stopped
  84. - name: Rename existing Postgres data directory
  85. ansible.builtin.command:
  86. cmd: "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
  87. register: matrix_postgres_upgrade_postgres_move_command_result
  88. changed_when: matrix_postgres_upgrade_postgres_move_command_result.rc == 0
  89. - ansible.builtin.debug:
  90. 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."
  91. - ansible.builtin.import_tasks: tasks/setup_postgres.yml
  92. - name: Ensure matrix-postgres autoruns and is restarted
  93. ansible.builtin.service:
  94. name: matrix-postgres
  95. enabled: true
  96. state: restarted
  97. daemon_reload: true
  98. - name: Wait a bit, so that Postgres can start
  99. ansible.builtin.wait_for:
  100. timeout: "{{ postgres_start_wait_time }}"
  101. delegate_to: 127.0.0.1
  102. become: false
  103. # Starting the database container had automatically created the default
  104. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  105. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  106. # We need to skip over those lines.
  107. - name: Generate Postgres database import command
  108. ansible.builtin.set_fact:
  109. matrix_postgres_import_command: >-
  110. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
  111. --log-driver=none
  112. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  113. --cap-drop=ALL
  114. --network={{ matrix_docker_network }}
  115. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  116. --entrypoint=/bin/sh
  117. --mount type=bind,src={{ postgres_dump_dir }},dst=/in,ro
  118. {{ matrix_postgres_docker_image_latest }}
  119. -c "cat /in/{{ postgres_dump_name }} |
  120. {{ 'gunzip |' if postgres_dump_name.endswith('.gz') else '' }}
  121. grep -vE '{{ matrix_postgres_import_roles_ignore_regex }}' |
  122. grep -vE '{{ matrix_postgres_import_databases_ignore_regex }}' |
  123. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  124. # This is a hack.
  125. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  126. #
  127. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  128. # which ruins the command (`matrix_postgres_import_command`)
  129. - name: Note about Postgres importing
  130. ansible.builtin.set_fact:
  131. dummy: true
  132. with_items:
  133. - >-
  134. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  135. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  136. delete the new database data (`rm -rf {{ matrix_postgres_data_path }}`)
  137. and restore the automatically-made backup (`mv {{ postgres_auto_upgrade_backup_data_path }} {{ matrix_postgres_data_path }}`).
  138. - name: Perform Postgres database import
  139. ansible.builtin.command:
  140. cmd: "{{ matrix_postgres_import_command }}"
  141. register: matrix_postgres_upgrade_postgres_import_command_result
  142. changed_when: matrix_postgres_upgrade_postgres_import_command_result.rc == 0
  143. - name: Delete Postgres database dump file
  144. ansible.builtin.file:
  145. path: "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
  146. state: absent
  147. - name: Ensure matrix-synapse is started
  148. ansible.builtin.service:
  149. name: matrix-synapse
  150. state: started
  151. daemon_reload: true
  152. - ansible.builtin.debug:
  153. 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."