Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pgsql backend #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,6 @@ pdns_mysql_schema_file: ""
# Override the schema used to initialize the SQLite database
# By default, this role tries to detect the correct file
pdns_sqlite_schema_file: ""

# same
pdns_pgsql_schema_file: ""
79 changes: 79 additions & 0 deletions tasks/database-pgsql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---

- name: Install the PostgreSQL dependencies
package:
name: "{{ pdns_pgsql_packages }}"
state: present

- name: Create the PowerDNS PostgreSQL databases
postgresql_db:
login_user: "{{ item['value']['priv_user'] }}"
login_password: "{{ item['value']['priv_password'] }}"
login_host: "{{ item['value']['host'] }}"
port: "{{ item['value']['port'] | default('5432') }}"
name: "{{ item['value']['dbname'] }}"
state: present
when: "item.key.split(':')[0] == 'gpgsql'"
with_dict: "{{ pdns_backends | combine(pdns_pgsql_databases_credentials, recursive=True) }}"

- name: Grant PowerDNS access to the PostgreSQL databases
postgresql_user:
login_user: "{{ item['value']['priv_user'] }}"
login_password: "{{ item['value']['priv_password'] }}"
login_host: "{{ item['value']['host'] }}"
port: "{{ item['value']['port'] | default('5432') }}"
name: "{{ item['value']['user'] }}"
password: "{{ item['value']['password'] }}"
db: "{{ item['value']['dbname'] }}"
priv: "CONNECT/CREATE"
role_attr_flags: NOSUPERUSER,NOCREATEROLE,NOCREATEDB,NOINHERIT,LOGIN,NOREPLICATION,NOBYPASSRLS
state: present
when: "item.key.split(':')[0] == 'gpgsql'"
with_dict: "{{ pdns_backends | combine(pdns_pgsql_databases_credentials, recursive=True) }}"


- name: Check if the PostgreSQL databases are empty
community.postgresql.postgresql_query:
db: "{{ item['value']['dbname'] }}"
login_user: "{{ item['value']['user'] }}"
login_password: "{{ item['value']['password'] }}"
login_host: "{{ item['value']['host'] }}"
query: SELECT COUNT(DISTINCT tablename) FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' and schemaname= %s;
positional_args:
- "{{ item['value']['dbname'] }}"
when: item.key.split(':')[0] == 'gpgsql'
with_dict: "{{ pdns_backends }}"
register: _pdns_check_pgsql_db
changed_when: False

- name: Determine location of the SQL file
shell:
cmd: |
for p in /usr/share/doc/pdns-backend-pgsql-{{ _pdns_running_version }}/schema.pgsql.sql /usr/share/doc/pdns-backend-pgsql/schema.pgsql.sql /usr/share/pdns-backend-pgsql/schema/schema.pgsql.sql /usr/share/dbconfig-common/data/pdns-backend-pgsql/install/pgsql /usr/share/doc/powerdns/schema.pgsql.sql; do
if [ -f $p ]; then
echo $p
exit 0
fi
done
echo "Can't determine path to PostgreSQL schema">&2
exit 1
changed_when: false
register: pdns_pgsql_schema_file_detected
when: pdns_pgsql_schema_file | length == 0

- name: Set the schema file variable
set_fact:
pdns_pgsql_schema_file_to_use: "{% if pdns_pgsql_schema_file | length == 0 %}{{ pdns_pgsql_schema_file_detected.stdout }}{% else %}{{ pdns_pgsql_schema_file }}{% endif %}"

- name: Import the PowerDNS PostgreSQL schema
postgresql_db:
login_user: "{{ item['item']['value']['user'] }}"
login_password: "{{ item['item']['value']['password'] }}"
login_host: "{{ item['item']['value']['host'] }}"
port: "{{ item['item']['port'] | default('5432') }}"
name: "{{ item.item['value']['dbname'] }}"
state: restore
target: "{{ pdns_pgsql_schema_file_to_use }}"
when: "item['item']['key'].split(':')[0] == 'gpgsql' and item['query_result'][0]['count'] == 0"
with_items: "{{ _pdns_check_pgsql_db['results'] }}"

7 changes: 7 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
tags:
- db
- mysql
- pgsql
- sqlite
- config

Expand All @@ -33,6 +34,12 @@
- db
- mysql

- include: database-pgsql.yml
when: "pdns_pgsql_databases_credentials | length > 0"
tags:
- db
- pgsql

- include: database-sqlite3.yml
when: "pdns_sqlite_databases_locations | length > 0"
tags:
Expand Down
4 changes: 4 additions & 0 deletions vars/Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pdns_mysql_packages:
- python-mysqldb
- python3-mysqldb

pdns_pgsql_packages:
- postgresql
- postgresql-contrib

# List of PowerDNS Authoritative Server Backends packages on Debian
default_pdns_backends_packages:
geo: pdns-backend-geo
Expand Down