Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Mar 24, 2023
1 parent 32f150f commit f04ae13
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
20 changes: 13 additions & 7 deletions docker/dns-bind.awk
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
BEGIN { num = 0; prev_line = 0; }
/^[^[:space:]]/ { is_dns = /^dns:/; }
/^[[:space:]]+bind_hosts:/ { is_dns_bh = 1; if (is_dns_bh && is_dns) prev_line = FNR; }
/^[^[:space:]]/ { is_dns = /^dns:/ }

/^[[:space:]]+bind_hosts:/ { if (is_dns) prev_line = FNR }

/^[[:space:]]+- .+/ {
if ((FNR - prev_line) == 1 && is_dns_bh) { addrs[num++] = $2; prev_line = FNR; }
if (FNR - prev_line == 1) {
addrs[addrsnum++] = $2
prev_line = FNR
}
}
/^[[:space:]]+port:/ { if (is_dns) port = $2; }

/^[[:space:]]+port:/ { if (is_dns) port = $2 }

END {
for (i = 0; i < num; i++) {
if (match(addrs[i], ":")) print "[" addrs[i] "]:" port; else print addrs[i] ":" port;
for (i in addrs) {
if (match(addrs[i], ":")) print "[" addrs[i] "]:" port; else print addrs[i] ":" port
}
}
53 changes: 33 additions & 20 deletions docker/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
# expansion (-f), and consider undefined variables as errors (-u).
set -e -f -u

# Function log is an echo wrapper that writes to stderr if the caller requested
# verbosity level greater than 0. Otherwise, it does nothing.
log() {
# if [ "$VERBOSE" -gt '0' ]
# then
echo "$1" 1>&2
# fi
# Function error_exit is an echo wrapper that writes to stderr and stops the
# script execution with code 1.
error_exit() {
echo "$1" 1>&2

exit 1
}

home_dir="/opt/adguardhome"
Expand All @@ -28,32 +27,46 @@ then
exit 0
fi

# Parse.

help_dir="${home_dir}/scripts"
readonly help_dir

web_host=$(awk -f "${help_dir}/web-bind.awk" "$filename")
# Parse web host

web_host="$( awk -f "${help_dir}/web-bind.awk" "$filename" )"
readonly web_host

dns_hosts=$(awk -f "${help_dir}/dns-bind.awk" "$filename")
if [ "$web_host" = '' ]
then
error_exit "no web bindings could be retrieved from $filename"
fi

# TODO(e.burkov): Deal with 0 port.
case $web_host
in
(*":0")
error_exit "0 in web port is not supported by healthcheck"
;;
(*)
# Go on.
;;
esac

# Parse DNS hosts

dns_hosts="$( awk -f "${help_dir}/dns-bind.awk" "$filename" )"
readonly dns_hosts

if [ "$dns_hosts" == '' ]
if [ "$dns_hosts" = '' ]
then
log "No DNS bindings could be retrieved from $filename"

exit 1
error_exit "no DNS bindings could be retrieved from $filename"
fi

# TODO(e.burkov): Deal with 0 port.
first=$(echo $dns_hosts | head -n 1)
first="$( echo "$dns_hosts" | head -n 1 )"
case $first
in
(*":0")
log "0 port is not supported"

exit 1
error_exit "0 in DNS port is not supported by healthcheck"
;;
(*)
# Go on.
Expand All @@ -66,5 +79,5 @@ wget "http://${web_host}" -O /dev/null || exit 1

echo "$dns_hosts" | while read -r host
do
nslookup -type=a localhost $host || exit 1
nslookup -type=a localhost "$host" || exit 1
done
4 changes: 3 additions & 1 deletion docker/web-bind.awk
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/^bind_host:/ { host = $2 }

/^bind_port:/ { port = $2 }
END { if (match(host, ":")) print "[" host "]:" port; else print host ":" port; }

END { if (match(host, ":")) print "[" host "]:" port; else print host ":" port }
6 changes: 3 additions & 3 deletions scripts/make/build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ readonly dist_docker_scripts

mkdir -p "$dist_docker_scripts"
cp "./docker/dns-bind.awk"\
"${dist_docker}/scripts/dns-bind.awk"
"${dist_docker_scripts}/dns-bind.awk"
cp "./docker/web-bind.awk"\
"${dist_docker}/scripts/web-bind.awk"
"${dist_docker_scripts}/web-bind.awk"
cp "./docker/healthcheck.sh"\
"${dist_docker}/scripts/healthcheck.sh"
"${dist_docker_scripts}/healthcheck.sh"

# Don't use quotes with $docker_tags and $debug_flags because we want word
# splitting and or an empty space if tags are empty.
Expand Down

0 comments on commit f04ae13

Please sign in to comment.