Simple functionality checks

Add script to verify basic functionality (port connectivity) for all
services and slightly more advanced functionality for pdns and pdnsmysql
This commit is contained in:
Bernhard "bero" Rosenkränzer (Boggins) 2025-02-17 20:03:27 +01:00
parent b8fb6d55be
commit a38d625025
8 changed files with 189 additions and 55 deletions

82
bin/check Executable file
View File

@ -0,0 +1,82 @@
#!/bin/bash
SERVICE="$1"
. /federated/lib/functions.sh
if [ -e /federated/services/$SERVICE/service ]; then
. /federated/services/$SERVICE/service
elif [ -d /federated/apps/$SERVICE ]; then
INTERNAL_IP="$(cat /federated/apps/$SERVICE/docker-compose.yml |grep 'ipv4_address:' |cut -d: -f2 |xargs echo)"
RELEVANT=false
while read r; do
[ -z "$r" ] && continue
if [ "$r" = "ports:" ]; then
RELEVANT=true
continue
fi
$RELEVANT || continue
if [ "$(echo $r |cut -b1)" != "-" ]; then
break
fi
P="$(echo $r |cut -b2- |xargs echo |sed -e 's,",,g')"
if echo $P |grep -q :; then
P="$(echo $P |cut -d: -f2-)"
fi
PUBLICPORTS="${PUBLICPORTS} ${P}"
done < <(cat /federated/apps/$SERVICE/docker-compose.yml)
unset RELEVANT
else
echo "Invalid service $SERVICE" >&2
exit 1
fi
if [ -n "${INTERNAL_IP}" ]; then
# Make sure the container is responding
for IP in ${INTERNAL_IP} ${EXTRA_IPS}; do
if ! ping -c3 ${IP}; then
echo "$1 container not responding on ${IP}" >&2
exit 1
fi
done
fi
if [ -n "${PORTS}" ]; then
# Make sure we can connect to the provided ports
for PORT in ${PORTS}; do
TRIES=5
while ! nc -z ${INTERNAL_IP} ${PORT}; do
sleep 5s
TRIES=$((TRIES-1))
if [ "$TRIES" = "0" ]; then
echo "$1 container fails to respond on port ${PORT}" >&2
exit 2
fi
done
done
fi
if [ -n "${PUBLICPORTS}" ]; then
# Make sure we can connect to the external ports on the public IP
IP="$(get_externalip)"
for PORT in ${PUBLICPORTS}; do
TRIES=5
if echo $PORT |grep -q '/udp$'; then
NC_OPTS="--udp"
PORT="$(echo $PORT |sed -e 's,/udp$,,')"
else
NC_OPTS=""
fi
while ! nc -z ${NC_OPTS} ${IP} ${PORT}; do
sleep 5s
TRIES=$((TRIES-1))
if [ "$TRIES" = "0" ]; then
echo "${SERVICE} container fails to respond on public port ${PORT}" >&2
exit 3
fi
done
done
fi
[ -e /federated/services/${SERVICE}/check ] && . /federated/services/${SERVICE}/check
exit 0

View File

@ -201,8 +201,13 @@ create_password() {
echo "$SECRET"; echo "$SECRET";
} }
get_externalip() { get_externalip() {
EXTERNALIP=`dig @resolver4.opendns.com myip.opendns.com +short 2> /dev/null` EXTERNALIP="$(dig @resolver4.opendns.com myip.opendns.com +short 2> /dev/null)"
echo "$EXTERNALIP"; if [ -n "$EXTERNALIP" ]; then
echo "$EXTERNALIP"
else
# Try to get a reasonable response even if opendns is down
ip route list default |sed -e 's,.*src ,,;s, .*,,'
fi
} }
start_service_convert() { start_service_convert() {
SERVICE="$1" SERVICE="$1"

View File

@ -11,16 +11,18 @@ config_pdns() {
mkdir -p /federated/apps/pdns/data/root mkdir -p /federated/apps/pdns/data/root
fi fi
. /federated/services/pdns/service
cat > /federated/apps/pdns/docker-compose.yml <<EOF cat > /federated/apps/pdns/docker-compose.yml <<EOF
services: services:
pdns: pdns:
image: pschiffe/pdns-mysql:\${IMAGE_VERSION} image: ${CONTAINER}:\${IMAGE_VERSION}
container_name: pdns container_name: pdns
hostname: pdns.$DOMAIN hostname: pdns.$DOMAIN
restart: always restart: always
networks: networks:
core: core:
ipv4_address: 192.168.0.11 ipv4_address: ${INTERNAL_IP}
ports: ports:
- "53:53" - "53:53"
- "53:53/udp" - "53:53/udp"
@ -34,10 +36,10 @@ networks:
external: true external: true
EOF EOF
EXTERNALIP=$(get_externalip); EXTERNALIP=$(get_externalip)
MYSQL_PASSWORD=`grep MYSQL_PASSWORD /federated/apps/pdnsmysql/.env | awk -F= '{ print $2 }'` MYSQL_PASSWORD=`grep MYSQL_PASSWORD /federated/apps/pdnsmysql/.env | awk -F= '{ print $2 }'`
PDNS_APIKEY=$(create_password); PDNS_APIKEY=$(create_password)
PDNS_WEBSERVER_PASSWORD=$(create_password); PDNS_WEBSERVER_PASSWORD=$(create_password)
cat > /federated/apps/pdns/.env <<EOF cat > /federated/apps/pdns/.env <<EOF
IMAGE_VERSION="4.9" IMAGE_VERSION="4.9"

View File

@ -7,6 +7,8 @@ PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sb
config_pdnsmysql() { config_pdnsmysql() {
echo -ne "* Configuring pdnsmysql container.." echo -ne "* Configuring pdnsmysql container.."
. /federated/services/pdnsmysql/service
if [ ! -d "/federated/apps/pdnsmysql" ]; then if [ ! -d "/federated/apps/pdnsmysql" ]; then
mkdir -p /federated/apps/pdnsmysql/data/var/lib/mysql mkdir -p /federated/apps/pdnsmysql/data/var/lib/mysql
fi fi
@ -14,13 +16,13 @@ config_pdnsmysql() {
cat > /federated/apps/pdnsmysql/docker-compose.yml <<EOF cat > /federated/apps/pdnsmysql/docker-compose.yml <<EOF
services: services:
mysql: mysql:
image: mariadb:\${IMAGE_VERSION} image: ${CONTAINER}:${VERSION}
container_name: pdnsmysql container_name: pdnsmysql
hostname: pdnsmysql.$DOMAIN hostname: pdnsmysql.$DOMAIN
restart: always restart: always
networks: networks:
core: core:
ipv4_address: 192.168.0.10 ipv4_address: ${INTERNAL_IP}
env_file: env_file:
- ./.env - ./.env
volumes: volumes:
@ -31,11 +33,11 @@ networks:
external: true external: true
EOF EOF
MYSQL_ROOTPASSWORD=$(create_password); MYSQL_ROOTPASSWORD=$(create_password)
MYSQL_PASSWORD=$(create_password); MYSQL_PASSWORD=$(create_password)
cat > /federated/apps/pdnsmysql/.env <<EOF cat > /federated/apps/pdnsmysql/.env <<EOF
IMAGE_VERSION="10.7.8" IMAGE_VERSION="${VERSION}"
MYSQL_ROOT_PASSWORD=$MYSQL_ROOTPASSWORD MYSQL_ROOT_PASSWORD=$MYSQL_ROOTPASSWORD
MYSQL_PASSWORD=$MYSQL_PASSWORD MYSQL_PASSWORD=$MYSQL_PASSWORD
MYSQL_DATABASE=pdns MYSQL_DATABASE=pdns

19
services/pdns/check Executable file
View File

@ -0,0 +1,19 @@
#!/bin/sh
. /federated/lib/functions.sh
. /federated/services/pdns/service
. /federated/apps/pdns/.env
. /etc/federated
EXTERNAL_IP=$(get_externalip)
# Check it is up and running and produces reasonable output
if [ "$(dig @${EXTERNAL_IP} ${DOMAIN} +short)" != "${EXTERNAL_IP}" ]; then
echo "PDNS returns invalid result for ${DOMAIN}"
fi
if [ "$(dig @${EXTERNAL_IP} pdns.${DOMAIN} +short |tail -n1)" != "${EXTERNAL_IP}" ]; then
echo "PDNS returns invalid result for federated.computer"
fi
if [ "$(dig @${EXTERNAL_IP} federated.computer +short)" != "5.161.88.87" ]; then
echo "PDNS returns invalid result for federated.computer"
fi
exit 0

6
services/pdns/service Normal file
View File

@ -0,0 +1,6 @@
CONTAINER=pschiffe/pdns-mysql
VERSION=4.9
DEPENDS=pdnsmysql
INTERNAL_IP=192.168.0.11
PORTS=8081
PUBLICPORTS="53 53/udp"

14
services/pdnsmysql/check Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
. /federated/services/pdnsmysql/service
. /federated/apps/pdnsmysql/.env
# Check it is up and running and produces reasonable output
TRIES=5
while ! docker exec -ti pdnsmysql mysql -p${MYSQL_ROOT_PASSWORD} mysql -e 'SELECT User FROM user WHERE User="root";'; do
TRIES=$((TRIES-1))
if [ "$TRIES" = 0 ]; then
echo "pdnsmysql not responding to SQL queries" >&2
exit 2
fi
done
exit 0

View File

@ -0,0 +1,4 @@
CONTAINER=mariadb
VERSION=10.7.8
INTERNAL_IP=192.168.0.10
PORTS=3306