2018-11-21 01:31:02 +01:00
#!/bin/bash
set -euo pipefail
2024-01-03 19:27:25 +01:00
# Configure db env vars
: " ${ PDNS_ADMIN_SQLA_DB_TYPE : =mysql } " # or postgres
2023-05-06 21:50:58 +02:00
: " ${ PDNS_ADMIN_SQLA_DB_HOST : = ${ MYSQL_ENV_MYSQL_HOST :- mysql } } "
: " ${ PDNS_ADMIN_SQLA_DB_PORT : = ${ MYSQL_ENV_MYSQL_PORT :- 3306 } } "
2024-01-03 19:27:25 +01:00
: " ${ PDNS_ADMIN_SQLA_DB_USER : = ${ MYSQL_ENV_MYSQL_USER :- ${ PGSQL_ENV_POSTGRES_USER :- root } } } "
2023-05-06 21:50:58 +02:00
if [ " ${ PDNS_ADMIN_SQLA_DB_USER } " = "root" ] ; then
: " ${ PDNS_ADMIN_SQLA_DB_PASSWORD : = $MYSQL_ENV_MYSQL_ROOT_PASSWORD } "
2018-11-21 01:31:02 +01:00
fi
2024-01-03 19:27:25 +01:00
: " ${ PDNS_ADMIN_SQLA_DB_PASSWORD : = ${ MYSQL_ENV_MYSQL_PASSWORD :- ${ PGSQL_ENV_POSTGRES_PASSWORD :- powerdnsadmin } } } "
: " ${ PDNS_ADMIN_SQLA_DB_NAME : = ${ MYSQL_ENV_MYSQL_DATABASE :- ${ PGSQL_ENV_POSTGRES_DB :- powerdnsadmin } } } "
2023-05-06 21:50:58 +02:00
2024-01-03 19:27:25 +01:00
# Cleanup quotes from db env vars
PDNS_ADMIN_SQLA_DB_TYPE = " ${ PDNS_ADMIN_SQLA_DB_TYPE //[ \' \" ] } "
2023-05-06 21:50:58 +02:00
PDNS_ADMIN_SQLA_DB_HOST = " ${ PDNS_ADMIN_SQLA_DB_HOST //[ \' \" ] } "
PDNS_ADMIN_SQLA_DB_PORT = " ${ PDNS_ADMIN_SQLA_DB_PORT //[ \' \" ] } "
PDNS_ADMIN_SQLA_DB_USER = " ${ PDNS_ADMIN_SQLA_DB_USER //[ \' \" ] } "
PDNS_ADMIN_SQLA_DB_PASSWORD = " ${ PDNS_ADMIN_SQLA_DB_PASSWORD //[ \' \" ] } "
PDNS_ADMIN_SQLA_DB_NAME = " ${ PDNS_ADMIN_SQLA_DB_NAME //[ \' \" ] } "
2018-11-21 01:31:02 +01:00
2024-01-03 19:27:25 +01:00
export PDNS_ADMIN_SQLA_DB_TYPE PDNS_ADMIN_SQLA_DB_HOST PDNS_ADMIN_SQLA_DB_PORT PDNS_ADMIN_SQLA_DB_USER PDNS_ADMIN_SQLA_DB_PASSWORD PDNS_ADMIN_SQLA_DB_NAME
2018-11-21 01:31:02 +01:00
# Configure pdns server env vars
2018-11-22 19:01:52 +01:00
: " ${ PDNS_API_URL : =http : // ${ PDNS_ENV_PDNS_webserver_host :- pdns } : ${ PDNS_ENV_PDNS_webserver_port :- 8081 } / } "
2018-11-21 01:31:02 +01:00
: " ${ PDNS_API_KEY : = ${ PDNS_ENV_PDNS_api_key :- } } "
: " ${ PDNS_VERSION : = ${ PDNS_ENV_VERSION :- } } "
# Generate secret key
[ -f /root/secret-key ] || tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c 32 > /root/secret-key || true
2023-05-06 21:50:58 +02:00
PDNS_ADMIN_SECRET_KEY = " $( cat /root/secret-key) "
2018-11-21 01:31:02 +01:00
export PDNS_ADMIN_SECRET_KEY
2024-01-01 20:39:33 +01:00
subvars --prefix 'SSL_' < '/Caddyfile.tpl' > '/etc/caddy/Caddyfile'
2023-12-19 23:12:51 +01:00
subvars --prefix 'PDNS_ADMIN_' < '/config.py.tpl' > '/opt/powerdns-admin/powerdnsadmin/default_config.py'
2018-11-21 01:31:02 +01:00
# Initialize DB if needed
2024-01-03 19:27:25 +01:00
if [ [ " ${ PDNS_ADMIN_SQLA_DB_TYPE } " = = 'mysql' ] ] ; then
SQL_COMMAND = " mysql -h ${ PDNS_ADMIN_SQLA_DB_HOST } -P ${ PDNS_ADMIN_SQLA_DB_PORT } -u ${ PDNS_ADMIN_SQLA_DB_USER } -p ${ PDNS_ADMIN_SQLA_DB_PASSWORD } -e "
elif [ [ " ${ PDNS_ADMIN_SQLA_DB_TYPE } " = = 'postgres' ] ] ; then
PGPASSWORD = " ${ PDNS_ADMIN_SQLA_DB_PASSWORD } "
export PGPASSWORD
SQL_COMMAND = " psql -h ${ PDNS_ADMIN_SQLA_DB_HOST } -p ${ PDNS_ADMIN_SQLA_DB_PORT } -U ${ PDNS_ADMIN_SQLA_DB_USER } -c "
else
>& 2 echo " Invalid DB type: ${ PDNS_ADMIN_SQLA_DB_TYPE } "
exit 1
fi
2018-11-21 01:31:02 +01:00
2024-01-03 19:27:25 +01:00
until $SQL_COMMAND ';' ; do
>& 2 echo 'DB is unavailable - sleeping'
2018-11-21 01:31:02 +01:00
sleep 1
done
2024-01-03 19:27:25 +01:00
if [ [ " ${ SKIP_DB_CREATE :- false } " != 'true' ] ] ; then
if [ [ " ${ PDNS_ADMIN_SQLA_DB_TYPE } " = = 'mysql' ] ] ; then
$SQL_COMMAND " CREATE DATABASE IF NOT EXISTS ${ PDNS_ADMIN_SQLA_DB_NAME } "
elif [ [ " ${ PDNS_ADMIN_SQLA_DB_TYPE } " = = 'postgres' ] ] ; then
echo " SELECT 'CREATE DATABASE ${ PDNS_ADMIN_SQLA_DB_NAME } ' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = ' ${ PDNS_ADMIN_SQLA_DB_NAME } ')\gexec " | ${ SQL_COMMAND : :- 3 }
fi
fi
2018-11-21 01:31:02 +01:00
flask db upgrade
# initial settings if not available in the DB
2024-01-03 19:27:25 +01:00
$SQL_COMMAND " INSERT INTO setting (name, value) SELECT * FROM (SELECT 'pdns_api_url', ' ${ PDNS_API_URL //[ \' \" ] } ') AS tmp WHERE NOT EXISTS (SELECT name FROM setting WHERE name = 'pdns_api_url') LIMIT 1; " " ${ PDNS_ADMIN_SQLA_DB_NAME } "
$SQL_COMMAND " INSERT INTO setting (name, value) SELECT * FROM (SELECT 'pdns_api_key', ' ${ PDNS_API_KEY //[ \' \" ] } ') AS tmp WHERE NOT EXISTS (SELECT name FROM setting WHERE name = 'pdns_api_key') LIMIT 1; " " ${ PDNS_ADMIN_SQLA_DB_NAME } "
$SQL_COMMAND " INSERT INTO setting (name, value) SELECT * FROM (SELECT 'pdns_version', ' ${ PDNS_VERSION //[ \' \" ] } ') AS tmp WHERE NOT EXISTS (SELECT name FROM setting WHERE name = 'pdns_version') LIMIT 1; " " ${ PDNS_ADMIN_SQLA_DB_NAME } "
2018-11-21 01:31:02 +01:00
# update pdns api settings if env changed
2024-01-03 19:27:25 +01:00
$SQL_COMMAND " UPDATE setting SET value=' ${ PDNS_API_URL //[ \' \" ] } ' WHERE name='pdns_api_url'; " " ${ PDNS_ADMIN_SQLA_DB_NAME } "
$SQL_COMMAND " UPDATE setting SET value=' ${ PDNS_API_KEY //[ \' \" ] } ' WHERE name='pdns_api_key'; " " ${ PDNS_ADMIN_SQLA_DB_NAME } "
$SQL_COMMAND " UPDATE setting SET value=' ${ PDNS_VERSION //[ \' \" ] } ' WHERE name='pdns_version'; " " ${ PDNS_ADMIN_SQLA_DB_NAME } "
2018-11-21 01:31:02 +01:00
mkdir -p /run/uwsgi
chown uwsgi: /run/uwsgi
2018-11-22 19:45:43 +01:00
exec " $@ "