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.
This commit is contained in:
Slavi Pantaleev 2019-01-01 13:31:01 +02:00
parent c59a53551a
commit a7f791f8f9
3 changed files with 60 additions and 68 deletions

View File

@ -4,41 +4,17 @@
# Generic tasks, no matter what kind of server we're using (internal/external) # Generic tasks, no matter what kind of server we're using (internal/external)
# #
- name: Determine existing Postgres version (check PG_VERSION file) - include: tasks/util/detect_existing_postgres_version.yml
stat:
path: "{{ matrix_postgres_data_path }}/PG_VERSION"
register: result_pg_version_stat
- name: Determine existing Postgres version (read PG_VERSION file) # If we have found an existing version (installed from before), we use its corresponding Docker image.
slurp: # If not, we install using the latest Postgres.
src: "{{ matrix_postgres_data_path }}/PG_VERSION" #
register: result_pg_version # Upgrading is supposed to be performed separately and explicitly (see `upgrade_postgres.yml`).
when: "result_pg_version_stat.stat.exists" - 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: Determine existing Postgres version (default to empty) - name: Warn if on an old version of Postgres
set_fact: debug:
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:
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" 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" when: "matrix_postgres_docker_image_to_use != matrix_postgres_docker_image_latest"

View File

@ -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." 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" when: "result_auto_upgrade_path.stat.exists"
- name: Determine existing Postgres version (check PG_VERSION file) - include: tasks/util/detect_existing_postgres_version.yml
stat:
path: "{{ matrix_postgres_data_path }}/PG_VERSION"
register: result_pg_version_stat
- name: Abort, if no existing Postgres version detected - name: Abort, if no existing Postgres version detected
fail: msg="Could not find existing Postgres installation" fail: msg="Could not find existing Postgres installation"
when: "not result_pg_version_stat.stat.exists" when: "not matrix_postgres_detected_existing"
- 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.')"
- name: Abort, if already at latest Postgres version - 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" 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: - 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 - name: Ensure matrix-synapse is stopped
service: name=matrix-synapse state=stopped service: name=matrix-synapse state=stopped
@ -96,7 +66,7 @@
--network={{ matrix_docker_network }} \ --network={{ matrix_docker_network }} \
--env-file={{ matrix_environment_variables_data_path }}/env-postgres-pgsql-docker \ --env-file={{ matrix_environment_variables_data_path }}/env-postgres-pgsql-docker \
-v {{ postgres_dump_dir }}:/out \ -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 - name: Ensure matrix-postgres is stopped
service: name=matrix-postgres state=stopped service: name=matrix-postgres state=stopped

View File

@ -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.')"