-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-entrypoint.sh
88 lines (65 loc) · 2.48 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
#!/bin/bash
set -e
check_env() {
if [[ -z ${DNS_AUTH_SERVICE} ]] && [[ -z ${DNS_AUTH_IP_ADDRESSES} ]]; then
echo "One of DNS_AUTH_SERVICE or DNS_AUTH_IP_ADDRESSES environment variables must be set."
exit 1
fi
if [[ -z ${DNS_FORWARDER_SERVICE} ]] && [[ -z ${DNS_FORWARDER_IP_ADDRESSES} ]]; then
echo "One of DNS_FORWARDER_SERVICE or DNS_FORWARDER_IP_ADDRESSES environment variables must be set."
exit 1
fi
if [[ -z ${DNS_AUTH_ZONES} ]]; then
echo "DNS_AUTH_ZONES environment variable must be set."
exit 1
fi
}
get_ip_list_from_service() {
SERVICE_NAME="${1}"
SEPARATOR="${2:-;}"
IP_LIST="$(getent ahostsv4 tasks."${SERVICE_NAME}"|awk '/STREAM/ {print $1}'|tr '\n' "${SEPARATOR}")"
echo "${IP_LIST::-1}"
}
build_forwarder_config() {
local ZONES_LIST="${1}"
local FORWARDERS="${2}"
IFS=", " read -r -a zones <<< "${ZONES_LIST}"
CONFIG=""
for zone in "${zones[@]}"; do
CONFIG="${CONFIG}${zone}=${FORWARDERS},"
done
echo "${CONFIG::-1}"
}
update_recursor_forwarder_config() {
local FORWARDERS=$1
local RECURSORS=$2
local CONFIG_FILE="${3:-/etc/pdns-recursor/recursor.conf}"
FORWARD_ZONES="forward-zones=${FORWARDERS}"
RECURSE_ZONES="forward-zones-recurse=${RECURSORS}"
sed -i -e "s/# forward-zones=/${FORWARD_ZONES}/" "${CONFIG_FILE}"
sed -i -e "s/# forward-zones-recurse=/${RECURSE_ZONES}/" "${CONFIG_FILE}"
}
main() {
# --help, --version
[[ ${1} == "--help" ]] || [[ ${1} == "--version" ]] && exec /usr/local/sbin/pdns_recursor "${1}"
# treat everything except "--" as exec cmd
[[ ${1:0:2} != "--" ]] && exec "${@}"
check_env
if [[ -n ${DNS_AUTH_SERVICE} ]]; then
FORWARDERS="$(get_ip_list_from_service "${DNS_AUTH_SERVICE}")"
elif [[ -n ${DNS_AUTH_IP_ADDRESSES} ]]; then
FORWARDERS="${DNS_AUTH_IP_ADDRESSES}"
fi
if [[ -n ${DNS_FORWARDER_SERVICE} ]]; then
FORWARDER_IP_LIST="$(get_ip_list_from_service "${DNS_FORWARDER_SERVICE}")"
elif [[ -n ${DNS_FORWARDER_IP_ADDRESSES} ]]; then
FORWARDER_IP_LIST="${DNS_FORWARDER_IP_ADDRESSES}"
fi
FORWARDER_CONFIG="$(build_forwarder_config "${DNS_AUTH_ZONES}" "${FORWARDERS}")"
RECURSOR_CONFIG="$(build_forwarder_config "." "${FORWARDER_IP_LIST}")"
update_recursor_forwarder_config "${FORWARDER_CONFIG}" "${RECURSOR_CONFIG}"
trap "rec_control quit" SIGHUP SIGINT SIGTERM
/usr/local/sbin/pdns_recursor "$@" &
wait
}
main "$@"