Skip to content

Commit

Permalink
Barman: do not override wal_level if not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
jt-edb authored and vibhorkumar123 committed Aug 31, 2022
1 parent 0baa42e commit 8c4d5bd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 10 deletions.
18 changes: 17 additions & 1 deletion roles/setup_barman/tasks/configure_pg_backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@
_barman_host: "{{ _barman_server_info[0].inventory_hostname }}"
when: use_hostname|bool

- name: Ensure wal_level is configured to replica
- name: Get wal_level value
community.postgresql.postgresql_query:
query: >-
SELECT setting FROM pg_settings
WHERE name = 'wal_level'
port: "{{ pg_port }}"
db: "{{ pg_database }}"
login_user: "{{ pg_owner }}"
login_unix_socket: "{{ pg_unix_socket_directories[0] }}"
no_log: "{{ disable_logging }}"
become: true
become_user: "{{ pg_owner }}"
register: wal_level_result

- name: Ensure wal_level is configured to replica if needed
include_role:
name: manage_dbserver
tasks_from: manage_postgres_params
Expand All @@ -23,6 +37,8 @@
- name: wal_level
value: "replica"
no_log: "{{ disable_logging }}"
when:
- wal_level_result.query_result[0].setting not in ['replica', 'logical']

- name: Ensure the replication slot barman exists
include_role:
Expand Down
2 changes: 1 addition & 1 deletion roles/setup_barman/tasks/setup_barman.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
os: "{{ ansible_distribution | replace('RedHat', 'RHEL') }}{{ ansible_distribution_major_version }}"

- name: Reference pg_type variables
include_vars: "{{ pg_type }}.yml"
include_vars: "{{ pg_type }}_{{ ansible_os_family }}.yml"

- name: Set barman_backup_method
set_fact:
Expand Down
12 changes: 12 additions & 0 deletions roles/setup_barman/tasks/update_pg_hba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
'databases': 'postgres',
'contype': 'hostssl',
'source': '{{ hostvars[inventory_hostname].barman_server_private_ip | default("127.0.0.1") }}/32'
},
{
'users': 'barman',
'databases': 'postgres',
'contype': 'host',
'source': '{{ hostvars[inventory_hostname].barman_server_private_ip | default("127.0.0.1") }}/32'
}
]
Expand All @@ -21,6 +27,12 @@
'databases': 'replication',
'contype': 'hostssl',
'source': '{{ hostvars[inventory_hostname].barman_server_private_ip }}/32'
},
{
'users': 'barman',
'databases': 'replication',
'contype': 'host',
'source': '{{ hostvars[inventory_hostname].barman_server_private_ip }}/32'
}
]
when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
pg_owner: enterprisedb
pg_port: 5444
pg_bin_path: "/usr/edb/as{{ pg_version }}/bin"
pg_database: "postgres"
pg_unix_socket_directories:
- "/var/run/edb-as"
8 changes: 8 additions & 0 deletions roles/setup_barman/vars/EPAS_RedHat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---

pg_owner: enterprisedb
pg_port: 5444
pg_bin_path: "/usr/edb/as{{ pg_version }}/bin"
pg_database: "postgres"
pg_unix_socket_directories:
- "/var/run/edb/as{{ pg_version }}"
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
pg_owner: postgres
pg_port: 5432
pg_bin_path: "/usr/pgsql{{ pg_version }}/bin"
pg_database: "postgres"
pg_unix_socket_directories:
- "/var/run/postgresql"
8 changes: 8 additions & 0 deletions roles/setup_barman/vars/PG_RedHat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---

pg_owner: postgres
pg_port: 5432
pg_bin_path: "/usr/pgsql{{ pg_version }}/bin"
pg_database: "postgres"
pg_unix_socket_directories:
- "/var/run/postgresql"
7 changes: 6 additions & 1 deletion tests/cases/setup_barman/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
- name: Initialize the user defined variables
set_fact:
disable_logging: false
pg_postgres_conf_params:
- name: wal_level
value: logical

roles:
- role: setup_repo
Expand All @@ -27,7 +30,9 @@
when: "'install_dbserver' in lookup('edb_devops.edb_postgres.supported_roles', wantlist=True)"
- role: init_dbserver
when: "'init_dbserver' in lookup('edb_devops.edb_postgres.supported_roles', wantlist=True)"
- role: manage_dbserver
when: "'manage_dbserver' in lookup('edb_devops.edb_postgres.supported_roles', wantlist=True)"
- role: setup_barmanserver
when: "'setup_barmanserver' in lookup('edb_devops.edb_postgres.supported_roles', wantlist=True)"
- role: setup_barman
when: "'setup_barman' in lookup('edb_devops.edb_postgres.supported_roles', wantlist=True)"
when: "'setup_barman' in lookup('edb_devops.edb_postgres.supported_roles', wantlist=True)"
36 changes: 29 additions & 7 deletions tests/tests/test_setup_barman.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_setup_barman_backup():
with host.sudo(barman_user):
cmd = host.run('barman list-server')
result = cmd.stdout.strip()

assert "primary1" in result, \
"%s backup server was not configured correctly" % "primary1"

Expand All @@ -30,7 +30,7 @@ def test_setup_barman_home_dir():
with host.sudo(barman_user):
cmd = host.run('barman show-server primary1-main | grep barman_home')
result = cmd.stdout.strip()

assert barman_home in result, \
"%s directory was not configured correctly" % barman_home

Expand All @@ -43,7 +43,7 @@ def test_setup_barman_status():
with host.sudo(barman_user):
cmd = host.run('barman status primary1-main | grep Active')
result = cmd.stdout.strip()

assert "True" in result, \
"primary1-main's server status is not active"

Expand All @@ -59,7 +59,7 @@ def test_setup_barman_user():

host= get_primary()
socket_dir = get_pg_unix_socket_dir()

with host.sudo(pg_user):
query = "Select * from pg_user where usename = '%s'" % barman_user
cmd = host.run('psql -At -p %s -h %s -c "%s" postgres' % (port, socket_dir, query))
Expand All @@ -68,6 +68,31 @@ def test_setup_barman_user():
assert len(result) > 0, \
"Barman user does not exist in primary database"

def test_setup_barman_logical_wal_level():
# Make sure setup_barman does not override wal_level if it has been
# previously configured to 'logical'.
ansible_vars = load_ansible_vars()
barman_user = ansible_vars['barman_user']
port = '5432'
pg_user = 'postgres'

if get_pg_type() == 'EPAS':
pg_user = 'enterprisedb'
port = '5444'

host= get_primary()
socket_dir = get_pg_unix_socket_dir()

with host.sudo(pg_user):
query = "SELECT setting FROM pg_settings WHERE name = 'wal_level'"
cmd = host.run(
'psql -At -p %s -h %s -c "%s" postgres' % (port, socket_dir, query)
)
result = cmd.stdout.strip()

assert result == 'logical', \
"wal_level should be set to 'logical'"

def test_setup_barman_backup():
ansible_vars = load_ansible_vars()
barman_user = ansible_vars['barman_user']
Expand All @@ -80,6 +105,3 @@ def test_setup_barman_backup():

assert len(result) > 0, \
"Backup for primary1-main's server has failed"



0 comments on commit 8c4d5bd

Please sign in to comment.