Skip to content

Commit

Permalink
Extension Auto-Setup: pg_cron
Browse files Browse the repository at this point in the history
  • Loading branch information
vitabaks committed Oct 6, 2023
1 parent 5ec8db5 commit d2ec775
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 67 deletions.
1 change: 1 addition & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
35 changes: 35 additions & 0 deletions roles/add-repository/tasks/extensions.yml
Original file line number Diff line number Diff line change
@@ -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

...
32 changes: 2 additions & 30 deletions roles/add-repository/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

...
18 changes: 18 additions & 0 deletions roles/packages/tasks/extensions.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
# Extension Auto-Setup: packages

# TimescaleDB (if 'enable_timescale' is 'true')
- name: Install TimescaleDB package
Expand Down Expand Up @@ -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

...
83 changes: 46 additions & 37 deletions roles/pre-checks/tasks/extensions.yml
Original file line number Diff line number Diff line change
@@ -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 ',<extension_name>' 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]

...

0 comments on commit d2ec775

Please sign in to comment.