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.
 
 

166 wiersze
6.4 KiB

  1. ---
  2. - name: Set default postgres_dump_dir, if not provided
  3. set_fact:
  4. postgres_dump_dir: "/tmp"
  5. when: "postgres_dump_dir|default('') == ''"
  6. - name: Set postgres_dump_name, if not provided
  7. 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. 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. set_fact:
  16. postgres_start_wait_time: 15
  17. when: "postgres_start_wait_time|default('') == ''"
  18. - name: Fail, if trying to upgrade external Postgres database
  19. fail:
  20. msg: "Your configuration indicates that you're not using Postgres from this role. There is nothing to upgrade."
  21. when: "not matrix_postgres_enabled|bool"
  22. - name: Check Postgres auto-upgrade backup data directory
  23. stat:
  24. path: "{{ postgres_auto_upgrade_backup_data_path }}"
  25. register: result_auto_upgrade_path
  26. - name: Abort, if existing Postgres auto-upgrade data path detected
  27. fail:
  28. 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."
  29. when: "result_auto_upgrade_path.stat.exists"
  30. - import_tasks: tasks/util/detect_existing_postgres_version.yml
  31. - name: Abort, if no existing Postgres version detected
  32. fail:
  33. msg: "Could not find existing Postgres installation"
  34. when: "not matrix_postgres_detected_existing|bool"
  35. - name: Abort, if already at latest Postgres version
  36. fail:
  37. msg: "You are already running the latest Postgres version supported ({{ matrix_postgres_docker_image_latest }}). Nothing to do"
  38. when: "matrix_postgres_detected_version_corresponding_docker_image == matrix_postgres_docker_image_latest"
  39. - debug:
  40. msg: "Upgrading database from {{ matrix_postgres_detected_version_corresponding_docker_image }} to {{ matrix_postgres_docker_image_latest }}"
  41. - name: Ensure matrix-synapse is stopped
  42. service:
  43. name: matrix-synapse
  44. state: stopped
  45. - name: Ensure matrix-postgres is started
  46. service:
  47. name: matrix-postgres
  48. state: started
  49. daemon_reload: yes
  50. - name: Wait a bit, so that Postgres can start
  51. wait_for:
  52. timeout: "{{ postgres_start_wait_time }}"
  53. delegate_to: 127.0.0.1
  54. become: false
  55. # We dump all databases, roles, etc.
  56. #
  57. # Because we'll be importing into a new container which initializes the default
  58. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`) by itself on startup,
  59. # we need to remove these from the dump, or we'll get errors saying these already exist.
  60. - name: Perform Postgres database dump
  61. command: >-
  62. /usr/bin/docker run --rm --name matrix-postgres-dump
  63. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  64. --network={{ matrix_docker_network }}
  65. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  66. --entrypoint=/bin/sh
  67. -v {{ postgres_dump_dir }}:/out
  68. {{ matrix_postgres_detected_version_corresponding_docker_image }}
  69. -c "pg_dumpall -h matrix-postgres
  70. {{ '| gzip -c ' if postgres_dump_name.endswith('.gz') else '' }}
  71. > /out/{{ postgres_dump_name }}"
  72. - name: Ensure matrix-postgres is stopped
  73. service:
  74. name: matrix-postgres
  75. state: stopped
  76. - name: Rename existing Postgres data directory
  77. command: "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
  78. - debug:
  79. 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."
  80. - import_tasks: tasks/setup_postgres.yml
  81. - name: Ensure matrix-postgres autoruns and is restarted
  82. service:
  83. name: matrix-postgres
  84. enabled: yes
  85. state: restarted
  86. daemon_reload: yes
  87. - name: Wait a bit, so that Postgres can start
  88. wait_for:
  89. timeout: "{{ postgres_start_wait_time }}"
  90. delegate_to: 127.0.0.1
  91. become: false
  92. # Starting the database container had automatically created the default
  93. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  94. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  95. # We need to skip over those lines.
  96. - name: Generate Postgres database import command
  97. set_fact:
  98. matrix_postgres_import_command: >-
  99. /usr/bin/docker run --rm --name matrix-postgres-import
  100. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  101. --cap-drop=ALL
  102. --network={{ matrix_docker_network }}
  103. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  104. --entrypoint=/bin/sh
  105. -v {{ postgres_dump_dir }}:/in:ro
  106. {{ matrix_postgres_docker_image_latest }}
  107. -c "cat /in/{{ postgres_dump_name }} |
  108. {{ 'gunzip |' if postgres_dump_name.endswith('.gz') else '' }}
  109. grep -vE '^CREATE ROLE {{ matrix_postgres_connection_username }}' |
  110. grep -vE '^CREATE DATABASE {{ matrix_postgres_db_name }}' |
  111. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  112. # This is a hack.
  113. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  114. #
  115. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  116. # which ruins the command (`matrix_postgres_import_command`)
  117. - name: Note about Postgres importing
  118. set_fact:
  119. dummy: true
  120. with_items:
  121. - >-
  122. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  123. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  124. delete the new database data (`rm -rf {{ matrix_postgres_data_path }}`)
  125. and restore the automatically-made backup (`mv {{ postgres_auto_upgrade_backup_data_path }} {{ matrix_postgres_data_path }}`).
  126. - name: Perform Postgres database import
  127. command: "{{ matrix_postgres_import_command }}"
  128. - name: Delete Postgres database dump file
  129. file:
  130. path: "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
  131. state: absent
  132. - name: Ensure matrix-synapse is started
  133. service:
  134. name: matrix-synapse
  135. state: started
  136. daemon_reload: yes
  137. - debug:
  138. 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."