Skip to content

Commit

Permalink
Add better schema detection, allowing overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlexis committed Jun 16, 2021
1 parent ba60f6f commit 9bca364
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 95 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ pdns_lmdb_databases_locations: []
Locations of the LMDB databases that have to be created if using the
`lmdb` backend.

Locations of the mysql and sqlite3 base schema.
When set, this value is used and they are not automatically detected.
```yaml
pdns_mysql_schema_file: ''
pdns_sqlite3_schema_file: ''
```

## Example Playbooks

Run as a master using the bind backend (when you already have a `named.conf` file):
Expand Down
8 changes: 8 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ pdns_sqlite_databases_locations: []
# in the given locations.
# NOTE: Requries lmdb backend to be installed on the machine.
pdns_lmdb_databases_locations: []

# Override the schema used to initialize the MySQL database
# By default, this role tries to detect the correct file
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: ""
68 changes: 19 additions & 49 deletions tasks/database-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,54 +43,24 @@
register: _pdns_check_mysql_db
changed_when: False

- block:

- name: Define the PowerDNS database MySQL schema file path on RedHat < 7 and PowerDNS < 4.2.0
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/pdns/schema.mysql.sql"
when: ansible_distribution_major_version | int < 7
and _pdns_running_version is version_compare('4.2.0', '<')

- name: Define the PowerDNS database MySQL schema file path on RedHat >= 7 or PowerDNS >= 4.2.0
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/pdns-backend-mysql-{{ _pdns_running_version }}/schema.mysql.sql"
when: ansible_distribution_major_version | int == 7
or _pdns_running_version is version_compare('4.2.0', '>=')

- name: Define the PowerDNS database MySQL schema file path on RedHat 8 and PowerDNS >= 4.2.0
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/pdns-backend-mysql/schema.mysql.sql"
when:
- ansible_distribution_major_version | int == 8
- _pdns_running_version is version_compare('4.2.0', '>=')

when: ansible_os_family == 'RedHat'

- block:

- name: Define the PowerDNS database MySQL schema file path on Debian
set_fact:
_pdns_mysql_schema_file: "/usr/share/dbconfig-common/data/pdns-backend-mysql/install/mysql"
when: pdns_install_repo | length == 0 and ansible_distribution_major_version | int < 10

- name: Define the PowerDNS database MySQL schema file path on Debian
set_fact:
_pdns_mysql_schema_file: "/usr/share/pdns-backend-mysql/schema/schema.mysql.sql"
when: pdns_install_repo | length == 0 and ansible_distribution_major_version | int >= 10

- name: Define the PowerDNS database MySQL schema file path on Debian
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/pdns-backend-mysql/schema.mysql.sql"
when: pdns_install_repo | length > 0

when: ansible_os_family == 'Debian'

- block:
- name: Define the PowerDNS DB schema file path on Arch Linux
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/powerdns/schema.mysql.sql"

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

- name: Set the schema file variable
set_fact:
pdns_mysql_schema_file_to_use: "{% if pdns_mysql_schema_file | length == 0 %}{{ pdns_mysql_schema_file_detected.stdout }}{% else %}{{ pdns_mysql_schema_file }}{% endif %}"

- name: Import the PowerDNS MySQL schema
mysql_db:
Expand All @@ -100,6 +70,6 @@
login_port: "{{ item['item']['port'] | default('3306') }}"
name: "{{ item.item['value']['dbname'] }}"
state: import
target: "{{ _pdns_mysql_schema_file }}"
target: "{{ pdns_mysql_schema_file_to_use }}"
when: "item['item']['key'].split(':')[0] == 'gmysql' and item['stdout'] == '0'"
with_items: "{{ _pdns_check_mysql_db['results'] }}"
70 changes: 24 additions & 46 deletions tasks/database-sqlite3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,30 @@
mode: 0750
with_items: "{{ pdns_sqlite_databases_locations }}"

- block:
- name: Define the PowerDNS SQLite schema file path on RedHat < 7 and PowerDNS < 4.2.0
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/pdns/schema.sqlite3.sql"
when: ansible_distribution_major_version | int < 7
and _pdns_running_version is version_compare('4.2.0', '<')

- name: Define the PowerDNS SQLite schema file path on RedHat >= 7 or PowerDNS >= 4.2.0
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/pdns-backend-sqlite-{{ _pdns_running_version }}/schema.sqlite3.sql"
when: ansible_distribution_major_version | int == 7
or _pdns_running_version is version_compare('4.2.0', '>=')

- name: Define the PowerDNS SQLite schema file path on RedHat 8 and PowerDNS >= 4.2.0
set_fact:
_pdns_mysql_schema_file: "/usr/share/doc/pdns/schema.sqlite3.sql"
when:
- ansible_distribution_major_version | int == 8
- _pdns_running_version is version_compare('4.2.0', '>=')

- name: Create the PowerDNS SQLite databases on RedHat
shell: "sqlite3 {{ item }} < {{ _pdns_mysql_schema_file }}"
args:
creates: "{{ item }}"
with_items: "{{ pdns_sqlite_databases_locations }}"

when: ansible_os_family == "RedHat"

- block:

- name: Create the PowerDNS SQLite databases on Debian
shell: "sqlite3 {{ item }} < /usr/share/doc/pdns-backend-sqlite3/schema.sqlite3.sql"
args:
creates: "{{ item }}"
with_items: "{{ pdns_sqlite_databases_locations }}"

when: ansible_os_family == "Debian"

- block:
- name: Initiate the SQLite database on Arch Linux
shell: "sqlite3 {{ item }} < /usr/share/doc/powerdns/schema.sqlite3.sql"
args:
creates: "{{ item }}"
with_items: "{{ pdns_sqlite_databases_locations }}"

when: ansible_os_family == "Archlinux"
- name: Determine location of the SQL file
shell:
cmd: |
for p in /usr/share/doc/pdns-backend-sqlite-{{ _pdns_running_version }}/schema.sql /usr/share/doc/pdns-backend-sqlite-{{ _pdns_running_version }}/schema.sqlite3.sql /usr/share/doc/pdns/schema.sqlite3.sql /usr/share/doc/pdns-backend-sqlite3/schema.sqlite3.sql /usr/share/doc/pdns-backend-sqlite/schema.sqlite3.sql /usr/share/doc/powerdns/schema.sqlite3.sql; do
if [ -f $p ]; then
echo $p
exit 0
fi
done
echo "Can't determine path to SQLite schema">&2
exit 1
changed_when: false
register: pdns_sqlite_schema_file_detected
when: pdns_sqlite_schema_file | length == 0

- name: Set the schema file variable
set_fact:
pdns_sqlite_schema_file_to_use: "{% if pdns_sqlite_schema_file | length == 0 %}{{ pdns_sqlite_schema_file_detected.stdout }}{% else %}{{ pdns_sqlite_schema_file }}{% endif %}"

- name: Create the PowerDNS SQLite databases
shell: "sqlite3 {{ item }} < {{ pdns_sqlite_schema_file_to_use }}"
args:
creates: "{{ item }}"
with_items: "{{ pdns_sqlite_databases_locations }}"

- name: Check the PowerDNS SQLite databases permissions
file:
Expand Down

0 comments on commit 9bca364

Please sign in to comment.