diff --git a/.dockerignore b/.dockerignore index 7451c5aba..e28ec0437 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,6 +3,7 @@ !docker/coturn/alpine/ !docker/coturn/debian/ !docker/coturn/rootfs/ +!docker/coturn/wireapp/ !cmake/ !CMakeLists.txt diff --git a/docker/coturn/wireapp/Dockerfile b/docker/coturn/wireapp/Dockerfile new file mode 100644 index 000000000..5915030da --- /dev/null +++ b/docker/coturn/wireapp/Dockerfile @@ -0,0 +1,20 @@ +# This image derives from another coturn image, and adds a script which +# polls the Prometheus metrics endpoint until there are no more active +# allocations. This is intended to be used as a pre-stop hook to allow graceful +# termination of the coturn process without disrupting active allocations. + +ARG coturn_image=coturn/coturn +ARG coturn_tag=latest + +FROM ${coturn_image}:${coturn_tag} + +USER root + +RUN apt-get update \ + && apt-get install -y --no-install-recommends --no-install-suggests \ + curl grep coreutils + +COPY docker/coturn/wireapp/pre-stop-hook.sh /usr/local/bin/pre-stop-hook +RUN chmod +x /usr/local/bin/pre-stop-hook + +USER nobody:nogroup diff --git a/docker/coturn/wireapp/pre-stop-hook.sh b/docker/coturn/wireapp/pre-stop-hook.sh new file mode 100644 index 000000000..5d4603701 --- /dev/null +++ b/docker/coturn/wireapp/pre-stop-hook.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# FUTUREWORK: this will break with IPv6. this should be fixed so it works with +# IPv6. + +set -uo pipefail + +SLEEPTIME=60 + +host="$1" +port="$2" + +url="http://$host:$port/metrics" + +echo "Polling coturn status on $url" + +while true; do + allocs=$(curl -s "$url" | grep -E '^turn_active_allocations' | cut -d' ' -f2) + if [ "$?" != 0 ]; then + echo "Could not retrieve metrics from coturn!" + exit 1 + fi + + if [ -z "$allocs" ]; then + echo "No more active allocations, exiting" + exit 0 + fi + if [ "$allocs" = 0 ]; then + echo "No more active allocations, exiting" + exit 0 + fi + + echo "Active allocations remaining, sleeping for $SLEEPTIME seconds" + sleep "$SLEEPTIME" +done