Skip to content

Commit

Permalink
Optimised configuration loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasge committed Dec 6, 2020
1 parent 57325b4 commit 86f0737
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ set -e
# Allows Netbox to be run as non-root users
umask 002

load_configuration() {
UNIT_SOCKET="/opt/unit/unit.sock"
UNIT_CONFIG="/etc/unit/nginx-unit.json"
wait_count=0
while [ ! -S $UNIT_SOCKET ]; do
if [ $wait_count -gt 10 ]; then
echo "$0: No control socket found; configuration will not be loaded."
return 1
fi
echo "$0: Waiting for control socket to be created..."
wait_count=$[$wait_count + 1]
sleep 1
done
# even when the control socket exists, it does not mean unit has finished initialisation
# this curl call will get a reply once unit is fully launched
curl --silent --output /dev/null --request GET --unix-socket $UNIT_SOCKET http://localhost/
echo "$0: Applying configuration from $UNIT_CONFIG";
RESP_CODE=$(curl \
--silent \
--output /dev/null \
--write-out '%{http_code}' \
--request PUT \
--data-binary @$UNIT_CONFIG \
--unix-socket $UNIT_SOCKET http://localhost/config \
)
if [ "$RESP_CODE" != "200" ]; then
echo "$0: Could no load Unit configuration"
kill $(cat /opt/unit/unit.pid)
else
echo "$0: Unit configuration loaded successfully"
fi
}

# Load correct Python3 env
source /opt/netbox/venv/bin/activate

Expand Down Expand Up @@ -65,16 +98,7 @@ fi

echo "✅ Initialisation is done."

(
UNIT_SOCKET="/opt/unit/unit.sock"
UNIT_CONFIG="/etc/unit/nginx-unit.json"
while [ ! -S $UNIT_SOCKET ]; do echo "$0: Waiting for control socket to be created..."; sleep 1; done
# even when the control socket exists, it does not mean unit has finished initialisation
# this curl call will get a reply once unit is fully launched
curl --silent --request GET --unix-socket $UNIT_SOCKET http://localhost/
echo "$0: Applying configuration from $UNIT_CONFIG";
curl --silent --request PUT --data-binary @$UNIT_CONFIG --unix-socket $UNIT_SOCKET http://localhost/config
)&
load_configuration&

# Launch whatever is passed by docker
# (i.e. the RUN instruction in the Dockerfile)
Expand Down

0 comments on commit 86f0737

Please sign in to comment.