Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

173 lines
6.7 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: Set postgres_force_upgrade, if not provided
  19. set_fact:
  20. postgres_force_upgrade: false
  21. when: "postgres_force_upgrade|default('') == ''"
  22. - name: Fail, if trying to upgrade external Postgres database
  23. 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. 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. 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. - import_tasks: tasks/util/detect_existing_postgres_version.yml
  35. - name: Abort, if no existing Postgres version detected
  36. 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. 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. - 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. service:
  47. name: matrix-synapse
  48. state: stopped
  49. - name: Ensure matrix-postgres is started
  50. service:
  51. name: matrix-postgres
  52. state: started
  53. daemon_reload: yes
  54. - name: Wait a bit, so that Postgres can start
  55. 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. command: >-
  66. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-dump
  67. --log-driver=none
  68. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  69. --network={{ matrix_docker_network }}
  70. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  71. --entrypoint=/bin/sh
  72. --mount type=bind,src={{ postgres_dump_dir }},dst=/out
  73. {{ matrix_postgres_detected_version_corresponding_docker_image }}
  74. -c "pg_dumpall -h matrix-postgres
  75. {{ '| gzip -c ' if postgres_dump_name.endswith('.gz') else '' }}
  76. > /out/{{ postgres_dump_name }}"
  77. - name: Ensure matrix-postgres is stopped
  78. service:
  79. name: matrix-postgres
  80. state: stopped
  81. - name: Rename existing Postgres data directory
  82. command: "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
  83. - debug:
  84. 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."
  85. - import_tasks: tasks/setup_postgres.yml
  86. - name: Ensure matrix-postgres autoruns and is restarted
  87. service:
  88. name: matrix-postgres
  89. enabled: yes
  90. state: restarted
  91. daemon_reload: yes
  92. - name: Wait a bit, so that Postgres can start
  93. wait_for:
  94. timeout: "{{ postgres_start_wait_time }}"
  95. delegate_to: 127.0.0.1
  96. become: false
  97. # Starting the database container had automatically created the default
  98. # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
  99. # The dump most likely contains those same entries and would try to re-create them, leading to errors.
  100. # We need to skip over those lines.
  101. - name: Generate Postgres database import command
  102. set_fact:
  103. matrix_postgres_import_command: >-
  104. {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
  105. --log-driver=none
  106. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  107. --cap-drop=ALL
  108. --network={{ matrix_docker_network }}
  109. --env-file={{ matrix_postgres_base_path }}/env-postgres-psql
  110. --entrypoint=/bin/sh
  111. --mount type=bind,src={{ postgres_dump_dir }},dst=/in,ro
  112. {{ matrix_postgres_docker_image_latest }}
  113. -c "cat /in/{{ postgres_dump_name }} |
  114. {{ 'gunzip |' if postgres_dump_name.endswith('.gz') else '' }}
  115. grep -vE '{{ matrix_postgres_import_roles_ignore_regex }}' |
  116. grep -vE '{{ matrix_postgres_import_databases_ignore_regex }}' |
  117. psql -v ON_ERROR_STOP=1 -h matrix-postgres"
  118. # This is a hack.
  119. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  120. #
  121. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  122. # which ruins the command (`matrix_postgres_import_command`)
  123. - name: Note about Postgres importing
  124. set_fact:
  125. dummy: true
  126. with_items:
  127. - >-
  128. Importing Postgres database using the following command: `{{ matrix_postgres_import_command }}`.
  129. If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
  130. delete the new database data (`rm -rf {{ matrix_postgres_data_path }}`)
  131. and restore the automatically-made backup (`mv {{ postgres_auto_upgrade_backup_data_path }} {{ matrix_postgres_data_path }}`).
  132. - name: Perform Postgres database import
  133. command: "{{ matrix_postgres_import_command }}"
  134. - name: Delete Postgres database dump file
  135. file:
  136. path: "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
  137. state: absent
  138. - name: Ensure matrix-synapse is started
  139. service:
  140. name: matrix-synapse
  141. state: started
  142. daemon_reload: yes
  143. - debug:
  144. 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."