Add postgres functions, set_listen_addresses and add_hba_entry

This commit is contained in:
Lance Edgar 2021-06-15 14:16:46 -05:00
parent 3287505f88
commit c413c2a1f2

View file

@ -27,7 +27,7 @@ Fabric Library for PostgreSQL
import os
import re
from rattail_fabric2 import apt
from rattail_fabric2 import apt, append, contains, sed, uncomment
def install(c):
@ -47,6 +47,47 @@ def get_version(c):
if match:
return float(match.group(1))
def set_listen_addresses(c, *addresses):
"""
Overwrite the `listen_addresses` config setting in `postgresql.conf`.
"""
version = get_version(c)
if 12 <= version < 13:
version = 12
addresses = ','.join(addresses)
# TODO: this does not seem to work, so cannot do this..? need to
# figure it out, since it's the whole reason i added this function
# addresses = addresses.replace('*', '\\\\*')
if not contains(c, '/etc/postgresql/{}/main/postgresql.conf'.format(version),
"listen_addresses = '{}'".format(addresses)):
uncomment(c, '/etc/postgresql/{}/main/postgresql.conf'.format(version),
r'^#listen_addresses = .*',
use_sudo=True)
sed(c, '/etc/postgresql/{}/main/postgresql.conf'.format(version),
r'listen_addresses = .*',
"listen_addresses = '{}'".format(addresses),
use_sudo=True)
restart(c)
def add_hba_entry(c, entry):
"""
Add an entry to the `pg_hba.conf` file.
"""
version = get_version(c)
if 12 <= version < 13:
version = 12
if not contains(c, '/etc/postgresql/{}/main/pg_hba.conf'.format(version),
entry, use_sudo=True):
append(c, '/etc/postgresql/{}/main/pg_hba.conf'.format(version),
entry, use_sudo=True)
reload_(c)
def restart(c):
"""
Restart the PostgreSQL database service
@ -54,12 +95,15 @@ def restart(c):
c.sudo('systemctl restart postgresql.service')
def reload(c):
def reload_(c):
"""
Reload config for the PostgreSQL database service
"""
c.sudo('systemctl reload postgresql.service')
# TODO: deprecate / remove this
reload = reload_
def sql(c, sql, database='', port=None, **kwargs):
"""