2017-08-01 04:07:30 +08:00
---
2017-09-08 21:53:24 +08:00
#
2019-07-08 13:05:31 +08:00
# Tasks related to setting up an internal postgres server
2017-09-08 21:53:24 +08:00
#
2017-08-01 04:07:30 +08:00
2019-01-12 23:53:00 +08:00
- import_tasks : "{{ role_path }}/tasks/migrate_postgres_data_directory.yml"
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2019-01-12 23:53:00 +08:00
- import_tasks : "{{ role_path }}/tasks/util/detect_existing_postgres_version.yml"
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2018-05-28 19:47:09 +08:00
2019-01-01 19:31:01 +08:00
# If we have found an existing version (installed from before), we use its corresponding Docker image.
# If not, we install using the latest Postgres.
#
# Upgrading is supposed to be performed separately and explicitly (see `upgrade_postgres.yml`).
- set_fact :
matrix_postgres_docker_image_to_use : "{{ matrix_postgres_docker_image_latest if matrix_postgres_detected_version_corresponding_docker_image == '' else matrix_postgres_detected_version_corresponding_docker_image }}"
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2018-11-01 15:48:20 +08:00
2020-09-01 18:12:11 +08:00
- name : Inject warning if on an old version of Postgres
set_fact :
matrix_playbook_runtime_results : |
{{
matrix_playbook_runtime_results|default([])
+
[
"NOTE: Your setup is on an old Postgres version ({{ matrix_postgres_docker_image_to_use }}), while {{ matrix_postgres_docker_image_latest }} is supported. You can upgrade using --tags=upgrade-postgres"
]
}}
2019-05-21 23:25:59 +08:00
when : "matrix_postgres_enabled|bool and matrix_postgres_docker_image_to_use != matrix_postgres_docker_image_latest"
2018-05-29 01:40:42 +08:00
2017-09-08 21:53:24 +08:00
# Even if we don't run the internal server, we still need this for running the CLI
2017-08-01 04:07:30 +08:00
- name : Ensure postgres Docker image is pulled
docker_image :
2018-11-01 14:46:47 +08:00
name : "{{ matrix_postgres_docker_image_to_use }}"
2019-05-22 18:43:33 +08:00
source : "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
2019-06-10 19:23:51 +08:00
force_source : "{{ matrix_postgres_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
force : "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_postgres_docker_image_force_pull }}"
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2017-08-01 04:07:30 +08:00
2019-01-12 23:53:00 +08:00
- name : Ensure Postgres paths exist
file :
path : "{{ item }}"
state : directory
mode : 0700
owner : "{{ matrix_user_username }}"
2020-05-02 01:59:32 +08:00
group : "{{ matrix_user_groupname }}"
2019-01-12 23:53:00 +08:00
with_items :
- "{{ matrix_postgres_base_path }}"
- "{{ matrix_postgres_data_path }}"
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2019-01-12 23:53:00 +08:00
2019-09-02 15:01:31 +08:00
# We do this as a separate task, because:
# - we'd like to do it for the data path only, not for the base path (which contains root-owned environment variable files we'd like to leave as-is)
# - we need to do it without `mode`, or we risk making certain `.conf` and other files's executable bit to flip to true
- name : Ensure Postgres data path ownership is correct
file :
path : "{{ matrix_postgres_data_path }}"
state : directory
owner : "{{ matrix_user_username }}"
2020-05-02 01:59:32 +08:00
group : "{{ matrix_user_groupname }}"
2019-09-02 15:01:31 +08:00
recurse : yes
when : matrix_postgres_enabled|bool
2017-08-01 04:07:30 +08:00
- name : Ensure Postgres environment variables file created
template :
2019-01-12 23:53:00 +08:00
src : "{{ role_path }}/templates/{{ item }}.j2"
dest : "{{ matrix_postgres_base_path }}/{{ item }}"
2017-08-01 04:07:30 +08:00
mode : 0640
with_items :
2019-01-12 23:53:00 +08:00
- "env-postgres-psql"
- "env-postgres-server"
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2017-08-01 04:07:30 +08:00
- name : Ensure matrix-postgres-cli script created
template :
src : "{{ role_path }}/templates/usr-local-bin/matrix-postgres-cli.j2"
2020-03-25 02:27:58 +08:00
dest : "{{ matrix_local_bin_path }}/matrix-postgres-cli"
2021-05-28 13:56:46 +08:00
mode : 0755
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2017-08-01 04:07:30 +08:00
2020-01-20 10:35:57 +08:00
- name : Ensure matrix-change-user-admin-status script created
2018-11-28 18:02:15 +08:00
template :
2020-01-20 10:35:57 +08:00
src : "{{ role_path }}/templates/usr-local-bin/matrix-change-user-admin-status.j2"
2020-03-25 02:27:58 +08:00
dest : "{{ matrix_local_bin_path }}/matrix-change-user-admin-status"
2021-05-28 13:56:46 +08:00
mode : 0755
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2018-11-28 18:02:15 +08:00
2020-01-22 10:29:17 +08:00
- name : (Migration) Ensure old matrix-make-user-admin script deleted
file :
2020-03-25 02:27:58 +08:00
path : "{{ matrix_local_bin_path }}/matrix-make-user-admin"
2020-01-22 10:29:17 +08:00
state : absent
when : matrix_postgres_enabled|bool
2019-07-08 13:05:31 +08:00
- name : Ensure matrix-postgres-update-user-password-hash script created
template :
src : "{{ role_path }}/templates/usr-local-bin/matrix-postgres-update-user-password-hash.j2"
2020-03-25 02:27:58 +08:00
dest : "{{ matrix_local_bin_path }}/matrix-postgres-update-user-password-hash"
2021-05-28 13:56:46 +08:00
mode : 0755
2019-07-08 13:05:31 +08:00
when : matrix_postgres_enabled|bool
2017-09-08 21:53:24 +08:00
2017-08-01 04:07:30 +08:00
- name : Ensure matrix-postgres.service installed
template :
src : "{{ role_path }}/templates/systemd/matrix-postgres.service.j2"
2020-03-25 02:27:58 +08:00
dest : "{{ matrix_systemd_path }}/matrix-postgres.service"
2017-09-08 21:53:24 +08:00
mode : 0644
2019-03-03 17:55:15 +08:00
register : matrix_postgres_systemd_service_result
2019-05-21 23:25:59 +08:00
when : matrix_postgres_enabled|bool
2017-09-08 21:53:24 +08:00
2019-03-03 17:55:15 +08:00
- name : Ensure systemd reloaded after matrix-postgres.service installation
service :
daemon_reload : yes
2019-05-21 23:25:59 +08:00
when : "matrix_postgres_enabled|bool and matrix_postgres_systemd_service_result.changed"
2019-03-03 17:55:15 +08:00
2020-12-14 03:40:32 +08:00
- include_tasks :
file : "{{ role_path }}/tasks/util/create_additional_databases.yml"
apply :
tags :
- always
2020-12-14 05:46:02 +08:00
when : "matrix_postgres_enabled|bool and matrix_postgres_additional_databases|length > 0"
2020-12-14 03:40:32 +08:00
2021-01-23 20:14:45 +08:00
- name : Check existence of matrix-postgres backup data path
stat :
path : "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
register : matrix_postgres_data_backup_path_stat
when : "matrix_postgres_enabled|bool"
- name : Inject warning if backup data remains
set_fact :
matrix_playbook_runtime_results : |
{{
matrix_playbook_runtime_results|default([])
+
[
"NOTE: You have some Postgres backup data in `{{ matrix_postgres_data_path }}-auto-upgrade-backup`, which was created during the last major Postgres update you ran. If your setup works well after this upgrade, feel free to delete this whole directory."
]
}}
when : "matrix_postgres_enabled|bool and matrix_postgres_data_backup_path_stat.stat.exists"
2017-09-08 21:53:24 +08:00
#
# Tasks related to getting rid of the internal postgres server (if it was previously enabled)
#
2017-09-12 04:50:14 +08:00
- name : Check existence of matrix-postgres service
2019-01-08 06:35:35 +08:00
stat :
2020-03-25 02:27:58 +08:00
path : "{{ matrix_systemd_path }}/matrix-postgres.service"
2017-09-08 21:53:24 +08:00
register : matrix_postgres_service_stat
2019-05-21 23:25:59 +08:00
when : "not matrix_postgres_enabled|bool"
2017-09-08 21:53:24 +08:00
- name : Ensure matrix-postgres is stopped
2019-01-08 06:35:35 +08:00
service :
name : matrix-postgres
state : stopped
daemon_reload : yes
2019-05-21 23:25:59 +08:00
when : "not matrix_postgres_enabled|bool and matrix_postgres_service_stat.stat.exists"
2017-09-08 21:53:24 +08:00
- name : Ensure matrix-postgres.service doesn't exist
file :
2020-03-25 02:27:58 +08:00
path : "{{ matrix_systemd_path }}/matrix-postgres.service"
2017-09-08 21:53:24 +08:00
state : absent
2019-05-21 23:25:59 +08:00
when : "not matrix_postgres_enabled|bool and matrix_postgres_service_stat.stat.exists"
2017-09-08 21:53:24 +08:00
2019-03-03 17:55:15 +08:00
- name : Ensure systemd reloaded after matrix-postgres.service removal
service :
daemon_reload : yes
2019-05-21 23:25:59 +08:00
when : "not matrix_postgres_enabled|bool and matrix_postgres_service_stat.stat.exists"
2019-03-03 17:55:15 +08:00
2017-09-12 04:50:14 +08:00
- name : Check existence of matrix-postgres local data path
2019-01-08 06:35:35 +08:00
stat :
path : "{{ matrix_postgres_data_path }}"
2017-09-08 21:53:24 +08:00
register : matrix_postgres_data_path_stat
2019-05-21 23:25:59 +08:00
when : "not matrix_postgres_enabled|bool"
2017-09-08 21:53:24 +08:00
# We just want to notify the user. Deleting data is too destructive.
2021-01-23 20:04:51 +08:00
- name : Inject warning if matrix-postgres local data remains
set_fact :
matrix_playbook_runtime_results : |
{{
matrix_playbook_runtime_results|default([])
+
[
"NOTE: You are not using a local PostgreSQL database, but some old data remains from before in `{{ matrix_postgres_data_path }}`. Feel free to delete it."
]
}}
2019-05-21 23:25:59 +08:00
when : "not matrix_postgres_enabled|bool and matrix_postgres_data_path_stat.stat.exists"
2019-04-30 21:30:26 +08:00
2019-07-08 13:05:31 +08:00
- name : Remove Postgres scripts
file :
2020-03-25 02:27:58 +08:00
path : "{{ matrix_local_bin_path }}/{{ item }}"
2019-07-08 13:05:31 +08:00
state : absent
with_items :
- matrix-postgres-cli
2020-01-20 10:35:57 +08:00
- matrix-change-user-admin-status
2019-07-08 13:05:31 +08:00
- matrix-postgres-update-user-password-hash
when : "not matrix_postgres_enabled|bool"