test/bin/check

99 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
#
# Check if a service is running/working properly
#
# The script checks that the internal IP is reachable, the service
# replies on expected ports, and any public ports are reachable on
# the external IP.
#
# Further checks can be added by adding a
# /federated/services/$SERVICE/check file
#
if [ "$#" != 1 ]; then
echo "Usage: $0 <service>"
exit 1
fi
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="-u"
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