2019-01-01 14:40:48 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# Pre-checks
|
|
|
|
|
2019-01-16 18:05:48 +02:00
|
|
|
- name: Fail if Postgres not enabled
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.fail:
|
2019-01-16 18:05:48 +02:00
|
|
|
msg: "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot import."
|
2022-07-18 10:22:05 +02:00
|
|
|
when: "not matrix_postgres_enabled | bool"
|
2019-01-16 18:05:48 +02:00
|
|
|
|
2019-01-01 14:40:48 +02:00
|
|
|
- name: Fail if playbook called incorrectly
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.fail:
|
2019-01-08 00:35:35 +02:00
|
|
|
msg: "The `server_path_postgres_dump` variable needs to be provided to this playbook, via --extra-vars"
|
2019-01-01 14:40:48 +02:00
|
|
|
when: "server_path_postgres_dump is not defined or server_path_postgres_dump.startswith('<')"
|
|
|
|
|
|
|
|
- name: Check if the provided Postgres dump file exists
|
2022-07-18 10:22:05 +02:00
|
|
|
ansible.builtin.stat:
|
2019-01-08 00:35:35 +02:00
|
|
|
path: "{{ server_path_postgres_dump }}"
|
2019-01-01 14:40:48 +02:00
|
|
|
register: result_server_path_postgres_dump_stat
|
|
|
|
|
|
|
|
- name: Fail if provided Postgres dump file doesn't exists
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.fail:
|
2019-01-08 00:35:35 +02:00
|
|
|
msg: "File cannot be found on the server at {{ server_path_postgres_dump }}"
|
2019-05-21 17:25:59 +02:00
|
|
|
when: "not result_server_path_postgres_dump_stat.stat.exists"
|
2019-01-01 14:40:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Defaults
|
|
|
|
|
|
|
|
- name: Set postgres_start_wait_time, if not provided
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.set_fact:
|
2019-01-01 14:40:48 +02:00
|
|
|
postgres_start_wait_time: 15
|
2022-07-18 10:22:05 +02:00
|
|
|
when: "postgres_start_wait_time | default('') == ''"
|
2019-01-01 14:40:48 +02:00
|
|
|
|
2019-01-21 08:34:49 +02:00
|
|
|
- name: Set postgres_import_wait_time, if not provided
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.set_fact:
|
2019-01-21 08:34:49 +02:00
|
|
|
postgres_import_wait_time: "{{ 7 * 86400 }}"
|
2022-07-18 10:22:05 +02:00
|
|
|
when: "postgres_import_wait_time | default('') == ''"
|
2019-01-21 08:34:49 +02:00
|
|
|
|
2021-01-22 17:39:08 +02:00
|
|
|
# By default, we connect and import into the main (`matrix`) database.
|
|
|
|
# Single-database dumps for Synapse may wish to import into `synapse` instead.
|
|
|
|
- name: Set postgres_default_import_database, if not provided
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.set_fact:
|
2021-01-22 17:39:08 +02:00
|
|
|
postgres_default_import_database: "{{ matrix_postgres_db_name }}"
|
2022-07-18 10:22:05 +02:00
|
|
|
when: "postgres_default_import_database | default('') == ''"
|
2021-01-22 17:39:08 +02:00
|
|
|
|
2019-01-01 14:40:48 +02:00
|
|
|
# Actual import work
|
|
|
|
|
|
|
|
- name: Ensure matrix-postgres is started
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.service:
|
2019-01-08 00:35:35 +02:00
|
|
|
name: matrix-postgres
|
|
|
|
state: started
|
2022-02-05 22:32:54 +02:00
|
|
|
daemon_reload: true
|
2019-01-01 14:40:48 +02:00
|
|
|
|
|
|
|
- name: Wait a bit, so that Postgres can start
|
2022-07-18 10:22:05 +02:00
|
|
|
ansible.builtin.wait_for:
|
2019-01-01 14:40:48 +02:00
|
|
|
timeout: "{{ postgres_start_wait_time }}"
|
|
|
|
delegate_to: 127.0.0.1
|
|
|
|
become: false
|
|
|
|
|
2022-07-18 15:15:04 +02:00
|
|
|
- ansible.builtin.import_tasks: tasks/detect_existing_postgres_version.yml
|
2019-01-21 07:32:19 +02:00
|
|
|
|
|
|
|
- name: Abort, if no existing Postgres version detected
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.fail:
|
2019-01-21 07:32:19 +02:00
|
|
|
msg: "Could not find existing Postgres installation"
|
2022-07-18 10:22:05 +02:00
|
|
|
when: "not matrix_postgres_detected_existing | bool"
|
2019-01-21 07:32:19 +02:00
|
|
|
|
2019-10-05 10:28:26 +02: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.
|
2019-01-21 08:33:10 +02:00
|
|
|
- name: Generate Postgres database import command
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.set_fact:
|
2019-01-21 08:33:10 +02:00
|
|
|
matrix_postgres_import_command: >-
|
2020-05-27 22:18:24 +02:00
|
|
|
{{ matrix_host_command_docker }} run --rm --name matrix-postgres-import
|
2020-09-01 08:03:06 +02:00
|
|
|
--log-driver=none
|
2019-01-27 20:25:13 +02:00
|
|
|
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
|
2019-01-28 11:43:32 +02:00
|
|
|
--cap-drop=ALL
|
2019-01-21 08:33:10 +02:00
|
|
|
--network={{ matrix_docker_network }}
|
|
|
|
--env-file={{ matrix_postgres_base_path }}/env-postgres-psql
|
2022-07-18 10:22:05 +02:00
|
|
|
--mount type=bind,src={{ server_path_postgres_dump }},dst=/{{ server_path_postgres_dump | basename }},ro
|
2019-01-21 08:33:10 +02:00
|
|
|
--entrypoint=/bin/sh
|
|
|
|
{{ matrix_postgres_docker_image_latest }}
|
2022-07-18 10:22:05 +02:00
|
|
|
-c "cat /{{ server_path_postgres_dump | basename }} |
|
2019-01-21 08:33:10 +02:00
|
|
|
{{ 'gunzip |' if server_path_postgres_dump.endswith('.gz') else '' }}
|
2020-12-14 22:28:20 +02:00
|
|
|
grep -vE '{{ matrix_postgres_import_roles_ignore_regex }}' |
|
|
|
|
grep -vE '{{ matrix_postgres_import_databases_ignore_regex }}' |
|
2021-01-22 17:39:08 +02:00
|
|
|
psql -v ON_ERROR_STOP=1 -h matrix-postgres --dbname={{ postgres_default_import_database }}"
|
2022-07-18 14:33:41 +02:00
|
|
|
tags:
|
|
|
|
- skip_ansible_lint
|
2019-10-05 10:28:26 +02: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`)
|
2019-01-21 08:33:10 +02:00
|
|
|
- name: Note about Postgres importing alternative
|
2022-07-18 09:39:08 +02:00
|
|
|
ansible.builtin.set_fact:
|
2019-10-05 10:28:26 +02: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 its existing data (`rm -rf {{ matrix_postgres_data_path }}/*`), start it again (`systemctl start matrix-postgres`)
|
|
|
|
and manually run the above import command directly on the server.
|
2019-01-21 08:33:10 +02:00
|
|
|
|
2019-01-01 14:40:48 +02:00
|
|
|
- name: Perform Postgres database import
|
2022-07-18 14:08:10 +02:00
|
|
|
ansible.builtin.command:
|
|
|
|
cmd: "{{ matrix_postgres_import_command }}"
|
2019-01-21 08:34:49 +02:00
|
|
|
async: "{{ postgres_import_wait_time }}"
|
|
|
|
poll: 10
|
2022-07-18 14:08:10 +02:00
|
|
|
register: matrix_postgres_import_postgres_command_result
|
|
|
|
changed_when: matrix_postgres_import_postgres_command_result.rc == 0
|