From d2ec77566fe5b52376e6828f521b56f07b7281b8 Mon Sep 17 00:00:00 2001 From: Vitaliy Kukharik Date: Fri, 6 Oct 2023 21:01:28 +0300 Subject: [PATCH] Extension Auto-Setup: pg_cron --- molecule/default/converge.yml | 1 + roles/add-repository/tasks/extensions.yml | 35 ++++++++++ roles/add-repository/tasks/main.yml | 32 +-------- roles/packages/tasks/extensions.yml | 18 +++++ roles/pre-checks/tasks/extensions.yml | 83 +++++++++++++---------- 5 files changed, 102 insertions(+), 67 deletions(-) create mode 100644 roles/add-repository/tasks/extensions.yml diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 7b9e00da4..4ae5da122 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -31,6 +31,7 @@ - name: Set variables for Extansions test ansible.builtin.set_fact: enable_pg_repack: true + enable_pg_cron: true - name: Set variables for PostgreSQL Cluster update test ansible.builtin.set_fact: diff --git a/roles/add-repository/tasks/extensions.yml b/roles/add-repository/tasks/extensions.yml new file mode 100644 index 000000000..61dc288a4 --- /dev/null +++ b/roles/add-repository/tasks/extensions.yml @@ -0,0 +1,35 @@ +--- +# Extension Auto-Setup: repository + +# TimescaleDB (if 'enable_timescale' is 'true') +- block: + # Debian based + - name: Add TimescaleDB repository apt-key + ansible.builtin.apt_key: + url: "https://packagecloud.io/timescale/timescaledb/gpgkey" + state: present + when: ansible_os_family == "Debian" + + - name: Add TimescaleDB repository + ansible.builtin.apt_repository: + repo: "deb https://packagecloud.io/timescale/timescaledb/{{ ansible_distribution | lower }}/ {{ ansible_distribution_release }} main" + state: present + update_cache: true + when: ansible_os_family == "Debian" + + # RedHat based + - name: Add TimescaleDB repository + ansible.builtin.yum_repository: + name: "timescale_timescaledb" + description: "timescaledb repo" + baseurl: "https://packagecloud.io/timescale/timescaledb/el/{{ ansible_distribution_major_version }}/x86_64" + gpgkey: "https://packagecloud.io/timescale/timescaledb/gpgkey" + gpgcheck: "no" + when: ansible_os_family == "RedHat" + environment: "{{ proxy_env | default({}) }}" + when: + - installation_method == "repo" + - (enable_timescale | default(false)| bool) or (enable_timescaledb | default(false)| bool) + tags: add_repo, timescaledb, timescale + +... diff --git a/roles/add-repository/tasks/main.yml b/roles/add-repository/tasks/main.yml index 15e219430..632cb4674 100644 --- a/roles/add-repository/tasks/main.yml +++ b/roles/add-repository/tasks/main.yml @@ -213,35 +213,7 @@ when: installation_method == "repo" and ansible_os_family == "RedHat" tags: add_repo -# timescaledb (if enable_timescale is defined) -- block: - # Debian based - - name: Add TimescaleDB repository apt-key - ansible.builtin.apt_key: - url: "https://packagecloud.io/timescale/timescaledb/gpgkey" - state: present - when: ansible_os_family == "Debian" - - - name: Add TimescaleDB repository - ansible.builtin.apt_repository: - repo: "deb https://packagecloud.io/timescale/timescaledb/{{ ansible_distribution | lower }}/ {{ ansible_distribution_release }} main" - state: present - update_cache: true - when: ansible_os_family == "Debian" - - # RedHat based - - name: Add TimescaleDB repository - ansible.builtin.yum_repository: - name: "timescale_timescaledb" - description: "timescaledb repo" - baseurl: "https://packagecloud.io/timescale/timescaledb/el/{{ ansible_distribution_major_version }}/x86_64" - gpgkey: "https://packagecloud.io/timescale/timescaledb/gpgkey" - gpgcheck: "no" - when: ansible_os_family == "RedHat" - environment: "{{ proxy_env | default({}) }}" - when: - - installation_method == "repo" - - (enable_timescale | default(false)| bool) or (enable_timescaledb | default(false)| bool) - tags: add_repo, timescaledb, timescale +- name: Extensions repository + ansible.builtin.import_tasks: extensions.yml ... diff --git a/roles/packages/tasks/extensions.yml b/roles/packages/tasks/extensions.yml index b20364b14..a9c96f6e3 100644 --- a/roles/packages/tasks/extensions.yml +++ b/roles/packages/tasks/extensions.yml @@ -1,4 +1,5 @@ --- +# Extension Auto-Setup: packages # TimescaleDB (if 'enable_timescale' is 'true') - name: Install TimescaleDB package @@ -34,4 +35,21 @@ when: enable_pg_repack | default(false)| bool tags: pg_repack +# pg_cron (if 'enable_pg_cron' is 'true') +- name: Install pg_cron package + ansible.builtin.package: + name: >- + {% if ansible_os_family == 'Debian' %} + postgresql-{{ postgresql_version }}-cron + {% else %} + pg_cron_{{ postgresql_version }} + {% endif %} + state: present + register: package_status + until: package_status is success + delay: 5 + retries: 3 + when: enable_pg_cron | default(false)| bool + tags: pg_cron + ... diff --git a/roles/pre-checks/tasks/extensions.yml b/roles/pre-checks/tasks/extensions.yml index 4544ba9fa..78412406f 100644 --- a/roles/pre-checks/tasks/extensions.yml +++ b/roles/pre-checks/tasks/extensions.yml @@ -1,40 +1,49 @@ --- -# TimescaleDB (if enable_timescale is defined) -- block: - - name: TimescaleDB | Checking PostgreSQL version - run_once: true - ansible.builtin.fail: - msg: - - "The current PostgreSQL version ({{ postgresql_version }}) is not supported by the TimescaleDB." - - "PostgreSQL version must be {{ timescale_minimal_pg_version }} or higher." - when: postgresql_version|string is version(timescale_minimal_pg_version|string, '<') +# TimescaleDB pre-check (if 'enable_timescale' is 'true') +- name: TimescaleDB | Checking PostgreSQL version + run_once: true + ansible.builtin.fail: + msg: + - "The current PostgreSQL version ({{ postgresql_version }}) is not supported by the TimescaleDB." + - "PostgreSQL version must be {{ timescale_minimal_pg_version }} or higher." + when: postgresql_version|string is version(timescale_minimal_pg_version|string, '<') - - name: TimescaleDB | Ensure 'timescaledb' is in 'shared_preload_libraries' - ansible.builtin.set_fact: - # This complex line does several things: - # 1. It takes the current list of PostgreSQL parameters, - # 2. Removes any item where the option is 'shared_preload_libraries', - # 3. Then appends a new 'shared_preload_libraries' item at the end. - # The new value of this item is based on whether 'timescaledb' is already present in the old value. - # If it is not present, it appends ',timescaledb' to the old value. Otherwise, it leaves the value unchanged. - postgresql_parameters: >- - {{ postgresql_parameters | rejectattr('option', 'equalto', 'shared_preload_libraries') | list - + [{'option': 'shared_preload_libraries', 'value': new_value}] }} - vars: - # Find the last item in postgresql_parameters where the option is 'shared_preload_libraries' - shared_preload_libraries_item: >- - {{ - postgresql_parameters - | selectattr('option', 'equalto', 'shared_preload_libraries') - | list | last | default({'value': ''}) - }} - # Determine the new value based on whether 'timescaledb' is already present - new_value: >- - {{ - (shared_preload_libraries_item.value ~ (',' if shared_preload_libraries_item.value else '') - if 'timescaledb' not in shared_preload_libraries_item.value.split(',') else shared_preload_libraries_item.value) - ~ ('timescaledb' if 'timescaledb' not in shared_preload_libraries_item.value.split(',') else '') - }} - when: (enable_timescale | default(false)| bool) or (enable_timescaledb | default(false)| bool) - tags: timescaledb, timescale +# Extension Auto-Setup: shared_preload_libraries +- name: Ensure extensions are in 'shared_preload_libraries' + run_once: true + ansible.builtin.set_fact: + # This complex line does several things: + # 1. It takes the current list of PostgreSQL parameters, + # 2. Removes any item where the option is 'shared_preload_libraries', + # 3. Then appends a new 'shared_preload_libraries' item at the end. + # The new value of this item is based on whether extension is already present in the old value. + # If it is not present, it appends ',' to the old value. Otherwise, it leaves the value unchanged. + postgresql_parameters: >- + {{ postgresql_parameters | rejectattr('option', 'equalto', 'shared_preload_libraries') | list + + [{'option': 'shared_preload_libraries', 'value': new_value}] }} + vars: + # Find the last item in postgresql_parameters where the option is 'shared_preload_libraries' + shared_preload_libraries_item: >- + {{ + postgresql_parameters + | selectattr('option', 'equalto', 'shared_preload_libraries') + | list | last | default({'value': ''}) + }} + # Determine the new value based on whether the extension is already present + new_value: >- + {{ + (shared_preload_libraries_item.value ~ (',' if shared_preload_libraries_item.value else '') + if item.extension not in shared_preload_libraries_item.value.split(',') else shared_preload_libraries_item.value) + ~ (item.extension if item.extension not in shared_preload_libraries_item.value.split(',') else '') + }} + loop: + - { extension: "timescaledb", enabled: (enable_timescale | default(false)| bool) or (enable_timescaledb | default(false)| bool) } + - { extension: "pg_cron", enabled: enable_pg_cron | default(false)| bool } + loop_control: + loop_var: item + label: "{{ item.extension }}" + when: item.enabled + tags: [timescaledb, timescale, pg_cron] + +...