Skip to content

Commit

Permalink
docker: add script and awk program
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Mar 23, 2023
1 parent df61741 commit 4db0d0e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
55 changes: 55 additions & 0 deletions docker/docker-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/sh

# AdGuard Home Docker healthcheck script

# Exit the script if a pipeline fails (-e), prevent accidental filename
# 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
}

# TODO(e.burkov): Deal with custom configuration file name.
filename='./AdGuardHome.yaml'
readonly filename

if ! [ -f "$filename" ]
then
log "AdGuard Home is probably not set up yet"

exit 0
fi

hosts=$(awk -f './docker/get_bind_hosts.awk' "$filename")
readonly hosts

if [ "$hosts" == '' ]
then
log "No hosts could be retrieved from $filename"

exit 1
fi

case "$hosts"
in
("0.0.0.0:"*|"[::]:"*)
# TODO(e.burkov): Deal with unspecified addresses.
log "The unspecified address isn't supported by healthcheck"

exit 1
;;
(*)
# Go on.
;;
esac

echo "$hosts" | while read -r host
do
echo $host
done
40 changes: 40 additions & 0 deletions docker/get_bind_hosts.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
BEGIN {
naddrs = 0
}
bind_subsection {
# Subsection of bind_hosts field lasts as long as there are array elements.
bind_subsection = /[[:space:]+]- /;
if (bind_subsection) {
addr = $2
if (addr == "0.0.0.0" || addr == "::") {
# Only return the unspecified address if it's set at least once.
addrs[0] = addr; naddrs = 1;
bind_subsection = 0;
} else {
addrs[naddrs++] = addr
}
}
}
dns_subsection && !bind_subsection {
bind_subsection = /[[:space:]+]bind_hosts:/;
if (/^[[:space:]]+port:/) {
port = $2;
};
next
}
/^[^[:space:]]/ {
dns_subsection = /^dns:/;
next
}
END {
for (i = 0; i < naddrs; i++) {
addr = addrs[i];
if (match(addr, ":")) {
# It's an IPv6.
print "[" addr "]:" port;
} else {
# It's an IPv4.
print addr ":" port;
}
}
}

0 comments on commit 4db0d0e

Please sign in to comment.