Files
mtusi2026lab5/infra/s3-gw-entrypoint.sh
T

29 lines
1.0 KiB
Bash

#!/bin/sh
# s3-gw shares the network namespace of the `s3` container. When s3 restarts (crash,
# `docker restart`, Docker daemon restart) it gets a NEW namespace and this one is left
# with only `lo`: HAProxy keeps running but nothing can reach it. Exit in that case so
# the restart policy starts s3-gw again inside the current namespace of s3.
haproxy -W -db -f /usr/local/etc/haproxy/haproxy.cfg &
pid=$!
trap 'kill -TERM "$pid" 2>/dev/null' TERM INT USR1
orphaned() {
# /proc/net/dev: two header lines (with '|') and one line per interface.
! grep -qv -e '^ *lo:' -e '|' /proc/net/dev
}
while kill -0 "$pid" 2>/dev/null; do
if orphaned; then
echo "s3-gw: network namespace of s3 is gone; exiting so the restart policy re-joins it" >&2
kill -TERM "$pid" 2>/dev/null
wait "$pid"
# Docker does not retry a start that fails because s3 is not running yet:
# give the restart policy of s3 time to bring it back first.
sleep 5
exit 1
fi
sleep 2 &
wait $!
done
wait "$pid"