2020-11-10 15:23:07 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
UNIT_CONFIG="${UNIT_CONFIG-/etc/unit/nginx-unit.json}"
|
2022-10-07 08:08:30 +02:00
|
|
|
# Also used in "nginx-unit.json"
|
2020-11-10 15:23:07 +01:00
|
|
|
UNIT_SOCKET="/opt/unit/unit.sock"
|
|
|
|
|
|
|
|
load_configuration() {
|
|
|
|
MAX_WAIT=10
|
|
|
|
WAIT_COUNT=0
|
|
|
|
while [ ! -S $UNIT_SOCKET ]; do
|
2021-01-31 16:39:45 +01:00
|
|
|
if [ $WAIT_COUNT -ge $MAX_WAIT ]; then
|
2020-11-10 15:23:07 +01:00
|
|
|
echo "⚠️ No control socket found; configuration will not be loaded."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
WAIT_COUNT=$((WAIT_COUNT + 1))
|
|
|
|
echo "⏳ Waiting for control socket to be created... (${WAIT_COUNT}/${MAX_WAIT})"
|
|
|
|
|
|
|
|
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/
|
|
|
|
|
2021-02-08 12:16:04 +01:00
|
|
|
echo "⚙️ 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
|
|
|
|
)
|
2020-11-10 15:23:07 +01:00
|
|
|
if [ "$RESP_CODE" != "200" ]; then
|
|
|
|
echo "⚠️ Could no load Unit configuration"
|
|
|
|
kill "$(cat /opt/unit/unit.pid)"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "✅ Unit configuration loaded successfully"
|
|
|
|
}
|
|
|
|
|
|
|
|
load_configuration &
|
|
|
|
|
|
|
|
exec unitd \
|
2021-02-08 12:16:04 +01:00
|
|
|
--no-daemon \
|
|
|
|
--control unix:$UNIT_SOCKET \
|
|
|
|
--pid /opt/unit/unit.pid \
|
|
|
|
--log /dev/stdout \
|
|
|
|
--state /opt/unit/state/ \
|
2021-07-30 21:42:05 +02:00
|
|
|
--tmp /opt/unit/tmp/ \
|
2021-09-24 08:16:07 +02:00
|
|
|
--user unit \
|
|
|
|
--group root
|