-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·174 lines (150 loc) · 4.98 KB
/
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
# entrypoint: script for staring bind9 in a container
set -eu
SELF="$(basename "$0" ".sh")"
BIND_DIR="${BIND_DIR:-/etc/bind}"
BIND_CONFIG="${BIND_CONFIG:-${BIND_DIR}/named.conf}"
ZONE_CONFIG="${ZONE_CONFIG:-${BIND_DIR}/zones.conf}"
ZONE_DIR="${ZONE_DIR:-${BIND_DIR}/zones}"
CONFIG_DIR="${CONFIG_DIR:-/config}"
usage() {
exception="${1:-}"
[ -n "$exception" ] && printf 'ERROR: %s\n\n' "$exception"
printf '%s\n' \
"Usage: $SELF [-h|--help] [arg [...]]" \
"" \
"-h / --help show this message" \
"-d / --debug print additional debugging messages" \
"" \
"--bind-dir BIND_DIR path of the bind configuration directory" \
" (default: '$BIND_DIR')" \
"--bind-config BIND_CONFIG full path of the 'named.conf' bind config file" \
" (default: '$BIND_CONFIG')" \
"--zone-dir ZONE_DIR path of a directory containing (only) zone" \
" files to automatically serve" \
" (default: '$ZONE_DIR')" \
"--zone-config ZONE_CONFIG full path of the 'zones.conf' file to generate" \
" from the files found in ZONE_DIR" \
" (default: '$ZONE_CONFIG')" \
"--config-dir CONFIG_DIR path of a directory whose contents could be" \
" copied into the BIND_DIR" \
" (default: '$CONFIG_DIR')" \
"" \
"-- Pass the remaining command-line arguments to" \
" bind directly" \
"" \
"Generates a zones.conf file which 'includes' all the zone files found in" \
"the ZONE_DIR, runs named-checkconf, then starts bind" \
"" # no trailing slash
[ -n "$exception" ] && exit 1
exit 0
}
warn() {
printf '%s %s %s\n' "$(date '+%FT%T%z')" "$SELF" "$*" >&2
}
die() {
warn "FATAL:" "$@"
exit 1
}
main() {
# arg-processing loop
while [ $# -gt 0 ]; do
arg="$1" # shift at end of loop
case "$arg" in
-h|-help|--help)
usage
;;
-d|--debug)
set -x
;;
--bind-dir)
shift || usage "--bind-dir requires an argument"
BIND_DIR="$1"
;;
--bind-config)
shift || usage "--bind-config requires an argument"
BIND_CONFIG="$1"
;;
--zone-config)
shift || usage "--zone-config requires an argument"
ZONE_CONFIG="$1"
;;
--zone-dir)
shift || usage "--zone-dir requires an argument"
ZONE_DIR="$1"
;;
--config-dir)
shift || usage "--config-dir requires an argument"
CONFIG_DIR="$1"
;;
--)
shift || true
break
;;
*)
# unknown arg, leave it in the positional params
break
;;
esac
shift || break
done
# ensure required environment variables are set
# : "${USER:?the USER environment variable must be set}"
# copy any config files & zones found in /config to /etc/bind
if [ -d "$CONFIG_DIR" ]; then
warn "found ${CONFIG_DIR}; copying contents to ${BIND_DIR}"
cp -vaf "${CONFIG_DIR}/." "${BIND_DIR}/" | while read -r LINE; do
warn "copy:" "$LINE"
done
fi
# cd to /etc/bind
cd "$BIND_DIR" || die "couldn't cd to ${BIND_DIR}/"
# optionally construct the ZONE_CONFIG inclusion file
if \
[ -f "$BIND_CONFIG" ] && \
grep -qF "include \"${ZONE_CONFIG}\";" "$BIND_CONFIG" \
; then
# named.conf includes a reference to zones.conf, a file
# that we dynamically create based on the contents of the
# zones subdirectory
warn "constructing ${ZONE_CONFIG}"
if [ -f "${ZONE_CONFIG}" ]; then
warn "found existing ${ZONE_CONFIG}, removing it for reconstruction"
rm -vf "${ZONE_CONFIG}" 2>&1 | while read -r LINE; do
warn "remove:" "$LINE"
done
fi
for zone_file in "$(basename "${ZONE_DIR}")"/*; do \
zone="$(basename "$zone_file")"
if [ "$zone" = '*' ]; then
die "no zone files were found in ${ZONE_DIR} but we need zone files" \
"so that we can generate ${ZONE_CONFIG} and we MUST generate that" \
"file because ${BIND_CONFIG} currently refers to it"
fi
printf 'zone "%s" IN { type primary; file "%s"; };\n' \
"$zone" "$zone_file" \
| tee -a "${ZONE_CONFIG}" \
| while read -r LINE; do
warn "config:" "$LINE"
done
done
fi
warn "resetting $BIND_DIR permissions"
chown -R root:root "$BIND_DIR"
chmod -R a-rwx,a+rX,u+w "$BIND_DIR"
chown named:named "$BIND_DIR"
warn "running named-checkconf"
# we can't wrap the output of this command because we want a non-zero exit to
# terminate the container
/usr/bin/named-checkconf -z \
|| die "named-checkconf failed"
# put the whole command in the positional params so we can report it
set -- /usr/sbin/named \
-u named \
-c "$BIND_CONFIG" \
-g \
"$@"
warn "exec-ing bind:" "$@"
exec "$@"
}
main "$@"; exit