2017-07-31 20:07:30 +00:00
---
2017-09-08 13:53:24 +00:00
#
2019-07-08 05:05:31 +00:00
# Tasks related to setting up an internal postgres server
2017-09-08 13:53:24 +00:00
#
2017-07-31 20:07:30 +00:00
2022-07-18 08:22:05 +00:00
- ansible.builtin.import_tasks : "{{ role_path }}/tasks/migrate_postgres_data_directory.yml"
when : matrix_postgres_enabled | bool
2019-01-12 15:53:00 +00:00
2022-07-18 13:15:04 +00:00
- ansible.builtin.import_tasks : "{{ role_path }}/tasks/detect_existing_postgres_version.yml"
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2018-05-28 11:47:09 +00:00
2019-01-01 11:31:01 +00: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`).
2022-07-18 07:39:08 +00:00
- ansible.builtin.set_fact :
2019-01-01 11:31:01 +00:00
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 }}"
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2018-11-01 07:48:20 +00:00
2021-11-11 13:42:34 +00:00
- name : Abort if on an unsupported Postgres version
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2021-11-11 13:42:34 +00:00
msg : "You're on Postgres {{ matrix_postgres_detected_version }}, which is no longer supported. To upgrade, see docs/maintenance-postgres.md"
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_enabled | bool and matrix_postgres_detected_version.startswith('9.')"
2021-11-11 13:42:34 +00:00
2020-09-01 10:12:11 +00:00
- name : Inject warning if on an old version of Postgres
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list : |
2020-09-01 10:12:11 +00:00
{{
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list | default([])
2020-09-01 10:12:11 +00:00
+
[
"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"
]
}}
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_enabled | bool and matrix_postgres_docker_image_to_use != matrix_postgres_docker_image_latest"
2018-05-28 17:40:42 +00:00
2017-09-08 13:53:24 +00:00
# Even if we don't run the internal server, we still need this for running the CLI
2017-07-31 20:07:30 +00:00
- name : Ensure postgres Docker image is pulled
2022-10-28 11:20:17 +00:00
community.docker.docker_image :
2018-11-01 06:46:47 +00:00
name : "{{ matrix_postgres_docker_image_to_use }}"
2019-05-22 10:43:33 +00:00
source : "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
2019-06-10 11:23:51 +00: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 }}"
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2022-03-17 17:04:38 +00:00
register : result
retries : "{{ matrix_container_retries_count }}"
delay : "{{ matrix_container_retries_delay }}"
until : result is not failed
2017-07-31 20:07:30 +00:00
2019-01-12 15:53:00 +00:00
- name : Ensure Postgres paths exist
2022-07-18 07:39:08 +00:00
ansible.builtin.file :
2019-01-12 15:53:00 +00:00
path : "{{ item }}"
state : directory
mode : 0700
owner : "{{ matrix_user_username }}"
2020-05-01 17:59:32 +00:00
group : "{{ matrix_user_groupname }}"
2019-01-12 15:53:00 +00:00
with_items :
- "{{ matrix_postgres_base_path }}"
- "{{ matrix_postgres_data_path }}"
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2019-01-12 15:53:00 +00:00
2019-09-02 07:01:31 +00: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
2022-07-18 07:39:08 +00:00
ansible.builtin.file :
2019-09-02 07:01:31 +00:00
path : "{{ matrix_postgres_data_path }}"
state : directory
owner : "{{ matrix_user_username }}"
2020-05-01 17:59:32 +00:00
group : "{{ matrix_user_groupname }}"
2022-02-05 20:32:54 +00:00
recurse : true
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2019-09-02 07:01:31 +00:00
2017-07-31 20:07:30 +00:00
- name : Ensure Postgres environment variables file created
2022-07-18 07:39:08 +00:00
ansible.builtin.template :
2019-01-12 15:53:00 +00:00
src : "{{ role_path }}/templates/{{ item }}.j2"
dest : "{{ matrix_postgres_base_path }}/{{ item }}"
2022-04-02 21:49:57 +00:00
owner : "{{ matrix_user_username }}"
group : "{{ matrix_user_groupname }}"
2017-07-31 20:07:30 +00:00
mode : 0640
with_items :
2019-01-12 15:53:00 +00:00
- "env-postgres-psql"
- "env-postgres-server"
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2017-07-31 20:07:30 +00:00
- name : Ensure matrix-postgres-cli script created
2022-07-18 07:39:08 +00:00
ansible.builtin.template :
2017-07-31 20:07:30 +00:00
src : "{{ role_path }}/templates/usr-local-bin/matrix-postgres-cli.j2"
2020-03-24 18:27:58 +00:00
dest : "{{ matrix_local_bin_path }}/matrix-postgres-cli"
2021-05-28 05:56:46 +00:00
mode : 0755
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2017-07-31 20:07:30 +00:00
2022-02-27 09:40:20 +00:00
- name : Ensure matrix-postgres-cli-non-interactive script created
2022-07-18 07:39:08 +00:00
ansible.builtin.template :
2022-02-27 09:40:20 +00:00
src : "{{ role_path }}/templates/usr-local-bin/matrix-postgres-cli-non-interactive.j2"
dest : "{{ matrix_local_bin_path }}/matrix-postgres-cli-non-interactive"
mode : 0755
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2022-02-27 09:40:20 +00:00
2020-01-20 02:35:57 +00:00
- name : Ensure matrix-change-user-admin-status script created
2022-07-18 07:39:08 +00:00
ansible.builtin.template :
2020-01-20 02:35:57 +00:00
src : "{{ role_path }}/templates/usr-local-bin/matrix-change-user-admin-status.j2"
2020-03-24 18:27:58 +00:00
dest : "{{ matrix_local_bin_path }}/matrix-change-user-admin-status"
2021-05-28 05:56:46 +00:00
mode : 0755
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2018-11-28 10:02:15 +00:00
2020-01-22 02:29:17 +00:00
- name : (Migration) Ensure old matrix-make-user-admin script deleted
2022-07-18 07:39:08 +00:00
ansible.builtin.file :
2020-03-24 18:27:58 +00:00
path : "{{ matrix_local_bin_path }}/matrix-make-user-admin"
2020-01-22 02:29:17 +00:00
state : absent
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2020-01-22 02:29:17 +00:00
2019-07-08 05:05:31 +00:00
- name : Ensure matrix-postgres-update-user-password-hash script created
2022-07-18 07:39:08 +00:00
ansible.builtin.template :
2019-07-08 05:05:31 +00:00
src : "{{ role_path }}/templates/usr-local-bin/matrix-postgres-update-user-password-hash.j2"
2020-03-24 18:27:58 +00:00
dest : "{{ matrix_local_bin_path }}/matrix-postgres-update-user-password-hash"
2021-05-28 05:56:46 +00:00
mode : 0755
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2017-09-08 13:53:24 +00:00
2017-07-31 20:07:30 +00:00
- name : Ensure matrix-postgres.service installed
2022-07-18 07:39:08 +00:00
ansible.builtin.template :
2017-07-31 20:07:30 +00:00
src : "{{ role_path }}/templates/systemd/matrix-postgres.service.j2"
2022-11-04 14:38:38 +00:00
dest : "{{ devture_systemd_docker_base_systemd_path }}/matrix-postgres.service"
2017-09-08 13:53:24 +00:00
mode : 0644
2019-03-03 09:55:15 +00:00
register : matrix_postgres_systemd_service_result
2022-07-18 08:22:05 +00:00
when : matrix_postgres_enabled | bool
2017-09-08 13:53:24 +00:00
2019-03-03 09:55:15 +00:00
- name : Ensure systemd reloaded after matrix-postgres.service installation
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2022-02-05 20:32:54 +00:00
daemon_reload : true
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_enabled | bool and matrix_postgres_systemd_service_result.changed"
2019-03-03 09:55:15 +00:00
2022-07-18 08:22:05 +00:00
- ansible.builtin.include_tasks :
2022-07-18 10:03:13 +00:00
file : "{{ role_path }}/tasks/util/create_additional_databases.yml"
2020-12-13 19:40:32 +00:00
apply :
tags :
- always
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_enabled | bool and matrix_postgres_additional_databases|length > 0"
2020-12-13 19:40:32 +00:00
2021-01-23 12:14:45 +00:00
- name : Check existence of matrix-postgres backup data path
2022-07-18 08:22:05 +00:00
ansible.builtin.stat :
2021-01-23 12:14:45 +00:00
path : "{{ matrix_postgres_data_path }}-auto-upgrade-backup"
register : matrix_postgres_data_backup_path_stat
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_enabled | bool"
2021-01-23 12:14:45 +00:00
- name : Inject warning if backup data remains
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list : |
2021-01-23 12:14:45 +00:00
{{
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list | default([])
2021-01-23 12:14:45 +00:00
+
[
"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."
]
}}
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_enabled | bool and matrix_postgres_data_backup_path_stat.stat.exists"
2021-01-23 12:14:45 +00:00
2017-09-08 13:53:24 +00:00
#
# Tasks related to getting rid of the internal postgres server (if it was previously enabled)
#
2017-09-11 20:50:14 +00:00
- name : Check existence of matrix-postgres service
2022-07-18 08:22:05 +00:00
ansible.builtin.stat :
2022-11-04 14:38:38 +00:00
path : "{{ devture_systemd_docker_base_systemd_path }}/matrix-postgres.service"
2017-09-08 13:53:24 +00:00
register : matrix_postgres_service_stat
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool"
2017-09-08 13:53:24 +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
2022-02-05 20:32:54 +00:00
daemon_reload : true
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool and matrix_postgres_service_stat.stat.exists"
2017-09-08 13:53:24 +00:00
- name : Ensure matrix-postgres.service doesn't exist
2022-07-18 07:39:08 +00:00
ansible.builtin.file :
2022-11-04 14:38:38 +00:00
path : "{{ devture_systemd_docker_base_systemd_path }}/matrix-postgres.service"
2017-09-08 13:53:24 +00:00
state : absent
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool and matrix_postgres_service_stat.stat.exists"
2017-09-08 13:53:24 +00:00
2019-03-03 09:55:15 +00:00
- name : Ensure systemd reloaded after matrix-postgres.service removal
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2022-02-05 20:32:54 +00:00
daemon_reload : true
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool and matrix_postgres_service_stat.stat.exists"
2019-03-03 09:55:15 +00:00
2017-09-11 20:50:14 +00:00
- name : Check existence of matrix-postgres local data path
2022-07-18 08:22:05 +00:00
ansible.builtin.stat :
2019-01-07 22:35:35 +00:00
path : "{{ matrix_postgres_data_path }}"
2017-09-08 13:53:24 +00:00
register : matrix_postgres_data_path_stat
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool"
2017-09-08 13:53:24 +00:00
# We just want to notify the user. Deleting data is too destructive.
2021-01-23 12:04:51 +00:00
- name : Inject warning if matrix-postgres local data remains
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list : |
2021-01-23 12:04:51 +00:00
{{
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list | default([])
2021-01-23 12:04:51 +00:00
+
[
"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."
]
}}
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool and matrix_postgres_data_path_stat.stat.exists"
2019-04-30 13:30:26 +00:00
2019-07-08 05:05:31 +00:00
- name : Remove Postgres scripts
2022-07-18 07:39:08 +00:00
ansible.builtin.file :
2020-03-24 18:27:58 +00:00
path : "{{ matrix_local_bin_path }}/{{ item }}"
2019-07-08 05:05:31 +00:00
state : absent
with_items :
- matrix-postgres-cli
2020-01-20 02:35:57 +00:00
- matrix-change-user-admin-status
2019-07-08 05:05:31 +00:00
- matrix-postgres-update-user-password-hash
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool"