From a7f791f8f9a2af8942cda4da9e0ba9e91f18982b Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Tue, 1 Jan 2019 13:31:01 +0200 Subject: [PATCH] Make Postgres version detection logic reusable to ease maintenance We've had that logic in 2 places so far, leading to duplication and a maintenance burden. In the future, we'll also have an import-postgres feature, which will also need Postgres version detection, leading to more benefit from that logic being reusable. --- .../tasks/setup/setup_postgres.yml | 46 +++++-------------- .../matrix-server/tasks/upgrade_postgres.yml | 40 ++-------------- .../util/detect_existing_postgres_version.yml | 46 +++++++++++++++++++ 3 files changed, 62 insertions(+), 70 deletions(-) create mode 100644 roles/matrix-server/tasks/util/detect_existing_postgres_version.yml diff --git a/roles/matrix-server/tasks/setup/setup_postgres.yml b/roles/matrix-server/tasks/setup/setup_postgres.yml index fdf253be..53491b55 100644 --- a/roles/matrix-server/tasks/setup/setup_postgres.yml +++ b/roles/matrix-server/tasks/setup/setup_postgres.yml @@ -4,41 +4,17 @@ # Generic tasks, no matter what kind of server we're using (internal/external) # -- name: Determine existing Postgres version (check PG_VERSION file) - stat: - path: "{{ matrix_postgres_data_path }}/PG_VERSION" - register: result_pg_version_stat - -- name: Determine existing Postgres version (read PG_VERSION file) - slurp: - src: "{{ matrix_postgres_data_path }}/PG_VERSION" - register: result_pg_version - when: "result_pg_version_stat.stat.exists" - -- 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', '') }}" - when: "result_pg_version_stat.stat.exists" - -- name: Determine Postgres version to use (default to latest) - set_fact: - matrix_postgres_docker_image_to_use: "{{ matrix_postgres_docker_image_latest }}" - -- name: Determine Postgres version to use (use 9.x, if detected) - set_fact: - matrix_postgres_docker_image_to_use: "{{ matrix_postgres_docker_image_v9 }}" - when: "pg_version.startswith('9.')" - -- 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.')" - -- debug: +- include: tasks/util/detect_existing_postgres_version.yml + +# 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 }}" + +- name: Warn if on an old version of Postgres + debug: msg: "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" when: "matrix_postgres_docker_image_to_use != matrix_postgres_docker_image_latest" diff --git a/roles/matrix-server/tasks/upgrade_postgres.yml b/roles/matrix-server/tasks/upgrade_postgres.yml index 05cf8d54..e6d3415c 100644 --- a/roles/matrix-server/tasks/upgrade_postgres.yml +++ b/roles/matrix-server/tasks/upgrade_postgres.yml @@ -35,48 +35,18 @@ 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 +- include: tasks/util/detect_existing_postgres_version.yml - 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: - matrix_postgres_docker_image_to_use: "{{ matrix_postgres_docker_image_latest }}" - -- name: Determine Postgres version to use (use 9.x, if detected) - set_fact: - matrix_postgres_docker_image_to_use: "{{ matrix_postgres_docker_image_v9 }}" - when: "pg_version.startswith('9.')" - -- 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.')" + when: "not matrix_postgres_detected_existing" - name: Abort, if already at latest Postgres version fail: msg="You are already running the latest Postgres version supported ({{ matrix_postgres_docker_image_latest }}). Nothing to do" - when: "matrix_postgres_docker_image_to_use == matrix_postgres_docker_image_latest" + when: "matrix_postgres_detected_version_corresponding_docker_image == matrix_postgres_docker_image_latest" - debug: - msg: "Upgrading database from {{ matrix_postgres_docker_image_to_use }} to {{ matrix_postgres_docker_image_latest }}" + msg: "Upgrading database from {{ matrix_postgres_detected_version_corresponding_docker_image }} to {{ matrix_postgres_docker_image_latest }}" - name: Ensure matrix-synapse is stopped service: name=matrix-synapse state=stopped @@ -96,7 +66,7 @@ --network={{ matrix_docker_network }} \ --env-file={{ matrix_environment_variables_data_path }}/env-postgres-pgsql-docker \ -v {{ postgres_dump_dir }}:/out \ - {{ matrix_postgres_docker_image_to_use }} pg_dump -h matrix-postgres {{ matrix_postgres_db_name }} -f /out/{{ postgres_dump_name }} + {{ matrix_postgres_detected_version_corresponding_docker_image }} pg_dump -h matrix-postgres {{ matrix_postgres_db_name }} -f /out/{{ postgres_dump_name }} - name: Ensure matrix-postgres is stopped service: name=matrix-postgres state=stopped diff --git a/roles/matrix-server/tasks/util/detect_existing_postgres_version.yml b/roles/matrix-server/tasks/util/detect_existing_postgres_version.yml new file mode 100644 index 00000000..a3fbe33e --- /dev/null +++ b/roles/matrix-server/tasks/util/detect_existing_postgres_version.yml @@ -0,0 +1,46 @@ +--- + +# This utility aims to determine if there is some existing Postgres version in use or not. +# If there is, it also tries to detect the Docker image that corresponds to that version. + +- name: Initialize Postgres version determination variables (default to empty) + set_fact: + matrix_postgres_detection_pg_version_path: "{{ matrix_postgres_data_path }}/PG_VERSION" + matrix_postgres_detected_existing: false + matrix_postgres_detected_version: "" + matrix_postgres_detected_version_corresponding_docker_image: "" + +- name: Determine existing Postgres version (check PG_VERSION file) + stat: + path: "{{ matrix_postgres_detection_pg_version_path }}" + register: result_pg_version_stat + +- set_fact: + matrix_postgres_detected_existing: true + when: "result_pg_version_stat.stat.exists" + +- name: Determine existing Postgres version (read PG_VERSION file) + slurp: + src: "{{ matrix_postgres_detection_pg_version_path }}" + register: result_pg_version + when: "matrix_postgres_detected_existing" + +- name: Determine existing Postgres version (make sense of PG_VERSION file) + set_fact: + matrix_postgres_detected_version: "{{ result_pg_version['content']|b64decode|replace('\n', '') }}" + when: "matrix_postgres_detected_existing" + +- name: Determine corresponding Docker image to detected version (assume default of latest) + set_fact: + matrix_postgres_detected_version_corresponding_docker_image: "{{ matrix_postgres_docker_image_latest }}" + when: "matrix_postgres_detected_version != ''" + +- name: Determine corresponding Docker image to detected version (use 9.x, if detected) + set_fact: + matrix_postgres_detected_version_corresponding_docker_image: "{{ matrix_postgres_docker_image_v9 }}" + when: "matrix_postgres_detected_version.startswith('9.')" + +- name: Determine corresponding Docker image to detected version (use 10.x, if detected) + set_fact: + matrix_postgres_detected_version_corresponding_docker_image: "{{ matrix_postgres_docker_image_v10 }}" + when: "matrix_postgres_detected_version == '10' or matrix_postgres_detected_version.startswith('10.')" \ No newline at end of file