2018-05-28 17:40:42 +00:00
---
- name : Set default postgres_dump_dir, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2018-05-28 17:40:42 +00:00
postgres_dump_dir : "/tmp"
2022-07-18 08:22:05 +00:00
when : "postgres_dump_dir | default('') == ''"
2018-05-28 17:40:42 +00:00
- name : Set postgres_dump_name, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2019-10-05 08:41:59 +00:00
postgres_dump_name : "matrix-postgres-dump.sql.gz"
2022-07-18 08:22:05 +00:00
when : "postgres_dump_name | default('') == ''"
2018-05-28 17:40:42 +00:00
2018-11-01 08:11:08 +00:00
- name : Set postgres_auto_upgrade_backup_data_path, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2018-05-28 17:40:42 +00:00
postgres_auto_upgrade_backup_data_path : "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
2022-07-18 08:22:05 +00:00
when : "postgres_auto_upgrade_backup_data_path | default('') == ''"
2018-05-28 17:40:42 +00:00
2018-11-01 08:11:08 +00:00
- name : Set postgres_start_wait_time, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2018-11-01 08:11:08 +00:00
postgres_start_wait_time : 15
2022-07-18 08:22:05 +00:00
when : "postgres_start_wait_time | default('') == ''"
2018-11-01 08:11:08 +00:00
2020-01-20 22:12:49 +00:00
- name : Set postgres_force_upgrade, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2020-01-20 22:12:49 +00:00
postgres_force_upgrade : false
2022-07-18 08:22:05 +00:00
when : "postgres_force_upgrade | default('') == ''"
2020-01-20 22:12:49 +00:00
2018-05-28 17:40:42 +00:00
- name : Fail, if trying to upgrade external Postgres database
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2019-01-16 16:05:48 +00:00
msg : "Your configuration indicates that you're not using Postgres from this role. There is nothing to upgrade."
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool"
2018-05-28 17:40:42 +00:00
- name : Check Postgres auto-upgrade backup data directory
2022-07-18 08:22:05 +00:00
ansible.builtin.stat :
2018-05-28 17:40:42 +00:00
path : "{{ postgres_auto_upgrade_backup_data_path }}"
register : result_auto_upgrade_path
- name : Abort, if existing Postgres auto-upgrade data path detected
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2018-05-28 17:40:42 +00:00
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."
when : "result_auto_upgrade_path.stat.exists"
2022-07-18 08:22:05 +00:00
- ansible.builtin.import_tasks : tasks/util/detect_existing_postgres_version.yml
2018-05-28 17:40:42 +00:00
- name : Abort, if no existing Postgres version detected
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2019-01-07 22:35:35 +00:00
msg : "Could not find existing Postgres installation"
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_detected_existing | bool"
2018-11-01 07:48:20 +00:00
2018-05-28 17:40:42 +00:00
- name : Abort, if already at latest Postgres version
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2019-01-07 22:35:35 +00:00
msg : "You are already running the latest Postgres version supported ({{ matrix_postgres_docker_image_latest }}). Nothing to do"
2020-01-20 22:12:49 +00:00
when : "matrix_postgres_detected_version_corresponding_docker_image == matrix_postgres_docker_image_latest and not postgres_force_upgrade"
2018-05-28 17:40:42 +00:00
2022-07-18 07:39:08 +00:00
- ansible.builtin.debug :
2019-01-01 11:31:01 +00:00
msg : "Upgrading database from {{ matrix_postgres_detected_version_corresponding_docker_image }} to {{ matrix_postgres_docker_image_latest }}"
2018-11-01 07:48:20 +00:00
2018-05-28 17:40:42 +00:00
- name : Ensure matrix-synapse is stopped
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-01-07 22:35:35 +00:00
name : matrix-synapse
state : stopped
2018-05-28 17:40:42 +00:00
- name : Ensure matrix-postgres is started
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-01-07 22:35:35 +00:00
name : matrix-postgres
state : started
2022-02-05 20:32:54 +00:00
daemon_reload : true
2018-05-28 17:40:42 +00:00
- name : Wait a bit, so that Postgres can start
2022-07-18 08:22:05 +00:00
ansible.builtin.wait_for :
2018-11-01 08:11:08 +00:00
timeout : "{{ postgres_start_wait_time }}"
2018-05-28 17:40:42 +00:00
delegate_to : 127.0 .0 .1
become : false
2019-10-05 08:28:26 +00:00
# We dump all databases, roles, etc.
#
# Because we'll be importing into a new container which initializes the default
# role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`) by itself on startup,
# we need to remove these from the dump, or we'll get errors saying these already exist.
2018-05-28 17:40:42 +00:00
- name : Perform Postgres database dump
2022-07-18 12:08:10 +00:00
ansible.builtin.command :
cmd : >-
{{ matrix_host_command_docker }} run --rm --name matrix-postgres-dump
--log-driver=none
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
--network={{ matrix_docker_network }}
--env-file={{ matrix_postgres_base_path }}/env-postgres-psql
--entrypoint=/bin/sh
--mount type=bind,src={{ postgres_dump_dir }},dst=/out
{{ matrix_postgres_detected_version_corresponding_docker_image }}
-c "pg_dumpall -h matrix-postgres
{{ '| gzip -c ' if postgres_dump_name.endswith('.gz') else '' }}
> /out/{{ postgres_dump_name }}"
register : matrix_postgres_upgrade_postgres_dump_command_result
changed_when : matrix_postgres_upgrade_postgres_dump_command_result.rc == 0
2022-07-18 12:33:41 +00:00
tags :
- skip_ansible_lint
2018-05-28 17:40:42 +00:00
- name : Ensure matrix-postgres is stopped
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-01-07 22:35:35 +00:00
name : matrix-postgres
state : stopped
2018-05-28 17:40:42 +00:00
- name : Rename existing Postgres data directory
2022-07-18 12:08:10 +00:00
ansible.builtin.command :
cmd : "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
register : matrix_postgres_upgrade_postgres_move_command_result
changed_when : matrix_postgres_upgrade_postgres_move_command_result.rc == 0
2018-05-28 17:40:42 +00:00
2022-07-18 07:39:08 +00:00
- ansible.builtin.debug :
2018-05-28 17:40:42 +00:00
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."
2022-07-18 08:22:05 +00:00
- ansible.builtin.import_tasks : tasks/setup_postgres.yml
2018-05-28 17:40:42 +00:00
- name : Ensure matrix-postgres autoruns and is restarted
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-01-07 22:35:35 +00:00
name : matrix-postgres
2022-02-05 20:32:54 +00:00
enabled : true
2019-01-07 22:35:35 +00:00
state : restarted
2022-02-05 20:32:54 +00:00
daemon_reload : true
2018-05-28 17:40:42 +00:00
- name : Wait a bit, so that Postgres can start
2022-07-18 08:22:05 +00:00
ansible.builtin.wait_for :
2018-11-01 08:11:08 +00:00
timeout : "{{ postgres_start_wait_time }}"
2018-05-28 17:40:42 +00:00
delegate_to : 127.0 .0 .1
become : false
2019-10-05 08:28:26 +00:00
# Starting the database container had automatically created the default
# role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`).
# The dump most likely contains those same entries and would try to re-create them, leading to errors.
# We need to skip over those lines.
- name : Generate Postgres database import command
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2019-10-05 08:28:26 +00:00
matrix_postgres_import_command : >-
2020-05-27 20:18:24 +00:00
{{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
2020-09-01 06:03:06 +00:00
--log-driver=none
2019-10-05 08:28:26 +00:00
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
--cap-drop=ALL
--network={{ matrix_docker_network }}
--env-file={{ matrix_postgres_base_path }}/env-postgres-psql
--entrypoint=/bin/sh
2020-11-25 08:49:59 +00:00
--mount type=bind,src={{ postgres_dump_dir }},dst=/in,ro
2019-10-05 08:28:26 +00:00
{{ matrix_postgres_docker_image_latest }}
-c "cat /in/{{ postgres_dump_name }} |
2019-10-05 08:41:59 +00:00
{{ 'gunzip |' if postgres_dump_name.endswith('.gz') else '' }}
2020-12-14 20:28:20 +00:00
grep -vE '{{ matrix_postgres_import_roles_ignore_regex }}' |
grep -vE '{{ matrix_postgres_import_databases_ignore_regex }}' |
2019-10-05 08:28:26 +00:00
psql -v ON_ERROR_STOP=1 -h matrix-postgres"
2022-07-18 12:33:41 +00:00
tags :
- skip_ansible_lint
2019-10-05 08:28:26 +00:00
# This is a hack.
# See: https://ansibledaily.com/print-to-standard-output-without-escaping/
#
# We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
# which ruins the command (`matrix_postgres_import_command`)
- name : Note about Postgres importing
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2019-10-05 08:28:26 +00:00
dummy : true
with_items :
- >-
Importing Postgres database using the following command : `{{ matrix_postgres_import_command }}`.
If this crashes, you can stop Postgres (`systemctl stop matrix-postgres`),
delete the new database data (`rm -rf {{ matrix_postgres_data_path }}`)
and restore the automatically-made backup (`mv {{ postgres_auto_upgrade_backup_data_path }} {{ matrix_postgres_data_path }}`).
2018-05-28 17:40:42 +00:00
- name : Perform Postgres database import
2022-07-18 12:08:10 +00:00
ansible.builtin.command :
cmd : "{{ matrix_postgres_import_command }}"
register : matrix_postgres_upgrade_postgres_import_command_result
changed_when : matrix_postgres_upgrade_postgres_import_command_result.rc == 0
2018-05-28 17:40:42 +00:00
- name : Delete Postgres database dump file
2022-07-18 07:39:08 +00:00
ansible.builtin.file :
2018-05-28 17:40:42 +00:00
path : "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
state : absent
- name : Ensure matrix-synapse is started
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2019-01-07 22:35:35 +00:00
name : matrix-synapse
state : started
2022-02-05 20:32:54 +00:00
daemon_reload : true
2018-05-28 17:40:42 +00:00
2022-07-18 07:39:08 +00:00
- ansible.builtin.debug :
2018-05-28 17:40:42 +00:00
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."