-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathdocker-entrypoint.sh
executable file
·149 lines (124 loc) · 5.21 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh
set -eu
: "${DEBUG:=0}"
if [ "${DEBUG}" -eq 1 ]; then
set -x
fi
##### Function definitions ####
deriveMySQLSettingsFromExistingConfigFile() {
if [ ! -f /etc/pdns/pdns.conf ]; then
echo "Use of existing file /etc/pdns/pdns.conf requested but file does not exist!"
exit 1
fi
PDNS_gmysql_host=$(sed -n 's/^gmysql-host=\(.*\)/\1/p' < /etc/pdns/pdns.conf)
PDNS_gmysql_port=$(sed -n 's/^gmysql-port=\(.*\)/\1/p' < /etc/pdns/pdns.conf)
PDNS_gmysql_user=$(sed -n 's/^gmysql-user=\(.*\)/\1/p' < /etc/pdns/pdns.conf)
PDNS_gmysql_password=$(sed -n 's/^gmysql-password=\(.*\)/\1/p' < /etc/pdns/pdns.conf)
PDNS_gmysql_dbname=$(sed -n 's/^gmysql-dbname=\(.*\)/\1/p' < /etc/pdns/pdns.conf)
}
deriveMySQLSettingsFromEnvironment() {
# Configure mysql env vars
: "${PDNS_gmysql_host:=${MYSQL_ENV_MYSQL_HOST:-mysql}}"
: "${PDNS_gmysql_port:=${MYSQL_ENV_MYSQL_PORT:-3306}}"
: "${PDNS_gmysql_user:=${MYSQL_ENV_MYSQL_USER:-root}}"
if [ "${PDNS_gmysql_user}" = 'root' ]; then
: "${PDNS_gmysql_password:=${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-}}"
fi
: "${PDNS_gmysql_password:=${MYSQL_ENV_MYSQL_PASSWORD:-powerdns}}"
: "${PDNS_gmysql_dbname:=${MYSQL_ENV_MYSQL_DATABASE:-powerdns}}"
# Use first part of node name as database name suffix
if [ "${NODE_NAME:-}" ]; then
NODE_NAME=$(echo "${NODE_NAME}" | sed -e 's/\..*//' -e 's/-//')
PDNS_gmysql_dbname="${PDNS_gmysql_dbname}${NODE_NAME}"
fi
export PDNS_gmysql_host PDNS_gmysql_port PDNS_gmysql_user PDNS_gmysql_password PDNS_gmysql_dbname
}
generateMySQLCommand() {
: "${MYSQL_CLIENT_EXTRA_PARAMS:=}"
# Password Auth
if [ "${PDNS_gmysql_password:-}" ]; then
MYSQL_CLIENT_EXTRA_PARAMS="${MYSQL_CLIENT_EXTRA_PARAMS} -p${PDNS_gmysql_password}"
fi
# Allow socket connections
if [ "${PDNS_gmysql_socket:-}" ]; then
export PDNS_gmysql_host='localhost'
MYSQL_CLIENT_EXTRA_PARAMS="${MYSQL_CLIENT_EXTRA_PARAMS} --socket=${PDNS_gmysql_socket}"
fi
MYSQL_COMMAND="mariadb -h ${PDNS_gmysql_host} -P ${PDNS_gmysql_port} -u ${PDNS_gmysql_user} ${MYSQL_CLIENT_EXTRA_PARAMS}"
}
createDatabaseIfRequested() {
# Initialize DB if needed
if [ "${SKIP_DB_CREATE:-false}" != 'true' ]; then
$MYSQL_COMMAND -e "CREATE DATABASE IF NOT EXISTS ${PDNS_gmysql_dbname}"
fi
}
initDatabase() {
if [ "${SKIP_DB_INIT:-false}" != 'true' ]; then
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
echo "Database exists and has no tables yet, doing init";
$MYSQL_COMMAND -D "$PDNS_gmysql_dbname" < /usr/share/doc/pdns/schema.mysql.sql
else
echo "Database exists but already has tables, will not try to init";
fi
fi
}
migrateDatabaseTo47() {
# 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
}
initSuperslave() {
if [ "${PDNS_autosecondary:-no}" = 'yes' ] || [ "${PDNS_superslave:-no}" = 'yes' ]; then
# Configure supermasters if needed
if [ "${SUPERMASTER_IPS:-}" ]; then
$MYSQL_COMMAND -D "$PDNS_gmysql_dbname" -e 'TRUNCATE supermasters;'
MYSQL_INSERT_SUPERMASTERS=''
if [ "${SUPERMASTER_COUNT:-0}" -eq 0 ]; then
SUPERMASTER_COUNT=10
fi
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 }')
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
fi
}
generateAndInstallConfigFileFromEnvironment() {
# Create config file from template
subvars --prefix 'PDNS_' < '/pdns.conf.tpl' > '/etc/pdns/pdns.conf'
}
#### End of function definitions, let's get to work ...
if [ "${USE_EXISTING_CONFIG_FILE:-false}" = 'true' ]; then
deriveMySQLSettingsFromExistingConfigFile
else
deriveMySQLSettingsFromEnvironment
fi
generateMySQLCommand
# Wait for MySQL to respond
until $MYSQL_COMMAND -e ';' ; do
>&2 echo 'MySQL is unavailable - sleeping'
sleep 3
done
createDatabaseIfRequested
initDatabase
migrateDatabaseTo47
initSuperslave
if [ "${USE_EXISTING_CONFIG_FILE:-false}" = 'false' ]; then
echo "(re-)generating config file from environment variables"
generateAndInstallConfigFileFromEnvironment
fi
exec "$@"