2017-12-11 14:18:53 +01:00
#!/bin/sh
2016-09-02 22:48:20 +02:00
2023-12-19 23:12:51 +01:00
set -eu
2016-09-02 22:48:20 +02:00
# Configure mysql env vars
2018-11-22 19:06:34 +01:00
: " ${ PDNS_gmysql_host : = ${ MYSQL_ENV_MYSQL_HOST :- mysql } } "
: " ${ PDNS_gmysql_port : = ${ MYSQL_ENV_MYSQL_PORT :- 3306 } } "
2016-09-02 22:48:20 +02:00
: " ${ PDNS_gmysql_user : = ${ MYSQL_ENV_MYSQL_USER :- root } } "
if [ " ${ PDNS_gmysql_user } " = 'root' ] ; then
2020-09-30 17:49:44 +02:00
: " ${ PDNS_gmysql_password : = ${ MYSQL_ENV_MYSQL_ROOT_PASSWORD :- } } "
2016-09-02 22:48:20 +02:00
fi
: " ${ PDNS_gmysql_password : = ${ MYSQL_ENV_MYSQL_PASSWORD :- powerdns } } "
: " ${ PDNS_gmysql_dbname : = ${ MYSQL_ENV_MYSQL_DATABASE :- powerdns } } "
2024-01-03 19:27:25 +01:00
# Use first part of node name as database name suffix
2020-03-29 19:01:11 +02:00
if [ " ${ NODE_NAME :- } " ] ; then
2023-12-19 23:12:51 +01:00
NODE_NAME = $( echo " ${ NODE_NAME } " | sed -e 's/\..*//' -e 's/-//' )
2020-03-29 19:01:11 +02:00
PDNS_gmysql_dbname = " ${ PDNS_gmysql_dbname } ${ NODE_NAME } "
fi
2016-09-02 22:48:20 +02:00
2020-03-29 19:01:11 +02:00
export PDNS_gmysql_host PDNS_gmysql_port PDNS_gmysql_user PDNS_gmysql_password PDNS_gmysql_dbname
2016-09-02 22:48:20 +02:00
2023-12-19 23:12:51 +01:00
EXTRA = ''
2020-09-30 17:49:44 +02:00
# Password Auth
2023-12-19 23:12:51 +01:00
if [ " ${ PDNS_gmysql_password } " ] ; then
2020-09-30 17:49:44 +02:00
EXTRA = " ${ EXTRA } -p ${ PDNS_gmysql_password } "
fi
2016-09-02 22:48:20 +02:00
2020-09-30 17:49:44 +02:00
# Allow socket connections
2023-12-19 23:12:51 +01:00
if [ " ${ PDNS_gmysql_socket :- } " ] ; then
export PDNS_gmysql_host = 'localhost'
2020-09-30 17:49:44 +02:00
EXTRA = " ${ EXTRA } --socket= ${ PDNS_gmysql_socket } "
fi
MYSQL_COMMAND = " mysql -h ${ PDNS_gmysql_host } -P ${ PDNS_gmysql_port } -u ${ PDNS_gmysql_user } ${ EXTRA } "
# Wait for MySQL to respond
2016-09-02 22:48:20 +02:00
until $MYSQL_COMMAND -e ';' ; do
>& 2 echo 'MySQL is unavailable - sleeping'
2020-03-29 19:01:11 +02:00
sleep 3
2016-09-02 22:48:20 +02:00
done
2020-09-30 17:49:44 +02:00
# Initialize DB if needed
2020-09-04 18:55:40 +02:00
if [ " ${ SKIP_DB_CREATE :- false } " != 'true' ] ; then
$MYSQL_COMMAND -e " CREATE DATABASE IF NOT EXISTS ${ PDNS_gmysql_dbname } "
fi
2016-09-02 22:48:20 +02:00
MYSQL_CHECK_IF_HAS_TABLE = " SELECT COUNT(DISTINCT table_name) FROM information_schema.columns WHERE table_schema = ' ${ PDNS_gmysql_dbname } '; "
MYSQL_NUM_TABLE = $( $MYSQL_COMMAND --batch --skip-column-names -e " $MYSQL_CHECK_IF_HAS_TABLE " )
if [ " $MYSQL_NUM_TABLE " -eq 0 ] ; then
$MYSQL_COMMAND -D " $PDNS_gmysql_dbname " < /usr/share/doc/pdns/schema.mysql.sql
fi
2023-01-16 14:10:39 +01:00
# SQL migration to version 4.7
MYSQL_CHECK_IF_47 = " SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = ' ${ PDNS_gmysql_dbname } ' AND table_name = 'domains' AND column_name = 'options'; "
MYSQL_NUM_TABLE = $( $MYSQL_COMMAND --batch --skip-column-names -e " $MYSQL_CHECK_IF_47 " )
if [ " $MYSQL_NUM_TABLE " -eq 0 ] ; then
echo 'Migrating MySQL schema to version 4.7...'
$MYSQL_COMMAND -D " $PDNS_gmysql_dbname " < /usr/share/doc/pdns/4.3.0_to_4.7.0_schema.mysql.sql
fi
2023-12-19 23:12:51 +01:00
if [ " ${ PDNS_superslave :- no } " = 'yes' ] ; then
2020-03-29 19:01:11 +02:00
# Configure supermasters if needed
if [ " ${ SUPERMASTER_IPS :- } " ] ; then
2023-12-19 23:12:51 +01:00
$MYSQL_COMMAND -D " $PDNS_gmysql_dbname " -e 'TRUNCATE supermasters;'
2020-03-29 19:01:11 +02:00
MYSQL_INSERT_SUPERMASTERS = ''
2023-12-19 23:12:51 +01:00
if [ " ${ SUPERMASTER_COUNT :- 0 } " -eq 0 ] ; then
2020-03-29 19:01:11 +02:00
SUPERMASTER_COUNT = 10
fi
2023-12-19 23:12:51 +01:00
i = 1; while [ $i -le " ${ SUPERMASTER_COUNT } " ] ; do
SUPERMASTER_HOST = $( echo " ${ SUPERMASTER_HOSTS :- } " | awk -v col = " $i " '{ print $col }' )
SUPERMASTER_IP = $( echo " ${ SUPERMASTER_IPS } " | awk -v col = " $i " '{ print $col }' )
2020-03-29 19:01:11 +02:00
if [ -z " ${ SUPERMASTER_HOST :- } " ] ; then
SUPERMASTER_HOST = $( hostname -f)
fi
if [ " ${ SUPERMASTER_IP :- } " ] ; then
MYSQL_INSERT_SUPERMASTERS = " ${ MYSQL_INSERT_SUPERMASTERS } INSERT INTO supermasters VALUES(' ${ SUPERMASTER_IP } ', ' ${ SUPERMASTER_HOST } ', 'admin'); "
fi
i = $(( i + 1 ))
done
$MYSQL_COMMAND -D " $PDNS_gmysql_dbname " -e " $MYSQL_INSERT_SUPERMASTERS "
fi
2016-09-02 22:48:20 +02:00
fi
2020-03-29 19:01:11 +02:00
# Create config file from template
2023-12-19 23:12:51 +01:00
subvars --prefix 'PDNS_' < '/pdns.conf.tpl' > '/etc/pdns/pdns.conf'
2020-03-29 19:01:11 +02:00
2018-11-22 19:45:43 +01:00
exec " $@ "