2018-05-28 19:40:42 +02:00
---
- name : Set default postgres_dump_dir, if not provided
set_fact :
postgres_dump_dir : "/tmp"
when : "postgres_dump_dir|default('') == ''"
- name : Set postgres_dump_name, if not provided
set_fact :
postgres_dump_name : "matrix-postgres.out"
when : "postgres_dump_name|default('') == ''"
2018-11-01 10:11:08 +02:00
- name : Set postgres_auto_upgrade_backup_data_path, if not provided
set_fact :
2018-05-28 19:40:42 +02:00
postgres_auto_upgrade_backup_data_path : "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
2018-11-01 09:45:48 +02:00
when : "postgres_auto_upgrade_backup_data_path|default('') == ''"
2018-05-28 19:40:42 +02:00
2018-11-01 10:11:08 +02:00
- name : Set postgres_start_wait_time, if not provided
set_fact :
postgres_start_wait_time : 15
when : "postgres_start_wait_time|default('') == ''"
2018-05-28 19:40:42 +02:00
- name : Fail, if trying to upgrade external Postgres database
fail :
msg : "Your configuration indicates that you're using an external Postgres database. Refusing to try and upgrade that."
when : "matrix_postgres_use_external"
- name : Check Postgres auto-upgrade backup data directory
stat :
path : "{{ postgres_auto_upgrade_backup_data_path }}"
register : result_auto_upgrade_path
- name : Abort, if existing Postgres auto-upgrade data path detected
fail :
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"
- name : Determine existing Postgres version (check PG_VERSION file)
stat :
path : "{{ matrix_postgres_data_path }}/PG_VERSION"
register : result_pg_version_stat
- name : Abort, if no existing Postgres version detected
fail : msg="Could not find existing Postgres installation"
when : "not result_pg_version_stat.stat.exists"
- name : Determine existing Postgres version (read PG_VERSION file)
slurp :
src : "{{ matrix_postgres_data_path }}/PG_VERSION"
register : result_pg_version
- name : Determine existing Postgres version (default to empty)
set_fact :
pg_version : ""
- name : Determine existing Postgres version (make sense of PG_VERSION file)
set_fact :
pg_version : "{{ result_pg_version['content']|b64decode|replace('\n', '') }}"
- name : Determine Postgres version to use (default to latest)
set_fact :
2018-11-01 08:46:47 +02:00
matrix_postgres_docker_image_to_use : "{{ matrix_postgres_docker_image_latest }}"
2018-05-28 19:40:42 +02:00
- name : Determine Postgres version to use (use 9.x, if detected)
set_fact :
2018-11-01 08:46:47 +02:00
matrix_postgres_docker_image_to_use : "{{ matrix_postgres_docker_image_v9 }}"
2018-05-28 19:40:42 +02:00
when : "pg_version.startswith('9.')"
2018-11-01 09:48:20 +02:00
- name : Determine Postgres version to use (use 10.x, if detected)
set_fact :
matrix_postgres_docker_image_to_use : "{{ matrix_postgres_docker_image_v10 }}"
when : "pg_version == '10' or pg_version.startswith('10.')"
2018-05-28 19:40:42 +02:00
- name : Abort, if already at latest Postgres version
2018-11-01 09:48:20 +02:00
fail : msg="You are already running the latest Postgres version supported ({{ matrix_postgres_docker_image_latest }}). Nothing to do"
2018-11-01 08:46:47 +02:00
when : "matrix_postgres_docker_image_to_use == matrix_postgres_docker_image_latest"
2018-05-28 19:40:42 +02:00
2018-11-01 09:48:20 +02:00
- debug :
msg : "Upgrading database from {{ matrix_postgres_docker_image_to_use }} to {{ matrix_postgres_docker_image_latest }}"
2018-05-28 19:40:42 +02:00
- name : Ensure matrix-synapse is stopped
service : name=matrix-synapse state=stopped
- name : Ensure matrix-postgres is started
service : name=matrix-postgres state=started daemon_reload=yes
- name : Wait a bit, so that Postgres can start
wait_for :
2018-11-01 10:11:08 +02:00
timeout : "{{ postgres_start_wait_time }}"
2018-05-28 19:40:42 +02:00
delegate_to : 127.0 .0 .1
become : false
- name : Perform Postgres database dump
command : |
/usr/bin/docker run --rm --name matrix-postgres-dump \
2018-11-01 09:46:52 +02:00
--network={{ matrix_docker_network }} \
2018-05-29 10:23:17 +02:00
--env-file={{ matrix_environment_variables_data_path }}/env-postgres-pgsql-docker \
2018-05-28 19:40:42 +02:00
-v {{ postgres_dump_dir }}:/out \
2018-11-01 09:46:52 +02:00
{{ matrix_postgres_docker_image_to_use }} pg_dump -h matrix-postgres {{ matrix_postgres_db_name }} -f /out/{{ postgres_dump_name }}
2018-05-28 19:40:42 +02:00
- name : Ensure matrix-postgres is stopped
service : name=matrix-postgres state=stopped
- name : Rename existing Postgres data directory
command : "mv {{ matrix_postgres_data_path }} {{ postgres_auto_upgrade_backup_data_path }}"
- debug :
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."
2018-10-21 12:14:47 +02:00
- include : tasks/setup/setup_postgres.yml
2018-05-28 19:40:42 +02:00
- name : Ensure matrix-postgres autoruns and is restarted
service : name=matrix-postgres enabled=yes state=restarted daemon_reload=yes
- name : Wait a bit, so that Postgres can start
wait_for :
2018-11-01 10:11:08 +02:00
timeout : "{{ postgres_start_wait_time }}"
2018-05-28 19:40:42 +02:00
delegate_to : 127.0 .0 .1
become : false
- name : Perform Postgres database import
command : |
/usr/bin/docker run --rm --name matrix-postgres-import \
2018-11-01 09:46:52 +02:00
--network={{ matrix_docker_network }} \
2018-05-29 10:23:17 +02:00
--env-file={{ matrix_environment_variables_data_path }}/env-postgres-pgsql-docker \
2018-05-28 19:40:42 +02:00
-v {{ postgres_dump_dir }}:/in:ro \
2018-11-01 09:46:52 +02:00
{{ matrix_postgres_docker_image_latest }} psql -h matrix-postgres -f /in/{{ postgres_dump_name }}
2018-05-28 19:40:42 +02:00
- name : Delete Postgres database dump file
file :
path : "{{ postgres_dump_dir }}/{{ postgres_dump_name }}"
state : absent
- name : Ensure matrix-synapse is started
service : name=matrix-synapse state=started daemon_reload=yes
- debug :
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."